1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/crypto/tests/unit/test_utils_httpmac.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.8 +Cu.import("resource://services-common/utils.js"); 1.9 +Cu.import("resource://services-crypto/utils.js"); 1.10 + 1.11 +function run_test() { 1.12 + initTestLogging(); 1.13 + 1.14 + run_next_test(); 1.15 +} 1.16 + 1.17 +add_test(function test_sha1() { 1.18 + _("Ensure HTTP MAC SHA1 generation works as expected."); 1.19 + 1.20 + let id = "vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7"; 1.21 + let key = "b8u1cc5iiio5o319og7hh8faf2gi5ym4aq0zwf112cv1287an65fudu5zj7zo7dz"; 1.22 + let ts = 1329181221; 1.23 + let method = "GET"; 1.24 + let nonce = "wGX71"; 1.25 + let uri = CommonUtils.makeURI("http://10.250.2.176/alias/"); 1.26 + 1.27 + let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, 1.28 + {ts: ts, nonce: nonce}); 1.29 + 1.30 + do_check_eq(btoa(result.mac), "jzh5chjQc2zFEvLbyHnPdX11Yck="); 1.31 + 1.32 + do_check_eq(result.getHeader(), 1.33 + 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' + 1.34 + 'ts="1329181221", nonce="wGX71", mac="jzh5chjQc2zFEvLbyHnPdX11Yck="'); 1.35 + 1.36 + let ext = "EXTRA DATA; foo,bar=1"; 1.37 + 1.38 + let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, 1.39 + {ts: ts, nonce: nonce, ext: ext}); 1.40 + do_check_eq(btoa(result.mac), "bNf4Fnt5k6DnhmyipLPkuZroH68="); 1.41 + do_check_eq(result.getHeader(), 1.42 + 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' + 1.43 + 'ts="1329181221", nonce="wGX71", mac="bNf4Fnt5k6DnhmyipLPkuZroH68=", ' + 1.44 + 'ext="EXTRA DATA; foo,bar=1"'); 1.45 + 1.46 + run_next_test(); 1.47 +}); 1.48 + 1.49 +add_test(function test_nonce_length() { 1.50 + _("Ensure custom nonce lengths are honoured."); 1.51 + 1.52 + function get_mac(length) { 1.53 + let uri = CommonUtils.makeURI("http://example.com/"); 1.54 + return CryptoUtils.computeHTTPMACSHA1("foo", "bar", "GET", uri, { 1.55 + nonce_bytes: length 1.56 + }); 1.57 + } 1.58 + 1.59 + let result = get_mac(12); 1.60 + do_check_eq(12, atob(result.nonce).length); 1.61 + 1.62 + let result = get_mac(2); 1.63 + do_check_eq(2, atob(result.nonce).length); 1.64 + 1.65 + let result = get_mac(0); 1.66 + do_check_eq(8, atob(result.nonce).length); 1.67 + 1.68 + let result = get_mac(-1); 1.69 + do_check_eq(8, atob(result.nonce).length); 1.70 + 1.71 + run_next_test(); 1.72 +});