blob: 069b76ffc6f48e2f27530a42be5b407eaf911727 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../fast/js/resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests cypto.subtle.digest.");
jsTestIsAsync = true;
// Builds a hex string representation of any array-like input (array or
// ArrayBufferView). The output looks like this:
// [ab 03 4c 99]
function byteArrayToHexString(bytes)
{
var hexBytes = [];
for (var i = 0; i < bytes.length; ++i) {
var byteString = bytes[i].toString(16);
if (byteString.length < 2)
byteString = "0" + byteString;
hexBytes.push(byteString);
}
return "[" + hexBytes.join(" ") + "]";
}
// Each sub-test run in this file is asynchronous. Chaining them together
// manually leads to very unreadable code, due to having closures within
// closures within closures. Instead of doing that, each subtest calls
// "startNextTest()" once it has completed.
currentTestIndex = 0;
function startNextTest()
{
var currentTest = allTests[currentTestIndex++];
if (!currentTest) {
finishJSTest();
return;
}
currentTest();
}
function rejectHandler(value, justPrint)
{
debug(" rejected with value of " + value);
if (!justPrint)
startNextTest();
}
function resultHandler(buffer, justPrint)
{
debug(" = " + byteArrayToHexString(new Uint8Array(buffer)));
if (!justPrint)
startNextTest();
}
function failHandler(value)
{
testFailed(value);
startNextTest();
}
allTests = [
function()
{
debug("SHA1 of [] -- with empty process()")
op = crypto.subtle.digest({name: 'sha-1'});
op.process(new Uint8Array([]));
op.finish().then(resultHandler, rejectHandler);
},
function()
{
debug("SHA1 of [] -- without calling process()")
op = crypto.subtle.digest({name: 'sha-1'});
op.finish().then(resultHandler, rejectHandler);
},
function()
{
debug("SHA1 of [0x0]")
op = crypto.subtle.digest({name: 'sha-1'});
op.process(new Uint8Array([0]));
op.finish().then(resultHandler, rejectHandler);
},
function()
{
debug("SHA1 of [0x0] -- as an ArrayBuffer")
op = crypto.subtle.digest({name: 'sha-1'});
op.process(new Uint8Array([0]).buffer);
op.finish().then(resultHandler, rejectHandler);
},
function()
{
debug("SHA1 of [0, 1, 2, 3, 4, 5] -- multipart")
op = crypto.subtle.digest({name: 'sha-1'});
op.process(new Uint8Array([0]));
op.process(new Uint8Array([1]));
op.process(new Uint8Array([2]));
op.process(new Uint8Array([3]));
op.process(new Uint8Array([4]));
op.process(new Uint8Array([5]));
op.finish().then(resultHandler, rejectHandler);
},
function()
{
debug("Call process() after finish()");
op = crypto.subtle.digest({name: 'sha-1'});
op.finish().then(resultHandler, rejectHandler);
// These should all be no-ops, since has already finished.
op.process(new Uint8Array([0]));
op.process(new Uint8Array([1]));
op.process(new Uint8Array([2]));
op.process(new Uint8Array([3]));
},
function()
{
debug("abort()");
op = crypto.subtle.digest({name: 'sha-1'});
op.abort().then(resultHandler, rejectHandler);
},
function()
{
debug("abort() and then abort()");
op = crypto.subtle.digest({name: 'sha-1'});
op.abort().then(failHandler, function(value) {
rejectHandler(value, true);
op.abort().then(resultHandler, rejectHandler);
});
},
function()
{
debug("process() and then abort()");
op = crypto.subtle.digest({name: 'sha-1'});
op.process(new Uint8Array([0]));
op.abort().then(resultHandler, rejectHandler);
},
function()
{
debug("finish() and then abort()");
op = crypto.subtle.digest({name: 'sha-1'});
op.finish().then(function(value) {
resultHandler(value, true);
op.abort().then(resultHandler, rejectHandler);
}, failHandler);
},
function()
{
debug("finish() and then process()");
op = crypto.subtle.digest({name: 'sha-1'});
op.finish().then(resultHandler, rejectHandler);
op.process(new Uint8Array([0]));
},
function()
{
debug("finish() and then finish()");
op = crypto.subtle.digest({name: 'sha-1'});
op.finish().then(function(value) {
resultHandler(value, true);
op.finish().then(resultHandler, rejectHandler);
}, failHandler);
},
function()
{
debug("SHA-256 rejects (dummy implementation)");
op = crypto.subtle.digest({name: 'sha-256'});
op.finish().then(resultHandler, rejectHandler);
},
function()
{
debug("Error during process() (dummy implementation rejects inputs over 6 bytes)");
op = crypto.subtle.digest({name: 'sha-1'});
op.process(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
op.finish().then(resultHandler, rejectHandler);
},
function()
{
op = crypto.subtle.digest({name: 'sha-1'});
shouldThrow("op.process(null)");
shouldThrow("op.process()");
shouldThrow("op.process(-1)");
startNextTest();
},
];
// Begin!
startNextTest();
</script>
<script src="../fast/js/resources/js-test-post.js"></script>
</body>
</html>