@@ -10,33 +10,74 @@ if (!common.hasCrypto) {
1010var tls = require ( 'tls' ) ;
1111var fs = require ( 'fs' ) ;
1212var path = require ( 'path' ) ;
13+ var finished = 0 ;
1314
14- var error = false ;
15-
16- // agent7-cert.pem is issued by the fake CNNIC root CA so that its
17- // hash is not listed in the whitelist.
18- var options = {
19- key : fs . readFileSync ( path . join ( common . fixturesDir , 'keys/agent7-key.pem' ) ) ,
20- cert : fs . readFileSync ( path . join ( common . fixturesDir , 'keys/agent7-cert.pem' ) )
21- } ;
22-
23- var server = tls . createServer ( options , function ( s ) {
24- s . resume ( ) ;
25- } ) . listen ( common . PORT , function ( ) {
26- var client = tls . connect ( {
27- port : common . PORT ,
28- rejectUnauthorized : true ,
15+ function filenamePEM ( n ) {
16+ return path . join ( common . fixturesDir , 'keys' , n + '.pem' ) ;
17+ }
18+
19+ function loadPEM ( n ) {
20+ return fs . readFileSync ( filenamePEM ( n ) ) ;
21+ }
22+
23+ var testCases = [
24+ { // Test 0: for the check of a cert not existed in the whitelist.
25+ // agent7-cert.pem is issued by the fake CNNIC root CA so that its
26+ // hash is not listed in the whitelist.
2927 // fake-cnnic-root-cert has the same subject name as the original
3028 // rootCA.
31- ca : [ fs . readFileSync ( path . join ( common . fixturesDir ,
32- 'keys/fake-cnnic-root-cert.pem' ) ) ]
33- } ) ;
34- client . on ( 'error' , function ( e ) {
35- assert . strictEqual ( e . code , 'CERT_REVOKED' ) ;
36- error = true ;
37- server . close ( ) ;
29+ serverOpts : {
30+ key : loadPEM ( 'agent7-key' ) ,
31+ cert : loadPEM ( 'agent7-cert' )
32+ } ,
33+ clientOpts : {
34+ port : common . PORT ,
35+ rejectUnauthorized : true ,
36+ ca : [ loadPEM ( 'fake-cnnic-root-cert' ) ]
37+ } ,
38+ errorCode : 'CERT_REVOKED'
39+ } ,
40+ // Test 1: for the fix of iojs#2061
41+ // agent6-cert.pem is signed by intermidate cert of ca3.
42+ // The server has a cert chain of agent6->ca3->ca1(root) but
43+ // tls.connect should be failed with an error of
44+ // UNABLE_TO_GET_ISSUER_CERT_LOCALLY since the root CA of ca1 is not
45+ // installed locally.
46+ {
47+ serverOpts : {
48+ ca : loadPEM ( 'ca3-key' ) ,
49+ key : loadPEM ( 'agent6-key' ) ,
50+ cert : loadPEM ( 'agent6-cert' )
51+ } ,
52+ clientOpts : {
53+ port : common . PORT ,
54+ rejectUnauthorized : true
55+ } ,
56+ errorCode : 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'
57+ }
58+ ] ;
59+
60+ function runTest ( tindex ) {
61+ var tcase = testCases [ tindex ] ;
62+
63+ if ( ! tcase ) return ;
64+
65+ var server = tls . createServer ( tcase . serverOpts , function ( s ) {
66+ s . resume ( ) ;
67+ } ) . listen ( common . PORT , function ( ) {
68+ var client = tls . connect ( tcase . clientOpts ) ;
69+ client . on ( 'error' , function ( e ) {
70+ assert . strictEqual ( e . code , tcase . errorCode ) ;
71+ server . close ( function ( ) {
72+ finished ++ ;
73+ runTest ( tindex + 1 ) ;
74+ } ) ;
75+ } ) ;
3876 } ) ;
39- } ) ;
77+ }
78+
79+ runTest ( 0 ) ;
80+
4081process . on ( 'exit' , function ( ) {
41- assert ( error ) ;
82+ assert . equal ( finished , testCases . length ) ;
4283} ) ;
0 commit comments