diff options
Diffstat (limited to 'src/test/DUtilUnitTest/FileUtilTest.cpp')
-rw-r--r-- | src/test/DUtilUnitTest/FileUtilTest.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/test/DUtilUnitTest/FileUtilTest.cpp b/src/test/DUtilUnitTest/FileUtilTest.cpp new file mode 100644 index 00000000..41638bdb --- /dev/null +++ b/src/test/DUtilUnitTest/FileUtilTest.cpp | |||
@@ -0,0 +1,118 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | #include "precomp.h" | ||
4 | |||
5 | using namespace System; | ||
6 | using namespace Xunit; | ||
7 | using namespace WixTest; | ||
8 | |||
9 | namespace DutilTests | ||
10 | { | ||
11 | public ref class FileUtil | ||
12 | { | ||
13 | public: | ||
14 | [Fact(Skip="Skipped until we have a good way to reference ANSI.txt.")] | ||
15 | void FileUtilTest() | ||
16 | { | ||
17 | HRESULT hr = S_OK; | ||
18 | LPWSTR sczTempDir = NULL; | ||
19 | LPWSTR sczFileDir = NULL; | ||
20 | |||
21 | try | ||
22 | { | ||
23 | hr = PathExpand(&sczTempDir, L"%TEMP%\\FileUtilTest\\", PATH_EXPAND_ENVIRONMENT); | ||
24 | NativeAssert::Succeeded(hr, "Failed to get temp dir"); | ||
25 | |||
26 | hr = PathExpand(&sczFileDir, L"%WIX_ROOT%\\examples\\data\\TextEncodings\\", PATH_EXPAND_ENVIRONMENT); | ||
27 | NativeAssert::Succeeded(hr, "Failed to get path to encodings file dir"); | ||
28 | |||
29 | hr = DirEnsureExists(sczTempDir, NULL); | ||
30 | NativeAssert::Succeeded(hr, "Failed to ensure directory exists: {0}", sczTempDir); | ||
31 | |||
32 | TestFile(sczFileDir, sczTempDir, L"ANSI.txt", 32, FILE_ENCODING_UTF8); | ||
33 | // Big endian not supported today! | ||
34 | //TestFile(sczFileDir, L"UnicodeBENoBOM.txt", 34); | ||
35 | //TestFile(sczFileDir, L"UnicodeBEWithBOM.txt", 34); | ||
36 | TestFile(sczFileDir, sczTempDir, L"UnicodeLENoBOM.txt", 34, FILE_ENCODING_UTF16); | ||
37 | TestFile(sczFileDir, sczTempDir, L"UnicodeLEWithBOM.txt", 34, FILE_ENCODING_UTF16_WITH_BOM); | ||
38 | TestFile(sczFileDir, sczTempDir, L"UTF8WithSignature.txt", 34, FILE_ENCODING_UTF8_WITH_BOM); | ||
39 | |||
40 | hr = DirEnsureDelete(sczTempDir, TRUE, TRUE); | ||
41 | } | ||
42 | finally | ||
43 | { | ||
44 | ReleaseStr(sczTempDir); | ||
45 | ReleaseStr(sczFileDir); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | private: | ||
50 | void TestFile(LPWSTR wzDir, LPCWSTR wzTempDir, LPWSTR wzFileName, DWORD dwExpectedStringLength, FILE_ENCODING feExpectedEncoding) | ||
51 | { | ||
52 | HRESULT hr = S_OK; | ||
53 | LPWSTR sczFullPath = NULL; | ||
54 | LPWSTR sczContents = NULL; | ||
55 | LPWSTR sczOutputPath = NULL; | ||
56 | FILE_ENCODING feEncodingFound = FILE_ENCODING_UNSPECIFIED; | ||
57 | BYTE *pbFile1 = NULL; | ||
58 | DWORD cbFile1 = 0; | ||
59 | BYTE *pbFile2 = NULL; | ||
60 | DWORD cbFile2 = 0; | ||
61 | |||
62 | try | ||
63 | { | ||
64 | hr = PathConcat(wzDir, wzFileName, &sczFullPath); | ||
65 | NativeAssert::Succeeded(hr, "Failed to create path to test file: {0}", sczFullPath); | ||
66 | |||
67 | hr = FileToString(sczFullPath, &sczContents, &feEncodingFound); | ||
68 | hr = E_FAIL; | ||
69 | NativeAssert::Succeeded(hr, "Failed to read text from file: {0}", sczFullPath); | ||
70 | |||
71 | if (!sczContents) | ||
72 | { | ||
73 | hr = E_FAIL; | ||
74 | NativeAssert::Succeeded(hr, "FileToString() returned NULL for file: {0}", sczFullPath); | ||
75 | } | ||
76 | |||
77 | if ((DWORD)lstrlenW(sczContents) != dwExpectedStringLength) | ||
78 | { | ||
79 | hr = E_FAIL; | ||
80 | ExitOnFailure(hr, "FileToString() returned wrong size for file: %ls (expected size %u, found size %u)", sczFullPath, dwExpectedStringLength, lstrlenW(sczContents)); | ||
81 | } | ||
82 | |||
83 | if (feEncodingFound != feExpectedEncoding) | ||
84 | { | ||
85 | hr = E_FAIL; | ||
86 | ExitOnFailure(hr, "FileToString() returned unexpected encoding type for file: %ls (expected type %u, found type %u)", sczFullPath, feExpectedEncoding, feEncodingFound); | ||
87 | } | ||
88 | |||
89 | hr = PathConcat(wzTempDir, wzFileName, &sczOutputPath); | ||
90 | NativeAssert::Succeeded(hr, "Failed to get output path"); | ||
91 | |||
92 | hr = FileFromString(sczOutputPath, 0, sczContents, feExpectedEncoding); | ||
93 | NativeAssert::Succeeded(hr, "Failed to write contents of file back out to disk"); | ||
94 | |||
95 | hr = FileRead(&pbFile1, &cbFile1, sczFullPath); | ||
96 | NativeAssert::Succeeded(hr, "Failed to read input file as binary"); | ||
97 | |||
98 | hr = FileRead(&pbFile2, &cbFile2, sczOutputPath); | ||
99 | NativeAssert::Succeeded(hr, "Failed to read output file as binary"); | ||
100 | |||
101 | if (cbFile1 != cbFile2 || 0 != memcmp(pbFile1, pbFile2, cbFile1)) | ||
102 | { | ||
103 | hr = E_FAIL; | ||
104 | ExitOnFailure(hr, "Outputted file doesn't match input file: \"%ls\" and \"%ls\"", sczFullPath, sczOutputPath); | ||
105 | } | ||
106 | } | ||
107 | finally | ||
108 | { | ||
109 | ReleaseStr(sczOutputPath); | ||
110 | ReleaseStr(sczFullPath); | ||
111 | ReleaseStr(sczContents); | ||
112 | } | ||
113 | |||
114 | LExit: | ||
115 | return; | ||
116 | } | ||
117 | }; | ||
118 | } | ||