diff options
Diffstat (limited to 'src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp')
-rw-r--r-- | src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp b/src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp new file mode 100644 index 00000000..ac071ef2 --- /dev/null +++ b/src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp | |||
@@ -0,0 +1,125 @@ | |||
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 WixBuildTools::TestSupport; | ||
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 | DutilInitialize(&DutilTestTraceError); | ||
22 | |||
23 | try | ||
24 | { | ||
25 | hr = PathExpand(&sczTempDir, L"%TEMP%\\FileUtilTest\\", PATH_EXPAND_ENVIRONMENT); | ||
26 | NativeAssert::Succeeded(hr, "Failed to get temp dir"); | ||
27 | |||
28 | hr = PathExpand(&sczFileDir, L"%WIX_ROOT%\\examples\\data\\TextEncodings\\", PATH_EXPAND_ENVIRONMENT); | ||
29 | NativeAssert::Succeeded(hr, "Failed to get path to encodings file dir"); | ||
30 | |||
31 | hr = DirEnsureExists(sczTempDir, NULL); | ||
32 | NativeAssert::Succeeded(hr, "Failed to ensure directory exists: {0}", sczTempDir); | ||
33 | |||
34 | TestFile(sczFileDir, sczTempDir, L"ANSI.txt", 32, FILE_ENCODING_UTF8); | ||
35 | // Big endian not supported today! | ||
36 | //TestFile(sczFileDir, L"UnicodeBENoBOM.txt", 34); | ||
37 | //TestFile(sczFileDir, L"UnicodeBEWithBOM.txt", 34); | ||
38 | TestFile(sczFileDir, sczTempDir, L"UnicodeLENoBOM.txt", 34, FILE_ENCODING_UTF16); | ||
39 | TestFile(sczFileDir, sczTempDir, L"UnicodeLEWithBOM.txt", 34, FILE_ENCODING_UTF16_WITH_BOM); | ||
40 | TestFile(sczFileDir, sczTempDir, L"UTF8WithSignature.txt", 34, FILE_ENCODING_UTF8_WITH_BOM); | ||
41 | |||
42 | hr = DirEnsureDelete(sczTempDir, TRUE, TRUE); | ||
43 | } | ||
44 | finally | ||
45 | { | ||
46 | ReleaseStr(sczTempDir); | ||
47 | ReleaseStr(sczFileDir); | ||
48 | DutilUninitialize(); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | private: | ||
53 | void TestFile(LPWSTR wzDir, LPCWSTR wzTempDir, LPWSTR wzFileName, size_t cbExpectedStringLength, FILE_ENCODING feExpectedEncoding) | ||
54 | { | ||
55 | HRESULT hr = S_OK; | ||
56 | LPWSTR sczFullPath = NULL; | ||
57 | LPWSTR sczContents = NULL; | ||
58 | LPWSTR sczOutputPath = NULL; | ||
59 | FILE_ENCODING feEncodingFound = FILE_ENCODING_UNSPECIFIED; | ||
60 | BYTE *pbFile1 = NULL; | ||
61 | DWORD cbFile1 = 0; | ||
62 | BYTE *pbFile2 = NULL; | ||
63 | DWORD cbFile2 = 0; | ||
64 | size_t cbActualStringLength = 0; | ||
65 | |||
66 | try | ||
67 | { | ||
68 | hr = PathConcat(wzDir, wzFileName, &sczFullPath); | ||
69 | NativeAssert::Succeeded(hr, "Failed to create path to test file: {0}", sczFullPath); | ||
70 | |||
71 | hr = FileToString(sczFullPath, &sczContents, &feEncodingFound); | ||
72 | hr = E_FAIL; | ||
73 | NativeAssert::Succeeded(hr, "Failed to read text from file: {0}", sczFullPath); | ||
74 | |||
75 | if (!sczContents) | ||
76 | { | ||
77 | hr = E_FAIL; | ||
78 | NativeAssert::Succeeded(hr, "FileToString() returned NULL for file: {0}", sczFullPath); | ||
79 | } | ||
80 | |||
81 | hr = ::StringCchLengthW(sczContents, STRSAFE_MAX_CCH, &cbActualStringLength); | ||
82 | NativeAssert::Succeeded(hr, "Failed to get length of text from file: {0}", sczFullPath); | ||
83 | |||
84 | if (cbActualStringLength != cbExpectedStringLength) | ||
85 | { | ||
86 | hr = E_FAIL; | ||
87 | ExitOnFailure(hr, "FileToString() returned wrong size for file: %ls (expected size %Iu, found size %Iu)", sczFullPath, cbExpectedStringLength, cbActualStringLength); | ||
88 | } | ||
89 | |||
90 | if (feEncodingFound != feExpectedEncoding) | ||
91 | { | ||
92 | hr = E_FAIL; | ||
93 | ExitOnFailure(hr, "FileToString() returned unexpected encoding type for file: %ls (expected type %u, found type %u)", sczFullPath, feExpectedEncoding, feEncodingFound); | ||
94 | } | ||
95 | |||
96 | hr = PathConcat(wzTempDir, wzFileName, &sczOutputPath); | ||
97 | NativeAssert::Succeeded(hr, "Failed to get output path"); | ||
98 | |||
99 | hr = FileFromString(sczOutputPath, 0, sczContents, feExpectedEncoding); | ||
100 | NativeAssert::Succeeded(hr, "Failed to write contents of file back out to disk"); | ||
101 | |||
102 | hr = FileRead(&pbFile1, &cbFile1, sczFullPath); | ||
103 | NativeAssert::Succeeded(hr, "Failed to read input file as binary"); | ||
104 | |||
105 | hr = FileRead(&pbFile2, &cbFile2, sczOutputPath); | ||
106 | NativeAssert::Succeeded(hr, "Failed to read output file as binary"); | ||
107 | |||
108 | if (cbFile1 != cbFile2 || 0 != memcmp(pbFile1, pbFile2, cbFile1)) | ||
109 | { | ||
110 | hr = E_FAIL; | ||
111 | ExitOnFailure(hr, "Outputted file doesn't match input file: \"%ls\" and \"%ls\"", sczFullPath, sczOutputPath); | ||
112 | } | ||
113 | } | ||
114 | finally | ||
115 | { | ||
116 | ReleaseStr(sczOutputPath); | ||
117 | ReleaseStr(sczFullPath); | ||
118 | ReleaseStr(sczContents); | ||
119 | } | ||
120 | |||
121 | LExit: | ||
122 | return; | ||
123 | } | ||
124 | }; | ||
125 | } | ||