@@ -10,6 +10,8 @@ const sinon = require('sinon');
1010
1111const chai = require ( 'chai' ) ;
1212const expect = chai . expect ;
13+ const fs = require ( 'fs' ) ;
14+ const path = require ( 'path' ) ;
1315
1416const yargs = require ( 'yargs' ) ;
1517const Bootstrap = require ( '../../../lib/contract-spi/bootstrap' ) ;
@@ -30,6 +32,7 @@ describe('server cmd', () => {
3032 it ( 'should configure the builder function' , ( ) => {
3133 sandbox . stub ( yargs , 'options' ) ;
3234 sandbox . stub ( yargs , 'usage' ) ;
35+ sandbox . stub ( yargs , 'check' ) ;
3336
3437 chaincodeServerCommand . builder ( yargs ) ;
3538
@@ -49,6 +52,7 @@ describe('server cmd', () => {
4952 expect ( args [ 'module-path' ] . default ) . to . deep . equal ( process . cwd ( ) ) ;
5053
5154 expect ( yargs . usage . calledOnce ) . to . be . true ;
55+ expect ( yargs . check . calledOnce ) . to . be . true ;
5256 } ) ;
5357 } ) ;
5458
@@ -64,6 +68,16 @@ describe('server cmd', () => {
6468 } ) ;
6569
6670 describe ( '.getArgs' , ( ) => {
71+ const certFileEncoded = path . join ( __dirname , '..' , 'test-cert.base64' ) ;
72+ const keyFileEncoded = path . join ( __dirname , '..' , 'test-key.base64' ) ;
73+ const caFileEncoded = path . join ( __dirname , '..' , 'test-ca.base64' ) ;
74+ const certFile = path . join ( __dirname , '..' , 'test-cert.pem' ) ;
75+ const keyFile = path . join ( __dirname , '..' , 'test-key.pem' ) ;
76+ const caFile = path . join ( __dirname , '..' , 'test-ca.pem' ) ;
77+ const cert = Buffer . from ( fs . readFileSync ( certFileEncoded ) . toString ( ) , 'base64' ) ;
78+ const key = Buffer . from ( fs . readFileSync ( keyFileEncoded ) . toString ( ) , 'base64' ) ;
79+ const ca = Buffer . from ( fs . readFileSync ( caFileEncoded ) . toString ( ) , 'base64' ) ;
80+
6781 it ( 'should return the arguments properly' , ( ) => {
6882 const argv = {
6983 'chaincode-address' : '0.0.0.0:9999' ,
@@ -83,5 +97,115 @@ describe('server cmd', () => {
8397 expect ( ret [ 'chaincode-id' ] ) . to . be . undefined ;
8498 expect ( ret [ 'extra-options' ] ) . to . be . undefined ;
8599 } ) ;
100+
101+ it ( 'should return the TLS arguments properly' , ( ) => {
102+ const argv = {
103+ 'chaincode-address' : '0.0.0.0:9999' ,
104+ 'chaincode-id' : 'test_id:1' ,
105+ 'module-path' : '/tmp/example' ,
106+ 'chaincode-tls-cert-path' : certFileEncoded ,
107+ 'chaincode-tls-key-path' : keyFileEncoded
108+ } ;
109+
110+ const ret = chaincodeServerCommand . getArgs ( { argv} ) ;
111+
112+ expect ( ret . address ) . to . equal ( '0.0.0.0:9999' ) ;
113+ expect ( ret . ccid ) . to . equal ( 'test_id:1' ) ;
114+
115+ expect ( ret . tlsProps ) . to . deep . equal ( {
116+ cert,
117+ key
118+ } ) ;
119+ } ) ;
120+
121+ it ( 'should return the mutual TLS arguments properly' , ( ) => {
122+ const argv = {
123+ 'chaincode-address' : '0.0.0.0:9999' ,
124+ 'chaincode-id' : 'test_id:1' ,
125+ 'module-path' : '/tmp/example' ,
126+ 'chaincode-tls-cert-path' : certFileEncoded ,
127+ 'chaincode-tls-key-path' : keyFileEncoded ,
128+ 'chaincode-tls-client-cacert-path' : caFileEncoded
129+ } ;
130+
131+ const ret = chaincodeServerCommand . getArgs ( { argv} ) ;
132+
133+ expect ( ret . address ) . to . equal ( '0.0.0.0:9999' ) ;
134+ expect ( ret . ccid ) . to . equal ( 'test_id:1' ) ;
135+
136+ expect ( ret . tlsProps ) . to . deep . equal ( {
137+ cert,
138+ key,
139+ clientCACerts : ca
140+ } ) ;
141+ } ) ;
142+
143+ it ( 'should return the TLS arguments with PEM files properly' , ( ) => {
144+ const argv = {
145+ 'chaincode-address' : '0.0.0.0:9999' ,
146+ 'chaincode-id' : 'test_id:1' ,
147+ 'module-path' : '/tmp/example' ,
148+ 'chaincode-tls-cert-file' : certFile ,
149+ 'chaincode-tls-key-file' : keyFile ,
150+ 'chaincode-tls-client-cacert-file' : caFile
151+ } ;
152+
153+ const ret = chaincodeServerCommand . getArgs ( { argv} ) ;
154+
155+ expect ( ret . address ) . to . equal ( '0.0.0.0:9999' ) ;
156+ expect ( ret . ccid ) . to . equal ( 'test_id:1' ) ;
157+
158+ expect ( ret . tlsProps ) . to . deep . equal ( {
159+ cert,
160+ key,
161+ clientCACerts : ca
162+ } ) ;
163+ } ) ;
164+ } ) ;
165+
166+ describe ( 'parse arguments' , ( ) => {
167+ it ( 'should parse the arguments successfully' , ( ) => {
168+ expect ( ( ) => {
169+ chaincodeServerCommand . builder ( yargs )
170+ . exitProcess ( false )
171+ . parse ( '--chaincode-id test_id:1 --chaincode-address 0.0.0.0:9999' ) ;
172+ } ) . not . to . throw ( ) ;
173+ } ) ;
174+
175+ it ( 'should parse the arguments successfully with TLS options' , ( ) => {
176+ expect ( ( ) => {
177+ chaincodeServerCommand . builder ( yargs )
178+ . exitProcess ( false )
179+ . parse ( '--chaincode-id test_id:1 --chaincode-address 0.0.0.0:9999 ' +
180+ '--chaincode-tls-key-file tls.key --chaincode-tls-cert-file tls.pem' ) ;
181+ } ) . not . to . throw ( ) ;
182+ } ) ;
183+
184+ it ( 'should throw when conflicting arguments are passed' , ( ) => {
185+ expect ( ( ) => {
186+ chaincodeServerCommand . builder ( yargs )
187+ . exitProcess ( false )
188+ . parse ( '--chaincode-id test_id:1 --chaincode-address 0.0.0.0:9999 ' +
189+ '--chaincode-tls-key-file tls.key --chaincode-tls-key-path tls.pem' ) ;
190+ } ) . to . throw ( ) ;
191+ } ) ;
192+
193+ it ( 'should throw when only TLS key is passed' , ( ) => {
194+ expect ( ( ) => {
195+ chaincodeServerCommand . builder ( yargs )
196+ . exitProcess ( false )
197+ . parse ( '--chaincode-id test_id:1 --chaincode-address 0.0.0.0:9999 ' +
198+ '--chaincode-tls-key-file tls.key' ) ;
199+ } ) . to . throw ( ) ;
200+ } ) ;
201+
202+ it ( 'should throw when only TLS cert is passed' , ( ) => {
203+ expect ( ( ) => {
204+ chaincodeServerCommand . builder ( yargs )
205+ . exitProcess ( false )
206+ . parse ( '--chaincode-id test_id:1 --chaincode-address 0.0.0.0:9999 ' +
207+ '--chaincode-tls-cert-file tls.pem' ) ;
208+ } ) . to . throw ( ) ;
209+ } ) ;
86210 } ) ;
87211} ) ;
0 commit comments