michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://services-common/utils.js"); michael@0: Cu.import("resource://services-crypto/utils.js"); michael@0: michael@0: function run_test() { michael@0: initTestLogging(); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_sha1() { michael@0: _("Ensure HTTP MAC SHA1 generation works as expected."); michael@0: michael@0: let id = "vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7"; michael@0: let key = "b8u1cc5iiio5o319og7hh8faf2gi5ym4aq0zwf112cv1287an65fudu5zj7zo7dz"; michael@0: let ts = 1329181221; michael@0: let method = "GET"; michael@0: let nonce = "wGX71"; michael@0: let uri = CommonUtils.makeURI("http://10.250.2.176/alias/"); michael@0: michael@0: let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, michael@0: {ts: ts, nonce: nonce}); michael@0: michael@0: do_check_eq(btoa(result.mac), "jzh5chjQc2zFEvLbyHnPdX11Yck="); michael@0: michael@0: do_check_eq(result.getHeader(), michael@0: 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' + michael@0: 'ts="1329181221", nonce="wGX71", mac="jzh5chjQc2zFEvLbyHnPdX11Yck="'); michael@0: michael@0: let ext = "EXTRA DATA; foo,bar=1"; michael@0: michael@0: let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, michael@0: {ts: ts, nonce: nonce, ext: ext}); michael@0: do_check_eq(btoa(result.mac), "bNf4Fnt5k6DnhmyipLPkuZroH68="); michael@0: do_check_eq(result.getHeader(), michael@0: 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' + michael@0: 'ts="1329181221", nonce="wGX71", mac="bNf4Fnt5k6DnhmyipLPkuZroH68=", ' + michael@0: 'ext="EXTRA DATA; foo,bar=1"'); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_nonce_length() { michael@0: _("Ensure custom nonce lengths are honoured."); michael@0: michael@0: function get_mac(length) { michael@0: let uri = CommonUtils.makeURI("http://example.com/"); michael@0: return CryptoUtils.computeHTTPMACSHA1("foo", "bar", "GET", uri, { michael@0: nonce_bytes: length michael@0: }); michael@0: } michael@0: michael@0: let result = get_mac(12); michael@0: do_check_eq(12, atob(result.nonce).length); michael@0: michael@0: let result = get_mac(2); michael@0: do_check_eq(2, atob(result.nonce).length); michael@0: michael@0: let result = get_mac(0); michael@0: do_check_eq(8, atob(result.nonce).length); michael@0: michael@0: let result = get_mac(-1); michael@0: do_check_eq(8, atob(result.nonce).length); michael@0: michael@0: run_next_test(); michael@0: });