1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/tests/xpcshell/test_readCertPrefs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.8 +Components.utils.import("resource://gre/modules/CertUtils.jsm"); 1.9 + 1.10 +const PREF_PREFIX = "certutils.certs."; 1.11 + 1.12 +function run_test() { 1.13 + run_next_test(); 1.14 +} 1.15 + 1.16 +function resetPrefs() { 1.17 + var prefs = Services.prefs.getChildList(PREF_PREFIX); 1.18 + prefs.forEach(Services.prefs.clearUserPref); 1.19 +} 1.20 + 1.21 +function attributes_match(aCert, aExpected) { 1.22 + if (Object.keys(aCert).length != Object.keys(aExpected).length) 1.23 + return false; 1.24 + 1.25 + for (var attribute in aCert) { 1.26 + if (!(attribute in aExpected)) 1.27 + return false; 1.28 + if (aCert[attribute] != aExpected[attribute]) 1.29 + return false; 1.30 + } 1.31 + 1.32 + return true; 1.33 +} 1.34 + 1.35 +function test_results(aCerts, aExpected) { 1.36 + do_check_eq(aCerts.length, aExpected.length); 1.37 + 1.38 + for (var i = 0; i < aCerts.length; i++) { 1.39 + if (!attributes_match(aCerts[i], aExpected[i])) { 1.40 + dump("Attributes for certificate " + (i + 1) + " did not match expected attributes\n"); 1.41 + dump("Saw: " + aCerts[i].toSource() + "\n"); 1.42 + dump("Expected: " + aExpected[i].toSource() + "\n"); 1.43 + do_check_true(false); 1.44 + } 1.45 + } 1.46 +} 1.47 + 1.48 +add_test(function test_singleCert() { 1.49 + Services.prefs.setCharPref(PREF_PREFIX + "1.attribute1", "foo"); 1.50 + Services.prefs.setCharPref(PREF_PREFIX + "1.attribute2", "bar"); 1.51 + 1.52 + var certs = readCertPrefs(PREF_PREFIX); 1.53 + test_results(certs, [{ 1.54 + attribute1: "foo", 1.55 + attribute2: "bar" 1.56 + }]); 1.57 + 1.58 + resetPrefs(); 1.59 + run_next_test(); 1.60 +}); 1.61 + 1.62 +add_test(function test_multipleCert() { 1.63 + Services.prefs.setCharPref(PREF_PREFIX + "1.md5Fingerprint", "cf84a9a2a804e021f27cb5128fe151f4"); 1.64 + Services.prefs.setCharPref(PREF_PREFIX + "1.nickname", "1st cert"); 1.65 + Services.prefs.setCharPref(PREF_PREFIX + "2.md5Fingerprint", "9441051b7eb50e5ca2226095af710c1a"); 1.66 + Services.prefs.setCharPref(PREF_PREFIX + "2.nickname", "2nd cert"); 1.67 + 1.68 + var certs = readCertPrefs(PREF_PREFIX); 1.69 + test_results(certs, [{ 1.70 + md5Fingerprint: "cf84a9a2a804e021f27cb5128fe151f4", 1.71 + nickname: "1st cert" 1.72 + }, { 1.73 + md5Fingerprint: "9441051b7eb50e5ca2226095af710c1a", 1.74 + nickname: "2nd cert" 1.75 + }]); 1.76 + 1.77 + resetPrefs(); 1.78 + run_next_test(); 1.79 +}); 1.80 + 1.81 +add_test(function test_skippedCert() { 1.82 + Services.prefs.setCharPref(PREF_PREFIX + "1.issuerName", "Mozilla"); 1.83 + Services.prefs.setCharPref(PREF_PREFIX + "1.nickname", "1st cert"); 1.84 + Services.prefs.setCharPref(PREF_PREFIX + "2.issuerName", "Top CA"); 1.85 + Services.prefs.setCharPref(PREF_PREFIX + "2.nickname", "2nd cert"); 1.86 + Services.prefs.setCharPref(PREF_PREFIX + "4.issuerName", "Unknown CA"); 1.87 + Services.prefs.setCharPref(PREF_PREFIX + "4.nickname", "Ignored cert"); 1.88 + 1.89 + var certs = readCertPrefs(PREF_PREFIX); 1.90 + test_results(certs, [{ 1.91 + issuerName: "Mozilla", 1.92 + nickname: "1st cert" 1.93 + }, { 1.94 + issuerName: "Top CA", 1.95 + nickname: "2nd cert" 1.96 + }]); 1.97 + 1.98 + resetPrefs(); 1.99 + run_next_test(); 1.100 +});