@@ -827,6 +827,18 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
827827
828828 go grpcServer .Serve (lis )
829829
830+ err = chaincodeInvokeChaincode (t , "" )
831+ if err != nil {
832+ t .Fail ()
833+ t .Logf ("Failed chaincode invoke chaincode : %s" , err )
834+ closeListenerAndSleep (lis )
835+ return
836+ }
837+
838+ closeListenerAndSleep (lis )
839+ }
840+
841+ func chaincodeInvokeChaincode (t * testing.T , user string ) (err error ) {
830842 var ctxt = context .Background ()
831843
832844 // Deploy first chaincode
@@ -836,15 +848,14 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
836848 f := "init"
837849 args := util .ToChaincodeArgs (f , "a" , "100" , "b" , "200" )
838850
839- spec1 := & pb.ChaincodeSpec {Type : 1 , ChaincodeID : cID1 , CtorMsg : & pb.ChaincodeInput {Args : args }}
851+ spec1 := & pb.ChaincodeSpec {Type : 1 , ChaincodeID : cID1 , CtorMsg : & pb.ChaincodeInput {Args : args }, SecureContext : user }
840852
841853 _ , err = deploy (ctxt , spec1 )
842854 chaincodeID1 := spec1 .ChaincodeID .Name
843855 if err != nil {
844856 t .Fail ()
845857 t .Logf ("Error initializing chaincode %s(%s)" , chaincodeID1 , err )
846858 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec1 })
847- closeListenerAndSleep (lis )
848859 return
849860 }
850861
@@ -859,7 +870,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
859870 f = "init"
860871 args = util .ToChaincodeArgs (f , "e" , "0" )
861872
862- spec2 := & pb.ChaincodeSpec {Type : 1 , ChaincodeID : cID2 , CtorMsg : & pb.ChaincodeInput {Args : args }}
873+ spec2 := & pb.ChaincodeSpec {Type : 1 , ChaincodeID : cID2 , CtorMsg : & pb.ChaincodeInput {Args : args }, SecureContext : user }
863874
864875 _ , err = deploy (ctxt , spec2 )
865876 chaincodeID2 := spec2 .ChaincodeID .Name
@@ -868,7 +879,6 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
868879 t .Logf ("Error initializing chaincode %s(%s)" , chaincodeID2 , err )
869880 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec1 })
870881 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec2 })
871- closeListenerAndSleep (lis )
872882 return
873883 }
874884
@@ -878,7 +888,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
878888 f = "invoke"
879889 args = util .ToChaincodeArgs (f , "e" , "1" )
880890
881- spec2 = & pb.ChaincodeSpec {Type : 1 , ChaincodeID : cID2 , CtorMsg : & pb.ChaincodeInput {Args : args }}
891+ spec2 = & pb.ChaincodeSpec {Type : 1 , ChaincodeID : cID2 , CtorMsg : & pb.ChaincodeInput {Args : args }, SecureContext : user }
882892 // Invoke chaincode
883893 var uuid string
884894 _ , uuid , _ , err = invoke (ctxt , spec2 , pb .Transaction_CHAINCODE_INVOKE )
@@ -888,7 +898,6 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
888898 t .Logf ("Error invoking <%s>: %s" , chaincodeID2 , err )
889899 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec1 })
890900 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec2 })
891- closeListenerAndSleep (lis )
892901 return
893902 }
894903
@@ -899,13 +908,67 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
899908 t .Logf ("Incorrect final state after transaction for <%s>: %s" , chaincodeID1 , err )
900909 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec1 })
901910 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec2 })
902- closeListenerAndSleep (lis )
903911 return
904912 }
905913
906914 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec1 })
907915 GetChain (DefaultChain ).Stop (ctxt , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec2 })
908- closeListenerAndSleep (lis )
916+
917+ return
918+ }
919+
920+ func TestChaincodeInvokeChaincodeWithSec (t * testing.T ) {
921+ testDBWrapper .CleanDB (t )
922+ viper .Set ("security.enabled" , "true" )
923+
924+ //Initialize crypto
925+ if err := crypto .Init (); err != nil {
926+ panic (fmt .Errorf ("Failed initializing the crypto layer [%s]" , err ))
927+ }
928+
929+ //set paths for memberservice to pick up
930+ viper .Set ("peer.fileSystemPath" , filepath .Join (os .TempDir (), "hyperledger" , "production" ))
931+ viper .Set ("server.rootpath" , filepath .Join (os .TempDir (), "ca" ))
932+
933+ var err error
934+ var memSrvcLis net.Listener
935+ if memSrvcLis , err = initMemSrvc (); err != nil {
936+ t .Fail ()
937+ t .Logf ("Error registering user %s" , err )
938+ return
939+ }
940+
941+ time .Sleep (2 * time .Second )
942+
943+ var peerLis net.Listener
944+ if peerLis , err = initPeer (); err != nil {
945+ finitMemSrvc (memSrvcLis )
946+ t .Fail ()
947+ t .Logf ("Error registering user %s" , err )
948+ return
949+ }
950+
951+ if err = crypto .RegisterClient ("jim" , nil , "jim" , "6avZQLwcUe9b" ); err != nil {
952+ finitMemSrvc (memSrvcLis )
953+ finitPeer (peerLis )
954+ t .Fail ()
955+ t .Logf ("Error registering user %s" , err )
956+ return
957+ }
958+
959+ //login as jim and test chaincode-chaincode interaction with security
960+ if err = chaincodeInvokeChaincode (t , "jim" ); err != nil {
961+ finitMemSrvc (memSrvcLis )
962+ finitPeer (peerLis )
963+ t .Fail ()
964+ t .Logf ("Error executing test %s" , err )
965+ return
966+ }
967+
968+ //cleanup
969+ finitMemSrvc (memSrvcLis )
970+ finitPeer (peerLis )
971+
909972}
910973
911974// Test the execution of a chaincode that invokes another chaincode with wrong parameters. Should receive error from
0 commit comments