1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/crypto/tests/unit/test_utils_sha1.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,37 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +_("Make sure sha1 digests works with various messages"); 1.8 + 1.9 +Cu.import("resource://services-crypto/utils.js"); 1.10 + 1.11 +function run_test() { 1.12 + let mes1 = "hello"; 1.13 + let mes2 = "world"; 1.14 + 1.15 + let dig0 = CryptoUtils.UTF8AndSHA1(mes1); 1.16 + do_check_eq(dig0, 1.17 + "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d"); 1.18 + 1.19 + _("Make sure right sha1 digests are generated"); 1.20 + let dig1 = CryptoUtils.sha1(mes1); 1.21 + do_check_eq(dig1, "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"); 1.22 + let dig2 = CryptoUtils.sha1(mes2); 1.23 + do_check_eq(dig2, "7c211433f02071597741e6ff5a8ea34789abbf43"); 1.24 + let dig12 = CryptoUtils.sha1(mes1 + mes2); 1.25 + do_check_eq(dig12, "6adfb183a4a2c94a2f92dab5ade762a47889a5a1"); 1.26 + let dig21 = CryptoUtils.sha1(mes2 + mes1); 1.27 + do_check_eq(dig21, "5715790a892990382d98858c4aa38d0617151575"); 1.28 + 1.29 + _("Repeated sha1s shouldn't change the digest"); 1.30 + do_check_eq(CryptoUtils.sha1(mes1), dig1); 1.31 + do_check_eq(CryptoUtils.sha1(mes2), dig2); 1.32 + do_check_eq(CryptoUtils.sha1(mes1 + mes2), dig12); 1.33 + do_check_eq(CryptoUtils.sha1(mes2 + mes1), dig21); 1.34 + 1.35 + _("Nested sha1 should work just fine"); 1.36 + let nest1 = CryptoUtils.sha1(CryptoUtils.sha1(CryptoUtils.sha1(CryptoUtils.sha1(CryptoUtils.sha1(mes1))))); 1.37 + do_check_eq(nest1, "23f340d0cff31e299158b3181b6bcc7e8c7f985a"); 1.38 + let nest2 = CryptoUtils.sha1(CryptoUtils.sha1(CryptoUtils.sha1(CryptoUtils.sha1(CryptoUtils.sha1(mes2))))); 1.39 + do_check_eq(nest2, "1f6453867e3fb9876ae429918a64cdb8dc5ff2d0"); 1.40 +}