Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/certificate_provider/basic.js |
| diff --git a/chrome/test/data/extensions/api_test/certificate_provider/basic.js b/chrome/test/data/extensions/api_test/certificate_provider/basic.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c7ae45214791b72e4fb6eba2e00228367dec41d |
| --- /dev/null |
| +++ b/chrome/test/data/extensions/api_test/certificate_provider/basic.js |
| @@ -0,0 +1,113 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +'use strict'; |
| + |
| +var assertEq = chrome.test.assertEq; |
| +var assertTrue = chrome.test.assertTrue; |
| +var callbackPass = chrome.test.callbackPass; |
| +var succeed = chrome.test.succeed; |
| + |
| +var data = { |
| + // X.509 certificate in DER encoding issued by 'root.pem' which is set to be |
| + // trusted by the test setup. |
| + // Generated by create_test_certs.sh . |
| + l1_leaf_cert: 'l1_leaf.der', |
| +}; |
| + |
| +// Reads the binary file at |path| and passes it as a Uint8Array to |callback|. |
| +function readFile(path, callback) { |
| + var oReq = new XMLHttpRequest(); |
| + oReq.responseType = "arraybuffer"; |
| + oReq.open("GET", path, true /* asynchronous */); |
| + oReq.onload = function() { |
| + var arrayBuffer = oReq.response; |
| + if (arrayBuffer) { |
| + callback(new Uint8Array(arrayBuffer)); |
| + } else { |
| + callback(null); |
| + } |
| + }; |
| + oReq.send(null); |
| +} |
| + |
| +// For each key in dictionary, replaces the path dictionary[key] by the content |
| +// of the resource located at that path stored in a Uint8Array. |
| +function readData(dictionary, callback) { |
|
bartfab (slow)
2015/09/04 09:21:57
Nit: Do we really need all this if in the end, we
pneubeck (no reviews)
2015/09/07 17:21:33
If possible I'd like to share this with the same f
|
| + var keys = Object.keys(dictionary); |
| + function recurse(index) { |
| + if (index >= keys.length) { |
|
bartfab (slow)
2015/09/04 09:21:57
Nit: How is ">" possible?
pneubeck (no reviews)
2015/09/07 17:21:33
Done.
|
| + callback(); |
| + return; |
| + } |
| + var key = keys[index]; |
| + var path = dictionary[key]; |
| + readFile(path, function(array) { |
| + assertTrue(!!array); |
| + dictionary[key] = array; |
| + recurse(index + 1); |
| + }); |
| + } |
| + |
| + recurse(0); |
|
bartfab (slow)
2015/09/04 09:21:57
Whatever happened to good old for loops?
pneubeck (no reviews)
2015/09/07 17:21:33
If we had an asynchronous for loop, I'd be happy t
|
| +} |
| + |
| +var signDigestRequest; |
| +var signCallback; |
| + |
| +function register() { |
| + assertTrue(!!chrome.certificateProvider); |
| + assertTrue(!!chrome.certificateProvider.onCertificatesRequested); |
| + assertTrue(!!chrome.certificateProvider.onSignDigestRequested); |
| + |
| + var validCertInfo = { |
| + certificate: data.l1_leaf_cert.buffer, |
| + supportedHashes: ['SHA1'] |
| + }; |
| + var invalidCert = new Uint8Array([1, 2, 3, 4, 5]); |
| + var invalidCertInfo = { |
| + certificate: invalidCert.buffer, |
| + supportedHashes: ['SHA256'] |
| + }; |
| + |
| + function checkResult(rejectedCerts) { |
| + assertEq(1, rejectedCerts.length); |
| + } |
|
bartfab (slow)
2015/09/04 09:21:57
Should this not verify which cert was rejected?
pneubeck (no reviews)
2015/09/07 17:21:33
Requires comparing arrays, which the standard java
|
| + |
| + function reportCertificates(reportCallback) { |
| + reportCallback([validCertInfo, invalidCertInfo], callbackPass(checkResult)); |
| + } |
| + |
| + chrome.certificateProvider.onCertificatesRequested.addListener( |
| + callbackPass(reportCertificates)); |
| + |
| + chrome.certificateProvider.onSignDigestRequested.addListener(function( |
| + request, callback) { |
| + signCallback = callback; |
| + signDigestRequest = request; |
| + succeed(); |
| + }); |
| + |
| + succeed(); |
| +} |
| + |
| +function replyWithSignature(signature) { |
| + signCallback(signature.buffer, function() {}); |
| +} |
| + |
| +function replyWithSignatureSecondTime() { |
| + var signature = new Uint8Array([1,2,3]); |
| + try { |
| + signCallback(signature.buffer, function() {}); |
| + } catch (e) { |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +function runTest() { |
| + chrome.test.runTests([register]); |
| +} |
| + |
| +readData(data, runTest); |