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://gre/modules/Log.jsm"); |
michael@0 | 7 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 8 | Cu.import("resource://services-common/hawkrequest.js"); |
michael@0 | 9 | |
michael@0 | 10 | function do_register_cleanup() { |
michael@0 | 11 | Services.prefs.resetUserPrefs(); |
michael@0 | 12 | |
michael@0 | 13 | // remove the pref change listener |
michael@0 | 14 | let hawk = new HAWKAuthenticatedRESTRequest("https://example.com"); |
michael@0 | 15 | hawk._intl.uninit(); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | function run_test() { |
michael@0 | 19 | Log.repository.getLogger("Services.Common.RESTRequest").level = |
michael@0 | 20 | Log.Level.Trace; |
michael@0 | 21 | initTestLogging("Trace"); |
michael@0 | 22 | |
michael@0 | 23 | run_next_test(); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | |
michael@0 | 27 | add_test(function test_intl_accept_language() { |
michael@0 | 28 | let testCount = 0; |
michael@0 | 29 | let languages = [ |
michael@0 | 30 | "zu-NP;vo", // Nepalese dialect of Zulu, defaulting to Volapük |
michael@0 | 31 | "fa-CG;ik", // Congolese dialect of Farsei, defaulting to Inupiaq |
michael@0 | 32 | ]; |
michael@0 | 33 | |
michael@0 | 34 | function setLanguagePref(lang) { |
michael@0 | 35 | let acceptLanguage = Cc["@mozilla.org/supports-string;1"] |
michael@0 | 36 | .createInstance(Ci.nsISupportsString); |
michael@0 | 37 | acceptLanguage.data = lang; |
michael@0 | 38 | Services.prefs.setComplexValue( |
michael@0 | 39 | "intl.accept_languages", Ci.nsISupportsString, acceptLanguage); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | let hawk = new HAWKAuthenticatedRESTRequest("https://example.com"); |
michael@0 | 43 | |
michael@0 | 44 | Services.prefs.addObserver("intl.accept_languages", checkLanguagePref, false); |
michael@0 | 45 | setLanguagePref(languages[testCount]); |
michael@0 | 46 | |
michael@0 | 47 | function checkLanguagePref() { |
michael@0 | 48 | var _done = false; |
michael@0 | 49 | CommonUtils.nextTick(function() { |
michael@0 | 50 | // Ensure we're only called for the number of entries in languages[]. |
michael@0 | 51 | do_check_true(testCount < languages.length); |
michael@0 | 52 | |
michael@0 | 53 | do_check_eq(hawk._intl.accept_languages, languages[testCount]); |
michael@0 | 54 | |
michael@0 | 55 | testCount++; |
michael@0 | 56 | if (testCount < languages.length) { |
michael@0 | 57 | // Set next language in prefs; Pref service will call checkNextLanguage. |
michael@0 | 58 | setLanguagePref(languages[testCount]); |
michael@0 | 59 | return; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // We've checked all the entries in languages[]. Cleanup and move on. |
michael@0 | 63 | do_print("Checked " + testCount + " languages. Removing checkLanguagePref as pref observer."); |
michael@0 | 64 | Services.prefs.removeObserver("intl.accept_languages", checkLanguagePref); |
michael@0 | 65 | run_next_test(); |
michael@0 | 66 | return; |
michael@0 | 67 | }); |
michael@0 | 68 | } |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | add_test(function test_hawk_authenticated_request() { |
michael@0 | 72 | let onProgressCalled = false; |
michael@0 | 73 | let postData = {your: "data"}; |
michael@0 | 74 | |
michael@0 | 75 | // An arbitrary date - Feb 2, 1971. It ends in a bunch of zeroes to make our |
michael@0 | 76 | // computation with the hawk timestamp easier, since hawk throws away the |
michael@0 | 77 | // millisecond values. |
michael@0 | 78 | let then = 34329600000; |
michael@0 | 79 | |
michael@0 | 80 | let clockSkew = 120000; |
michael@0 | 81 | let timeOffset = -1 * clockSkew; |
michael@0 | 82 | let localTime = then + clockSkew; |
michael@0 | 83 | |
michael@0 | 84 | // Set the accept-languages pref to the Nepalese dialect of Zulu. |
michael@0 | 85 | let acceptLanguage = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString); |
michael@0 | 86 | acceptLanguage.data = 'zu-NP'; // omit trailing ';', which our HTTP libs snip |
michael@0 | 87 | Services.prefs.setComplexValue('intl.accept_languages', Ci.nsISupportsString, acceptLanguage); |
michael@0 | 88 | |
michael@0 | 89 | let credentials = { |
michael@0 | 90 | id: "eyJleHBpcmVzIjogMTM2NTAxMDg5OC4x", |
michael@0 | 91 | key: "qTZf4ZFpAMpMoeSsX3zVRjiqmNs=", |
michael@0 | 92 | algorithm: "sha256" |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | let server = httpd_setup({ |
michael@0 | 96 | "/elysium": function(request, response) { |
michael@0 | 97 | do_check_true(request.hasHeader("Authorization")); |
michael@0 | 98 | |
michael@0 | 99 | // check that the header timestamp is our arbitrary system date, not |
michael@0 | 100 | // today's date. Note that hawk header timestamps are in seconds, not |
michael@0 | 101 | // milliseconds. |
michael@0 | 102 | let authorization = request.getHeader("Authorization"); |
michael@0 | 103 | let tsMS = parseInt(/ts="(\d+)"/.exec(authorization)[1], 10) * 1000; |
michael@0 | 104 | do_check_eq(tsMS, then); |
michael@0 | 105 | |
michael@0 | 106 | // This testing can be a little wonky. In an environment where |
michael@0 | 107 | // pref("intl.accept_languages") === 'en-US, en' |
michael@0 | 108 | // the header is sent as: |
michael@0 | 109 | // 'en-US,en;q=0.5' |
michael@0 | 110 | // hence our fake value for acceptLanguage. |
michael@0 | 111 | let lang = request.getHeader("Accept-Language"); |
michael@0 | 112 | do_check_eq(lang, acceptLanguage); |
michael@0 | 113 | |
michael@0 | 114 | let message = "yay"; |
michael@0 | 115 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 116 | response.bodyOutputStream.write(message, message.length); |
michael@0 | 117 | } |
michael@0 | 118 | }); |
michael@0 | 119 | |
michael@0 | 120 | function onProgress() { |
michael@0 | 121 | onProgressCalled = true; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | function onComplete(error) { |
michael@0 | 125 | do_check_eq(200, this.response.status); |
michael@0 | 126 | do_check_eq(this.response.body, "yay"); |
michael@0 | 127 | do_check_true(onProgressCalled); |
michael@0 | 128 | |
michael@0 | 129 | Services.prefs.resetUserPrefs(); |
michael@0 | 130 | let pref = Services.prefs.getComplexValue( |
michael@0 | 131 | "intl.accept_languages", Ci.nsIPrefLocalizedString); |
michael@0 | 132 | do_check_neq(acceptLanguage.data, pref.data); |
michael@0 | 133 | |
michael@0 | 134 | server.stop(run_next_test); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | let url = server.baseURI + "/elysium"; |
michael@0 | 138 | let extra = { |
michael@0 | 139 | now: localTime, |
michael@0 | 140 | localtimeOffsetMsec: timeOffset |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | let request = new HAWKAuthenticatedRESTRequest(url, credentials, extra); |
michael@0 | 144 | |
michael@0 | 145 | // Allow hawk._intl to respond to the language pref change |
michael@0 | 146 | CommonUtils.nextTick(function() { |
michael@0 | 147 | request.post(postData, onComplete, onProgress); |
michael@0 | 148 | }); |
michael@0 | 149 | }); |
michael@0 | 150 | |
michael@0 | 151 | add_test(function test_hawk_language_pref_changed() { |
michael@0 | 152 | let languages = [ |
michael@0 | 153 | "zu-NP", // Nepalese dialect of Zulu |
michael@0 | 154 | "fa-CG", // Congolese dialect of Farsi |
michael@0 | 155 | ]; |
michael@0 | 156 | |
michael@0 | 157 | let credentials = { |
michael@0 | 158 | id: "eyJleHBpcmVzIjogMTM2NTAxMDg5OC4x", |
michael@0 | 159 | key: "qTZf4ZFpAMpMoeSsX3zVRjiqmNs=", |
michael@0 | 160 | algorithm: "sha256", |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | function setLanguage(lang) { |
michael@0 | 164 | let acceptLanguage = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); |
michael@0 | 165 | acceptLanguage.data = lang; |
michael@0 | 166 | Services.prefs.setComplexValue("intl.accept_languages", Ci.nsISupportsString, acceptLanguage); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | let server = httpd_setup({ |
michael@0 | 170 | "/foo": function(request, response) { |
michael@0 | 171 | do_check_eq(languages[1], request.getHeader("Accept-Language")); |
michael@0 | 172 | |
michael@0 | 173 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 174 | }, |
michael@0 | 175 | }); |
michael@0 | 176 | |
michael@0 | 177 | let url = server.baseURI + "/foo"; |
michael@0 | 178 | let postData = {}; |
michael@0 | 179 | let request; |
michael@0 | 180 | |
michael@0 | 181 | setLanguage(languages[0]); |
michael@0 | 182 | |
michael@0 | 183 | // A new request should create the stateful object for tracking the current |
michael@0 | 184 | // language. |
michael@0 | 185 | request = new HAWKAuthenticatedRESTRequest(url, credentials); |
michael@0 | 186 | CommonUtils.nextTick(testFirstLanguage); |
michael@0 | 187 | |
michael@0 | 188 | function testFirstLanguage() { |
michael@0 | 189 | do_check_eq(languages[0], request._intl.accept_languages); |
michael@0 | 190 | |
michael@0 | 191 | // Change the language pref ... |
michael@0 | 192 | setLanguage(languages[1]); |
michael@0 | 193 | CommonUtils.nextTick(testRequest); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | function testRequest() { |
michael@0 | 197 | // Change of language pref should be picked up, which we can see on the |
michael@0 | 198 | // server by inspecting the request headers. |
michael@0 | 199 | request = new HAWKAuthenticatedRESTRequest(url, credentials); |
michael@0 | 200 | request.post({}, function(error) { |
michael@0 | 201 | do_check_null(error); |
michael@0 | 202 | do_check_eq(200, this.response.status); |
michael@0 | 203 | |
michael@0 | 204 | Services.prefs.resetUserPrefs(); |
michael@0 | 205 | |
michael@0 | 206 | server.stop(run_next_test); |
michael@0 | 207 | }); |
michael@0 | 208 | } |
michael@0 | 209 | }); |
michael@0 | 210 |