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 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 5 | Cu.import("resource://services-sync/status.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://testing-common/services/sync/fakeservices.js"); |
michael@0 | 9 | Cu.import("resource://testing-common/services/sync/utils.js"); |
michael@0 | 10 | |
michael@0 | 11 | function baseHandler(eolCode, request, response, statusCode, status, body) { |
michael@0 | 12 | let alertBody = { |
michael@0 | 13 | code: eolCode, |
michael@0 | 14 | message: "Service is EOLed.", |
michael@0 | 15 | url: "http://getfirefox.com", |
michael@0 | 16 | }; |
michael@0 | 17 | response.setHeader("X-Weave-Timestamp", "" + new_timestamp(), false); |
michael@0 | 18 | response.setHeader("X-Weave-Alert", "" + JSON.stringify(alertBody), false); |
michael@0 | 19 | response.setStatusLine(request.httpVersion, statusCode, status); |
michael@0 | 20 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function handler513(request, response) { |
michael@0 | 24 | let statusCode = 513; |
michael@0 | 25 | let status = "Upgrade Required"; |
michael@0 | 26 | let body = "{}"; |
michael@0 | 27 | baseHandler("hard-eol", request, response, statusCode, status, body); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | function handler200(eolCode) { |
michael@0 | 31 | return function (request, response) { |
michael@0 | 32 | let statusCode = 200; |
michael@0 | 33 | let status = "OK"; |
michael@0 | 34 | let body = "{\"meta\": 123456789010}"; |
michael@0 | 35 | baseHandler(eolCode, request, response, statusCode, status, body); |
michael@0 | 36 | }; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | function sync_httpd_setup(infoHandler) { |
michael@0 | 40 | let handlers = { |
michael@0 | 41 | "/1.1/johndoe/info/collections": infoHandler, |
michael@0 | 42 | }; |
michael@0 | 43 | return httpd_setup(handlers); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function setUp(server) { |
michael@0 | 47 | yield configureIdentity({username: "johndoe"}); |
michael@0 | 48 | Service.serverURL = server.baseURI + "/"; |
michael@0 | 49 | Service.clusterURL = server.baseURI + "/"; |
michael@0 | 50 | new FakeCryptoService(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | function run_test() { |
michael@0 | 54 | run_next_test(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | function do_check_soft_eol(eh, start) { |
michael@0 | 58 | // We subtract 1000 because the stored value is in second precision. |
michael@0 | 59 | do_check_true(eh.earliestNextAlert >= (start + eh.MINIMUM_ALERT_INTERVAL_MSEC - 1000)); |
michael@0 | 60 | do_check_eq("soft-eol", eh.currentAlertMode); |
michael@0 | 61 | } |
michael@0 | 62 | function do_check_hard_eol(eh, start) { |
michael@0 | 63 | // We subtract 1000 because the stored value is in second precision. |
michael@0 | 64 | do_check_true(eh.earliestNextAlert >= (start + eh.MINIMUM_ALERT_INTERVAL_MSEC - 1000)); |
michael@0 | 65 | do_check_eq("hard-eol", eh.currentAlertMode); |
michael@0 | 66 | do_check_true(Status.eol); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | add_identity_test(this, function test_200_hard() { |
michael@0 | 70 | let eh = Service.errorHandler; |
michael@0 | 71 | let start = Date.now(); |
michael@0 | 72 | let server = sync_httpd_setup(handler200("hard-eol")); |
michael@0 | 73 | yield setUp(server); |
michael@0 | 74 | |
michael@0 | 75 | let deferred = Promise.defer(); |
michael@0 | 76 | let obs = function (subject, topic, data) { |
michael@0 | 77 | Svc.Obs.remove("weave:eol", obs); |
michael@0 | 78 | do_check_eq("hard-eol", subject.code); |
michael@0 | 79 | do_check_hard_eol(eh, start); |
michael@0 | 80 | do_check_eq(Service.scheduler.eolInterval, Service.scheduler.syncInterval); |
michael@0 | 81 | eh.clearServerAlerts(); |
michael@0 | 82 | server.stop(deferred.resolve); |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | Svc.Obs.add("weave:eol", obs); |
michael@0 | 86 | Service._fetchInfo(); |
michael@0 | 87 | Service.scheduler.adjustSyncInterval(); // As if we failed or succeeded in syncing. |
michael@0 | 88 | yield deferred.promise; |
michael@0 | 89 | }); |
michael@0 | 90 | |
michael@0 | 91 | add_identity_test(this, function test_513_hard() { |
michael@0 | 92 | let eh = Service.errorHandler; |
michael@0 | 93 | let start = Date.now(); |
michael@0 | 94 | let server = sync_httpd_setup(handler513); |
michael@0 | 95 | yield setUp(server); |
michael@0 | 96 | |
michael@0 | 97 | let deferred = Promise.defer(); |
michael@0 | 98 | let obs = function (subject, topic, data) { |
michael@0 | 99 | Svc.Obs.remove("weave:eol", obs); |
michael@0 | 100 | do_check_eq("hard-eol", subject.code); |
michael@0 | 101 | do_check_hard_eol(eh, start); |
michael@0 | 102 | do_check_eq(Service.scheduler.eolInterval, Service.scheduler.syncInterval); |
michael@0 | 103 | eh.clearServerAlerts(); |
michael@0 | 104 | server.stop(deferred.resolve); |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | Svc.Obs.add("weave:eol", obs); |
michael@0 | 108 | try { |
michael@0 | 109 | Service._fetchInfo(); |
michael@0 | 110 | Service.scheduler.adjustSyncInterval(); // As if we failed or succeeded in syncing. |
michael@0 | 111 | } catch (ex) { |
michael@0 | 112 | // Because fetchInfo will fail on a 513. |
michael@0 | 113 | } |
michael@0 | 114 | yield deferred.promise; |
michael@0 | 115 | }); |
michael@0 | 116 | |
michael@0 | 117 | add_identity_test(this, function test_200_soft() { |
michael@0 | 118 | let eh = Service.errorHandler; |
michael@0 | 119 | let start = Date.now(); |
michael@0 | 120 | let server = sync_httpd_setup(handler200("soft-eol")); |
michael@0 | 121 | yield setUp(server); |
michael@0 | 122 | |
michael@0 | 123 | let deferred = Promise.defer(); |
michael@0 | 124 | let obs = function (subject, topic, data) { |
michael@0 | 125 | Svc.Obs.remove("weave:eol", obs); |
michael@0 | 126 | do_check_eq("soft-eol", subject.code); |
michael@0 | 127 | do_check_soft_eol(eh, start); |
michael@0 | 128 | do_check_eq(Service.scheduler.singleDeviceInterval, Service.scheduler.syncInterval); |
michael@0 | 129 | eh.clearServerAlerts(); |
michael@0 | 130 | server.stop(deferred.resolve); |
michael@0 | 131 | }; |
michael@0 | 132 | |
michael@0 | 133 | Svc.Obs.add("weave:eol", obs); |
michael@0 | 134 | Service._fetchInfo(); |
michael@0 | 135 | Service.scheduler.adjustSyncInterval(); // As if we failed or succeeded in syncing. |
michael@0 | 136 | yield deferred.promise; |
michael@0 | 137 | }); |