summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/unit/test_iniProcessor.js
blob: 3d3886d35baa2ffcb127a504ecd556e843089604 (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
var Ci = Components.interfaces;
var Cc = Components.classes;
var Cr = Components.results;

var testnum = 0;
var factory;

function parserForFile(filename) {
    let parser = null;
    try {
        let file = do_get_file(filename);
        do_check_true(!!file);
        parser = factory.createINIParser(file);
        do_check_true(!!parser);
    } catch(e) {
	dump("INFO | caught error: " + e);
        // checkParserOutput will handle a null parser when it's expected.
    }
    return parser;
    
}

function checkParserOutput(parser, expected) {
    // If the expected output is null, we expect the parser to have
    // failed (and vice-versa).
    if (!parser || !expected) {
        do_check_eq(parser, null);
        do_check_eq(expected, null);
        return;
    }

    let output = getParserOutput(parser);
    for (let section in expected) {
        do_check_true(section in output);
        for (let key in expected[section]) {
            do_check_true(key in output[section]);
            do_check_eq(output[section][key], expected[section][key]);
            delete output[section][key];
        }
        for (let key in output[section])
            do_check_eq(key, "wasn't expecting this key!");
        delete output[section];
    }
    for (let section in output)
        do_check_eq(section, "wasn't expecting this section!");
}

function getParserOutput(parser) {
    let output = {};

    let sections = parser.getSections();
    do_check_true(!!sections);
    while (sections.hasMore()) {
        let section = sections.getNext();
        do_check_false(section in output); // catch dupes
        output[section] = {};

        let keys = parser.getKeys(section);
        do_check_true(!!keys);
        while (keys.hasMore()) {
            let key = keys.getNext();
            do_check_false(key in output[section]); // catch dupes
            let value = parser.getString(section, key);
            output[section][key] = value;
        }
    }
    return output;
}

function run_test() {
try {

var testdata = [
    { filename: "data/iniparser01.ini", reference: {} },
    { filename: "data/iniparser02.ini", reference: {} },
    { filename: "data/iniparser03.ini", reference: {} },
    { filename: "data/iniparser04.ini", reference: {} },
    { filename: "data/iniparser05.ini", reference: {} },
    { filename: "data/iniparser06.ini", reference: {} },
    { filename: "data/iniparser07.ini", reference: {} },
    { filename: "data/iniparser08.ini", reference: { section1: { name1: "" }} },
    { filename: "data/iniparser09.ini", reference: { section1: { name1: "value1" } } },
    { filename: "data/iniparser10.ini", reference: { section1: { name1: "value1" } } },
    { filename: "data/iniparser11.ini", reference: { section1: { name1: "value1" } } },
    { filename: "data/iniparser12.ini", reference: { section1: { name1: "value1" } } },
    { filename: "data/iniparser13.ini", reference: { section1: { name1: "value1" } } },
    { filename: "data/iniparser14.ini", reference: 
                    { section1: { name1: "value1", name2: "value2" },
                      section2: { name1: "value1", name2: "foopy"  }} },
    { filename: "data/iniparser15.ini", reference: 
                    { section1: { name1: "newValue1" },
                      section2: { name1: "foopy"     }} },
    { filename: "data/iniparser16.ini", reference: 
                    { "☺♫": { "♫": "☻", "♪": "♥"  },
                       "☼": { "♣": "♠", "♦": "♥"  }} },

    ];

    testdata.push( { filename: "data/iniparser01-utf8BOM.ini",
                     reference: testdata[0].reference } );
    testdata.push( { filename: "data/iniparser02-utf8BOM.ini",
                     reference: testdata[1].reference } );
    testdata.push( { filename: "data/iniparser03-utf8BOM.ini",
                     reference: testdata[2].reference } );
    testdata.push( { filename: "data/iniparser04-utf8BOM.ini",
                     reference: testdata[3].reference } );
    testdata.push( { filename: "data/iniparser05-utf8BOM.ini",
                     reference: testdata[4].reference } );
    testdata.push( { filename: "data/iniparser06-utf8BOM.ini",
                     reference: testdata[5].reference } );
    testdata.push( { filename: "data/iniparser07-utf8BOM.ini",
                     reference: testdata[6].reference } );
    testdata.push( { filename: "data/iniparser08-utf8BOM.ini",
                     reference: testdata[7].reference } );
    testdata.push( { filename: "data/iniparser09-utf8BOM.ini",
                     reference: testdata[8].reference } );
    testdata.push( { filename: "data/iniparser10-utf8BOM.ini",
                     reference: testdata[9].reference } );
    testdata.push( { filename: "data/iniparser11-utf8BOM.ini",
                     reference: testdata[10].reference } );
    testdata.push( { filename: "data/iniparser12-utf8BOM.ini",
                     reference: testdata[11].reference } );
    testdata.push( { filename: "data/iniparser13-utf8BOM.ini",
                     reference: testdata[12].reference } );
    testdata.push( { filename: "data/iniparser14-utf8BOM.ini",
                     reference: testdata[13].reference } );
    testdata.push( { filename: "data/iniparser15-utf8BOM.ini",
                     reference: testdata[14].reference } );
    testdata.push( { filename: "data/iniparser16-utf8BOM.ini",
                     reference: testdata[15].reference } );

    let os = Cc["@mozilla.org/xre/app-info;1"]
             .getService(Ci.nsIXULRuntime).OS;
    if("WINNT" === os) {
        testdata.push( { filename: "data/iniparser01-utf16leBOM.ini",
                         reference: testdata[0].reference } );
        testdata.push( { filename: "data/iniparser02-utf16leBOM.ini",
                         reference: testdata[1].reference } );
        testdata.push( { filename: "data/iniparser03-utf16leBOM.ini",
                         reference: testdata[2].reference } );
        testdata.push( { filename: "data/iniparser04-utf16leBOM.ini",
                         reference: testdata[3].reference } );
        testdata.push( { filename: "data/iniparser05-utf16leBOM.ini",
                         reference: testdata[4].reference } );
        testdata.push( { filename: "data/iniparser06-utf16leBOM.ini",
                         reference: testdata[5].reference } );
        testdata.push( { filename: "data/iniparser07-utf16leBOM.ini",
                         reference: testdata[6].reference } );
        testdata.push( { filename: "data/iniparser08-utf16leBOM.ini",
                         reference: testdata[7].reference } );
        testdata.push( { filename: "data/iniparser09-utf16leBOM.ini",
                         reference: testdata[8].reference } );
        testdata.push( { filename: "data/iniparser10-utf16leBOM.ini",
                         reference: testdata[9].reference } );
        testdata.push( { filename: "data/iniparser11-utf16leBOM.ini",
                         reference: testdata[10].reference } );
        testdata.push( { filename: "data/iniparser12-utf16leBOM.ini",
                         reference: testdata[11].reference } );
        testdata.push( { filename: "data/iniparser13-utf16leBOM.ini",
                         reference: testdata[12].reference } );
        testdata.push( { filename: "data/iniparser14-utf16leBOM.ini",
                         reference: testdata[13].reference } );
        testdata.push( { filename: "data/iniparser15-utf16leBOM.ini",
                         reference: testdata[14].reference } );
        testdata.push( { filename: "data/iniparser16-utf16leBOM.ini",
                         reference: testdata[15].reference } );
    }

/* ========== 0 ========== */
factory = Cc["@mozilla.org/xpcom/ini-processor-factory;1"].
          getService(Ci.nsIINIParserFactory);
do_check_true(!!factory);

// Test reading from a variety of files. While we're at it, write out each one
// and read it back to ensure that nothing changed.
while (testnum < testdata.length) {
    dump("\nINFO | test #" + ++testnum);
    let filename = testdata[testnum -1].filename;
    dump(", filename " + filename + "\n");
    let parser = parserForFile(filename);
    checkParserOutput(parser, testdata[testnum - 1].reference);
    if (!parser)
        continue;
    do_check_true(parser instanceof Ci.nsIINIParserWriter);
    // write contents out to a new file
    let newfilename = filename + ".new";
    let newfile = do_get_file(filename);
    newfile.leafName += ".new";
    parser.writeFile(newfile);
    // read new file and make sure the contents are the same.
    parser = parserForFile(newfilename);
    checkParserOutput(parser, testdata[testnum - 1].reference);
    // cleanup after the test
    newfile.remove(false);
}

dump("INFO | test #" + ++testnum + "\n");

// test writing to a new file.
var newfile = do_get_file("data/");
newfile.append("nonexistent-file.ini");
if (newfile.exists())
    newfile.remove(false);
do_check_false(newfile.exists());

var parser = factory.createINIParser(newfile);
do_check_true(!!parser);
do_check_true(parser instanceof Ci.nsIINIParserWriter);
checkParserOutput(parser, {});
parser.writeFile();
do_check_true(newfile.exists());

// test adding a new section and new key
parser.setString("section", "key", "value");
parser.writeFile();
do_check_true(newfile.exists());
checkParserOutput(parser, {section: {key: "value"} });
// read it in again, check for same data.
parser = parserForFile("data/nonexistent-file.ini");
checkParserOutput(parser, {section: {key: "value"} });
// cleanup after the test
newfile.remove(false);

dump("INFO | test #" + ++testnum + "\n");

// test modifying a existing key's value (in an existing section)
parser = parserForFile("data/iniparser09.ini");
checkParserOutput(parser, {section1: {name1: "value1"} });

do_check_true(parser instanceof Ci.nsIINIParserWriter);
parser.setString("section1", "name1", "value2");
checkParserOutput(parser, {section1: {name1: "value2"} });

dump("INFO | test #" + ++testnum + "\n");

// test trying to set illegal characters
var caughtError;
caughtError = false;
checkParserOutput(parser, {section1: {name1: "value2"} });

// Bad characters in section name
try { parser.SetString("bad\0", "ok", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("bad\r", "ok", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("bad\n", "ok", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("bad[", "ok", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("bad]", "ok", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);

// Bad characters in key name
caughtError = false;
try { parser.SetString("ok", "bad\0", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("ok", "bad\r", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("ok", "bad\n", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("ok", "bad=", "ok"); } catch (e) { caughtError = true; }
do_check_true(caughtError);

// Bad characters in value
caughtError = false;
try { parser.SetString("ok", "ok", "bad\0"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("ok", "ok", "bad\r"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("ok", "ok", "bad\n"); } catch (e) { caughtError = true; }
do_check_true(caughtError);
caughtError = false;
try { parser.SetString("ok", "ok", "bad="); } catch (e) { caughtError = true; }
do_check_true(caughtError);

} catch(e) {
    throw "FAILED in test #" + testnum + " -- " + e;
}
}