aboutsummaryrefslogtreecommitdiff
path: root/src/libs/dutil/test/DUtilUnitTest/IniUtilTest.cpp
blob: 82a12ee82598b2986c5cefe55d12fa3244d5a4a6 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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;

typedef HRESULT (__clrcall *IniFormatParameters)(
    INI_HANDLE
    );

namespace DutilTests
{
    public ref class IniUtil
    {
    public:
        [Fact]
        void IniUtilTest()
        {
            HRESULT hr = S_OK;
            LPWSTR sczTempIniFilePath = NULL;
            LPWSTR sczTempIniFileDir = NULL;
            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";
            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";

            DutilInitialize(&DutilTestTraceError);

            try
            {
                hr = PathExpand(&sczTempIniFilePath, L"%TEMP%\\IniUtilTest\\Test.ini", PATH_EXPAND_ENVIRONMENT);
                NativeAssert::Succeeded(hr, "Failed to get path to temp INI file");

                hr = PathGetDirectory(sczTempIniFilePath, &sczTempIniFileDir);
                NativeAssert::Succeeded(hr, "Failed to get directory to temp INI file");

                hr = DirEnsureDelete(sczTempIniFileDir, TRUE, TRUE);
                if (E_PATHNOTFOUND == hr)
                {
                    hr = S_OK;
                }
                NativeAssert::Succeeded(hr, "Failed to delete IniUtilTest directory: {0}", sczTempIniFileDir);

                hr = DirEnsureExists(sczTempIniFileDir, NULL);
                NativeAssert::Succeeded(hr, "Failed to ensure temp directory exists: {0}", sczTempIniFileDir);

                // Tests parsing, then modifying a regular INI file
                TestReadThenWrite(sczTempIniFilePath, StandardIniFormat, wzIniContents);

                // Tests programmatically creating from scratch, then parsing an INI file
                TestWriteThenRead(sczTempIniFilePath, StandardIniFormat);

                // Tests parsing, then modifying a regular INI file
                TestReadThenWrite(sczTempIniFilePath, ScriptFormat, wzScriptContents);

                // Tests programmatically creating from scratch, then parsing an INI file
                TestWriteThenRead(sczTempIniFilePath, ScriptFormat);
            }
            finally
            {
                ReleaseStr(sczTempIniFilePath);
                ReleaseStr(sczTempIniFileDir);
                DutilUninitialize();
            }
        }

    private:
        void AssertValue(INI_HANDLE iniHandle, LPCWSTR wzValueName, LPCWSTR wzValue)
        {
            HRESULT hr = S_OK;
            LPWSTR sczValue = NULL;

            try
            {
                hr = IniGetValue(iniHandle, wzValueName, &sczValue);
                NativeAssert::Succeeded(hr, "Failed to get ini value: {0}", wzValueName);

                if (0 != wcscmp(sczValue, wzValue))
                {
                    hr = E_FAIL;
                    ExitOnFailure(hr, "Expected to find value in INI: '%ls'='%ls' - but found value '%ls' instead", wzValueName, wzValue, sczValue);
                }
            }
            finally
            {
                ReleaseStr(sczValue);
            }

        LExit:
            return;
        }

        void AssertNoValue(INI_HANDLE iniHandle, LPCWSTR wzValueName)
        {
            HRESULT hr = S_OK;
            LPWSTR sczValue = NULL;

            try
            {
                hr = IniGetValue(iniHandle, wzValueName, &sczValue);
                if (E_NOTFOUND != hr)
                {
                    if (SUCCEEDED(hr))
                    {
                        hr = E_FAIL;
                    }
                    ExitOnFailure(hr, "INI value shouldn't have been found: %ls", wzValueName);
                }
            }
            finally
            {
                ReleaseStr(sczValue);
            }

        LExit:
            return;
        }

        static HRESULT StandardIniFormat(__inout INI_HANDLE iniHandle)
        {
            HRESULT hr = S_OK;

            hr = IniSetOpenTag(iniHandle, L"[", L"]");
            NativeAssert::Succeeded(hr, "Failed to set open tag settings on ini handle");

            hr = IniSetValueStyle(iniHandle, NULL, L"=");
            NativeAssert::Succeeded(hr, "Failed to set value separator setting on ini handle");

            hr = IniSetCommentStyle(iniHandle, L";");
            NativeAssert::Succeeded(hr, "Failed to set comment style setting on ini handle");

            return hr;
        }

        static HRESULT ScriptFormat(__inout INI_HANDLE iniHandle)
        {
            HRESULT hr = S_OK;

            hr = IniSetValueStyle(iniHandle, L"setf ~", L" ");
            NativeAssert::Succeeded(hr, "Failed to set value separator setting on ini handle");

            return hr;
        }

        void TestReadThenWrite(LPWSTR wzIniFilePath, IniFormatParameters SetFormat, LPCWSTR wzContents)
        {
            HRESULT hr = S_OK;
            INI_HANDLE iniHandle = NULL;
            INI_HANDLE iniHandle2 = NULL;
            INI_VALUE *rgValues = NULL;
            DWORD cValues = 0;

            try
            {
                hr = FileWrite(wzIniFilePath, 0, reinterpret_cast<LPCBYTE>(wzContents), lstrlenW(wzContents) * sizeof(WCHAR), NULL);
                NativeAssert::Succeeded(hr, "Failed to write out INI file");

                hr = IniInitialize(&iniHandle);
                NativeAssert::Succeeded(hr, "Failed to initialize INI object");

                hr = SetFormat(iniHandle);
                NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");

                hr = IniParse(iniHandle, wzIniFilePath, NULL);
                NativeAssert::Succeeded(hr, "Failed to parse INI file");

                hr = IniGetValueList(iniHandle, &rgValues, &cValues);
                NativeAssert::Succeeded(hr, "Failed to get list of values in INI");

                NativeAssert::Equal<DWORD>(5, cValues);

                AssertValue(iniHandle, L"PlainValue", L"Blah");
                AssertNoValue(iniHandle, L"PlainValue2");
                AssertValue(iniHandle, L"Section1\\Section1ValueA", L"Foo");
                AssertValue(iniHandle, L"Section1\\Section1ValueB", L"Bar");
                AssertValue(iniHandle, L"Section2\\Section2ValueA", L"Cha");
                AssertNoValue(iniHandle, L"Section1\\ValueDoesntExist");
                AssertValue(iniHandle, L"Section2\\Array[0]", L"Arr");

                hr = IniSetValue(iniHandle, L"PlainValue2", L"Blah2");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Section1\\CreatedValue", L"Woo");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Section2\\Array[0]", L"Arrmod");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniGetValueList(iniHandle, &rgValues, &cValues);
                NativeAssert::Succeeded(hr, "Failed to get list of values in INI");

                NativeAssert::Equal<DWORD>(7, cValues);

                AssertValue(iniHandle, L"PlainValue", L"Blah");
                AssertValue(iniHandle, L"PlainValue2", L"Blah2");
                AssertValue(iniHandle, L"Section1\\Section1ValueA", L"Foo");
                AssertValue(iniHandle, L"Section1\\Section1ValueB", L"Bar");
                AssertValue(iniHandle, L"Section2\\Section2ValueA", L"Cha");
                AssertNoValue(iniHandle, L"Section1\\ValueDoesntExist");
                AssertValue(iniHandle, L"Section1\\CreatedValue", L"Woo");
                AssertValue(iniHandle, L"Section2\\Array[0]", L"Arrmod");

                // Try deleting a value as well
                hr = IniSetValue(iniHandle, L"Section1\\Section1ValueB", NULL);
                NativeAssert::Succeeded(hr, "Failed to kill value in INI");

                hr = IniWriteFile(iniHandle, NULL, FILE_ENCODING_UNSPECIFIED);
                NativeAssert::Succeeded(hr, "Failed to write ini file back out to disk");

                ReleaseNullIni(iniHandle);
                // Now re-parse the INI we just wrote and make sure it matches the values we expect
                hr = IniInitialize(&iniHandle2);
                NativeAssert::Succeeded(hr, "Failed to initialize INI object");

                hr = SetFormat(iniHandle2);
                NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");

                hr = IniParse(iniHandle2, wzIniFilePath, NULL);
                NativeAssert::Succeeded(hr, "Failed to parse INI file");

                hr = IniGetValueList(iniHandle2, &rgValues, &cValues);
                NativeAssert::Succeeded(hr, "Failed to get list of values in INI");

                NativeAssert::Equal<DWORD>(6, cValues);

                AssertValue(iniHandle2, L"PlainValue", L"Blah");
                AssertValue(iniHandle2, L"PlainValue2", L"Blah2");
                AssertValue(iniHandle2, L"Section1\\Section1ValueA", L"Foo");
                AssertNoValue(iniHandle2, L"Section1\\Section1ValueB");
                AssertValue(iniHandle2, L"Section2\\Section2ValueA", L"Cha");
                AssertNoValue(iniHandle2, L"Section1\\ValueDoesntExist");
                AssertValue(iniHandle2, L"Section1\\CreatedValue", L"Woo");
                AssertValue(iniHandle2, L"Section2\\Array[0]", L"Arrmod");
            }
            finally
            {
                ReleaseIni(iniHandle);
                ReleaseIni(iniHandle2);
            }
        }

