Skip to content

Commit b3e8995

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

File tree

3 files changed

+83
-44
lines changed

3 files changed

+83
-44
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-rsa",
3-
"version": "0.1.33",
3+
"version": "0.1.40",
44
"description": "Node.js RSA library",
55
"main": "src/NodeRSA.js",
66
"scripts": {
@@ -16,7 +16,9 @@
1616
"crypto",
1717
"assymetric",
1818
"encryption",
19-
"decryption"
19+
"decryption",
20+
"sign",
21+
"verify"
2022
],
2123
"author": "rzcoder",
2224
"license": "BSD",

src/NodeRSA.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = (function() {
2222
*/
2323
function NodeRSA(arg) {
2424
this.keyPair = new rsa.Key();
25-
this.$cache = {}
25+
this.$cache = {};
2626

2727
if (_.isObject(arg)) {
2828
this.generateKeyPair(arg.b, arg.e);
@@ -165,14 +165,14 @@ module.exports = (function() {
165165
*/
166166
NodeRSA.prototype.sign = function(buffer, encoding, source_encoding) {
167167
if (!this.isPrivate()) {
168-
throw Error("isn't private key")
168+
throw Error("It is not private key");
169169
}
170170

171-
encoding = (!encoding || encoding == 'buffer' ? null : encoding)
171+
encoding = (!encoding || encoding == 'buffer' ? null : encoding);
172172
var signer = crypt.createSign('RSA-SHA256');
173173
signer.update(this.$getDataForEcrypt(buffer, source_encoding));
174174
return signer.sign(this.getPrivatePEM(), encoding);
175-
}
175+
};
176176

177177
/**
178178
* Verifying signed data
@@ -184,25 +184,33 @@ module.exports = (function() {
184184
* @returns {*}
185185
*/
186186
NodeRSA.prototype.verify = function(buffer, signature, source_encoding, signature_encoding) {
187-
signature_encoding = (!signature_encoding || signature_encoding == 'buffer' ? null : signature_encoding)
187+
signature_encoding = (!signature_encoding || signature_encoding == 'buffer' ? null : signature_encoding);
188188
var verifier = crypt.createVerify('RSA-SHA256');
189189
verifier.update(this.$getDataForEcrypt(buffer, source_encoding));
190190
return verifier.verify(this.getPublicPEM(), signature, signature_encoding);
191-
}
191+
};
192192

193193
NodeRSA.prototype.getPrivatePEM = function () {
194-
return this.$cache.privatePEM
195-
}
194+
if (!this.isPrivate()) {
195+
throw Error("It is not private key");
196+
}
197+
198+
return this.$cache.privatePEM;
199+
};
196200

197201
NodeRSA.prototype.getPublicPEM = function () {
198-
return this.$cache.publicPEM
199-
}
202+
if (!this.isPublic()) {
203+
throw Error("It is not public key");
204+
}
205+
206+
return this.$cache.publicPEM;
207+
};
200208

201209
/**
202210
* Preparing given data for encrypting/signing. Just make new/return Buffer object.
203211
*
204212
* @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string.
205-
* @param source_encoding {string} - optional. Encoding for given string. Default utf8.
213+
* @param encoding {string} - optional. Encoding for given string. Default utf8.
206214
* @returns {Buffer}
207215
*/
208216
NodeRSA.prototype.$getDataForEcrypt = function(buffer, encoding) {
@@ -213,9 +221,9 @@ module.exports = (function() {
213221
} else if (_.isObject(buffer)) {
214222
return new Buffer(JSON.stringify(buffer));
215223
} else {
216-
throw Error("Unexpected data type")
224+
throw Error("Unexpected data type");
217225
}
218-
}
226+
};
219227

220228
/**
221229
*
@@ -241,9 +249,9 @@ module.exports = (function() {
241249
* Recalculating properties
242250
*/
243251
NodeRSA.prototype.$recalculateCache = function() {
244-
this.$cache.privatePEM = this.$makePrivatePEM()
245-
this.$cache.publicPEM = this.$makePublicPEM()
246-
}
252+
this.$cache.privatePEM = this.$makePrivatePEM();
253+
this.$cache.publicPEM = this.$makePublicPEM();
254+
};
247255

248256
/**
249257
* private

test/tests.js

Lines changed: 55 additions & 26 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 = {
@@ -104,32 +104,48 @@ describe("NodeRSA", function(){
104104
"KY4kQIIx8JEBsAYzgyP2iy0CAwEAAQ==\n"+
105105
"-----END PUBLIC KEY-----";
106106

107-
it(".loadFromPrivatePEM() should load private key from PEM string", function(){
108-
privateNodeRSA = new NodeRSA(privateKeyPEM);
109-
assert.instanceOf(privateNodeRSA.keyPair, Object);
110-
assert(privateNodeRSA.isPrivate());
111-
assert(privateNodeRSA.isPublic());
112-
assert(!privateNodeRSA.isPublic(true));
113-
});
107+
describe("Good cases", function () {
108+
it(".loadFromPrivatePEM() should load private key from PEM string", function(){
109+
privateNodeRSA = new NodeRSA(privateKeyPEM);
110+
assert.instanceOf(privateNodeRSA.keyPair, Object);
111+
assert(privateNodeRSA.isPrivate());
112+
assert(privateNodeRSA.isPublic());
113+
assert(!privateNodeRSA.isPublic(true));
114+
});
114115

115-
it(".loadFromPublicPEM() should load public key from PEM string", function(){
116-
publicNodeRSA = new NodeRSA(publicKeyPEM);
117-
assert.instanceOf(privateNodeRSA.keyPair, Object);
118-
assert(publicNodeRSA.isPublic());
119-
assert(publicNodeRSA.isPublic(true));
120-
assert(!publicNodeRSA.isPrivate());
121-
});
116+
it(".loadFromPublicPEM() should load public key from PEM string", function(){
117+
publicNodeRSA = new NodeRSA(publicKeyPEM);
118+
assert.instanceOf(privateNodeRSA.keyPair, Object);
119+
assert(publicNodeRSA.isPublic());
120+
assert(publicNodeRSA.isPublic(true));
121+
assert(!publicNodeRSA.isPrivate());
122+
});
122123

123-
it(".toPrivatePEM() should return private PEM string", function(){
124-
assert.equal(privateNodeRSA.getPrivatePEM(), privateKeyPEM);
125-
});
124+
it(".getPrivatePEM() should return private PEM string", function(){
125+
assert.equal(privateNodeRSA.getPrivatePEM(), privateKeyPEM);
126+
});
127+
128+
it(".getPublicPEM() from public key should return public PEM string", function(){
129+
assert.equal(publicNodeRSA.getPublicPEM(), publicKeyPEM);
130+
});
126131

127-
it(".toPublicPEM() from public key should return public PEM string", function(){
128-
assert.equal(publicNodeRSA.getPublicPEM(), publicKeyPEM);
132+
it(".getPublicPEM() from private key should return public PEM string", function(){
133+
assert.equal(privateNodeRSA.getPublicPEM(), publicKeyPEM);
134+
});
129135
});
130136

131-
it(".toPublicPEM() from private key should return public PEM string", function(){
132-
assert.equal(privateNodeRSA.getPublicPEM(), publicKeyPEM);
137+
describe("Bad cases", function () {
138+
it("not public key", function(){
139+
var key = new NodeRSA();
140+
assert.throw(function(){ key.getPrivatePEM(); }, Error, "It is not private key");
141+
assert.throw(function(){ key.getPublicPEM(); }, Error, "It is not public key");
142+
});
143+
144+
it("not private key", function(){
145+
var key = new NodeRSA(publicKeyPEM);
146+
assert.throw(function(){ key.getPrivatePEM(); }, Error, "It is not private key");
147+
assert.doesNotThrow(function(){ key.getPublicPEM(); }, Error, "It is not public key");
148+
});
133149
});
134150
});
135151
});
@@ -156,6 +172,19 @@ describe("NodeRSA", function(){
156172
});
157173
}
158174
});
175+
176+
describe("Bad cases", function () {
177+
it("unsupported data types", function(){
178+
assert.throw(function(){ generatedKeys[0].encrypt(null); }, Error, "Unexpected data type");
179+
assert.throw(function(){ generatedKeys[0].encrypt(undefined); }, Error, "Unexpected data type");
180+
assert.throw(function(){ generatedKeys[0].encrypt(true); }, Error, "Unexpected data type");
181+
});
182+
183+
it("incorrect key for decrypting", function(){
184+
var encrypted = generatedKeys[0].encrypt('data');
185+
assert.notEqual('data', generatedKeys[1].decrypt(encrypted));
186+
});
187+
});
159188
});
160189

161190
describe("Signing & verifying", function () {
@@ -179,14 +208,14 @@ describe("NodeRSA", function(){
179208
});
180209

181210
describe("Bad cases", function () {
182-
it("incorrect information", function(){
211+
it("incorrect data for verifying", function(){
183212
var signed = generatedKeys[0].sign('data1');
184213
assert(! generatedKeys[0].verify('data2', signed));
185214
});
186215

187216
it("incorrect key for signing", function(){
188217
var key = new NodeRSA(generatedKeys[0].getPublicPEM());
189-
key.sign('data');
218+
assert.throw(function(){ key.sign('data'); }, Error, "It is not private key");
190219
});
191220

192221
it("incorrect key for verifying", function(){

0 commit comments

Comments
 (0)