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