        void TestWriteThenRead(LPWSTR wzIniFilePath, IniFormatParameters SetFormat)
        {
            HRESULT hr = S_OK;
            INI_HANDLE iniHandle = NULL;
            INI_HANDLE iniHandle2 = NULL;
            INI_VALUE *rgValues = NULL;
            DWORD cValues = 0;

            try
            {
                hr = FileEnsureDelete(wzIniFilePath);
                NativeAssert::Succeeded(hr, "Failed to ensure file is deleted");

                hr = IniInitialize(&iniHandle);
                NativeAssert::Succeeded(hr, "Failed to initialize INI object");

                hr = SetFormat(iniHandle);
                NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");

                hr = IniGetValueList(iniHandle, &rgValues, &cValues);
                NativeAssert::Succeeded(hr, "Failed to get list of values in INI");
                
                NativeAssert::Equal<DWORD>(0, cValues);

                hr = IniSetValue(iniHandle, L"Value1", L"BlahTypo");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Value2", L"Blah2");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Section1\\Value1", L"Section1Value1");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Section1\\Value2", L"Section1Value2");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Section2\\Value1", L"Section2Value1");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Section2\\Array[0]", L"Arr");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Value3", L"Blah3");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Value4", L"Blah4");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Value4", NULL);
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniSetValue(iniHandle, L"Value1", L"Blah1");
                NativeAssert::Succeeded(hr, "Failed to set value in INI");

                hr = IniGetValueList(iniHandle, &rgValues, &cValues);
                NativeAssert::Succeeded(hr, "Failed to get list of values in INI");

                NativeAssert::Equal<DWORD>(8, cValues);

                AssertValue(iniHandle, L"Value1", L"Blah1");
                AssertValue(iniHandle, L"Value2", L"Blah2");
                AssertValue(iniHandle, L"Value3", L"Blah3");
                AssertNoValue(iniHandle, L"Value4");
                AssertValue(iniHandle, L"Section1\\Value1", L"Section1Value1");
                AssertValue(iniHandle, L"Section1\\Value2", L"Section1Value2");
                AssertValue(iniHandle, L"Section2\\Value1", L"Section2Value1");
                AssertValue(iniHandle, L"Section2\\Array[0]", L"Arr");

                hr = IniWriteFile(iniHandle, wzIniFilePath, FILE_ENCODING_UNSPECIFIED);
                NativeAssert::Succeeded(hr, "Failed to write ini file back out to disk");

                ReleaseNullIni(iniHandle);
                // Now re-parse the INI we just wrote and make sure it matches the values we expect
                hr = IniInitialize(&iniHandle2);
                NativeAssert::Succeeded(hr, "Failed to initialize INI object");

                hr = SetFormat(iniHandle2);
                NativeAssert::Succeeded(hr, "Failed to set parameters for INI file");

                hr = IniParse(iniHandle2, wzIniFilePath, NULL);
                NativeAssert::Succeeded(hr, "Failed to parse INI file");

                hr = IniGetValueList(iniHandle2, &rgValues, &cValues);
                NativeAssert::Succeeded(hr, "Failed to get list of values in INI");

                NativeAssert::Equal<DWORD>(7, cValues);

                AssertValue(iniHandle2, L"Value1", L"Blah1");
                AssertValue(iniHandle2, L"Value2", L"Blah2");
                AssertValue(iniHandle2, L"Value3", L"Blah3");
                AssertNoValue(iniHandle2, L"Value4");
                AssertValue(iniHandle2, L"Section1\\Value1", L"Section1Value1");
                AssertValue(iniHandle2, L"Section1\\Value2", L"Section1Value2");
                AssertValue(iniHandle2, L"Section2\\Value1", L"Section2Value1");
                AssertValue(iniHandle2, L"Section2\\Array[0]", L"Arr");
            }
            finally
            {
                ReleaseIni(iniHandle);
                ReleaseIni(iniHandle2);
            }
        }
    };
}