@@ -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