Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | const Ci = Components.interfaces; |
michael@0 | 2 | const Cc = Components.classes; |
michael@0 | 3 | const Cr = Components.results; |
michael@0 | 4 | |
michael@0 | 5 | let testnum = 0; |
michael@0 | 6 | let factory; |
michael@0 | 7 | |
michael@0 | 8 | function parserForFile(filename) { |
michael@0 | 9 | let parser = null; |
michael@0 | 10 | try { |
michael@0 | 11 | let file = do_get_file(filename); |
michael@0 | 12 | do_check_true(!!file); |
michael@0 | 13 | parser = factory.createINIParser(file); |
michael@0 | 14 | do_check_true(!!parser); |
michael@0 | 15 | } catch(e) { |
michael@0 | 16 | dump("INFO | caught error: " + e); |
michael@0 | 17 | // checkParserOutput will handle a null parser when it's expected. |
michael@0 | 18 | } |
michael@0 | 19 | return parser; |
michael@0 | 20 | |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function checkParserOutput(parser, expected) { |
michael@0 | 24 | // If the expected output is null, we expect the parser to have |
michael@0 | 25 | // failed (and vice-versa). |
michael@0 | 26 | if (!parser || !expected) { |
michael@0 | 27 | do_check_eq(parser, null); |
michael@0 | 28 | do_check_eq(expected, null); |
michael@0 | 29 | return; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | let output = getParserOutput(parser); |
michael@0 | 33 | for (let section in expected) { |
michael@0 | 34 | do_check_true(section in output); |
michael@0 | 35 | for (let key in expected[section]) { |
michael@0 | 36 | do_check_true(key in output[section]); |
michael@0 | 37 | do_check_eq(output[section][key], expected[section][key]); |
michael@0 | 38 | delete output[section][key]; |
michael@0 | 39 | } |
michael@0 | 40 | for (let key in output[section]) |
michael@0 | 41 | do_check_eq(key, "wasn't expecting this key!"); |
michael@0 | 42 | delete output[section]; |
michael@0 | 43 | } |
michael@0 | 44 | for (let section in output) |
michael@0 | 45 | do_check_eq(section, "wasn't expecting this section!"); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function getParserOutput(parser) { |
michael@0 | 49 | let output = {}; |
michael@0 | 50 | |
michael@0 | 51 | let sections = parser.getSections(); |
michael@0 | 52 | do_check_true(!!sections); |
michael@0 | 53 | while (sections.hasMore()) { |
michael@0 | 54 | let section = sections.getNext(); |
michael@0 | 55 | do_check_false(section in output); // catch dupes |
michael@0 | 56 | output[section] = {}; |
michael@0 | 57 | |
michael@0 | 58 | let keys = parser.getKeys(section); |
michael@0 | 59 | do_check_true(!!keys); |
michael@0 | 60 | while (keys.hasMore()) { |
michael@0 | 61 | let key = keys.getNext(); |
michael@0 | 62 | do_check_false(key in output[section]); // catch dupes |
michael@0 | 63 | let value = parser.getString(section, key); |
michael@0 | 64 | output[section][key] = value; |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | return output; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function run_test() { |
michael@0 | 71 | try { |
michael@0 | 72 | |
michael@0 | 73 | let testdata = [ |
michael@0 | 74 | { filename: "data/iniparser01.ini", reference: {} }, |
michael@0 | 75 | { filename: "data/iniparser02.ini", reference: {} }, |
michael@0 | 76 | { filename: "data/iniparser03.ini", reference: {} }, |
michael@0 | 77 | { filename: "data/iniparser04.ini", reference: {} }, |
michael@0 | 78 | { filename: "data/iniparser05.ini", reference: {} }, |
michael@0 | 79 | { filename: "data/iniparser06.ini", reference: {} }, |
michael@0 | 80 | { filename: "data/iniparser07.ini", reference: {} }, |
michael@0 | 81 | { filename: "data/iniparser08.ini", reference: { section1: { name1: "" }} }, |
michael@0 | 82 | { filename: "data/iniparser09.ini", reference: { section1: { name1: "value1" } } }, |
michael@0 | 83 | { filename: "data/iniparser10.ini", reference: { section1: { name1: "value1" } } }, |
michael@0 | 84 | { filename: "data/iniparser11.ini", reference: { section1: { name1: "value1" } } }, |
michael@0 | 85 | { filename: "data/iniparser12.ini", reference: { section1: { name1: "value1" } } }, |
michael@0 | 86 | { filename: "data/iniparser13.ini", reference: { section1: { name1: "value1" } } }, |
michael@0 | 87 | { filename: "data/iniparser14.ini", reference: |
michael@0 | 88 | { section1: { name1: "value1", name2: "value2" }, |
michael@0 | 89 | section2: { name1: "value1", name2: "foopy" }} }, |
michael@0 | 90 | { filename: "data/iniparser15.ini", reference: |
michael@0 | 91 | { section1: { name1: "newValue1" }, |
michael@0 | 92 | section2: { name1: "foopy" }} }, |
michael@0 | 93 | { filename: "data/iniparser16.ini", reference: |
michael@0 | 94 | { "☺♫": { "♫": "☻", "♪": "♥" }, |
michael@0 | 95 | "☼": { "♣": "♠", "♦": "♥" }} }, |
michael@0 | 96 | |
michael@0 | 97 | ]; |
michael@0 | 98 | |
michael@0 | 99 | testdata.push( { filename: "data/iniparser01-utf8BOM.ini", |
michael@0 | 100 | reference: testdata[0].reference } ); |
michael@0 | 101 | testdata.push( { filename: "data/iniparser02-utf8BOM.ini", |
michael@0 | 102 | reference: testdata[1].reference } ); |
michael@0 | 103 | testdata.push( { filename: "data/iniparser03-utf8BOM.ini", |
michael@0 | 104 | reference: testdata[2].reference } ); |
michael@0 | 105 | testdata.push( { filename: "data/iniparser04-utf8BOM.ini", |
michael@0 | 106 | reference: testdata[3].reference } ); |
michael@0 | 107 | testdata.push( { filename: "data/iniparser05-utf8BOM.ini", |
michael@0 | 108 | reference: testdata[4].reference } ); |
michael@0 | 109 | testdata.push( { filename: "data/iniparser06-utf8BOM.ini", |
michael@0 | 110 | reference: testdata[5].reference } ); |
michael@0 | 111 | testdata.push( { filename: "data/iniparser07-utf8BOM.ini", |
michael@0 | 112 | reference: testdata[6].reference } ); |
michael@0 | 113 | testdata.push( { filename: "data/iniparser08-utf8BOM.ini", |
michael@0 | 114 | reference: testdata[7].reference } ); |
michael@0 | 115 | testdata.push( { filename: "data/iniparser09-utf8BOM.ini", |
michael@0 | 116 | reference: testdata[8].reference } ); |
michael@0 | 117 | testdata.push( { filename: "data/iniparser10-utf8BOM.ini", |
michael@0 | 118 | reference: testdata[9].reference } ); |
michael@0 | 119 | testdata.push( { filename: "data/iniparser11-utf8BOM.ini", |
michael@0 | 120 | reference: testdata[10].reference } ); |
michael@0 | 121 | testdata.push( { filename: "data/iniparser12-utf8BOM.ini", |
michael@0 | 122 | reference: testdata[11].reference } ); |
michael@0 | 123 | testdata.push( { filename: "data/iniparser13-utf8BOM.ini", |
michael@0 | 124 | reference: testdata[12].reference } ); |
michael@0 | 125 | testdata.push( { filename: "data/iniparser14-utf8BOM.ini", |
michael@0 | 126 | reference: testdata[13].reference } ); |
michael@0 | 127 | testdata.push( { filename: "data/iniparser15-utf8BOM.ini", |
michael@0 | 128 | reference: testdata[14].reference } ); |
michael@0 | 129 | testdata.push( { filename: "data/iniparser16-utf8BOM.ini", |
michael@0 | 130 | reference: testdata[15].reference } ); |
michael@0 | 131 | |
michael@0 | 132 | let os = Cc["@mozilla.org/xre/app-info;1"] |
michael@0 | 133 | .getService(Ci.nsIXULRuntime).OS; |
michael@0 | 134 | if("WINNT" === os) { |
michael@0 | 135 | testdata.push( { filename: "data/iniparser01-utf16leBOM.ini", |
michael@0 | 136 | reference: testdata[0].reference } ); |
michael@0 | 137 | testdata.push( { filename: "data/iniparser02-utf16leBOM.ini", |
michael@0 | 138 | reference: testdata[1].reference } ); |
michael@0 | 139 | testdata.push( { filename: "data/iniparser03-utf16leBOM.ini", |
michael@0 | 140 | reference: testdata[2].reference } ); |
michael@0 | 141 | testdata.push( { filename: "data/iniparser04-utf16leBOM.ini", |
michael@0 | 142 | reference: testdata[3].reference } ); |
michael@0 | 143 | testdata.push( { filename: "data/iniparser05-utf16leBOM.ini", |
michael@0 | 144 | reference: testdata[4].reference } ); |
michael@0 | 145 | testdata.push( { filename: "data/iniparser06-utf16leBOM.ini", |
michael@0 | 146 | reference: testdata[5].reference } ); |
michael@0 | 147 | testdata.push( { filename: "data/iniparser07-utf16leBOM.ini", |
michael@0 | 148 | reference: testdata[6].reference } ); |
michael@0 | 149 | testdata.push( { filename: "data/iniparser08-utf16leBOM.ini", |
michael@0 | 150 | reference: testdata[7].reference } ); |
michael@0 | 151 | testdata.push( { filename: "data/iniparser09-utf16leBOM.ini", |
michael@0 | 152 | reference: testdata[8].reference } ); |
michael@0 | 153 | testdata.push( { filename: "data/iniparser10-utf16leBOM.ini", |
michael@0 | 154 | reference: testdata[9].reference } ); |
michael@0 | 155 | testdata.push( { filename: "data/iniparser11-utf16leBOM.ini", |
michael@0 | 156 | reference: testdata[10].reference } ); |
michael@0 | 157 | testdata.push( { filename: "data/iniparser12-utf16leBOM.ini", |
michael@0 | 158 | reference: testdata[11].reference } ); |
michael@0 | 159 | testdata.push( { filename: "data/iniparser13-utf16leBOM.ini", |
michael@0 | 160 | reference: testdata[12].reference } ); |
michael@0 | 161 | testdata.push( { filename: "data/iniparser14-utf16leBOM.ini", |
michael@0 | 162 | reference: testdata[13].reference } ); |
michael@0 | 163 | testdata.push( { filename: "data/iniparser15-utf16leBOM.ini", |
michael@0 | 164 | reference: testdata[14].reference } ); |
michael@0 | 165 | testdata.push( { filename: "data/iniparser16-utf16leBOM.ini", |
michael@0 | 166 | reference: testdata[15].reference } ); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | /* ========== 0 ========== */ |
michael@0 | 170 | factory = Cc["@mozilla.org/xpcom/ini-processor-factory;1"]. |
michael@0 | 171 | getService(Ci.nsIINIParserFactory); |
michael@0 | 172 | do_check_true(!!factory); |
michael@0 | 173 | |
michael@0 | 174 | // Test reading from a variety of files. While we're at it, write out each one |
michael@0 | 175 | // and read it back to ensure that nothing changed. |
michael@0 | 176 | while (testnum < testdata.length) { |
michael@0 | 177 | dump("\nINFO | test #" + ++testnum); |
michael@0 | 178 | let filename = testdata[testnum -1].filename; |
michael@0 | 179 | dump(", filename " + filename + "\n"); |
michael@0 | 180 | let parser = parserForFile(filename); |
michael@0 | 181 | checkParserOutput(parser, testdata[testnum - 1].reference); |
michael@0 | 182 | if (!parser) |
michael@0 | 183 | continue; |
michael@0 | 184 | do_check_true(parser instanceof Ci.nsIINIParserWriter); |
michael@0 | 185 | // write contents out to a new file |
michael@0 | 186 | let newfilename = filename + ".new"; |
michael@0 | 187 | let newfile = do_get_file(filename); |
michael@0 | 188 | newfile.leafName += ".new"; |
michael@0 | 189 | parser.writeFile(newfile); |
michael@0 | 190 | // read new file and make sure the contents are the same. |
michael@0 | 191 | parser = parserForFile(newfilename); |
michael@0 | 192 | checkParserOutput(parser, testdata[testnum - 1].reference); |
michael@0 | 193 | // cleanup after the test |
michael@0 | 194 | newfile.remove(false); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | dump("INFO | test #" + ++testnum + "\n"); |
michael@0 | 198 | |
michael@0 | 199 | // test writing to a new file. |
michael@0 | 200 | let newfile = do_get_file("data/"); |
michael@0 | 201 | newfile.append("nonexistent-file.ini"); |
michael@0 | 202 | if (newfile.exists()) |
michael@0 | 203 | newfile.remove(false); |
michael@0 | 204 | do_check_false(newfile.exists()); |
michael@0 | 205 | |
michael@0 | 206 | let parser = factory.createINIParser(newfile); |
michael@0 | 207 | do_check_true(!!parser); |
michael@0 | 208 | do_check_true(parser instanceof Ci.nsIINIParserWriter); |
michael@0 | 209 | checkParserOutput(parser, {}); |
michael@0 | 210 | parser.writeFile(); |
michael@0 | 211 | do_check_true(newfile.exists()); |
michael@0 | 212 | |
michael@0 | 213 | // test adding a new section and new key |
michael@0 | 214 | parser.setString("section", "key", "value"); |
michael@0 | 215 | parser.writeFile(); |
michael@0 | 216 | do_check_true(newfile.exists()); |
michael@0 | 217 | checkParserOutput(parser, {section: {key: "value"} }); |
michael@0 | 218 | // read it in again, check for same data. |
michael@0 | 219 | parser = parserForFile("data/nonexistent-file.ini"); |
michael@0 | 220 | checkParserOutput(parser, {section: {key: "value"} }); |
michael@0 | 221 | // cleanup after the test |
michael@0 | 222 | newfile.remove(false); |
michael@0 | 223 | |
michael@0 | 224 | dump("INFO | test #" + ++testnum + "\n"); |
michael@0 | 225 | |
michael@0 | 226 | // test modifying a existing key's value (in an existing section) |
michael@0 | 227 | parser = parserForFile("data/iniparser09.ini"); |
michael@0 | 228 | checkParserOutput(parser, {section1: {name1: "value1"} }); |
michael@0 | 229 | |
michael@0 | 230 | do_check_true(parser instanceof Ci.nsIINIParserWriter); |
michael@0 | 231 | parser.setString("section1", "name1", "value2"); |
michael@0 | 232 | checkParserOutput(parser, {section1: {name1: "value2"} }); |
michael@0 | 233 | |
michael@0 | 234 | dump("INFO | test #" + ++testnum + "\n"); |
michael@0 | 235 | |
michael@0 | 236 | // test trying to set illegal characters |
michael@0 | 237 | let caughtError; |
michael@0 | 238 | caughtError = false; |
michael@0 | 239 | checkParserOutput(parser, {section1: {name1: "value2"} }); |
michael@0 | 240 | |
michael@0 | 241 | // Bad characters in section name |
michael@0 | 242 | try { parser.SetString("bad\0", "ok", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 243 | do_check_true(caughtError); |
michael@0 | 244 | caughtError = false; |
michael@0 | 245 | try { parser.SetString("bad\r", "ok", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 246 | do_check_true(caughtError); |
michael@0 | 247 | caughtError = false; |
michael@0 | 248 | try { parser.SetString("bad\n", "ok", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 249 | do_check_true(caughtError); |
michael@0 | 250 | caughtError = false; |
michael@0 | 251 | try { parser.SetString("bad[", "ok", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 252 | do_check_true(caughtError); |
michael@0 | 253 | caughtError = false; |
michael@0 | 254 | try { parser.SetString("bad]", "ok", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 255 | do_check_true(caughtError); |
michael@0 | 256 | |
michael@0 | 257 | // Bad characters in key name |
michael@0 | 258 | caughtError = false; |
michael@0 | 259 | try { parser.SetString("ok", "bad\0", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 260 | do_check_true(caughtError); |
michael@0 | 261 | caughtError = false; |
michael@0 | 262 | try { parser.SetString("ok", "bad\r", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 263 | do_check_true(caughtError); |
michael@0 | 264 | caughtError = false; |
michael@0 | 265 | try { parser.SetString("ok", "bad\n", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 266 | do_check_true(caughtError); |
michael@0 | 267 | caughtError = false; |
michael@0 | 268 | try { parser.SetString("ok", "bad=", "ok"); } catch (e) { caughtError = true; } |
michael@0 | 269 | do_check_true(caughtError); |
michael@0 | 270 | |
michael@0 | 271 | // Bad characters in value |
michael@0 | 272 | caughtError = false; |
michael@0 | 273 | try { parser.SetString("ok", "ok", "bad\0"); } catch (e) { caughtError = true; } |
michael@0 | 274 | do_check_true(caughtError); |
michael@0 | 275 | caughtError = false; |
michael@0 | 276 | try { parser.SetString("ok", "ok", "bad\r"); } catch (e) { caughtError = true; } |
michael@0 | 277 | do_check_true(caughtError); |
michael@0 | 278 | caughtError = false; |
michael@0 | 279 | try { parser.SetString("ok", "ok", "bad\n"); } catch (e) { caughtError = true; } |
michael@0 | 280 | do_check_true(caughtError); |
michael@0 | 281 | caughtError = false; |
michael@0 | 282 | try { parser.SetString("ok", "ok", "bad="); } catch (e) { caughtError = true; } |
michael@0 | 283 | do_check_true(caughtError); |
michael@0 | 284 | |
michael@0 | 285 | } catch(e) { |
michael@0 | 286 | throw "FAILED in test #" + testnum + " -- " + e; |
michael@0 | 287 | } |
michael@0 | 288 | } |