@@ -2,13 +2,239 @@ var pem = require(".."),
2
2
testCase = require ( 'nodeunit' ) . testCase ;
3
3
4
4
exports [ "General Tests" ] = {
5
- "Sample test" : function ( test ) {
6
-
7
- pem . createCertificate ( null , function ( error , data ) {
5
+
6
+ "Create default sized Private key" : function ( test ) {
7
+ pem . createPrivateKey ( function ( error , data ) {
8
+ var key = ( data && data . key || "" ) . toString ( ) ;
9
+ test . ifError ( error ) ;
10
+ test . ok ( key ) ;
11
+ test . ok ( key . match ( / ^ \n * \- \- \- \- \- B E G I N R S A P R I V A T E K E Y \- \- \- \- \- \n / ) ) ;
12
+ test . ok ( key . match ( / \n \- \- \- \- \- E N D R S A P R I V A T E K E Y \- \- \- \- \- \n * $ / ) ) ;
13
+ test . ok ( key . trim ( ) . length > 850 && key . trim ( ) . length < 900 ) ;
14
+ test . done ( ) ;
15
+ } ) ;
16
+ } ,
17
+
18
+ "Create 2048bit Private key" : function ( test ) {
19
+ pem . createPrivateKey ( 2048 , function ( error , data ) {
20
+ var key = ( data && data . key || "" ) . toString ( ) ;
21
+ test . ifError ( error ) ;
22
+ test . ok ( key ) ;
23
+ test . ok ( key . match ( / ^ \n * \- \- \- \- \- B E G I N R S A P R I V A T E K E Y \- \- \- \- \- \n / ) ) ;
24
+ test . ok ( key . match ( / \n \- \- \- \- \- E N D R S A P R I V A T E K E Y \- \- \- \- \- \n * $ / ) ) ;
25
+ test . ok ( key . trim ( ) . length > 1650 && key . trim ( ) . length < 1700 ) ;
26
+ test . done ( ) ;
27
+ } ) ;
28
+ } ,
29
+
30
+ "Create default CSR" : function ( test ) {
31
+ pem . createCSR ( function ( error , data ) {
32
+ var csr = ( data && data . csr || "" ) . toString ( ) ;
33
+ test . ifError ( error ) ;
34
+ test . ok ( csr ) ;
35
+ test . ok ( csr . match ( / ^ \n * \- \- \- \- \- B E G I N C E R T I F I C A T E R E Q U E S T \- \- \- \- \- \n / ) ) ;
36
+ test . ok ( csr . match ( / \n \- \- \- \- \- E N D C E R T I F I C A T E R E Q U E S T \- \- \- \- \- \n * $ / ) ) ;
37
+
38
+ test . ok ( data && data . clientKey ) ;
39
+
40
+ test . done ( ) ;
41
+ } ) ;
42
+ } ,
43
+
44
+ "Create CSR with own key" : function ( test ) {
45
+ pem . createPrivateKey ( function ( error , data ) {
46
+ var key = ( data && data . key || "" ) . toString ( ) ;
47
+
48
+ pem . createCSR ( { clientKey : key } , function ( error , data ) {
49
+ var csr = ( data && data . csr || "" ) . toString ( ) ;
50
+ test . ifError ( error ) ;
51
+ test . ok ( csr ) ;
52
+ test . ok ( csr . match ( / ^ \n * \- \- \- \- \- B E G I N C E R T I F I C A T E R E Q U E S T \- \- \- \- \- \n / ) ) ;
53
+ test . ok ( csr . match ( / \n \- \- \- \- \- E N D C E R T I F I C A T E R E Q U E S T \- \- \- \- \- \n * $ / ) ) ;
54
+
55
+ test . equal ( data && data . clientKey , key ) ;
56
+
57
+ test . ok ( data && data . clientKey ) ;
58
+
59
+ test . done ( ) ;
60
+ } ) ;
61
+
62
+ } ) ;
63
+ } ,
64
+
65
+ "Create default certificate" : function ( test ) {
66
+ pem . createCertificate ( function ( error , data ) {
67
+ var certificate = ( data && data . certificate || "" ) . toString ( ) ;
68
+ test . ifError ( error ) ;
69
+ test . ok ( certificate ) ;
70
+ test . ok ( certificate . match ( / ^ \n * \- \- \- \- \- B E G I N C E R T I F I C A T E \- \- \- \- \- \n / ) ) ;
71
+ test . ok ( certificate . match ( / \n \- \- \- \- \- E N D C E R T I F I C A T E \- \- \- \- \- \n * $ / ) ) ;
72
+
73
+ test . ok ( ( data && data . clientKey ) != ( data && data . serviceKey ) ) ;
74
+
75
+ test . ok ( data && data . clientKey ) ;
76
+ test . ok ( data && data . serviceKey ) ;
77
+ test . ok ( data && data . csr ) ;
78
+
79
+ test . done ( ) ;
80
+ } ) ;
81
+ } ,
82
+
83
+ "Create self signed certificate" : function ( test ) {
84
+ pem . createCertificate ( { selfSigned : true } , function ( error , data ) {
85
+ var certificate = ( data && data . certificate || "" ) . toString ( ) ;
8
86
test . ifError ( error ) ;
9
- test . ok ( data ) ;
10
- console . log ( data ) ;
87
+ test . ok ( certificate ) ;
88
+ test . ok ( certificate . match ( / ^ \n * \- \- \- \- \- B E G I N C E R T I F I C A T E \- \- \- \- \- \n / ) ) ;
89
+ test . ok ( certificate . match ( / \n \- \- \- \- \- E N D C E R T I F I C A T E \- \- \- \- \- \n * $ / ) ) ;
90
+
91
+ test . ok ( ( data && data . clientKey ) == ( data && data . serviceKey ) ) ;
92
+
93
+ test . ok ( data && data . clientKey ) ;
94
+ test . ok ( data && data . serviceKey ) ;
95
+ test . ok ( data && data . csr ) ;
96
+
11
97
test . done ( ) ;
12
98
} ) ;
99
+ } ,
100
+
101
+ "Read default cert data from CSR" : function ( test ) {
102
+ pem . createCSR ( function ( error , data ) {
103
+ var csr = ( data && data . csr || "" ) . toString ( ) ;
104
+ test . ifError ( error ) ;
105
+
106
+ pem . readCertificateInfo ( csr , function ( error , data ) {
107
+ test . ifError ( error ) ;
108
+ test . deepEqual ( data , {
109
+ country : '' ,
110
+ state : '' ,
111
+ locality : '' ,
112
+ organization : '' ,
113
+ organizationUnit : '' ,
114
+ commonName : 'localhost' ,
115
+ emailAddress : '' } )
116
+ test . done ( ) ;
117
+ } ) ;
118
+ } ) ;
119
+ } ,
120
+
121
+ "Read edited cert data from CSR" : function ( test ) {
122
+ var certInfo = { country :"EE" ,
123
+ state :"Harjumaa" ,
124
+ locality :"Tallinn" ,
125
+ organization :"Node.ee" ,
126
+ organizationUnit :"test" ,
127
+ commonName :"www.node.ee" ,
128
+ emailAddress :
"[email protected] " } ;
129
+ pem . createCSR ( Object . create ( certInfo ) , function ( error , data ) {
130
+ var csr = ( data && data . csr || "" ) . toString ( ) ;
131
+ test . ifError ( error ) ;
132
+
133
+ pem . readCertificateInfo ( csr , function ( error , data ) {
134
+ test . ifError ( error ) ;
135
+ test . deepEqual ( data , certInfo )
136
+ test . done ( ) ;
137
+ } ) ;
138
+ } ) ;
139
+ } ,
140
+
141
+ "Read default cert data from certificate" : function ( test ) {
142
+ pem . createCertificate ( function ( error , data ) {
143
+ var certificate = ( data && data . certificate || "" ) . toString ( ) ;
144
+ test . ifError ( error ) ;
145
+
146
+ pem . readCertificateInfo ( certificate , function ( error , data ) {
147
+ test . ifError ( error ) ;
148
+ test . deepEqual ( data , {
149
+ country : '' ,
150
+ state : '' ,
151
+ locality : '' ,
152
+ organization : '' ,
153
+ organizationUnit : '' ,
154
+ commonName : 'localhost' ,
155
+ emailAddress : '' } )
156
+ test . done ( ) ;
157
+ } ) ;
158
+ } ) ;
159
+ } ,
160
+
161
+ "Read edited cert data from certificate" : function ( test ) {
162
+ var certInfo = { country :"EE" ,
163
+ state :"Harjumaa" ,
164
+ locality :"Tallinn" ,
165
+ organization :"Node.ee" ,
166
+ organizationUnit :"test" ,
167
+ commonName :"www.node.ee" ,
168
+ emailAddress :
"[email protected] " } ;
169
+ pem . createCertificate ( Object . create ( certInfo ) , function ( error , data ) {
170
+ var certificate = ( data && data . certificate || "" ) . toString ( ) ;
171
+ test . ifError ( error ) ;
172
+
173
+ pem . readCertificateInfo ( certificate , function ( error , data ) {
174
+ test . ifError ( error ) ;
175
+ test . deepEqual ( data , certInfo )
176
+ test . done ( ) ;
177
+ } ) ;
178
+ } ) ;
179
+ } ,
180
+
181
+ "Get public key from private key" : function ( test ) {
182
+ pem . createPrivateKey ( function ( error , data ) {
183
+ var key = ( data && data . key || "" ) . toString ( ) ;
184
+ test . ifError ( error ) ;
185
+ test . ok ( key ) ;
186
+
187
+ pem . getPublicKey ( key , function ( error , data ) {
188
+ var pubkey = ( data && data . publicKey || "" ) . toString ( ) ;
189
+ test . ifError ( error ) ;
190
+ test . ok ( pubkey ) ;
191
+
192
+ test . ok ( pubkey . match ( / ^ \n * \- \- \- \- \- B E G I N P U B L I C K E Y \- \- \- \- \- \n / ) ) ;
193
+ test . ok ( pubkey . match ( / \n \- \- \- \- \- E N D P U B L I C K E Y \- \- \- \- \- \n * $ / ) ) ;
194
+
195
+ test . done ( ) ;
196
+ } ) ;
197
+
198
+ } ) ;
199
+ } ,
200
+
201
+ "Get public key from CSR" : function ( test ) {
202
+ pem . createCSR ( function ( error , data ) {
203
+ var key = ( data && data . clientKey || "" ) . toString ( ) ;
204
+ test . ifError ( error ) ;
205
+ test . ok ( key ) ;
206
+
207
+ pem . getPublicKey ( key , function ( error , data ) {
208
+ var pubkey = ( data && data . publicKey || "" ) . toString ( ) ;
209
+ test . ifError ( error ) ;
210
+ test . ok ( pubkey ) ;
211
+
212
+ test . ok ( pubkey . match ( / ^ \n * \- \- \- \- \- B E G I N P U B L I C K E Y \- \- \- \- \- \n / ) ) ;
213
+ test . ok ( pubkey . match ( / \n \- \- \- \- \- E N D P U B L I C K E Y \- \- \- \- \- \n * $ / ) ) ;
214
+
215
+ test . done ( ) ;
216
+ } ) ;
217
+
218
+ } ) ;
219
+ } ,
220
+
221
+ "Get public key from certificate" : function ( test ) {
222
+ pem . createCertificate ( function ( error , data ) {
223
+ var key = ( data && data . clientKey || "" ) . toString ( ) ;
224
+ test . ifError ( error ) ;
225
+ test . ok ( key ) ;
226
+
227
+ pem . getPublicKey ( key , function ( error , data ) {
228
+ var pubkey = ( data && data . publicKey || "" ) . toString ( ) ;
229
+ test . ifError ( error ) ;
230
+ test . ok ( pubkey ) ;
231
+
232
+ test . ok ( pubkey . match ( / ^ \n * \- \- \- \- \- B E G I N P U B L I C K E Y \- \- \- \- \- \n / ) ) ;
233
+ test . ok ( pubkey . match ( / \n \- \- \- \- \- E N D P U B L I C K E Y \- \- \- \- \- \n * $ / ) ) ;
234
+
235
+ test . done ( ) ;
236
+ } ) ;
237
+
238
+ } ) ;
13
239
}
14
240
}
0 commit comments