Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
6 const prefs = require("sdk/preferences/service");
7 const Branch = prefs.Branch;
8 const { Cc, Ci, Cu } = require("chrome");
9 const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
11 const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
13 exports.testReset = function(assert) {
14 prefs.reset("test_reset_pref");
15 assert.equal(prefs.has("test_reset_pref"), false);
16 assert.equal(prefs.isSet("test_reset_pref"), false);
17 prefs.set("test_reset_pref", 5);
18 assert.equal(prefs.has("test_reset_pref"), true);
19 assert.equal(prefs.isSet("test_reset_pref"), true);
20 assert.equal(prefs.keys("test_reset_pref").toString(), "test_reset_pref");
21 };
23 exports.testGetAndSet = function(assert) {
24 let svc = Cc["@mozilla.org/preferences-service;1"].
25 getService(Ci.nsIPrefService).
26 getBranch(null);
27 svc.setCharPref("test_set_get_pref", "a normal string");
28 assert.equal(prefs.get("test_set_get_pref"), "a normal string",
29 "preferences-service should read from " +
30 "application-wide preferences service");
32 prefs.set("test_set_get_pref.integer", 1);
33 assert.equal(prefs.get("test_set_get_pref.integer"), 1,
34 "set/get integer preference should work");
36 assert.equal(
37 prefs.keys("test_set_get_pref").sort().toString(),
38 ["test_set_get_pref.integer","test_set_get_pref"].sort().toString());
40 prefs.set("test_set_get_number_pref", 42);
41 assert.throws(
42 function() { prefs.set("test_set_get_number_pref", 3.14159); },
43 /cannot store non-integer number: 3.14159/,
44 "setting a float preference should raise an error"
45 );
46 assert.equal(prefs.get("test_set_get_number_pref"), 42,
47 "bad-type write attempt should not overwrite");
49 // 0x80000000 (no), 0x7fffffff (yes), -0x80000000 (yes), -0x80000001 (no)
50 assert.throws(
51 function() { prefs.set("test_set_get_number_pref", Math.pow(2, 31)); },
52 new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
53 "2147483648, as number pref values must be in the signed 32\\-bit " +
54 "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " +
55 "range, store them as strings."),
56 "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
57 );
58 assert.equal(prefs.get("test_set_get_number_pref"), 42,
59 "out-of-range write attempt should not overwrite 1");
60 prefs.set("test_set_get_number_pref", Math.pow(2, 31)-1);
61 assert.equal(prefs.get("test_set_get_number_pref"), 0x7fffffff,
62 "in-range write attempt should work 1");
63 prefs.set("test_set_get_number_pref", -Math.pow(2, 31));
64 assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
65 "in-range write attempt should work 2");
66 assert.throws(
67 function() { prefs.set("test_set_get_number_pref", -0x80000001); },
68 new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
69 "\\-2147483649, as number pref values must be in the signed 32-bit " +
70 "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " +
71 "range, store them as strings."),
72 "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
73 );
74 assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
75 "out-of-range write attempt should not overwrite 2");
78 prefs.set("test_set_get_pref.string", "foo");
79 assert.equal(prefs.get("test_set_get_pref.string"), "foo",
80 "set/get string preference should work");
82 prefs.set("test_set_get_pref.boolean", true);
83 assert.equal(prefs.get("test_set_get_pref.boolean"), true,
84 "set/get boolean preference should work");
86 prefs.set("test_set_get_unicode_pref", String.fromCharCode(960));
87 assert.equal(prefs.get("test_set_get_unicode_pref"),
88 String.fromCharCode(960),
89 "set/get unicode preference should work");
91 var unsupportedValues = [null, [], undefined];
92 unsupportedValues.forEach(
93 function(value) {
94 assert.throws(
95 function() { prefs.set("test_set_pref", value); },
96 new RegExp("can't set pref test_set_pref to value '" + value + "'; " +
97 "it isn't a string, integer, or boolean"),
98 "Setting a pref to " + uneval(value) + " should raise error"
99 );
100 });
101 };
103 exports.testPrefClass = function(assert) {
104 var branch = Branch("test_foo");
106 assert.equal(branch.test, undefined, "test_foo.test is undefined");
107 branch.test = true;
108 assert.equal(branch.test, true, "test_foo.test is true");
109 delete branch.test;
110 assert.equal(branch.test, undefined, "test_foo.test is undefined");
111 };
113 exports.testGetSetLocalized = function(assert) {
114 let prefName = "general.useragent.locale";
116 // Ensure that "general.useragent.locale" is a 'localized' pref
117 let bundleURL = "chrome://global/locale/intl.properties";
118 prefs.setLocalized(prefName, bundleURL);
120 // Fetch the expected value directly from the property file
121 let expectedValue = BundleService.createBundle(bundleURL).
122 GetStringFromName(prefName).
123 toLowerCase();
125 assert.equal(prefs.getLocalized(prefName).toLowerCase(),
126 expectedValue,
127 "get localized preference");
129 // Undo our modification
130 prefs.reset(prefName);
131 }
133 // TEST: setting and getting preferences with special characters work
134 exports.testSpecialChars = function(assert) {
135 let chars = specialChars.split('');
136 const ROOT = "test.";
138 chars.forEach(function(char) {
139 let rand = Math.random() + "";
140 prefs.set(ROOT+char, rand);
141 assert.equal(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!");
142 });
143 };
145 require('sdk/test').run(exports);