-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplist-parser.spec.ts
More file actions
334 lines (287 loc) · 10.8 KB
/
Copy pathplist-parser.spec.ts
File metadata and controls
334 lines (287 loc) · 10.8 KB
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
import assert from 'node:assert/strict';
import {describe, it} from 'node:test';
import {parsePlist as parseXmlPlist} from '../../../src/lib/plist/plist-parser.js';
import {
findFirstReplacementCharacter,
fixMultipleXmlDeclarations,
hasUnicodeReplacementCharacter,
isValidXml,
trimBeforeXmlDeclaration,
} from '../../../src/lib/plist/utils.js';
import type {PlistArray, PlistDictionary} from '../../../src/lib/types.js';
describe('Plist Parser', function () {
describe('XML Cleaning Logic', function () {
it('should detect Unicode replacement characters', function () {
const validXml =
'<?xml version="1.0" encoding="UTF-8"?><plist><dict><key>test</key><string>value</string></dict></plist>';
const invalidXml =
'<?xml version="1.0" encoding="UTF-8"?><plist><dict><key>test</key><string>val�ue</string></dict></plist>';
assert.strictEqual(hasUnicodeReplacementCharacter(validXml), false);
assert.strictEqual(hasUnicodeReplacementCharacter(invalidXml), true);
});
it('should find the position of the first replacement character', function () {
const xml =
'<?xml version="1.0" encoding="UTF-8"?><plist><dict><key>test</key><string>val�ue</string></dict></plist>';
const position = findFirstReplacementCharacter(xml);
assert.strictEqual(position, xml.indexOf('�'));
assert.ok(position > 0);
});
it('should handle the case where there is no tag before the replacement character', function () {
const xml =
'�<?xml version="1.0" encoding="UTF-8"?><plist><dict><key>test</key><string>value</string></dict></plist>';
const result = parseXmlPlist(xml);
assert.strictEqual(result.test, 'value');
});
it('should handle the case where there is no tag after the replacement character', function () {
const xml =
'<?xml version="1.0" encoding="UTF-8"?><plist><dict><key>test</key><string>value</string></dict></plist>�';
const result = parseXmlPlist(xml);
assert.strictEqual(result.test, 'value');
});
});
describe('XML Preprocessing Functions', function () {
it('should trim content before XML declaration', function () {
const xml = 'garbage data<?xml version="1.0" encoding="UTF-8"?><plist></plist>';
const trimmed = trimBeforeXmlDeclaration(xml);
assert.strictEqual(trimmed, '<?xml version="1.0" encoding="UTF-8"?><plist></plist>');
});
it('should fix multiple XML declarations', function () {
const xml = '<?xml version="1.0" encoding="UTF-8"?><some-tag><?xml version="1.1"?><plist></plist>';
const fixed = fixMultipleXmlDeclarations(xml);
assert.ok(fixed.includes('<?xml version="1.0" encoding="UTF-8"?>'));
assert.ok(fixed.includes('<some-tag>'));
assert.ok(fixed.includes('<plist></plist>'));
assert.ok(!fixed.includes('<?xml version="1.1"?>'));
});
it('should validate XML content', function () {
assert.strictEqual(isValidXml('<?xml version="1.0"?><plist></plist>'), true);
assert.strictEqual(isValidXml(''), false);
assert.strictEqual(isValidXml(' '), false);
assert.strictEqual(isValidXml('not xml'), false);
});
});
describe('Error Handling', function () {
it('should handle completely invalid XML', function () {
try {
parseXmlPlist('not xml at all');
assert.fail('Should have thrown an error for invalid XML');
} catch (error) {
assert.ok(error !== null && error !== undefined);
}
});
it('should handle XML without a plist element', function () {
try {
parseXmlPlist('<?xml version="1.0"?><not-a-plist></not-a-plist>');
assert.fail('Should have thrown an error for missing plist element');
} catch (error) {
assert.ok(error !== null && error !== undefined);
}
});
it('should handle XML with malformed tags', function () {
try {
parseXmlPlist('<?xml version="1.0"?><plist><dict><key>test</key><string>value</string></dict>');
assert.fail('Should have thrown an error for malformed tags');
} catch (error) {
assert.ok(error !== null && error !== undefined);
}
});
});
describe('Complex XML Structures', function () {
it('should parse nested dictionaries correctly', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>level1</key>
<dict>
<key>level2</key>
<dict>
<key>level3</key>
<string>deep value</string>
</dict>
</dict>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.ok('level1' in result);
// Type assertions for nested properties
const level1 = result.level1 as PlistDictionary;
assert.ok('level2' in level1);
const level2 = level1.level2 as PlistDictionary;
assert.strictEqual(level2.level3, 'deep value');
// Nested keys must not leak into ancestor dicts
assert.deepStrictEqual(Object.keys(result), ['level1']);
assert.deepStrictEqual(Object.keys(level1), ['level2']);
assert.deepStrictEqual(Object.keys(level2), ['level3']);
});
it('should not let a nested key overwrite an outer key with the same name', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>Result</key>
<string>ok</string>
<key>Nested</key>
<dict>
<key>Result</key>
<integer>1</integer>
<key>Inner</key>
<string>x</string>
</dict>
<key>Name</key>
<string>dev</string>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.strictEqual(result.Result, 'ok');
assert.strictEqual(result.Name, 'dev');
assert.ok(!('Inner' in result));
const nested = result.Nested as PlistDictionary;
assert.strictEqual(nested.Result, 1);
assert.strictEqual(nested.Inner, 'x');
});
it('should not leak keys from dicts nested inside arrays', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>Status</key>
<string>Complete</string>
<key>Items</key>
<array>
<dict>
<key>Status</key>
<string>Pending</string>
<key>Id</key>
<integer>7</integer>
</dict>
</array>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.strictEqual(result.Status, 'Complete');
assert.deepStrictEqual(Object.keys(result), ['Status', 'Items']);
const items = result.Items as PlistArray;
const item = items[0] as PlistDictionary;
assert.strictEqual(item.Status, 'Pending');
assert.strictEqual(item.Id, 7);
});
it('should parse mixed arrays and dictionaries', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>mixedArray</key>
<array>
<string>text</string>
<integer>123</integer>
<dict>
<key>nestedKey</key>
<string>nestedValue</string>
</dict>
</array>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.ok(Array.isArray(result.mixedArray));
// Type assertion for array
const mixedArray = result.mixedArray as PlistArray;
assert.strictEqual(mixedArray.length, 3);
assert.strictEqual(mixedArray[0], 'text');
assert.strictEqual(mixedArray[1], 123);
// Type assertion for nested object in array
const nestedObj = mixedArray[2] as PlistDictionary;
assert.strictEqual(nestedObj.nestedKey, 'nestedValue');
});
it('should handle XML with comments', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<!-- This is a comment -->
<key>commentedKey</key>
<string>value</string>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.strictEqual(result.commentedKey, 'value');
});
it('should handle XML with CDATA sections', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>cdataKey</key>
<string><![CDATA[<html>This is HTML content</html>]]></string>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.strictEqual(result.cdataKey, '<html>This is HTML content</html>');
});
});
describe('Special Data Types', function () {
it('should parse date values correctly', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>dateKey</key>
<date>2023-05-21T12:34:56Z</date>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.ok('dateKey' in result);
// Type assertion for date
const dateValue = result.dateKey as Date;
assert.ok(dateValue instanceof Date);
assert.strictEqual(dateValue.toISOString(), '2023-05-21T12:34:56.000Z');
});
it('should parse data values correctly', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>dataKey</key>
<data>SGVsbG8gV29ybGQ=</data>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.ok('dataKey' in result);
// Type assertion for buffer
const dataValue = result.dataKey as Buffer;
assert.strictEqual(Buffer.isBuffer(dataValue), true);
assert.strictEqual(dataValue.toString(), 'Hello World');
});
it('should handle empty elements correctly', function () {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>emptyString</key>
<string></string>
<key>emptyArray</key>
<array></array>
<key>emptyDict</key>
<dict></dict>
</dict>
</plist>
`;
const result = parseXmlPlist(xml);
assert.strictEqual(result.emptyString, '');
assert.ok(Array.isArray(result.emptyArray));
assert.strictEqual(result.emptyArray.length, 0);
assert.ok(typeof result.emptyDict === 'object' && result.emptyDict !== null && !Array.isArray(result.emptyDict));
// Type assertion for empty dictionary
const emptyDict = result.emptyDict as PlistDictionary;
assert.strictEqual(Object.keys(emptyDict).length, 0);
});
});
});