toolkit/modules/tests/xpcshell/test_readCertPrefs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Components.utils.import("resource://gre/modules/Services.jsm");
     5 Components.utils.import("resource://gre/modules/CertUtils.jsm");
     7 const PREF_PREFIX = "certutils.certs.";
     9 function run_test() {
    10   run_next_test();
    11 }
    13 function resetPrefs() {
    14   var prefs = Services.prefs.getChildList(PREF_PREFIX);
    15   prefs.forEach(Services.prefs.clearUserPref);
    16 }
    18 function attributes_match(aCert, aExpected) {
    19   if (Object.keys(aCert).length != Object.keys(aExpected).length)
    20     return false;
    22   for (var attribute in aCert) {
    23     if (!(attribute in aExpected))
    24       return false;
    25     if (aCert[attribute] != aExpected[attribute])
    26       return false;
    27   }
    29   return true;
    30 }
    32 function test_results(aCerts, aExpected) {
    33   do_check_eq(aCerts.length, aExpected.length);
    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 }
    45 add_test(function test_singleCert() {
    46   Services.prefs.setCharPref(PREF_PREFIX + "1.attribute1", "foo");
    47   Services.prefs.setCharPref(PREF_PREFIX + "1.attribute2", "bar");
    49   var certs = readCertPrefs(PREF_PREFIX);
    50   test_results(certs, [{
    51     attribute1: "foo",
    52     attribute2: "bar"
    53   }]);
    55   resetPrefs();
    56   run_next_test();
    57 });
    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");
    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   }]);
    74   resetPrefs();
    75   run_next_test();
    76 });
    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");
    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   }]);
    95   resetPrefs();
    96   run_next_test();
    97 });

mercurial