11var chai = require ( 'chai' ) ,
2+ crypto = require ( 'crypto' ) ,
3+ sandbox = require ( 'sinon' ) . sandbox . create ( ) ,
24 expect = chai . expect ,
35 should = chai . should ,
46 rewire = require ( 'rewire' ) ,
@@ -24,15 +26,9 @@ var chai = require('chai'),
2426 SubscribeURL : 'https://www.amazonaws.com' ,
2527 Type : 'SubscriptionConfirmation'
2628 } ) ,
27- utf8Message = {
28- Type : 'Notification' ,
29- MessageId : '1' ,
30- TopicArn : 'arn' ,
29+ utf8Message = _ . extend ( { } , validMessage , {
3130 Message : 'A Message For you!' ,
32- Timestamp : ( new Date ) . toISOString ( ) ,
33- SignatureVersion : '1' ,
34- SigningCertURL : "https://localhost:56789/cert.pem"
35- } ,
31+ } ) ,
3632 utf8SubscriptionControlMessage = _ . extend ( { } , utf8Message , {
3733 Token : 'Nonce' ,
3834 SubscribeURL : 'https://www.amazonaws.com' ,
@@ -48,15 +44,20 @@ describe('Message Validator', function () {
4844 if ( err ) throw err ;
4945
5046 var crypto = require ( 'crypto' ) ,
51- validMessages = [ validMessage , validSubscriptionControlMessage ] ;
47+ validMessages = [
48+ validMessage ,
49+ validSubscriptionControlMessage ,
50+ utf8Message ,
51+ utf8SubscriptionControlMessage
52+ ] ;
5253
5354 for ( var i = 0 ; i < validMessages . length ; i ++ ) {
5455 var signer = crypto . createSign ( 'RSA-SHA1' ) ;
5556
5657 for ( var j = 0 ; j < signableKeysForSubscription . length ; j ++ ) {
5758 if ( signableKeysForSubscription [ j ] in validMessages [ i ] ) {
5859 signer . update ( signableKeysForSubscription [ j ] + "\n"
59- + validMessages [ i ] [ signableKeysForSubscription [ j ] ] + "\n" ) ;
60+ + validMessages [ i ] [ signableKeysForSubscription [ j ] ] + "\n" , 'utf8' ) ;
6061 }
6162 }
6263
@@ -71,6 +72,10 @@ describe('Message Validator', function () {
7172 } ) ;
7273 } ) ;
7374
75+ afterEach ( function ( ) {
76+ sandbox . restore ( ) ;
77+ } ) ;
78+
7479 describe ( 'validator interface' , function ( ) {
7580 it ( 'should call the provided callback with the validated message' , function ( done ) {
7681 ( new MessageValidator ( / ^ l o c a l h o s t : 5 6 7 8 9 $ / ) )
@@ -198,38 +203,27 @@ describe('Message Validator', function () {
198203 } ) ;
199204
200205 describe ( 'UTF8 message validation' , function ( ) {
201- before ( function ( done ) {
202- pem . createCertificate ( { } , function ( err , certHash ) {
203- if ( err ) throw err ;
204-
205- var crypto = require ( 'crypto' ) ,
206- validMessages = [ utf8Message , utf8SubscriptionControlMessage ] ;
207-
208- for ( var i = 0 ; i < validMessages . length ; i ++ ) {
209- var signer = crypto . createSign ( 'RSA-SHA1' ) ;
210-
211- for ( var j = 0 ; j < signableKeysForSubscription . length ; j ++ ) {
212- if ( signableKeysForSubscription [ j ] in validMessages [ i ] ) {
213- signer . update ( signableKeysForSubscription [ j ] + "\n"
214- + validMessages [ i ] [ signableKeysForSubscription [ j ] ] + "\n" , 'utf8' ) ;
215- }
216- }
217-
218- validMessages [ i ] [ 'Signature' ]
219- = signer . sign ( certHash . serviceKey , 'base64' ) ;
220- }
221-
222- MessageValidator . __set__ ( 'getCertificate' , function ( url , cb ) {
223- cb ( null , certHash . certificate ) ;
224- } ) ;
225-
226- done ( ) ;
227- } ) ;
228- } ) ;
229206
230207 it ( 'should accept a valid UTF8 message' , function ( done ) {
231208 ( new MessageValidator ( / ^ l o c a l h o s t : 5 6 7 8 9 $ / , 'utf8' ) )
232209 . validate ( utf8Message , done ) ;
233210 } ) ;
234211 } ) ;
212+
213+ describe ( 'invalid signing cert' , function ( ) {
214+ it ( 'should catch any errors thrown during verification' , function ( done ) {
215+ var verifier = {
216+ update : sandbox . spy ( ) ,
217+ verify : sandbox . stub ( ) . throws ( )
218+ } ;
219+ sandbox . stub ( crypto , 'createVerify' ) . returns ( verifier ) ;
220+
221+ ( new MessageValidator ( / ^ l o c a l h o s t : 5 6 7 8 9 $ / , 'utf8' ) )
222+ . validate ( utf8Message , function ( err , result ) {
223+ expect ( err ) . not . to . be . undefined ;
224+ expect ( result ) . to . be . undefined ;
225+ done ( ) ;
226+ } ) ;
227+ } ) ;
228+ } ) ;
235229} ) ;
0 commit comments