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 | // newPset: returns an empty nsIUrlClassifierPrefixSet. |
michael@0 | 2 | function newPset() { |
michael@0 | 3 | let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] |
michael@0 | 4 | .createInstance(Ci.nsIUrlClassifierPrefixSet); |
michael@0 | 5 | pset.init("all"); |
michael@0 | 6 | return pset; |
michael@0 | 7 | } |
michael@0 | 8 | |
michael@0 | 9 | // arrContains: returns true if |arr| contains the element |target|. Uses binary |
michael@0 | 10 | // search and requires |arr| to be sorted. |
michael@0 | 11 | function arrContains(arr, target) { |
michael@0 | 12 | let start = 0; |
michael@0 | 13 | let end = arr.length - 1; |
michael@0 | 14 | let i = 0; |
michael@0 | 15 | |
michael@0 | 16 | while (end > start) { |
michael@0 | 17 | i = start + (end - start >> 1); |
michael@0 | 18 | let value = arr[i]; |
michael@0 | 19 | |
michael@0 | 20 | if (value < target) |
michael@0 | 21 | start = i+1; |
michael@0 | 22 | else if (value > target) |
michael@0 | 23 | end = i-1; |
michael@0 | 24 | else |
michael@0 | 25 | break; |
michael@0 | 26 | } |
michael@0 | 27 | if (start == end) |
michael@0 | 28 | i = start; |
michael@0 | 29 | |
michael@0 | 30 | return (!(i < 0 || i >= arr.length) && arr[i] == target); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | // checkContents: Check whether the PrefixSet pset contains |
michael@0 | 34 | // the prefixes in the passed array. |
michael@0 | 35 | function checkContents(pset, prefixes) { |
michael@0 | 36 | var outcount = {}, outset = {}; |
michael@0 | 37 | outset = pset.getPrefixes(outcount); |
michael@0 | 38 | let inset = prefixes; |
michael@0 | 39 | do_check_eq(inset.length, outset.length); |
michael@0 | 40 | inset.sort(function(x,y) x - y); |
michael@0 | 41 | for (let i = 0; i < inset.length; i++) { |
michael@0 | 42 | do_check_eq(inset[i], outset[i]); |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function wrappedProbe(pset, prefix) { |
michael@0 | 47 | return pset.contains(prefix); |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | // doRandomLookups: we use this to test for false membership with random input |
michael@0 | 51 | // over the range of prefixes (unsigned 32-bits integers). |
michael@0 | 52 | // pset: a nsIUrlClassifierPrefixSet to test. |
michael@0 | 53 | // prefixes: an array of prefixes supposed to make up the prefix set. |
michael@0 | 54 | // N: number of random lookups to make. |
michael@0 | 55 | function doRandomLookups(pset, prefixes, N) { |
michael@0 | 56 | for (let i = 0; i < N; i++) { |
michael@0 | 57 | let randInt = prefixes[0]; |
michael@0 | 58 | while (arrContains(prefixes, randInt)) |
michael@0 | 59 | randInt = Math.floor(Math.random() * Math.pow(2, 32)); |
michael@0 | 60 | |
michael@0 | 61 | do_check_false(wrappedProbe(pset, randInt)); |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | // doExpectedLookups: we use this to test expected membership. |
michael@0 | 66 | // pset: a nsIUrlClassifierPrefixSet to test. |
michael@0 | 67 | // prefixes: |
michael@0 | 68 | function doExpectedLookups(pset, prefixes, N) { |
michael@0 | 69 | for (let i = 0; i < N; i++) { |
michael@0 | 70 | prefixes.forEach(function (x) { |
michael@0 | 71 | dump("Checking " + x + "\n"); |
michael@0 | 72 | do_check_true(wrappedProbe(pset, x)); |
michael@0 | 73 | }); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | // testBasicPset: A very basic test of the prefix set to make sure that it |
michael@0 | 78 | // exists and to give a basic example of its use. |
michael@0 | 79 | function testBasicPset() { |
michael@0 | 80 | let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] |
michael@0 | 81 | .createInstance(Ci.nsIUrlClassifierPrefixSet); |
michael@0 | 82 | let prefixes = [2,50,100,2000,78000,1593203]; |
michael@0 | 83 | pset.setPrefixes(prefixes, prefixes.length); |
michael@0 | 84 | |
michael@0 | 85 | do_check_true(wrappedProbe(pset, 100)); |
michael@0 | 86 | do_check_false(wrappedProbe(pset, 100000)); |
michael@0 | 87 | do_check_true(wrappedProbe(pset, 1593203)); |
michael@0 | 88 | do_check_false(wrappedProbe(pset, 999)); |
michael@0 | 89 | do_check_false(wrappedProbe(pset, 0)); |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | checkContents(pset, prefixes); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function testDuplicates() { |
michael@0 | 96 | let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] |
michael@0 | 97 | .createInstance(Ci.nsIUrlClassifierPrefixSet); |
michael@0 | 98 | let prefixes = [1,1,2,2,2,3,3,3,3,3,3,5,6,6,7,7,9,9,9]; |
michael@0 | 99 | pset.setPrefixes(prefixes, prefixes.length); |
michael@0 | 100 | |
michael@0 | 101 | do_check_true(wrappedProbe(pset, 1)); |
michael@0 | 102 | do_check_true(wrappedProbe(pset, 2)); |
michael@0 | 103 | do_check_true(wrappedProbe(pset, 5)); |
michael@0 | 104 | do_check_true(wrappedProbe(pset, 9)); |
michael@0 | 105 | do_check_false(wrappedProbe(pset, 4)); |
michael@0 | 106 | do_check_false(wrappedProbe(pset, 8)); |
michael@0 | 107 | |
michael@0 | 108 | |
michael@0 | 109 | checkContents(pset, prefixes); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function testSimplePset() { |
michael@0 | 113 | let pset = newPset(); |
michael@0 | 114 | let prefixes = [1,2,100,400,123456789]; |
michael@0 | 115 | pset.setPrefixes(prefixes, prefixes.length); |
michael@0 | 116 | |
michael@0 | 117 | doRandomLookups(pset, prefixes, 100); |
michael@0 | 118 | doExpectedLookups(pset, prefixes, 1); |
michael@0 | 119 | |
michael@0 | 120 | |
michael@0 | 121 | checkContents(pset, prefixes); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | function testReSetPrefixes() { |
michael@0 | 125 | let pset = newPset(); |
michael@0 | 126 | let prefixes = [1, 5, 100, 1000, 150000]; |
michael@0 | 127 | pset.setPrefixes(prefixes, prefixes.length); |
michael@0 | 128 | |
michael@0 | 129 | doExpectedLookups(pset, prefixes, 1); |
michael@0 | 130 | |
michael@0 | 131 | let secondPrefixes = [12, 50, 300, 2000, 5000, 200000]; |
michael@0 | 132 | pset.setPrefixes(secondPrefixes, secondPrefixes.length); |
michael@0 | 133 | |
michael@0 | 134 | doExpectedLookups(pset, secondPrefixes, 1); |
michael@0 | 135 | for (let i = 0; i < prefixes.length; i++) { |
michael@0 | 136 | do_check_false(wrappedProbe(pset, prefixes[i])); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | |
michael@0 | 140 | checkContents(pset, secondPrefixes); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | function testLargeSet() { |
michael@0 | 144 | let N = 1000; |
michael@0 | 145 | let arr = []; |
michael@0 | 146 | |
michael@0 | 147 | for (let i = 0; i < N; i++) { |
michael@0 | 148 | let randInt = Math.floor(Math.random() * Math.pow(2, 32)); |
michael@0 | 149 | arr.push(randInt); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | arr.sort(function(x,y) x - y); |
michael@0 | 153 | |
michael@0 | 154 | let pset = newPset(); |
michael@0 | 155 | pset.setPrefixes(arr, arr.length); |
michael@0 | 156 | |
michael@0 | 157 | doExpectedLookups(pset, arr, 1); |
michael@0 | 158 | doRandomLookups(pset, arr, 1000); |
michael@0 | 159 | |
michael@0 | 160 | checkContents(pset, arr); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | function testTinySet() { |
michael@0 | 164 | let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] |
michael@0 | 165 | .createInstance(Ci.nsIUrlClassifierPrefixSet); |
michael@0 | 166 | let prefixes = [1]; |
michael@0 | 167 | pset.setPrefixes(prefixes, prefixes.length); |
michael@0 | 168 | |
michael@0 | 169 | do_check_true(wrappedProbe(pset, 1)); |
michael@0 | 170 | do_check_false(wrappedProbe(pset, 100000)); |
michael@0 | 171 | checkContents(pset, prefixes); |
michael@0 | 172 | |
michael@0 | 173 | prefixes = []; |
michael@0 | 174 | pset.setPrefixes(prefixes, prefixes.length); |
michael@0 | 175 | do_check_false(wrappedProbe(pset, 1)); |
michael@0 | 176 | checkContents(pset, prefixes); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | let tests = [testBasicPset, |
michael@0 | 180 | testSimplePset, |
michael@0 | 181 | testReSetPrefixes, |
michael@0 | 182 | testLargeSet, |
michael@0 | 183 | testDuplicates, |
michael@0 | 184 | testTinySet]; |
michael@0 | 185 | |
michael@0 | 186 | function run_test() { |
michael@0 | 187 | // None of the tests use |executeSoon| or any sort of callbacks, so we can |
michael@0 | 188 | // just run them in succession. |
michael@0 | 189 | for (let i = 0; i < tests.length; i++) { |
michael@0 | 190 | dump("Running " + tests[i].name + "\n"); |
michael@0 | 191 | tests[i](); |
michael@0 | 192 | } |
michael@0 | 193 | } |