services/crypto/tests/unit/test_utils_httpmac.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
     5 Cu.import("resource://services-common/utils.js");
     6 Cu.import("resource://services-crypto/utils.js");
     8 function run_test() {
     9   initTestLogging();
    11   run_next_test();
    12 }
    14 add_test(function test_sha1() {
    15   _("Ensure HTTP MAC SHA1 generation works as expected.");
    17   let id = "vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7";
    18   let key = "b8u1cc5iiio5o319og7hh8faf2gi5ym4aq0zwf112cv1287an65fudu5zj7zo7dz";
    19   let ts = 1329181221;
    20   let method = "GET";
    21   let nonce = "wGX71";
    22   let uri = CommonUtils.makeURI("http://10.250.2.176/alias/");
    24   let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri,
    25                                               {ts: ts, nonce: nonce});
    27   do_check_eq(btoa(result.mac), "jzh5chjQc2zFEvLbyHnPdX11Yck=");
    29   do_check_eq(result.getHeader(),
    30               'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' +
    31               'ts="1329181221", nonce="wGX71", mac="jzh5chjQc2zFEvLbyHnPdX11Yck="');
    33   let ext = "EXTRA DATA; foo,bar=1";
    35   let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri,
    36                                               {ts: ts, nonce: nonce, ext: ext});
    37   do_check_eq(btoa(result.mac), "bNf4Fnt5k6DnhmyipLPkuZroH68=");
    38   do_check_eq(result.getHeader(),
    39               'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' +
    40               'ts="1329181221", nonce="wGX71", mac="bNf4Fnt5k6DnhmyipLPkuZroH68=", ' +
    41               'ext="EXTRA DATA; foo,bar=1"');
    43   run_next_test();
    44 });
    46 add_test(function test_nonce_length() {
    47   _("Ensure custom nonce lengths are honoured.");
    49   function get_mac(length) {
    50     let uri = CommonUtils.makeURI("http://example.com/");
    51     return CryptoUtils.computeHTTPMACSHA1("foo", "bar", "GET", uri, {
    52       nonce_bytes: length
    53     });
    54   }
    56   let result = get_mac(12);
    57   do_check_eq(12, atob(result.nonce).length);
    59   let result = get_mac(2);
    60   do_check_eq(2, atob(result.nonce).length);
    62   let result = get_mac(0);
    63   do_check_eq(8, atob(result.nonce).length);
    65   let result = get_mac(-1);
    66   do_check_eq(8, atob(result.nonce).length);
    68   run_next_test();
    69 });

mercurial