Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 5 | Components.utils.import("resource://gre/modules/CertUtils.jsm"); |
michael@0 | 6 | |
michael@0 | 7 | const PREF_PREFIX = "certutils.certs."; |
michael@0 | 8 | |
michael@0 | 9 | function run_test() { |
michael@0 | 10 | run_next_test(); |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | function resetPrefs() { |
michael@0 | 14 | var prefs = Services.prefs.getChildList(PREF_PREFIX); |
michael@0 | 15 | prefs.forEach(Services.prefs.clearUserPref); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | function attributes_match(aCert, aExpected) { |
michael@0 | 19 | if (Object.keys(aCert).length != Object.keys(aExpected).length) |
michael@0 | 20 | return false; |
michael@0 | 21 | |
michael@0 | 22 | for (var attribute in aCert) { |
michael@0 | 23 | if (!(attribute in aExpected)) |
michael@0 | 24 | return false; |
michael@0 | 25 | if (aCert[attribute] != aExpected[attribute]) |
michael@0 | 26 | return false; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | return true; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function test_results(aCerts, aExpected) { |
michael@0 | 33 | do_check_eq(aCerts.length, aExpected.length); |
michael@0 | 34 | |
michael@0 | 35 | for (var i = 0; i < aCerts.length; i++) { |
michael@0 | 36 | if (!attributes_match(aCerts[i], aExpected[i])) { |
michael@0 | 37 | dump("Attributes for certificate " + (i + 1) + " did not match expected attributes\n"); |
michael@0 | 38 | dump("Saw: " + aCerts[i].toSource() + "\n"); |
michael@0 | 39 | dump("Expected: " + aExpected[i].toSource() + "\n"); |
michael@0 | 40 | do_check_true(false); |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | add_test(function test_singleCert() { |
michael@0 | 46 | Services.prefs.setCharPref(PREF_PREFIX + "1.attribute1", "foo"); |
michael@0 | 47 | Services.prefs.setCharPref(PREF_PREFIX + "1.attribute2", "bar"); |
michael@0 | 48 | |
michael@0 | 49 | var certs = readCertPrefs(PREF_PREFIX); |
michael@0 | 50 | test_results(certs, [{ |
michael@0 | 51 | attribute1: "foo", |
michael@0 | 52 | attribute2: "bar" |
michael@0 | 53 | }]); |
michael@0 | 54 | |
michael@0 | 55 | resetPrefs(); |
michael@0 | 56 | run_next_test(); |
michael@0 | 57 | }); |
michael@0 | 58 | |
michael@0 | 59 | add_test(function test_multipleCert() { |
michael@0 | 60 | Services.prefs.setCharPref(PREF_PREFIX + "1.md5Fingerprint", "cf84a9a2a804e021f27cb5128fe151f4"); |
michael@0 | 61 | Services.prefs.setCharPref(PREF_PREFIX + "1.nickname", "1st cert"); |
michael@0 | 62 | Services.prefs.setCharPref(PREF_PREFIX + "2.md5Fingerprint", "9441051b7eb50e5ca2226095af710c1a"); |
michael@0 | 63 | Services.prefs.setCharPref(PREF_PREFIX + "2.nickname", "2nd cert"); |
michael@0 | 64 | |
michael@0 | 65 | var certs = readCertPrefs(PREF_PREFIX); |
michael@0 | 66 | test_results(certs, [{ |
michael@0 | 67 | md5Fingerprint: "cf84a9a2a804e021f27cb5128fe151f4", |
michael@0 | 68 | nickname: "1st cert" |
michael@0 | 69 | }, { |
michael@0 | 70 | md5Fingerprint: "9441051b7eb50e5ca2226095af710c1a", |
michael@0 | 71 | nickname: "2nd cert" |
michael@0 | 72 | }]); |
michael@0 | 73 | |
michael@0 | 74 | resetPrefs(); |
michael@0 | 75 | run_next_test(); |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | add_test(function test_skippedCert() { |
michael@0 | 79 | Services.prefs.setCharPref(PREF_PREFIX + "1.issuerName", "Mozilla"); |
michael@0 | 80 | Services.prefs.setCharPref(PREF_PREFIX + "1.nickname", "1st cert"); |
michael@0 | 81 | Services.prefs.setCharPref(PREF_PREFIX + "2.issuerName", "Top CA"); |
michael@0 | 82 | Services.prefs.setCharPref(PREF_PREFIX + "2.nickname", "2nd cert"); |
michael@0 | 83 | Services.prefs.setCharPref(PREF_PREFIX + "4.issuerName", "Unknown CA"); |
michael@0 | 84 | Services.prefs.setCharPref(PREF_PREFIX + "4.nickname", "Ignored cert"); |
michael@0 | 85 | |
michael@0 | 86 | var certs = readCertPrefs(PREF_PREFIX); |
michael@0 | 87 | test_results(certs, [{ |
michael@0 | 88 | issuerName: "Mozilla", |
michael@0 | 89 | nickname: "1st cert" |
michael@0 | 90 | }, { |
michael@0 | 91 | issuerName: "Top CA", |
michael@0 | 92 | nickname: "2nd cert" |
michael@0 | 93 | }]); |
michael@0 | 94 | |
michael@0 | 95 | resetPrefs(); |
michael@0 | 96 | run_next_test(); |
michael@0 | 97 | }); |