Bug 1940967 - Vendor glean_parser v16.2.0 r=TravisLong,mach-reviewers,ahal
[gecko.git] / security / manager / ssl / tests / unit / test_constructX509FromBase64.js
blob400724bef81004766efed4461427c360e80efead
1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 "use strict";
7 // Checks that ConstructX509FromBase64() accepts valid input and rejects invalid
8 // input.
10 do_get_profile(); // Must be called before getting nsIX509CertDB
11 const certDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
12   Ci.nsIX509CertDB
15 function excMessage(e) {
16   if (e.message) {
17     let msg = e.message;
18     if (e.data) {
19       msg = msg + ": " + e.data;
20     }
21     return msg;
22   }
24   return e.toString();
27 function testGood(data) {
28   try {
29     let cert = certDB.constructX509FromBase64(data.cert);
30     equal(
31       cert.commonName,
32       data.cn,
33       "Actual and expected commonName should match"
34     );
35   } catch (e) {
36     info(`Exception: ${excMessage(e)}`);
37     ok(false, `Should not have gotten an exception for "CN=${data.cn}"`);
38   }
41 function testBad(data) {
42   throws(
43     () => certDB.constructX509FromBase64(data.input),
44     data.result,
45     `Should get "${data.result}" for "${data.input}"`
46   );
49 function run_test() {
50   const badCases = [
51     // Wrong type or too short
52     { input: null, result: /NS_ERROR_ILLEGAL_VALUE/ },
53     { input: "", result: /NS_ERROR_ILLEGAL_VALUE/ },
54     { input: "=", result: /NS_ERROR_ILLEGAL_VALUE/ },
55     { input: "==", result: /NS_ERROR_ILLEGAL_VALUE/ },
56     // Not base64
57     { input: "forty-four dead stone lions", result: /NS_ERROR_ILLEGAL_VALUE/ },
58     // Not a cert
59     {
60       input: "Zm9ydHktZm91ciBkZWFkIHN0b25lIGxpb25z",
61       result: /NS_ERROR_FAILURE/,
62     },
63   ];
65   // Real certs with all three padding levels
66   const goodCases = [
67     {
68       cn: "A",
69       cert: "MIHhMIGcAgEAMA0GCSqGSIb3DQEBBQUAMAwxCjAIBgNVBAMTAUEwHhcNMTEwMzIzMjMyNTE3WhcNMTEwNDIyMjMyNTE3WjAMMQowCAYDVQQDEwFBMEwwDQYJKoZIhvcNAQEBBQADOwAwOAIxANFm7ZCfYNJViaDWTFuMClX3+9u18VFGiyLfM6xJrxir4QVtQC7VUC/WUGoBUs9COQIDAQABMA0GCSqGSIb3DQEBBQUAAzEAx2+gIwmuYjJO5SyabqIm4lB1MandHH1HQc0y0tUFshBOMESTzQRPSVwPn77a6R9t",
70     },
71     {
72       cn: "Bo",
73       cert: "MIHjMIGeAgEAMA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNVBAMTAkJvMB4XDTExMDMyMzIzMjYwMloXDTExMDQyMjIzMjYwMlowDTELMAkGA1UEAxMCQm8wTDANBgkqhkiG9w0BAQEFAAM7ADA4AjEA1FoSl9w9HqMqVgk2K0J3OTiRsgHeNsQdPUl6S82ME33gH+E56PcWZA3nse+fpS3NAgMBAAEwDQYJKoZIhvcNAQEFBQADMQAo/e3BvQAmygiATljQ68tWPoWcbMwa1xxAvpWTEc1LOvMqeDBinBUqbAbSmPhGWb4=",
74     },
75     {
76       cn: "Cid",
77       cert: "MIHlMIGgAgEAMA0GCSqGSIb3DQEBBQUAMA4xDDAKBgNVBAMTA0NpZDAeFw0xMTAzMjMyMzI2MzJaFw0xMTA0MjIyMzI2MzJaMA4xDDAKBgNVBAMTA0NpZDBMMA0GCSqGSIb3DQEBAQUAAzsAMDgCMQDUUxlF5xKN+8KCSsR83sN+SRwJmZdliXsnBB7PU0OgbmOWN0u8yehRkmu39kN9tzcCAwEAATANBgkqhkiG9w0BAQUFAAMxAJ3UScNqRcjHFrNu4nuwRldZLJlVJvRYXp982V4/kYodQEGN4gJ+Qyj+HTsaXy5x/w==",
78     },
79   ];
81   for (let badCase of badCases) {
82     testBad(badCase);
83   }
84   for (let goodCase of goodCases) {
85     testGood(goodCase);
86   }