aboutsummaryrefslogtreecommitdiff
path: root/src/test/DUtilUnitTest/IniUtilTest.cpp
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2020-07-11 21:11:30 +1000
committerSean Hall <r.sean.hall@gmail.com>2020-07-12 11:51:29 +1000
commit9c4b5559ccb55491fe68b0096d1be0496fd6fcc7 (patch)
treefaa51204fc2e6e3dfd1b00ae03c7184c4fb0b4bf /src/test/DUtilUnitTest/IniUtilTest.cpp
parentf651268309263fa67fa84caf9fb6dbebb5c900d9 (diff)
downloadwix-9c4b5559ccb55491fe68b0096d1be0496fd6fcc7.tar.gz
wix-9c4b5559ccb55491fe68b0096d1be0496fd6fcc7.tar.bz2
wix-9c4b5559ccb55491fe68b0096d1be0496fd6fcc7.zip
Import DUtilUnitTest from old wix4 repo.
Diffstat (limited to 'src/test/DUtilUnitTest/IniUtilTest.cpp')
-rw-r--r--src/test/DUtilUnitTest/IniUtilTest.cpp342
1 files changed, 342 insertions, 0 deletions
diff --git a/src/test/DUtilUnitTest/IniUtilTest.cpp b/src/test/DUtilUnitTest/IniUtilTest.cpp
new file mode 100644
index 00000000..e28f357e
--- /dev/null
+++ b/src/test/DUtilUnitTest/IniUtilTest.cpp
@@ -0,0 +1,342 @@
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
5using namespace System;
6using namespace Xunit;
7using namespace WixTest;
8
9typedef HRESULT (__clrcall *IniFormatParameters)(
10 INI_HANDLE
11 );
12
13namespace DutilTests
14{
15 public ref class IniUtil
16 {
17 public:
18 [Fact]
19 void IniUtilTest()
20 {
21 HRESULT hr = S_OK;
22 LPWSTR sczTempIniFilePath = NULL;
23 LPWSTR sczTempIniFileDir = NULL;
24 LPWSTR wzIniContents = L" PlainValue = \t Blah \r\n;CommentHere\r\n[Section1]\r\n ;Another Comment With = Equal Sign\r\nSection1ValueA=Foo\r\n\r\nSection1ValueB=Bar\r\n[Section2]\r\nSection2ValueA=Cha\r\nArray[0]=Arr\r\n";
25 LPWSTR wzScriptContents = L"setf ~PlainValue Blah\r\n;CommentHere\r\n\r\nsetf ~Section1\\Section1ValueA Foo\r\n\r\nsetf ~Section1\\Section1ValueB Bar\r\nsetf ~Section2\\Section2ValueA Cha\r\nsetf ~Section2\\Array[0] Arr\r\n";
26
27 try
28 {
29 hr = PathExpand(&sczTempIniFilePath, L"%TEMP%\\IniUtilTest\\Test.ini", PATH_EXPAND_ENVIRONMENT);
30 NativeAssert::Succeeded(hr, "Failed to get path to temp INI file");
31
32 hr = PathGetDirectory(sczTempIniFilePath, &sczTempIniFileDir);
33 NativeAssert::Succeeded(hr, "Failed to get directory to temp INI file");
34
35 hr = DirEnsureDelete(sczTempIniFileDir, TRUE, TRUE);
36 if (E_PATHNOTFOUND == hr)
37 {
38 hr = S_OK;
39 }
40 NativeAssert::Succeeded(hr, "Failed to delete IniUtilTest directory: {0}", sczTempIniFileDir);
41
42 hr = DirEnsureExists(sczTempIniFileDir, NULL);
43 NativeAssert::Succeeded(hr, "Failed to ensure temp directory exists: {0}", sczTempIniFileDir);
44
45 // Tests parsing, then modifying a regular INI file
46 TestReadThenWrite(sczTempIniFilePath, StandardIniFormat, wzIniContents);
47
48 // Tests programmatically creating from scratch, then parsing an INI file
49 TestWriteThenRead(sczTempIniFilePath, StandardIniFormat);
50
51 // Tests parsing, then modifying a regular INI file
52 TestReadThenWrite(sczTempIniFilePath, ScriptFormat, wzScriptContents);
53
54 // Tests programmatically creating from scratch, then parsing an INI file
55 TestWriteThenRead(sczTempIniFilePath, ScriptFormat);
56 }
57 finally
58 {
59 ReleaseStr(sczTempIniFilePath);
60 ReleaseStr(sczTempIniFileDir);
61 }
62 }
63
64 private:
65 void AssertValue(INI_HANDLE iniHandle, LPCWSTR wzValueName, LPCWSTR wzValue)
66 {
67 HRESULT hr = S_OK;
68 LPWSTR sczValue = NULL;
69
70 try
71 {
72 hr = IniGetValue(iniHandle, wzValueName, &sczValue);
73 NativeAssert::Succeeded(hr, "Failed to get ini value: {0}", wzValueName);
74
75 if (0 != wcscmp(sczValue, wzValue))
76 {
77 hr = E_FAIL;
78 ExitOnFailure(hr, "Expected to find value in INI: '%ls'='%ls' - but found value '%ls' instead", wzValueName, wzValue, sczValue);
79 }
80 }
81 finally
82 {
83 ReleaseStr(sczValue);
84 }
85
86 LExit:
87 return;
88 }
89
90 void AssertNoValue(INI_HANDLE iniHandle, LPCWSTR wzValueName)
91 {
92 HRESULT hr = S_OK;
93 LPWSTR sczValue = NULL;
94
95 try
96 {
97 hr = IniGetValue(iniHandle, wzValueName, &sczValue);
98 if (E_NOTFOUND != hr)
99 {
100 if (SUCCEEDED(hr))
101 {
102 hr = E_FAIL;
103 }
104 ExitOnFailure(hr, "INI value shouldn't have been found: %ls", wzValueName);
105 }
106 }
107 finally
108 {
109 ReleaseStr(sczValue);
110 }
111
112 LExit:
113 return;
114 }
115
116 static HRESULT StandardIniFormat(__inout INI_HANDLE iniHandle)
117 {
118 HRESULT hr = S_OK;
119
120 hr = IniSetOpenTag(iniHandle, L"[", L"]");
121 NativeAssert::Succeeded(hr, "Failed to set open tag settings on ini handle");
122
123 hr = IniSetValueStyle(iniHandle, NULL, L"=");
124 NativeAssert::Succeeded(hr, "Failed to set value separator setting on ini handle");
125
126 hr = IniSetCommentStyle(iniHandle, L";");
127 NativeAssert::Succeeded(hr, "Failed to set comment style setting on ini handle");
128
129 return hr;
130 }
131
132 static HRESULT ScriptFormat(__inout INI_HANDLE iniHandle)
133 {
134 HRESULT hr = S_OK;
135
136 hr = IniSetValueStyle(iniHandle, L"setf ~", L" ");
137 NativeAssert::Succeeded(hr, "Failed to set value separator setting on ini handle");
138
139 return hr;
140 }
141
142 void TestReadThenWrite(LPWSTR wzIniFilePath, IniFormatParameters SetFormat, LPCWSTR wzContents)
143 {
144 HRESULT hr = S_OK;
145 INI_HANDLE iniHandle = NULL;
146 INI_HANDLE iniHandle2 = NULL;
147 INI_VALUE *rgValues = NULL;
148 DWORD cValues = 0;
149
150 try
151 {
152 hr = FileWrite(wzIniFilePath, 0, reinterpret_cast<LPCBYTE>(wzContents), lstrlenW(wzContents) * sizeof(WCHAR), NULL);
153 NativeAssert::Succeeded(hr, "Failed to write out INI file");
154
155 hr = IniInitialize(&iniHandle);
156 NativeAssert::Succeeded(hr, "Failed to initialize INI object");
157
158 hr = SetFormat(iniHandle);
159 NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");
160
161 hr = IniParse(iniHandle, wzIniFilePath, NULL);
162 NativeAssert::Succeeded(hr, "Failed to parse INI file");
163
164 hr = IniGetValueList(iniHandle, &rgValues, &cValues);
165 NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
166
167 NativeAssert::Equal<DWORD>(5, cValues);
168
169 AssertValue(iniHandle, L"PlainValue", L"Blah");
170 AssertNoValue(iniHandle, L"PlainValue2");
171 AssertValue(iniHandle, L"Section1\\Section1ValueA", L"Foo");
172 AssertValue(iniHandle, L"Section1\\Section1ValueB", L"Bar");
173 AssertValue(iniHandle, L"Section2\\Section2ValueA", L"Cha");
174 AssertNoValue(iniHandle, L"Section1\\ValueDoesntExist");
175 AssertValue(iniHandle, L"Section2\\Array[0]", L"Arr");
176
177 hr = IniSetValue(iniHandle, L"PlainValue2", L"Blah2");
178 NativeAssert::Succeeded(hr, "Failed to set value in INI");
179
180 hr = IniSetValue(iniHandle, L"Section1\\CreatedValue", L"Woo");
181 NativeAssert::Succeeded(hr, "Failed to set value in INI");
182
183 hr = IniSetValue(iniHandle, L"Section2\\Array[0]", L"Arrmod");
184 NativeAssert::Succeeded(hr, "Failed to set value in INI");
185
186 hr = IniGetValueList(iniHandle, &rgValues, &cValues);
187 NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
188
189 NativeAssert::Equal<DWORD>(7, cValues);
190
191 AssertValue(iniHandle, L"PlainValue", L"Blah");
192 AssertValue(iniHandle, L"PlainValue2", L"Blah2");
193 AssertValue(iniHandle, L"Section1\\Section1ValueA", L"Foo");
194 AssertValue(iniHandle, L"Section1\\Section1ValueB", L"Bar");
195 AssertValue(iniHandle, L"Section2\\Section2ValueA", L"Cha");
196 AssertNoValue(iniHandle, L"Section1\\ValueDoesntExist");
197 AssertValue(iniHandle, L"Section1\\CreatedValue", L"Woo");
198 AssertValue(iniHandle, L"Section2\\Array[0]", L"Arrmod");
199
200 // Try deleting a value as well
201 hr = IniSetValue(iniHandle, L"Section1\\Section1ValueB", NULL);
202 NativeAssert::Succeeded(hr, "Failed to kill value in INI");
203
204 hr = IniWriteFile(iniHandle, NULL, FILE_ENCODING_UNSPECIFIED);
205 NativeAssert::Succeeded(hr, "Failed to write ini file back out to disk");
206
207 ReleaseNullIni(iniHandle);
208 // Now re-parse the INI we just wrote and make sure it matches the values we expect
209 hr = IniInitialize(&iniHandle2);
210 NativeAssert::Succeeded(hr, "Failed to initialize INI object");
211
212 hr = SetFormat(iniHandle2);
213 NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");
214
215 hr = IniParse(iniHandle2, wzIniFilePath, NULL);
216 NativeAssert::Succeeded(hr, "Failed to parse INI file");
217
218 hr = IniGetValueList(iniHandle2, &rgValues, &cValues);
219 NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
220
221 NativeAssert::Equal<DWORD>(6, cValues);
222
223 AssertValue(iniHandle2, L"PlainValue", L"Blah");
224 AssertValue(iniHandle2, L"PlainValue2", L"Blah2");
225 AssertValue(iniHandle2, L"Section1\\Section1ValueA", L"Foo");
226 AssertNoValue(iniHandle2, L"Section1\\Section1ValueB");
227 AssertValue(iniHandle2, L"Section2\\Section2ValueA", L"Cha");
228 AssertNoValue(iniHandle2, L"Section1\\ValueDoesntExist");
229 AssertValue(iniHandle2, L"Section1\\CreatedValue", L"Woo");
230 AssertValue(iniHandle2, L"Section2\\Array[0]", L"Arrmod");
231 }
232 finally
233 {
234 ReleaseIni(iniHandle);
235 ReleaseIni(iniHandle2);
236 }
237 }
238
239 void TestWriteThenRead(LPWSTR wzIniFilePath, IniFormatParameters SetFormat)
240 {
241 HRESULT hr = S_OK;
242 INI_HANDLE iniHandle = NULL;
243 INI_HANDLE iniHandle2 = NULL;
244 INI_VALUE *rgValues = NULL;
245 DWORD cValues = 0;
246
247 try
248 {
249 hr = FileEnsureDelete(wzIniFilePath);
250 NativeAssert::Succeeded(hr, "Failed to ensure file is deleted");
251
252 hr = IniInitialize(&iniHandle);
253 NativeAssert::Succeeded(hr, "Failed to initialize INI object");
254
255 hr = SetFormat(iniHandle);
256 NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");
257
258 hr = IniGetValueList(iniHandle, &rgValues, &cValues);
259 NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
260
261 NativeAssert::Equal<DWORD>(0, cValues);
262
263 hr = IniSetValue(iniHandle, L"Value1", L"BlahTypo");
264 NativeAssert::Succeeded(hr, "Failed to set value in INI");
265
266 hr = IniSetValue(iniHandle, L"Value2", L"Blah2");
267 NativeAssert::Succeeded(hr, "Failed to set value in INI");
268
269 hr = IniSetValue(iniHandle, L"Section1\\Value1", L"Section1Value1");
270 NativeAssert::Succeeded(hr, "Failed to set value in INI");
271
272 hr = IniSetValue(iniHandle, L"Section1\\Value2", L"Section1Value2");
273 NativeAssert::Succeeded(hr, "Failed to set value in INI");
274
275 hr = IniSetValue(iniHandle, L"Section2\\Value1", L"Section2Value1");
276 NativeAssert::Succeeded(hr, "Failed to set value in INI");
277
278 hr = IniSetValue(iniHandle, L"Section2\\Array[0]", L"Arr");
279 NativeAssert::Succeeded(hr, "Failed to set value in INI");
280
281 hr = IniSetValue(iniHandle, L"Value3", L"Blah3");
282 NativeAssert::Succeeded(hr, "Failed to set value in INI");
283
284 hr = IniSetValue(iniHandle, L"Value4", L"Blah4");
285 NativeAssert::Succeeded(hr, "Failed to set value in INI");
286
287 hr = IniSetValue(iniHandle, L"Value4", NULL);
288 NativeAssert::Succeeded(hr, "Failed to set value in INI");
289
290 hr = IniSetValue(iniHandle, L"Value1", L"Blah1");
291 NativeAssert::Succeeded(hr, "Failed to set value in INI");
292
293 hr = IniGetValueList(iniHandle, &rgValues, &cValues);
294 NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
295
296 NativeAssert::Equal<DWORD>(8, cValues);
297
298 AssertValue(iniHandle, L"Value1", L"Blah1");
299 AssertValue(iniHandle, L"Value2", L"Blah2");
300 AssertValue(iniHandle, L"Value3", L"Blah3");
301 AssertNoValue(iniHandle, L"Value4");
302 AssertValue(iniHandle, L"Section1\\Value1", L"Section1Value1");
303 AssertValue(iniHandle, L"Section1\\Value2", L"Section1Value2");
304 AssertValue(iniHandle, L"Section2\\Value1", L"Section2Value1");
305 AssertValue(iniHandle, L"Section2\\Array[0]", L"Arr");
306
307 hr = IniWriteFile(iniHandle, wzIniFilePath, FILE_ENCODING_UNSPECIFIED);
308 NativeAssert::Succeeded(hr, "Failed to write ini file back out to disk");
309
310 ReleaseNullIni(iniHandle);
311 // Now re-parse the INI we just wrote and make sure it matches the values we expect
312 hr = IniInitialize(&iniHandle2);
313 NativeAssert::Succeeded(hr, "Failed to initialize INI object");
314
315 hr = SetFormat(iniHandle2);
316 NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");
317
318 hr = IniParse(iniHandle2, wzIniFilePath, NULL);
319 NativeAssert::Succeeded(hr, "Failed to parse INI file");
320
321 hr = IniGetValueList(iniHandle2, &rgValues, &cValues);
322 NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
323
324 NativeAssert::Equal<DWORD>(7, cValues);
325
326 AssertValue(iniHandle2, L"Value1", L"Blah1");
327 AssertValue(iniHandle2, L"Value2", L"Blah2");
328 AssertValue(iniHandle2, L"Value3", L"Blah3");
329 AssertNoValue(iniHandle2, L"Value4");
330 AssertValue(iniHandle2, L"Section1\\Value1", L"Section1Value1");
331 AssertValue(iniHandle2, L"Section1\\Value2", L"Section1Value2");
332 AssertValue(iniHandle2, L"Section2\\Value1", L"Section2Value1");
333 AssertValue(iniHandle2, L"Section2\\Array[0]", L"Arr");
334 }
335 finally
336 {
337 ReleaseIni(iniHandle);
338 ReleaseIni(iniHandle2);
339 }
340 }
341 };
342}