test_utils_httpmac.js (2121B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { CryptoUtils } = ChromeUtils.importESModule( 5 "moz-src:///services/crypto/modules/utils.sys.mjs" 6 ); 7 8 add_test(function setup() { 9 initTestLogging(); 10 run_next_test(); 11 }); 12 13 add_task(async function test_sha1() { 14 _("Ensure HTTP MAC SHA1 generation works as expected."); 15 16 let id = "vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7"; 17 let key = "b8u1cc5iiio5o319og7hh8faf2gi5ym4aq0zwf112cv1287an65fudu5zj7zo7dz"; 18 let ts = 1329181221; 19 let method = "GET"; 20 let nonce = "wGX71"; 21 let uri = CommonUtils.makeURI("http://10.250.2.176/alias/"); 22 23 let result = await CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, { 24 ts, 25 nonce, 26 }); 27 28 Assert.equal(btoa(result.mac), "jzh5chjQc2zFEvLbyHnPdX11Yck="); 29 30 Assert.equal( 31 result.getHeader(), 32 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' + 33 'ts="1329181221", nonce="wGX71", mac="jzh5chjQc2zFEvLbyHnPdX11Yck="' 34 ); 35 36 let ext = "EXTRA DATA; foo,bar=1"; 37 38 result = await CryptoUtils.computeHTTPMACSHA1(id, key, method, uri, { 39 ts, 40 nonce, 41 ext, 42 }); 43 Assert.equal(btoa(result.mac), "bNf4Fnt5k6DnhmyipLPkuZroH68="); 44 Assert.equal( 45 result.getHeader(), 46 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' + 47 'ts="1329181221", nonce="wGX71", mac="bNf4Fnt5k6DnhmyipLPkuZroH68=", ' + 48 'ext="EXTRA DATA; foo,bar=1"' 49 ); 50 }); 51 52 add_task(async function test_nonce_length() { 53 _("Ensure custom nonce lengths are honoured."); 54 55 function get_mac(length) { 56 let uri = CommonUtils.makeURI("http://example.com/"); 57 return CryptoUtils.computeHTTPMACSHA1("foo", "bar", "GET", uri, { 58 nonce_bytes: length, 59 }); 60 } 61 62 let result = await get_mac(12); 63 Assert.equal(12, atob(result.nonce).length); 64 65 result = await get_mac(2); 66 Assert.equal(2, atob(result.nonce).length); 67 68 result = await get_mac(0); 69 Assert.equal(8, atob(result.nonce).length); 70 71 result = await get_mac(-1); 72 Assert.equal(8, atob(result.nonce).length); 73 });