1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 var assertEq = chrome.test.assertEq;
8 var assertTrue = chrome.test.assertTrue;
9 var callbackPass = chrome.test.callbackPass;
10 var succeed = chrome.test.succeed;
12 // X.509 certificate in DER encoding issued by 'root.pem' which is set to be
13 // trusted by the test setup.
14 // Read from 'l1_leaf.der', generated by create_test_certs.sh .
15 var l1_leaf_cert = null;
17 // Reads the binary file at |path| and passes it as a Uint8Array to |callback|.
18 function readFile(path, callback) {
19 var oReq = new XMLHttpRequest();
20 oReq.responseType = "arraybuffer";
21 oReq.open("GET", path, true /* asynchronous */);
22 oReq.onload = function() {
23 var arrayBuffer = oReq.response;
25 callback(new Uint8Array(arrayBuffer));
33 function compareBuffers(a, b) {
34 if (a.length != b.length)
36 for (var i = 0; i < a.length; i++) {
43 var signDigestRequest;
47 assertTrue(!!chrome.certificateProvider);
48 assertTrue(!!chrome.certificateProvider.onCertificatesRequested);
49 assertTrue(!!chrome.certificateProvider.onSignDigestRequested);
52 certificate: l1_leaf_cert.buffer,
53 supportedHashes: ['SHA1']
55 var invalidCert = new Uint8Array([1, 2, 3, 4, 5]);
56 var invalidCertInfo = {
57 certificate: invalidCert.buffer,
58 supportedHashes: ['SHA256']
61 function checkResult(rejectedCerts) {
62 assertEq(1, rejectedCerts.length);
63 assertTrue(compareBuffers(invalidCert, new Uint8Array(rejectedCerts[0])));
66 function reportCertificates(reportCallback) {
67 reportCallback([validCertInfo, invalidCertInfo], callbackPass(checkResult));
70 chrome.certificateProvider.onCertificatesRequested.addListener(
71 callbackPass(reportCertificates));
73 chrome.certificateProvider.onSignDigestRequested.addListener(function(
76 compareBuffers(l1_leaf_cert, new Uint8Array(request.certificate)));
77 // The sign request must refer to the only hash that was declared to be
79 assertEq(1, validCertInfo.supportedHashes.length);
80 assertEq(validCertInfo.supportedHashes[0], request.hash);
81 signCallback = callback;
82 signDigestRequest = request;
89 function replyWithSignature(signature) {
90 signCallback(signature.buffer);
93 function replyWithSignatureSecondTime() {
94 var signature = new Uint8Array([1,2,3]);
96 signCallback(signature.buffer);
104 chrome.test.runTests([register]);
107 readFile('l1_leaf.der', function(cert) {