Skip to content

Commit c8e6c87

Browse files
davidlehndlongley
authored andcommitted
Add rsa.generateKeyPair usePureJavaScript tests.
- Add some purejs tests for all test platforms. - Skip PEM comparisons for cases where results may not be deterministic.
1 parent 57b81c2 commit c8e6c87

File tree

2 files changed

+86
-34
lines changed

2 files changed

+86
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Forge ChangeLog
3636
- Add IRC to "Contact" section of README.
3737
- Add "Security Considerations" section to README.
3838
- Add pbkdf2 usePureJavaScript test.
39-
- Add async rsa.generateKeyPair tests.
39+
- Add rsa.generateKeyPair async and usePureJavaScript tests.
4040

4141
### Removed
4242

tests/unit/rsa.js

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var ASSERT = require('assert');
2+
var FORGE = require('../../lib/forge');
23
var PKI = require('../../lib/pki');
34
var RSA = require('../../lib/rsa');
45
var MD = require('../../lib/md');
@@ -82,8 +83,8 @@ var support = require('./support');
8283
ASSERT.equal(pem1.publicKey, pem2.publicKey);
8384
}
8485

85-
// create constant prng
86-
function _constPrng() {
86+
// create same prng
87+
function _samePrng() {
8788
var prng = RANDOM.createInstance();
8889
prng.seedFileSync = function(needed) {
8990
return UTIL.fillString('a', needed);
@@ -93,24 +94,32 @@ var support = require('./support');
9394

9495
// generate pair in sync mode
9596
function _genSync(options) {
97+
options = options || {samePrng: false};
9698
var pair;
97-
if(options.random) {
98-
pair = RSA.generateKeyPair(512);
99+
if(options.samePrng) {
100+
pair = RSA.generateKeyPair(512, {prng: _samePrng()});
99101
} else {
100-
pair = RSA.generateKeyPair(512, {prng: _constPrng()});
102+
pair = RSA.generateKeyPair(512);
101103
}
102104
_pairCheck(pair);
103105
return pair;
104106
}
105107

106108
// generate pair in async mode
107109
function _genAsync(options, callback) {
110+
if(typeof callback !== 'function') {
111+
callback = options;
112+
options = {samePrng: false};
113+
}
108114
var genOptions = {
109115
bits: 512,
110116
workerScript: '/forge/prime.worker.js'
111117
};
112-
if(!options.random) {
113-
genOptions.prng = _constPrng();
118+
if(options.samePrng) {
119+
genOptions.prng = _samePrng();
120+
}
121+
if('workers' in options) {
122+
genOptions.workers = options.workers;
114123
}
115124
RSA.generateKeyPair(genOptions, function(err, pair) {
116125
ASSERT.ifError(err);
@@ -119,66 +128,109 @@ var support = require('./support');
119128
});
120129
}
121130

122-
// skip async tests if in PhantomJS
123-
var _itAsync = support.isPhantomJS ? it.skip : it;
124-
125131
it('should generate 512 bit key pair (sync)', function() {
126-
_genSync({random: true});
132+
_genSync();
133+
});
134+
135+
it('should generate 512 bit key pair (sync+purejs)', function() {
136+
// save
137+
var purejs = FORGE.options.usePureJavaScript;
138+
// test pure mode
139+
FORGE.options.usePureJavaScript = true;
140+
_genSync();
141+
// restore
142+
FORGE.options.usePureJavaScript = purejs;
127143
});
128144

129-
_itAsync('should generate 512 bit key pair (async)', function(done) {
130-
_genAsync({random: true}, function() {
145+
it('should generate 512 bit key pair (async)', function(done) {
146+
_genAsync(function() {
147+
done();
148+
});
149+
});
150+
151+
it('should generate 512 bit key pair (async+purejs)', function(done) {
152+
// save
153+
var purejs = FORGE.options.usePureJavaScript;
154+
// test pure mode
155+
FORGE.options.usePureJavaScript = true;
156+
_genAsync(function() {
157+
// restore
158+
FORGE.options.usePureJavaScript = purejs;
159+
done();
160+
});
161+
});
162+
163+
it('should generate 512 bit key pair (async+workers)', function(done) {
164+
_genAsync({
165+
workers: -1
166+
}, function() {
131167
done();
132168
});
133169
});
134170

135171
it('should generate the same 512 bit key pair (sync+sync)', function() {
136-
var pair1 = _genSync({random: false});
137-
var pair2 = _genSync({random: false});
172+
var pair1 = _genSync({samePrng: true});
173+
var pair2 = _genSync({samePrng: true});
174+
_pairCmp(pair1, pair2);
175+
});
176+
177+
it('should generate the same 512 bit key pair (sync+purejs)', function() {
178+
var pair1 = _genSync({samePrng: true});
179+
// save
180+
var purejs = FORGE.options.usePureJavaScript;
181+
// test pure mode
182+
FORGE.options.usePureJavaScript = true;
183+
var pair2 = _genSync({samePrng: true});
184+
// restore
185+
FORGE.options.usePureJavaScript = purejs;
138186
_pairCmp(pair1, pair2);
139187
});
140188

141-
it('should generate the same 512 bit key pair (sync+async)', function(done) {
142-
var pair1 = _genSync({random: false});
143-
_genAsync({random: false}, function(pair2) {
144-
_pairCmp(pair1, pair2);
189+
it('should generate 512 bit key pairs (sync+async)', function(done) {
190+
var pair1 = _genSync({samePrng: true});
191+
_genAsync({samePrng: true}, function(pair2) {
192+
// check if the same on supported deterministic platforms
193+
if(UTIL.isNodejs) {
194+
_pairCmp(pair1, pair2);
195+
}
145196
done();
146197
});
147198
});
148199

149-
it('should generate the same 512 bit key pair (async+sync)', function(done) {
150-
_genAsync({random: false}, function(pair1) {
151-
var pair2 = _genSync({random: false});
152-
_pairCmp(pair1, pair2);
200+
it('should generate 512 bit key pairs (async+sync)', function(done) {
201+
_genAsync({samePrng: true}, function(pair1) {
202+
var pair2 = _genSync({samePrng: true});
203+
// check if the same on supported deterministic platforms
204+
if(UTIL.isNodejs) {
205+
_pairCmp(pair1, pair2);
206+
}
153207
done();
154208
});
155209
});
156210

157-
it('should generate the same 512 bit key pair (async+async)', function(done) {
211+
it('should generate 512 bit key pairs (async+async)', function(done) {
158212
var pair1;
159213
var pair2;
160-
// compare and finish when both complete
214+
// finish when both complete
161215
function _done() {
162216
if(pair1 && pair2) {
163-
_pairCmp(pair1, pair2);
217+
// check if the same on supported deterministic platforms
218+
if(UTIL.isNodejs) {
219+
_pairCmp(pair1, pair2);
220+
}
164221
done();
165222
}
166223
}
167-
_genAsync({random: false}, function(pair) {
224+
_genAsync({samePrng: true}, function(pair) {
168225
pair1 = pair;
169226
_done();
170227
});
171-
_genAsync({random: false}, function(pair) {
228+
_genAsync({samePrng: true}, function(pair) {
172229
pair2 = pair;
173230
_done();
174231
});
175232
});
176233

177-
it.skip('should generate the same 512 bit key pair (sync+async+purejs)', function(done) {
178-
// FIXME: add sync+async+purejs
179-
done();
180-
});
181-
182234
it('should convert private key to/from PEM', function() {
183235
var privateKey = PKI.privateKeyFromPem(_pem.privateKey);
184236
ASSERT.equal(PKI.privateKeyToPem(privateKey), _pem.privateKey);

0 commit comments

Comments
 (0)