Skip to content

Commit 34cc44b

Browse files
committed
New tests based on tape.
1 parent 9c93575 commit 34cc44b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+18441
-995
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
33
- "0.10"
4-
script: "make test"
4+
script: "npm run testall"

Makefile

-10
This file was deleted.

README.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Documentation
3030
* [Utilities](#utilities)
3131
* [Examples](#examples)
3232
* [System requirements](#system-requirements)
33+
* [Development and testing](#development-and-testing)
3334
* [Contributors](#contributors)
3435

3536

@@ -262,6 +263,12 @@ Decodes Base-64 encoded string and returns `Uint8Array` of bytes.
262263
Encodes `Uint8Array` or `Array` of bytes into string using Base-64 encoding.
263264

264265

266+
Examples
267+
--------
268+
269+
*TODO*
270+
271+
265272
System requirements
266273
-------------------
267274

@@ -279,10 +286,51 @@ Other systems:
279286
* Node.js (we test on 0.10 and later)
280287

281288

282-
Examples
283-
--------
289+
Development and testing
290+
------------------------
291+
292+
Install NPM modules needed for development:
293+
294+
$ npm install
295+
296+
To build minified version:
297+
298+
$ npm run build
299+
300+
Tests use minified version, so make sure to rebuild it every time you change
301+
`nacl.js`.
302+
303+
### Testing
304+
305+
To run tests in Node.js:
306+
307+
$ npm test
308+
309+
To run full suite of tests in Node.hs, including comparing outputs of
310+
JavaScript port to outputs of the original C version:
311+
312+
$ npm run testall
313+
314+
To prepare tests for browsers:
315+
316+
$ npm run browser
317+
318+
and then open `tests/browser/test.html` to run them.
319+
320+
To run headless browser tests with `testling`:
321+
322+
$ npm run testling
323+
324+
(If you get `Error: spawn ENOENT`, install *xvfb*: `sudo apt-get install xvfb`.)
325+
326+
### Benchmarking
327+
328+
To run benchmarks in Node.js:
329+
330+
$ npm run bench
331+
332+
To run benchmarks in a browser, open `test/benchmark/bench.html`.
284333

285-
*TODO*
286334

287335
Contributors
288336
------------

package.json

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "tweetnacl",
3+
"version": "0.9.0",
4+
"description": "Port of TweetNaCl cryptographic library to JavaScript",
5+
"main": "nacl.js",
6+
"directories": {
7+
"example": "examples",
8+
"test": "tests"
9+
},
10+
"scripts": {
11+
"build": "./node_modules/.bin/uglifyjs nacl.js -c -m -o nacl.min.js",
12+
"test": "./node_modules/.bin/tape test/*.js | ./node_modules/.bin/faucet",
13+
"testall": "make -C test/c && ./node_modules/.bin/tape test/*.js test/c/*.js | ./node_modules/.bin/faucet",
14+
"browser": "./node_modules/.bin/browserify test/browser/init.js test/*.js > test/browser/_bundle.js",
15+
"testling": "./node_modules/.bin/browserify test/*.js | ./node_modules/.bin/testling",
16+
"bench": "node test/benchmark/bench.js"
17+
},
18+
"testling": {
19+
"files": "test/*.js",
20+
"browsers": [
21+
"ie/11..latest",
22+
"chrome/22..latest",
23+
"firefox/16..latest",
24+
"safari/latest",
25+
"opera/11.0..latest",
26+
"iphone/6..latest",
27+
"ipad/6..latest",
28+
"android-browser/latest"
29+
]
30+
},
31+
"repository": {
32+
"type": "git",
33+
"url": "https://github.com/dchest/tweetnacl-js.git"
34+
},
35+
"keywords": [
36+
"crypto",
37+
"nacl",
38+
"salsa20",
39+
"poly1305",
40+
"curve25519",
41+
"ed25519",
42+
"signatures",
43+
"public",
44+
"key",
45+
"cryptography",
46+
"encrypt",
47+
"hash"
48+
],
49+
"author": "TweetNaCl-js contributors",
50+
"license": "Public domain",
51+
"bugs": {
52+
"url": "https://github.com/dchest/tweetnacl-js/issues"
53+
},
54+
"homepage": "https://dchest.github.io/tweetnacl-js",
55+
"devDependencies": {
56+
"browserify": "^4.2.0",
57+
"faucet": "0.0.1",
58+
"tap-browser-color": "^0.1.2",
59+
"tape": "^2.13.3",
60+
"testling": "^1.7.0",
61+
"uglify-js": "^2.4.14"
62+
}
63+
}

test/00-utils.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../nacl.min.js');
2+
var test = require('tape');
3+
4+
var b64Vectors = require('./data/base64.random');
5+
6+
function arraysEqual(t, a, b) {
7+
if (a.length != b.length) {
8+
t.fail('different lengths: ' + a.length + ' and ' + b.length);
9+
return;
10+
}
11+
for (var i = 0; i < a.length; i++) {
12+
if (a[i] != b[i]) {
13+
t.fail('differ:\nexpected: [' + Array.prototype.join.call(b, ',') +
14+
']\nactual: [' + Array.prototype.join.call(a, ',') + ']');
15+
return;
16+
}
17+
}
18+
t.pass('arrays should be equal');
19+
}
20+
21+
var testBytes = new Uint8Array([208,159,209,128,208,184,208,178,208,181,209,130,44,32,78,97,67,108]);
22+
var utf8String = "Привет, NaCl";
23+
var b64String = "0J/RgNC40LLQtdGCLCBOYUNs";
24+
25+
test('nacl.util.decodeUTF8', function(t) {
26+
t.plan(1);
27+
arraysEqual(t, nacl.util.decodeUTF8(utf8String), testBytes);
28+
});
29+
30+
test('nacl.util.encodeUTF8', function(t) {
31+
t.plan(1);
32+
t.equal(nacl.util.encodeUTF8(testBytes), utf8String);
33+
});
34+
35+
test('nacl.util.decodeBase64', function(t) {
36+
t.plan(1);
37+
arraysEqual(t, nacl.util.decodeBase64(b64String), testBytes);
38+
});
39+
40+
test('nacl.util.encodeBase64', function(t) {
41+
t.plan(1);
42+
t.equal(nacl.util.encodeBase64(testBytes), b64String);
43+
});
44+
45+
test('nacl.util.encodeBase64 random test vectors', function(t) {
46+
b64Vectors.forEach(function(vec) {
47+
var b = new Uint8Array(vec[0]);
48+
var s = vec[1];
49+
t.equal(nacl.util.encodeBase64(b), s);
50+
arraysEqual(t, nacl.util.decodeBase64(s), b);
51+
});
52+
t.end();
53+
});

test/01-verify.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../nacl.min.js');
2+
var test = require('tape');
3+
4+
test('nacl.verify', function(t) {
5+
t.ok(nacl.verify(new Uint8Array(1), new Uint8Array(1)), 'equal arrays of length 1 should verify');
6+
t.ok(nacl.verify(new Uint8Array(1000), new Uint8Array(1000)), 'equal arrays of length 1000 should verify');
7+
var a = new Uint8Array(764), b = new Uint8Array(764);
8+
for (i = 0; i < a.length; i++) a[i] = b[i] = i & 0xff;
9+
t.ok(nacl.verify(a, b), 'equal arrays should verify');
10+
t.ok(nacl.verify(a, a), 'same arrays should verify');
11+
b[0] = 255;
12+
t.notOk(nacl.verify(a, b), 'different arrays don\'t verify');
13+
t.notOk(nacl.verify(new Uint8Array(1), new Uint8Array(10)), 'arrays of different lengths should not verify');
14+
t.notOk(nacl.verify(new Uint8Array(0), new Uint8Array(0)), 'zero-length arrays should not verify');
15+
t.end();
16+
});

test/02-randombytes.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../nacl.min.js');
2+
var test = require('tape');
3+
4+
test('nacl.randomBytes', function(t) {
5+
t.plan(1);
6+
var set = {}, s, i;
7+
for (i = 0; i < 10000; i++) {
8+
s = nacl.util.encodeBase64(nacl.randomBytes(32));
9+
if (set[s]) {
10+
t.fail("duplicate random sequence! ", s);
11+
return;
12+
}
13+
set[s] = true;
14+
}
15+
t.pass('no collisions');
16+
});

test/03-onetimeauth.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../nacl.min.js');
2+
var test = require('tape');
3+
4+
var specVectors = require('./data/onetimeauth.spec');
5+
6+
var enc = nacl.util.encodeBase64,
7+
dec = nacl.util.decodeBase64;
8+
9+
test('nacl.lowlevel.crypto_onetimeauth specified vectors', function(t) {
10+
var out = new Uint8Array(16);
11+
specVectors.forEach(function(v) {
12+
nacl.lowlevel.crypto_onetimeauth(out, 0, v.m, 0, v.m.length, v.k);
13+
t.equal(enc(out), enc(v.out));
14+
});
15+
t.end();
16+
});
17+

test/04-secretbox.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../nacl.min.js');
2+
var test = require('tape');
3+
4+
var randomVectors = require('./data/secretbox.random');
5+
6+
var enc = nacl.util.encodeBase64,
7+
dec = nacl.util.decodeBase64;
8+
9+
test('nacl.secretbox and nacl.secretbox.open', function(t) {
10+
var key = new Uint8Array(nacl.secretbox.keyLength);
11+
var nonce = new Uint8Array(nacl.secretbox.nonceLength);
12+
for (var i = 0; i < key.length; i++) key[i] = i & 0xff;
13+
for (i = 0; i < nonce.length; i++) nonce[i] = (32+i) & 0xff;
14+
var msg = nacl.util.decodeUTF8('message to encrypt');
15+
var box = nacl.secretbox(msg, nonce, key);
16+
var openedMsg = nacl.secretbox.open(box, nonce, key);
17+
t.equal(nacl.util.encodeUTF8(openedMsg), nacl.util.encodeUTF8(msg), 'opened messages should be equal');
18+
t.end();
19+
});
20+
21+
test('nacl.secretbox.open with invalid box', function(t) {
22+
var key = new Uint8Array(nacl.secretbox.keyLength);
23+
var nonce = new Uint8Array(nacl.secretbox.nonceLength);
24+
t.equal(nacl.secretbox.open(new Uint8Array(0), nonce, key), false);
25+
t.equal(nacl.secretbox.open(new Uint8Array(10), nonce, key), false);
26+
t.equal(nacl.secretbox.open(new Uint8Array(100), nonce, key), false);
27+
t.end();
28+
});
29+
30+
test('nacl.secretbox.open with invalid nonce', function(t) {
31+
var key = new Uint8Array(nacl.secretbox.keyLength);
32+
var nonce = new Uint8Array(nacl.secretbox.nonceLength);
33+
for (i = 0; i < nonce.length; i++) nonce[i] = i & 0xff;
34+
var msg = nacl.util.decodeUTF8('message to encrypt');
35+
var box = nacl.secretbox(msg, nonce, key);
36+
t.equal(nacl.util.encodeUTF8(nacl.secretbox.open(box, nonce, key)),
37+
nacl.util.encodeUTF8(msg));
38+
nonce[0] = 255;
39+
t.equal(nacl.secretbox.open(box, nonce, key), false);
40+
t.end();
41+
});
42+
43+
test('nacl.secretbox.open with invalid key', function(t) {
44+
var key = new Uint8Array(nacl.secretbox.keyLength);
45+
for (var i = 0; i < key.length; i++) key[i] = i & 0xff;
46+
var nonce = new Uint8Array(nacl.secretbox.nonceLength);
47+
var msg = nacl.util.decodeUTF8('message to encrypt');
48+
var box = nacl.secretbox(msg, nonce, key);
49+
t.equal(nacl.util.encodeUTF8(nacl.secretbox.open(box, nonce, key)),
50+
nacl.util.encodeUTF8(msg));
51+
key[0] = 255;
52+
t.equal(nacl.secretbox.open(box, nonce, key), false);
53+
t.end();
54+
});
55+
56+
test('nacl.secretbox random test vectors', function(t) {
57+
randomVectors.forEach(function(vec) {
58+
var key = dec(vec[0]);
59+
var nonce = dec(vec[1]);
60+
var msg = dec(vec[2]);
61+
var goodBox = dec(vec[3]);
62+
var box = nacl.secretbox(msg, nonce, key);
63+
t.equal(enc(box), enc(goodBox));
64+
var openedBox = nacl.secretbox.open(goodBox, nonce, key);
65+
t.equal(enc(openedBox), enc(msg));
66+
});
67+
t.end();
68+
});

test/05-scalarmult.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../nacl.min.js');
2+
var test = require('tape');
3+
4+
var randomVectors = require('./data/scalarmult.random');
5+
6+
var enc = nacl.util.encodeBase64,
7+
dec = nacl.util.decodeBase64;
8+
9+
test('nacl.scalarMult.base', function(t) {
10+
// This takes takes a bit of time.
11+
// Similar to https://code.google.com/p/go/source/browse/curve25519/curve25519_test.go?repo=crypto
12+
var golden = new Uint8Array([0x89, 0x16, 0x1f, 0xde, 0x88, 0x7b, 0x2b, 0x53, 0xde, 0x54,
13+
0x9a, 0xf4, 0x83, 0x94, 0x01, 0x06, 0xec, 0xc1, 0x14, 0xd6, 0x98, 0x2d,
14+
0xaa, 0x98, 0x25, 0x6d, 0xe2, 0x3b, 0xdf, 0x77, 0x66, 0x1a]);
15+
var input = new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
17+
for (var i = 0; i < 200; i++) {
18+
input = nacl.scalarMult.base(input);
19+
}
20+
t.equal(enc(input), enc(golden));
21+
t.end();
22+
});
23+
24+
test('nacl.scalarMult and nacl.scalarMult.base random test vectors', function(t) {
25+
randomVectors.forEach(function(vec) {
26+
var pk1 = dec(vec[0]);
27+
var sk1 = dec(vec[1]);
28+
var pk2 = dec(vec[2]);
29+
var sk2 = dec(vec[3]);
30+
var out = dec(vec[4]);
31+
32+
var jpk1 = nacl.scalarMult.base(sk1);
33+
t.equal(enc(jpk1), enc(pk1));
34+
var jpk2 = nacl.scalarMult.base(sk2);
35+
t.equal(enc(jpk2), enc(pk2));
36+
var jout1 = nacl.scalarMult(sk1, pk2);
37+
t.equal(enc(jout1), enc(out));
38+
var jout2 = nacl.scalarMult(sk2, pk1);
39+
t.equal(enc(jout2), enc(out));
40+
});
41+
t.end();
42+
});
43+

0 commit comments

Comments
 (0)