Skip to content

Commit 21b8fe5

Browse files
committed
[Tests] improve test organization
1 parent 7d19782 commit 21b8fe5

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

test/index.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,48 @@ var crypto = require('crypto')
22
var constants = require('constants')
33
var parseKey = require('parse-asn1')
44
var BN = require('bn.js')
5-
var tape = require('tape')
5+
var test = require('tape')
66
var crt = require('../')
77

8-
require('./fixtures').forEach(function (fixture, i) {
9-
var key = new Buffer(fixture, 'hex')
10-
var priv = parseKey(key)
8+
test('browserify-rsa', function (t) {
9+
require('./fixtures').forEach(function (fixture, i) {
10+
var key = new Buffer(fixture, 'hex')
11+
var priv = parseKey(key)
1112

12-
for (var j1 = 1; j1 < 31; ++j1) {
13-
tape.test('r is coprime with n ' + (i + 1) + ' run ' + j1, function (t) {
14-
var r = crt.getr(priv)
15-
t.equals(r.gcd(priv.modulus).toString(), '1', 'are coprime')
16-
t.end()
17-
})
18-
}
13+
t.test('fixture ' + (i + 1), function (st) {
14+
for (var j1 = 1; j1 < 31; ++j1) {
15+
var r = crt.getr(priv)
16+
st.equal(
17+
String(r.gcd(priv.modulus)),
18+
'1',
19+
'r is coprime with n ' + (i + 1) + ' run ' + j1
20+
)
21+
}
1922

20-
var len = priv.modulus.byteLength()
21-
for (var j2 = 1; j2 < 41; ++j2) {
22-
tape.test('round trip key ' + (i + 1) + ' run ' + j2, function (t) {
23-
var r
24-
do {
25-
r = new BN(crypto.randomBytes(len))
26-
} while (r.cmp(priv.modulus) >= 0)
27-
var buf = r.toArrayLike(Buffer, 'be')
28-
if (buf.byteLength < priv.modulus.byteLength()) {
29-
var tmp = new Buffer(priv.modulus.byteLength() - buf.byteLength)
30-
tmp.fill(0)
31-
buf = Buffer.concat([tmp, buf])
23+
var len = priv.modulus.byteLength()
24+
for (var j2 = 1; j2 < 41; ++j2) {
25+
var r
26+
do {
27+
r = new BN(crypto.randomBytes(len))
28+
} while (r.cmp(priv.modulus) >= 0)
29+
var buf = r.toArrayLike(Buffer, 'be')
30+
if (buf.byteLength < priv.modulus.byteLength()) {
31+
var tmp = new Buffer(priv.modulus.byteLength() - buf.byteLength)
32+
tmp.fill(0)
33+
buf = Buffer.concat([tmp, buf])
34+
}
35+
var nodeEncrypt = crypto.privateDecrypt({
36+
padding: constants.RSA_NO_PADDING,
37+
key: key
38+
}, buf).toString('hex')
39+
st.equal(
40+
crt(buf, priv).toString('hex'),
41+
nodeEncrypt,
42+
'round trip key ' + (i + 1) + ' run ' + j2 + ' equal encrypts'
43+
)
3244
}
33-
var nodeEncrypt = crypto.privateDecrypt({
34-
padding: constants.RSA_NO_PADDING,
35-
key: key
36-
}, buf).toString('hex')
37-
t.equals(crt(buf, priv).toString('hex'), nodeEncrypt, 'equal encrypts')
38-
t.end()
45+
46+
st.end()
3947
})
40-
}
48+
})
4149
})

0 commit comments

Comments
 (0)