services/crypto/tests/unit/test_utils_httpmac.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:a92e28b32e3a
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
5 Cu.import("resource://services-common/utils.js");
6 Cu.import("resource://services-crypto/utils.js");
7
8 function run_test() {
9 initTestLogging();
10
11 run_next_test();
12 }
13
14 add_test(function test_sha1() {
15 _("Ensure HTTP MAC SHA1 generation works as expected.");
16
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/");
23
24 let result = CryptoUtils.computeHTTPMACSHA1(id, key, method, uri,
25 {ts: ts, nonce: nonce});
26
27 do_check_eq(btoa(result.mac), "jzh5chjQc2zFEvLbyHnPdX11Yck=");
28
29 do_check_eq(result.getHeader(),
30 'MAC id="vmo1txkttblmn51u2p3zk2xiy16hgvm5ok8qiv1yyi86ffjzy9zj0ez9x6wnvbx7", ' +
31 'ts="1329181221", nonce="wGX71", mac="jzh5chjQc2zFEvLbyHnPdX11Yck="');
32
33 let ext = "EXTRA DATA; foo,bar=1";
34
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"');
42
43 run_next_test();
44 });
45
46 add_test(function test_nonce_length() {
47 _("Ensure custom nonce lengths are honoured.");
48
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 }
55
56 let result = get_mac(12);
57 do_check_eq(12, atob(result.nonce).length);
58
59 let result = get_mac(2);
60 do_check_eq(2, atob(result.nonce).length);
61
62 let result = get_mac(0);
63 do_check_eq(8, atob(result.nonce).length);
64
65 let result = get_mac(-1);
66 do_check_eq(8, atob(result.nonce).length);
67
68 run_next_test();
69 });

mercurial