2
2
3
3
const assert = require ( 'assert' ) ;
4
4
const urlBase64 = require ( 'urlsafe-base64' ) ;
5
+ const sinon = require ( 'sinon' ) ;
6
+ const crypto = require ( 'crypto' ) ;
5
7
const webPush = require ( '../src/index' ) ;
6
8
const vapidHelper = require ( '../src/vapid-helper' ) ;
7
9
@@ -13,6 +15,16 @@ const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
13
15
const VALID_EXPIRATION = Math . floor ( Date . now ( ) / 1000 ) + ( 60 * 60 * 12 ) ;
14
16
15
17
suite ( 'Test Vapid Helpers' , function ( ) {
18
+ const sandbox = sinon . sandbox . create ( ) ;
19
+
20
+ beforeEach ( function ( ) {
21
+ sandbox . restore ( ) ;
22
+ } ) ;
23
+
24
+ after ( function ( ) {
25
+ sandbox . restore ( ) ;
26
+ } ) ;
27
+
16
28
test ( 'is defined' , function ( ) {
17
29
assert ( webPush . generateVAPIDKeys ) ;
18
30
assert ( webPush . getVapidHeaders ) ;
@@ -30,6 +42,26 @@ suite('Test Vapid Helpers', function() {
30
42
assert . equal ( urlBase64 . decode ( keys . publicKey ) . length , 65 ) ;
31
43
} ) ;
32
44
45
+ test ( 'generate vapid keys with padding' , function ( ) {
46
+ sandbox . stub ( crypto , 'createECDH' ) . callsFake ( ( ) => {
47
+ return {
48
+ generateKeys : ( ) => { } ,
49
+ getPublicKey : ( ) => new Buffer ( 60 ) ,
50
+ getPrivateKey : ( ) => new Buffer ( 27 )
51
+ } ;
52
+ } ) ;
53
+
54
+ const keys = webPush . generateVAPIDKeys ( ) ;
55
+ assert ( keys . privateKey ) ;
56
+ assert ( keys . publicKey ) ;
57
+
58
+ assert . equal ( typeof keys . privateKey , 'string' ) ;
59
+ assert . equal ( typeof keys . publicKey , 'string' ) ;
60
+
61
+ assert . equal ( urlBase64 . decode ( keys . privateKey ) . length , 32 ) ;
62
+ assert . equal ( urlBase64 . decode ( keys . publicKey ) . length , 65 ) ;
63
+ } ) ;
64
+
33
65
test ( 'generate new vapid keys between calls' , function ( ) {
34
66
const keys = webPush . generateVAPIDKeys ( ) ;
35
67
assert ( keys . privateKey ) ;
0 commit comments