Skip to content

Commit 0cefeba

Browse files
committed
update tests
1 parent 4cb31d9 commit 0cefeba

File tree

1 file changed

+130
-4
lines changed

1 file changed

+130
-4
lines changed

test/tests.js

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,10 @@ describe("NodeRSA", function(){
360360
var suit = dataBundle[i];
361361

362362
it("`encrypt()` should encrypt " + i, function () {
363-
key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length];
364-
key.setOptions({encryptionScheme: scheme});
363+
key = new NodeRSA(generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(), {
364+
environment: env,
365+
encryptionScheme: scheme
366+
});
365367
encrypted[i] = key.encrypt(suit.data);
366368
assert(Buffer.isBuffer(encrypted[i]));
367369
assert(encrypted[i].length > 0);
@@ -384,8 +386,10 @@ describe("NodeRSA", function(){
384386
var suit = dataBundle[i];
385387

386388
it("`encryptPrivate()` should encrypt " + i, function () {
387-
key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length];
388-
key.setOptions({encryptionScheme: scheme});
389+
key = new NodeRSA(generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(), {
390+
environment: env,
391+
encryptionScheme: scheme
392+
});
389393
encrypted[i] = key.encryptPrivate(suit.data);
390394
assert(Buffer.isBuffer(encrypted[i]));
391395
assert(encrypted[i].length > 0);
@@ -428,6 +432,128 @@ describe("NodeRSA", function(){
428432
}
429433
})(environments[env]);
430434
}
435+
436+
describe("Compatibility of different environments", function () {
437+
var encrypted = {};
438+
var decrypted = {};
439+
for (var i in dataBundle) {
440+
(function (i) {
441+
var key1 = null;
442+
var key2 = null;
443+
var suit = dataBundle[i];
444+
445+
it("`encrypt()` by browser" + i, function () {
446+
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey();
447+
key1 = new NodeRSA(key, {environment: 'browser'});
448+
key2 = new NodeRSA(key, {environment: 'node'});
449+
encrypted[i] = key1.encrypt(suit.data);
450+
assert(Buffer.isBuffer(encrypted[i]));
451+
assert(encrypted[i].length > 0);
452+
});
453+
454+
it("`decrypt()` by node" + i, function () {
455+
decrypted[i] = key2.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
456+
if (Buffer.isBuffer(decrypted[i])) {
457+
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex'));
458+
} else {
459+
assert(_.isEqual(suit.data, decrypted[i]));
460+
}
461+
});
462+
})(i);
463+
}
464+
465+
for (var i in dataBundle) {
466+
(function (i) {
467+
var key1 = null;
468+
var key2 = null;
469+
var suit = dataBundle[i];
470+
471+
it("`encryptPrivate()` by browser " + i, function () {
472+
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey();
473+
key1 = new NodeRSA(key, {
474+
environment: 'browser',
475+
encryptionScheme: 'pkcs1'
476+
});
477+
key2 = new NodeRSA(key, {
478+
environment: 'node',
479+
encryptionScheme: 'pkcs1'
480+
});
481+
encrypted[i] = key1.encryptPrivate(suit.data);
482+
assert(Buffer.isBuffer(encrypted[i]));
483+
assert(encrypted[i].length > 0);
484+
});
485+
486+
it("`decryptPublic()` by node " + i, function () {
487+
decrypted[i] = key2.decryptPublic(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
488+
if (Buffer.isBuffer(decrypted[i])) {
489+
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex'));
490+
} else {
491+
assert(_.isEqual(suit.data, decrypted[i]));
492+
}
493+
});
494+
})(i);
495+
}
496+
497+
var encrypted = {};
498+
var decrypted = {};
499+
for (var i in dataBundle) {
500+
(function (i) {
501+
var key1 = null;
502+
var key2 = null;
503+
var suit = dataBundle[i];
504+
505+
it("`encrypt()` by node" + i, function () {
506+
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey();
507+
key1 = new NodeRSA(key, {environment: 'node'});
508+
key2 = new NodeRSA(key, {environment: 'browser'});
509+
encrypted[i] = key1.encrypt(suit.data);
510+
assert(Buffer.isBuffer(encrypted[i]));
511+
assert(encrypted[i].length > 0);
512+
});
513+
514+
it("`decrypt()` by browser" + i, function () {
515+
decrypted[i] = key2.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
516+
if (Buffer.isBuffer(decrypted[i])) {
517+
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex'));
518+
} else {
519+
assert(_.isEqual(suit.data, decrypted[i]));
520+
}
521+
});
522+
})(i);
523+
}
524+
525+
for (var i in dataBundle) {
526+
(function (i) {
527+
var key1 = null;
528+
var key2 = null;
529+
var suit = dataBundle[i];
530+
531+
it("`encryptPrivate()` by node " + i, function () {
532+
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey();
533+
key1 = new NodeRSA(key, {
534+
environment: 'browser',
535+
encryptionScheme: 'pkcs1'
536+
});
537+
key2 = new NodeRSA(key, {
538+
environment: 'node',
539+
encryptionScheme: 'pkcs1'
540+
});
541+
encrypted[i] = key1.encryptPrivate(suit.data);
542+
assert(Buffer.isBuffer(encrypted[i]));
543+
assert(encrypted[i].length > 0);
544+
});
545+
546+
it("`decryptPublic()` by browser " + i, function () {
547+
decrypted[i] = key2.decryptPublic(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
548+
if (Buffer.isBuffer(decrypted[i])) {
549+
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex'));
550+
} else {
551+
assert(_.isEqual(suit.data, decrypted[i]));
552+
}
553+
});
554+
})(i);
555+
}
556+
});
431557
});
432558

433559
describe("Signing & verifying", function () {

0 commit comments

Comments
 (0)