Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Feb 16, 2017
1 parent 4880903 commit 9c18c58
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 91 deletions.
2 changes: 1 addition & 1 deletion test/aes.js
Expand Up @@ -24,7 +24,7 @@ describe("WebCrypto Aes", function () {

context("Generate key", () => {

// Algs
// Keys
KEYS.forEach(key => {
// length
[128, 192, 256].forEach(length => {
Expand Down
24 changes: 12 additions & 12 deletions test/ec.js
Expand Up @@ -31,17 +31,17 @@ describe("WebCrypto EC", () => {
assert.equal(pkey.extractable, false);
assert.equal(pkey.usages.toString(), "sign");

let pubkey = keyPair.publicKey;
assert.equal(pubkey.type, "public");
assert.equal(pubkey.algorithm.name, "ECDSA");
assert.equal(pubkey.algorithm.namedCurve, "P-256");
assert.equal(pubkey.extractable, true);
assert.equal(pubkey.usages.toString(), "");
let pubKey = keyPair.publicKey;
assert.equal(pubKey.type, "public");
assert.equal(pubKey.algorithm.name, "ECDSA");
assert.equal(pubKey.algorithm.namedCurve, "P-256");
assert.equal(pubKey.extractable, true);
assert.equal(pubKey.usages.toString(), "");
})
.then(done, done);
});

// Algs
// Keys
KEYS.forEach(key => {
// namedCurve
NAMED_CURVES.forEach(namedCurve => {
Expand All @@ -61,7 +61,7 @@ describe("WebCrypto EC", () => {
webcrypto.subtle.generateKey(alg, true, key.usages)
.then(keyPair => {
assert.equal(!!(keyPair.privateKey || keyPair.publicKey), true, "KeyPair is empty");
// save keays for next tests
// save keys for next tests
keyTemplate.privateKey = keyPair.privateKey;
keyTemplate.publicKey = keyPair.publicKey;

Expand Down Expand Up @@ -189,12 +189,12 @@ describe("WebCrypto EC", () => {
webcrypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256"}, false, ["deriveKey", "deriveBits"])
.then(function(key2){
webcrypto.subtle.exportKey(format ,key1.publicKey)
.then(function(keydata1){
.then(function(keyData1){
webcrypto.subtle.exportKey(format ,key2.publicKey)
.then(function(keydata2){
webcrypto.subtle.importKey(format , keydata1, { name: "ECDH", namedCurve: "P-256" }, true, [])
.then(function(keyData2){
webcrypto.subtle.importKey(format , keyData1, { name: "ECDH", namedCurve: "P-256" }, true, [])
.then(function(pub1){
webcrypto.subtle.importKey(format , keydata2, { name: "ECDH", namedCurve: "P-256" }, true, [])
webcrypto.subtle.importKey(format , keyData2, { name: "ECDH", namedCurve: "P-256" }, true, [])
.then(function(pub2){
webcrypto.subtle.deriveBits({ name: "ECDH", namedCurve: "P-256", public: pub1 }, key2.privateKey, 128)
.then(function(bits1){
Expand Down
24 changes: 22 additions & 2 deletions test/helper.js
Expand Up @@ -10,16 +10,36 @@ function checkAlgorithms(alg1, alg2) {
assert.equal(alg1[i], alg2[i]);
}
}

exports.checkAlgorithms = checkAlgorithms;

function checkBuffers(buf1, buf2) {
let _buf1 = new Uint8Array(buf1);
let _buf2 = new Uint8Array(buf2);

assert.equal(_buf1.length, _buf2.length);
// ceck values
// check values
_buf1.forEach((v, i) => {
assert.equal(v, _buf2[i], "Buffers have different values");
});
}
exports.checkBuffers = checkBuffers;

exports.checkBuffers = checkBuffers;

function PromiseThrows(promise, done) {
promise
.then(() => {
return true;
})
.catch((e) => {
assert.equal(!!e, true);
return false;
})
.then((error) => {
if (error)
throw new Error("Must be error");
})
.then(done, done);
}

exports.PromiseThrows = PromiseThrows;
2 changes: 1 addition & 1 deletion test/hmac.js
Expand Up @@ -44,7 +44,7 @@ describe("WebCrypto", function () {

context("export/import", () => {
keys.forEach(hmac => {
// fromat
// format
["jwk", "raw"].forEach(format => {
it(`${hmac.name} format:${format}`, done => {
subtle.exportKey(format, hmac.key)
Expand Down
20 changes: 19 additions & 1 deletion test/key_storage.js
Expand Up @@ -8,7 +8,7 @@ var deleteFolderRecursive = function (path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
if (fs.lstatSync(curPath).isDirectory()) { // recursion
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
Expand Down Expand Up @@ -70,6 +70,24 @@ describe("Key storage", function () {
})
});

it("Set secret key", () => {
const key = { type: "secret" };
assert.throws(() => {
webcrypto.keyStorage.setItem("secret_key", key)
});
})

it("Set unknown key type", () => {
const key = { type: "wrong type" };
assert.throws(() => {
webcrypto.keyStorage.setItem("secret_key", key)
});
})

it("Get non-existent item, must be null", () => {
assert.equal(webcrypto.keyStorage.getItem("null"), null);
})

it("read storage from folder", () => {
let WebCrypto = require("../buildjs/webcrypto");
let crypto = new WebCrypto({ directory: "test_storage" });
Expand Down
14 changes: 7 additions & 7 deletions test/native.js
Expand Up @@ -87,7 +87,7 @@ describe("native", function () {
})
})

it("pksc8 RSA", function (done) {
it("pkcs8 RSA", function (done) {
native.Key.generateRsa(1024, native.RsaPublicExponent.RSA_3, function (err, key) {
test_export(key, false, done);
})
Expand Down Expand Up @@ -125,11 +125,11 @@ describe("native", function () {
})
}

it("encypt RSA OAEP without label", function (done) {
it("encrypt RSA OAEP without label", function (done) {
test_rsa_oaep_enc_dec("sha1", new Buffer("Hello world"), null, done);
})

it("encypt RSA OAEP with label", function (done) {
it("encrypt RSA OAEP with label", function (done) {
test_rsa_oaep_enc_dec("sha1", new Buffer("Hello world"), new Buffer("1234567890"), done);
})

Expand Down Expand Up @@ -227,7 +227,7 @@ describe("native", function () {
})
})

it("pksc8 EC", function (done) {
it("pkcs8 EC", function (done) {
native.Key.generateEc(native.EcNamedCurves.secp256k1, function (err, key) {
test_export(key, false, done);
})
Expand Down Expand Up @@ -296,7 +296,7 @@ describe("native", function () {
assert(!err, true, `import: ${err}`);
key.export(function (err, r) {
assert(!err, true, `export: ${err}`);
assert.equal(Buffer.compare(raw, r) == 0, true, "exported datas are not equal");
assert.equal(Buffer.compare(raw, r) == 0, true, "exported data is not equal");
done();
});
});
Expand Down Expand Up @@ -396,10 +396,10 @@ describe("native", function () {
test_encrypt_gcm(32, new Buffer(""), 13, done);
})

function test_digest(md, mdlen, done) {
function test_digest(md, mdLen, done) {
native.Core.digest(md, TEST_MESSAGE, function (err, digest) {
assert.equal(!err, true, err);
assert.equal(digest.length, mdlen, "Wrong digest length");
assert.equal(digest.length, mdLen, "Wrong digest length");
done()
});
}
Expand Down
64 changes: 0 additions & 64 deletions test/node-webcrypto-ossl-tests.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/rsa.js
Expand Up @@ -47,7 +47,7 @@ describe("WebCrypto RSA", () => {
.then(done, done);
});

// Algs
// Keys
KEYS.forEach(key => {
// Digest
DIGEST.forEach(digest => {
Expand All @@ -73,7 +73,7 @@ describe("WebCrypto RSA", () => {
webcrypto.subtle.generateKey(alg, true, key.usages)
.then(keyPair => {
assert.equal(!!(keyPair.privateKey || keyPair.publicKey), true, "KeyPair is empty");
// save keays for next tests
// save keys for next tests
keyTemplate.privateKey = keyPair.privateKey;
keyTemplate.publicKey = keyPair.publicKey;

Expand Down Expand Up @@ -119,7 +119,7 @@ describe("WebCrypto RSA", () => {
return Promise.reject(e);
})
.then(enc => {
assert.equal(!!enc, true, "Has no encrpted value");
assert.equal(!!enc, true, "Has no encrypted value");
assert.notEqual(enc.length, 0, "Has empty encrypted value");
return webcrypto.subtle.decrypt({ name: key.publicKey.algorithm.name, label: label }, key.privateKey, enc)
})
Expand Down

0 comments on commit 9c18c58

Please sign in to comment.