Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 7 | XPCOMUtils.defineLazyModuleGetter(this, "Experiments", |
michael@0 | 8 | "resource:///modules/experiments/Experiments.jsm"); |
michael@0 | 9 | |
michael@0 | 10 | const FILE_MANIFEST = "experiments.manifest"; |
michael@0 | 11 | const MANIFEST_HANDLER = "manifests/handler"; |
michael@0 | 12 | |
michael@0 | 13 | const SEC_IN_ONE_DAY = 24 * 60 * 60; |
michael@0 | 14 | const MS_IN_ONE_DAY = SEC_IN_ONE_DAY * 1000; |
michael@0 | 15 | |
michael@0 | 16 | let gProfileDir = null; |
michael@0 | 17 | let gHttpServer = null; |
michael@0 | 18 | let gHttpRoot = null; |
michael@0 | 19 | let gDataRoot = null; |
michael@0 | 20 | let gReporter = null; |
michael@0 | 21 | let gPolicy = null; |
michael@0 | 22 | let gManifestObject = null; |
michael@0 | 23 | let gManifestHandlerURI = null; |
michael@0 | 24 | let gTimerScheduleOffset = -1; |
michael@0 | 25 | |
michael@0 | 26 | function run_test() { |
michael@0 | 27 | run_next_test(); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | add_task(function* test_setup() { |
michael@0 | 31 | loadAddonManager(); |
michael@0 | 32 | gProfileDir = do_get_profile(); |
michael@0 | 33 | yield removeCacheFile(); |
michael@0 | 34 | |
michael@0 | 35 | gHttpServer = new HttpServer(); |
michael@0 | 36 | gHttpServer.start(-1); |
michael@0 | 37 | let port = gHttpServer.identity.primaryPort; |
michael@0 | 38 | gHttpRoot = "http://localhost:" + port + "/"; |
michael@0 | 39 | gDataRoot = gHttpRoot + "data/"; |
michael@0 | 40 | gManifestHandlerURI = gHttpRoot + MANIFEST_HANDLER; |
michael@0 | 41 | gHttpServer.registerDirectory("/data/", do_get_cwd()); |
michael@0 | 42 | gHttpServer.registerPathHandler("/" + MANIFEST_HANDLER, (request, response) => { |
michael@0 | 43 | response.setStatusLine(null, 200, "OK"); |
michael@0 | 44 | response.write(JSON.stringify(gManifestObject)); |
michael@0 | 45 | response.processAsync(); |
michael@0 | 46 | response.finish(); |
michael@0 | 47 | }); |
michael@0 | 48 | do_register_cleanup(() => gHttpServer.stop(() => {})); |
michael@0 | 49 | |
michael@0 | 50 | disableCertificateChecks(); |
michael@0 | 51 | |
michael@0 | 52 | Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, true); |
michael@0 | 53 | Services.prefs.setIntPref(PREF_LOGGING_LEVEL, 0); |
michael@0 | 54 | Services.prefs.setBoolPref(PREF_LOGGING_DUMP, true); |
michael@0 | 55 | Services.prefs.setCharPref(PREF_MANIFEST_URI, gManifestHandlerURI); |
michael@0 | 56 | Services.prefs.setIntPref(PREF_FETCHINTERVAL, 0); |
michael@0 | 57 | |
michael@0 | 58 | gReporter = yield getReporter("json_payload_simple"); |
michael@0 | 59 | yield gReporter.collectMeasurements(); |
michael@0 | 60 | let payload = yield gReporter.getJSONPayload(true); |
michael@0 | 61 | do_register_cleanup(() => gReporter._shutdown()); |
michael@0 | 62 | |
michael@0 | 63 | gPolicy = new Experiments.Policy(); |
michael@0 | 64 | patchPolicy(gPolicy, { |
michael@0 | 65 | updatechannel: () => "nightly", |
michael@0 | 66 | healthReportPayload: () => {}, |
michael@0 | 67 | oneshotTimer: (callback, timeout, thisObj, name) => gTimerScheduleOffset = timeout, |
michael@0 | 68 | }); |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | function checkExperimentListsEqual(list, list2) { |
michael@0 | 72 | Assert.equal(list.length, list2.length, "Lists should have the same length.") |
michael@0 | 73 | |
michael@0 | 74 | for (let i=0; i<list.length; ++i) { |
michael@0 | 75 | for (let k of Object.keys(list[i])) { |
michael@0 | 76 | Assert.equal(list[i][k], list2[i][k], |
michael@0 | 77 | "Field '" + k + "' should match for list entry " + i + "."); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function checkExperimentSerializations(experimentEntryIterator) { |
michael@0 | 83 | for (let experiment of experimentEntryIterator) { |
michael@0 | 84 | let experiment2 = new Experiments.ExperimentEntry(gPolicy); |
michael@0 | 85 | let jsonStr = JSON.stringify(experiment.toJSON()); |
michael@0 | 86 | Assert.ok(experiment2.initFromCacheData(JSON.parse(jsonStr)), |
michael@0 | 87 | "Should have initialized successfully from JSON serialization."); |
michael@0 | 88 | Assert.equal(JSON.stringify(experiment), JSON.stringify(experiment2), |
michael@0 | 89 | "Object stringifications should match."); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // Set up an experiments instance and check if it is properly restored from cache. |
michael@0 | 94 | |
michael@0 | 95 | add_task(function* test_cache() { |
michael@0 | 96 | // The manifest data we test with. |
michael@0 | 97 | |
michael@0 | 98 | gManifestObject = { |
michael@0 | 99 | "version": 1, |
michael@0 | 100 | experiments: [ |
michael@0 | 101 | { |
michael@0 | 102 | id: EXPERIMENT1_ID, |
michael@0 | 103 | xpiURL: gDataRoot + EXPERIMENT1_XPI_NAME, |
michael@0 | 104 | xpiHash: EXPERIMENT1_XPI_SHA1, |
michael@0 | 105 | maxActiveSeconds: 10 * SEC_IN_ONE_DAY, |
michael@0 | 106 | appName: ["XPCShell"], |
michael@0 | 107 | channel: ["nightly"], |
michael@0 | 108 | }, |
michael@0 | 109 | { |
michael@0 | 110 | id: EXPERIMENT2_ID, |
michael@0 | 111 | xpiURL: gDataRoot + EXPERIMENT2_XPI_NAME, |
michael@0 | 112 | xpiHash: EXPERIMENT2_XPI_SHA1, |
michael@0 | 113 | maxActiveSeconds: 10 * SEC_IN_ONE_DAY, |
michael@0 | 114 | appName: ["XPCShell"], |
michael@0 | 115 | channel: ["nightly"], |
michael@0 | 116 | }, |
michael@0 | 117 | { |
michael@0 | 118 | id: EXPERIMENT3_ID, |
michael@0 | 119 | xpiURL: "https://inval.id/foo.xpi", |
michael@0 | 120 | xpiHash: "sha1:0000000000000000000000000000000000000000", |
michael@0 | 121 | maxActiveSeconds: 10 * SEC_IN_ONE_DAY, |
michael@0 | 122 | appName: ["XPCShell"], |
michael@0 | 123 | channel: ["nightly"], |
michael@0 | 124 | }, |
michael@0 | 125 | ], |
michael@0 | 126 | }; |
michael@0 | 127 | |
michael@0 | 128 | // Setup dates for the experiments. |
michael@0 | 129 | |
michael@0 | 130 | let baseDate = new Date(2014, 5, 1, 12); |
michael@0 | 131 | let startDates = []; |
michael@0 | 132 | let endDates = []; |
michael@0 | 133 | |
michael@0 | 134 | for (let i=0; i<gManifestObject.experiments.length; ++i) { |
michael@0 | 135 | let experiment = gManifestObject.experiments[i]; |
michael@0 | 136 | startDates.push(futureDate(baseDate, (50 + (150 * i)) * MS_IN_ONE_DAY)); |
michael@0 | 137 | endDates .push(futureDate(startDates[i], 50 * MS_IN_ONE_DAY)); |
michael@0 | 138 | experiment.startTime = dateToSeconds(startDates[i]); |
michael@0 | 139 | experiment.endTime = dateToSeconds(endDates[i]); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | // Data to compare the result of Experiments.getExperiments() against. |
michael@0 | 143 | |
michael@0 | 144 | let experimentListData = [ |
michael@0 | 145 | { |
michael@0 | 146 | id: EXPERIMENT2_ID, |
michael@0 | 147 | name: "Test experiment 2", |
michael@0 | 148 | description: "And yet another experiment that experiments experimentally.", |
michael@0 | 149 | }, |
michael@0 | 150 | { |
michael@0 | 151 | id: EXPERIMENT1_ID, |
michael@0 | 152 | name: EXPERIMENT1_NAME, |
michael@0 | 153 | description: "Yet another experiment that experiments experimentally.", |
michael@0 | 154 | }, |
michael@0 | 155 | ]; |
michael@0 | 156 | |
michael@0 | 157 | // Trigger update & re-init, clock set to before any activation. |
michael@0 | 158 | |
michael@0 | 159 | let now = baseDate; |
michael@0 | 160 | defineNow(gPolicy, now); |
michael@0 | 161 | |
michael@0 | 162 | let experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 163 | yield experiments.updateManifest(); |
michael@0 | 164 | let list = yield experiments.getExperiments(); |
michael@0 | 165 | Assert.equal(list.length, 0, "Experiment list should be empty."); |
michael@0 | 166 | checkExperimentSerializations(experiments._experiments.values()); |
michael@0 | 167 | |
michael@0 | 168 | yield experiments.uninit(); |
michael@0 | 169 | experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 170 | |
michael@0 | 171 | yield experiments._run(); |
michael@0 | 172 | list = yield experiments.getExperiments(); |
michael@0 | 173 | Assert.equal(list.length, 0, "Experiment list should be empty."); |
michael@0 | 174 | checkExperimentSerializations(experiments._experiments.values()); |
michael@0 | 175 | |
michael@0 | 176 | // Re-init, clock set for experiment 1 to start. |
michael@0 | 177 | |
michael@0 | 178 | now = futureDate(startDates[0], 5 * MS_IN_ONE_DAY); |
michael@0 | 179 | defineNow(gPolicy, now); |
michael@0 | 180 | |
michael@0 | 181 | yield experiments.uninit(); |
michael@0 | 182 | experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 183 | yield experiments._run(); |
michael@0 | 184 | |
michael@0 | 185 | list = yield experiments.getExperiments(); |
michael@0 | 186 | Assert.equal(list.length, 1, "Experiment list should have 1 entry now."); |
michael@0 | 187 | |
michael@0 | 188 | experimentListData[1].active = true; |
michael@0 | 189 | experimentListData[1].endDate = now.getTime() + 10 * MS_IN_ONE_DAY; |
michael@0 | 190 | checkExperimentListsEqual(experimentListData.slice(1), list); |
michael@0 | 191 | checkExperimentSerializations(experiments._experiments.values()); |
michael@0 | 192 | |
michael@0 | 193 | let branch = yield experiments.getExperimentBranch(EXPERIMENT1_ID); |
michael@0 | 194 | Assert.strictEqual(branch, null); |
michael@0 | 195 | |
michael@0 | 196 | yield experiments.setExperimentBranch(EXPERIMENT1_ID, "testbranch"); |
michael@0 | 197 | branch = yield experiments.getExperimentBranch(EXPERIMENT1_ID); |
michael@0 | 198 | Assert.strictEqual(branch, "testbranch"); |
michael@0 | 199 | |
michael@0 | 200 | // Re-init, clock set for experiment 1 to stop. |
michael@0 | 201 | |
michael@0 | 202 | now = futureDate(now, 20 * MS_IN_ONE_DAY); |
michael@0 | 203 | defineNow(gPolicy, now); |
michael@0 | 204 | |
michael@0 | 205 | yield experiments.uninit(); |
michael@0 | 206 | experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 207 | yield experiments._run(); |
michael@0 | 208 | |
michael@0 | 209 | list = yield experiments.getExperiments(); |
michael@0 | 210 | Assert.equal(list.length, 1, "Experiment list should have 1 entry."); |
michael@0 | 211 | |
michael@0 | 212 | experimentListData[1].active = false; |
michael@0 | 213 | experimentListData[1].endDate = now.getTime(); |
michael@0 | 214 | checkExperimentListsEqual(experimentListData.slice(1), list); |
michael@0 | 215 | checkExperimentSerializations(experiments._experiments.values()); |
michael@0 | 216 | |
michael@0 | 217 | branch = yield experiments.getExperimentBranch(EXPERIMENT1_ID); |
michael@0 | 218 | Assert.strictEqual(branch, "testbranch"); |
michael@0 | 219 | |
michael@0 | 220 | // Re-init, clock set for experiment 2 to start. |
michael@0 | 221 | |
michael@0 | 222 | now = futureDate(startDates[1], 20 * MS_IN_ONE_DAY); |
michael@0 | 223 | defineNow(gPolicy, now); |
michael@0 | 224 | |
michael@0 | 225 | yield experiments.uninit(); |
michael@0 | 226 | experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 227 | yield experiments._run(); |
michael@0 | 228 | |
michael@0 | 229 | list = yield experiments.getExperiments(); |
michael@0 | 230 | Assert.equal(list.length, 2, "Experiment list should have 2 entries."); |
michael@0 | 231 | |
michael@0 | 232 | experimentListData[0].active = true; |
michael@0 | 233 | experimentListData[0].endDate = now.getTime() + 10 * MS_IN_ONE_DAY; |
michael@0 | 234 | checkExperimentListsEqual(experimentListData, list); |
michael@0 | 235 | checkExperimentSerializations(experiments._experiments.values()); |
michael@0 | 236 | |
michael@0 | 237 | // Re-init, clock set for experiment 2 to stop. |
michael@0 | 238 | |
michael@0 | 239 | now = futureDate(now, 20 * MS_IN_ONE_DAY); |
michael@0 | 240 | defineNow(gPolicy, now); |
michael@0 | 241 | |
michael@0 | 242 | yield experiments.uninit(); |
michael@0 | 243 | experiments = new Experiments.Experiments(gPolicy); |
michael@0 | 244 | yield experiments._run(); |
michael@0 | 245 | |
michael@0 | 246 | list = yield experiments.getExperiments(); |
michael@0 | 247 | Assert.equal(list.length, 2, "Experiment list should have 2 entries."); |
michael@0 | 248 | |
michael@0 | 249 | experimentListData[0].active = false; |
michael@0 | 250 | experimentListData[0].endDate = now.getTime(); |
michael@0 | 251 | checkExperimentListsEqual(experimentListData, list); |
michael@0 | 252 | checkExperimentSerializations(experiments._experiments.values()); |
michael@0 | 253 | |
michael@0 | 254 | // Cleanup. |
michael@0 | 255 | |
michael@0 | 256 | yield experiments._toggleExperimentsEnabled(false); |
michael@0 | 257 | yield experiments.uninit(); |
michael@0 | 258 | yield removeCacheFile(); |
michael@0 | 259 | }); |