1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// 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.
#include "precomp.h"
using namespace System;
using namespace Xunit;
using namespace WixInternal::TestSupport;
namespace DutilTests
{
public ref class FileUtil
{
public:
[Fact(Skip="Skipped until we have a good way to reference ANSI.txt.")]
void FileUtilTest()
{
HRESULT hr = S_OK;
LPWSTR sczTempDir = NULL;
LPWSTR sczFileDir = NULL;
DutilInitialize(&DutilTestTraceError);
try
{
hr = PathExpand(&sczTempDir, L"%TEMP%\\FileUtilTest\\", PATH_EXPAND_ENVIRONMENT);
NativeAssert::Succeeded(hr, "Failed to get temp dir");
hr = PathExpand(&sczFileDir, L"%WIX_ROOT%\\examples\\data\\TextEncodings\\", PATH_EXPAND_ENVIRONMENT);
NativeAssert::Succeeded(hr, "Failed to get path to encodings file dir");
hr = DirEnsureExists(sczTempDir, NULL);
NativeAssert::Succeeded(hr, "Failed to ensure directory exists: {0}", sczTempDir);
TestFile(sczFileDir, sczTempDir, L"ANSI.txt", 32, FILE_ENCODING_UTF8);
// Big endian not supported today!
//TestFile(sczFileDir, L"UnicodeBENoBOM.txt", 34);
//TestFile(sczFileDir, L"UnicodeBEWithBOM.txt", 34);
TestFile(sczFileDir, sczTempDir, L"UnicodeLENoBOM.txt", 34, FILE_ENCODING_UTF16);
TestFile(sczFileDir, sczTempDir, L"UnicodeLEWithBOM.txt", 34, FILE_ENCODING_UTF16_WITH_BOM);
TestFile(sczFileDir, sczTempDir, L"UTF8WithSignature.txt", 34, FILE_ENCODING_UTF8_WITH_BOM);
hr = DirEnsureDelete(sczTempDir, TRUE, TRUE);
}
finally
{
ReleaseStr(sczTempDir);
ReleaseStr(sczFileDir);
DutilUninitialize();
}
}
private:
void TestFile(LPWSTR wzDir, LPCWSTR wzTempDir, LPWSTR wzFileName, size_t cbExpectedStringLength, FILE_ENCODING feExpectedEncoding)
{
HRESULT hr = S_OK;
LPWSTR sczFullPath = NULL;
LPWSTR sczContents = NULL;
LPWSTR sczOutputPath = NULL;
FILE_ENCODING feEncodingFound = FILE_ENCODING_UNSPECIFIED;
BYTE *pbFile1 = NULL;
SIZE_T cbFile1 = 0;
BYTE *pbFile2 = NULL;
SIZE_T cbFile2 = 0;
size_t cbActualStringLength = 0;
try
{
hr = PathConcat(wzDir, wzFileName, &sczFullPath);
NativeAssert::Succeeded(hr, "Failed to create path to test file: {0}", sczFullPath);
hr = FileToString(sczFullPath, &sczContents, &feEncodingFound);
hr = E_FAIL;
NativeAssert::Succeeded(hr, "Failed to read text from file: {0}", sczFullPath);
if (!sczContents)
{
hr = E_FAIL;
NativeAssert::Succeeded(hr, "FileToString() returned NULL for file: {0}", sczFullPath);
}
hr = ::StringCchLengthW(sczContents, STRSAFE_MAX_CCH, &cbActualStringLength);
NativeAssert::Succeeded(hr, "Failed to get length of text from file: {0}", sczFullPath);
if (cbActualStringLength != cbExpectedStringLength)
{
hr = E_FAIL;
ExitOnFailure(hr, "FileToString() returned wrong size for file: %ls (expected size %Iu, found size %Iu)", sczFullPath, cbExpectedStringLength, cbActualStringLength);
}
if (feEncodingFound != feExpectedEncoding)
{
hr = E_FAIL;
ExitOnFailure(hr, "FileToString() returned unexpected encoding type for file: %ls (expected type %u, found type %u)", sczFullPath, feExpectedEncoding, feEncodingFound);
}
hr = PathConcat(wzTempDir, wzFileName, &sczOutputPath);
NativeAssert::Succeeded(hr, "Failed to get output path");
hr = FileFromString(sczOutputPath, 0, sczContents, feExpectedEncoding);
NativeAssert::Succeeded(hr, "Failed to write contents of file back out to disk");
hr = FileRead(&pbFile1, &cbFile1, sczFullPath);
NativeAssert::Succeeded(hr, "Failed to read input file as binary");
hr = FileRead(&pbFile2, &cbFile2, sczOutputPath);
NativeAssert::Succeeded(hr, "Failed to read output file as binary");
if (cbFile1 != cbFile2 || 0 != memcmp(pbFile1, pbFile2, cbFile1))
{
hr = E_FAIL;
ExitOnFailure(hr, "Outputted file doesn't match input file: \"%ls\" and \"%ls\"", sczFullPath, sczOutputPath);
}
}
finally
{
ReleaseStr(sczOutputPath);
ReleaseStr(sczFullPath);
ReleaseStr(sczContents);
}
LExit:
return;
}
};
}
|