Skip to content

Commit 9850d4c

Browse files
committed
tests refactoring (wip)
1 parent 2835ca5 commit 9850d4c

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

src/NodeRSA.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ module.exports = (function() {
114114
* Check if keypair contains private key
115115
*/
116116
NodeRSA.prototype.isPrivate = function() {
117-
return this.keyPair.n && this.keyPair.e && this.keyPair.d;
117+
return this.keyPair.n && this.keyPair.e && this.keyPair.d || false;
118118
};
119119

120120
/**
121121
* Check if keypair contains public key
122122
* @param strict {boolean} - public key only, return false if have private exponent
123123
*/
124124
NodeRSA.prototype.isPublic = function(strict) {
125-
return this.keyPair.n && this.keyPair.e && !(strict && this.keyPair.d);
125+
return this.keyPair.n && this.keyPair.e && !(strict && this.keyPair.d) || false;
126126
};
127127

128128
/**
@@ -164,6 +164,10 @@ module.exports = (function() {
164164
* @returns {string|Buffer}
165165
*/
166166
NodeRSA.prototype.sign = function(buffer, encoding, source_encoding) {
167+
if (!this.isPrivate()) {
168+
throw Error("isn't private key")
169+
}
170+
167171
encoding = (!encoding || encoding == 'buffer' ? null : encoding)
168172
var signer = crypt.createSign('RSA-SHA256');
169173
signer.update(this.$getDataForEcrypt(buffer, source_encoding));

test/tests.js

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ describe("NodeRSA", function(){
1111
{b: 512, e: 3},
1212
{b: 512, e: 5},
1313
{b: 512, e: 257},
14-
{b: 512, e: 65537},
15-
{b: 768}, // 'e' should be 65537
16-
{b: 1024} // 'e' should be 65537
14+
//{b: 512, e: 65537},
15+
//{b: 768}, // 'e' should be 65537
16+
//{b: 1024} // 'e' should be 65537
1717
];
1818

1919
var dataBundle = {
@@ -135,39 +135,64 @@ describe("NodeRSA", function(){
135135
});
136136

137137
describe("Encrypting & decrypting", function(){
138-
var encrypted = {};
139-
var decrypted = {};
138+
describe("Good cases", function () {
139+
var encrypted = {};
140+
var decrypted = {};
141+
142+
for(var i in dataBundle) {
143+
var suit = dataBundle[i];
144+
var key = null;
145+
146+
it("should encrypt "+i, function(){
147+
key = generatedKeys[Math.round(Math.random()*1000) % generatedKeys.length];
148+
encrypted[i] = key.encrypt(suit.data);
149+
assert(Buffer.isBuffer(encrypted[i]));
150+
assert(encrypted[i].length > 0);
151+
});
140152

141-
for(var i in dataBundle) {
142-
var suit = dataBundle[i];
153+
it("should decrypt "+i, function(){
154+
decrypted[i] = key.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
155+
assert(_.isEqual(suit.data, decrypted[i]));
156+
});
157+
}
158+
});
159+
});
143160

144-
it("should encrypt "+i, function(){
145-
encrypted[i] = generatedKeys[0].encrypt(suit.data);
146-
assert(Buffer.isBuffer(encrypted[i]));
147-
assert(encrypted[i].length > 0);
148-
});
161+
describe("Signing & verifying", function () {
162+
describe("Good cases", function () {
163+
var signed = {};
164+
var key = null;
165+
166+
for(var i in dataBundle) {
167+
var suit = dataBundle[i];
168+
it("should sign "+i, function(){
169+
key = generatedKeys[Math.round(Math.random()*1000) % generatedKeys.length];
170+
signed[i] = key.sign(suit.data);
171+
assert(Buffer.isBuffer(signed[i]));
172+
assert(signed[i].length > 0);
173+
});
149174

150-
it("should decrypt "+i, function(){
151-
decrypted[i] = generatedKeys[0].decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding);
152-
assert(_.isEqual(suit.data, decrypted[i]));
175+
it("should verify "+i, function(){
176+
assert(key.verify(suit.data, signed[i]));
177+
});
178+
}
179+
});
180+
181+
describe("Bad cases", function () {
182+
it("incorrect information", function(){
183+
var signed = generatedKeys[0].sign('data1');
184+
assert(! generatedKeys[0].verify('data2', signed));
153185
});
154-
}
155-
});
156186

157-
describe("Signing & verifying", function () {
158-
var signed = {};
159-
160-
for(var i in dataBundle) {
161-
var suit = dataBundle[i];
162-
it("should sign "+i, function(){
163-
signed[i] = generatedKeys[0].sign(suit.data);
164-
assert(Buffer.isBuffer(signed[i]));
165-
assert(signed[i].length > 0);
187+
it("incorrect key for signing", function(){
188+
var key = new NodeRSA(generatedKeys[0].getPublicPEM());
189+
key.sign('data');
166190
});
167191

168-
it("should verify "+i, function(){
169-
assert(generatedKeys[0].verify(suit.data, signed[i]));
192+
it("incorrect key for verifying", function(){
193+
var signed = generatedKeys[0].sign('data');
194+
assert(! generatedKeys[1].verify('data', signed));
170195
});
171-
}
196+
});
172197
});
173198
});

0 commit comments

Comments
 (0)