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-crypto/utils.js"); |
michael@0 | 5 | Cu.import("resource://services-common/async.js"); |
michael@0 | 6 | Cu.import("resource://services-common/rest.js"); |
michael@0 | 7 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 8 | |
michael@0 | 9 | function run_test() { |
michael@0 | 10 | initTestLogging("Trace"); |
michael@0 | 11 | run_next_test(); |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | add_test(function test_authenticated_request() { |
michael@0 | 15 | _("Ensure that sending a MAC authenticated GET request works as expected."); |
michael@0 | 16 | |
michael@0 | 17 | let message = "Great Success!"; |
michael@0 | 18 | |
michael@0 | 19 | // TODO: We use a preset key here, but use getTokenFromBrowserIDAssertion() |
michael@0 | 20 | // from TokenServerClient to get a real one when possible. (Bug 745800) |
michael@0 | 21 | let id = "eyJleHBpcmVzIjogMTM2NTAxMDg5OC4x"; |
michael@0 | 22 | let key = "qTZf4ZFpAMpMoeSsX3zVRjiqmNs="; |
michael@0 | 23 | let method = "GET"; |
michael@0 | 24 | |
michael@0 | 25 | let nonce = btoa(CryptoUtils.generateRandomBytes(16)); |
michael@0 | 26 | let ts = Math.floor(Date.now() / 1000); |
michael@0 | 27 | let extra = {ts: ts, nonce: nonce}; |
michael@0 | 28 | |
michael@0 | 29 | let auth; |
michael@0 | 30 | |
michael@0 | 31 | let server = httpd_setup({"/foo": function(request, response) { |
michael@0 | 32 | do_check_true(request.hasHeader("Authorization")); |
michael@0 | 33 | do_check_eq(auth, request.getHeader("Authorization")); |
michael@0 | 34 | |
michael@0 | 35 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 36 | response.bodyOutputStream.write(message, message.length); |
michael@0 | 37 | } |
michael@0 | 38 | }); |
michael@0 | 39 | let uri = CommonUtils.makeURI(server.baseURI + "/foo"); |
michael@0 | 40 | let sig = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, extra); |
michael@0 | 41 | auth = sig.getHeader(); |
michael@0 | 42 | |
michael@0 | 43 | let req = new TokenAuthenticatedRESTRequest(uri, {id: id, key: key}, extra); |
michael@0 | 44 | let cb = Async.makeSpinningCallback(); |
michael@0 | 45 | req.get(cb); |
michael@0 | 46 | let result = cb.wait(); |
michael@0 | 47 | |
michael@0 | 48 | do_check_eq(null, result); |
michael@0 | 49 | do_check_eq(message, req.response.body); |
michael@0 | 50 | |
michael@0 | 51 | server.stop(run_next_test); |
michael@0 | 52 | }); |