@@ -136,6 +136,10 @@ func createCCDataRWset(nameK, nameV, version string, policy []byte) ([]byte, err
136
136
}
137
137
138
138
func createLSCCTx (ccname , ccver , f string , res []byte ) (* common.Envelope , error ) {
139
+ return createLSCCTxPutCds (ccname , ccver , f , res , nil , true )
140
+ }
141
+
142
+ func createLSCCTxPutCds (ccname , ccver , f string , res , cdsbytes []byte , putcds bool ) (* common.Envelope , error ) {
139
143
cds := & peer.ChaincodeDeploymentSpec {
140
144
ChaincodeSpec : & peer.ChaincodeSpec {
141
145
ChaincodeId : & peer.ChaincodeID {
@@ -151,14 +155,30 @@ func createLSCCTx(ccname, ccver, f string, res []byte) (*common.Envelope, error)
151
155
return nil , err
152
156
}
153
157
154
- cis := & peer.ChaincodeInvocationSpec {
155
- ChaincodeSpec : & peer.ChaincodeSpec {
156
- ChaincodeId : & peer.ChaincodeID {Name : "lscc" },
157
- Input : & peer.ChaincodeInput {
158
- Args : [][]byte {[]byte (f ), []byte ("barf" ), cdsBytes },
158
+ var cis * peer.ChaincodeInvocationSpec
159
+ if putcds {
160
+ if cdsbytes != nil {
161
+ cdsBytes = cdsbytes
162
+ }
163
+ cis = & peer.ChaincodeInvocationSpec {
164
+ ChaincodeSpec : & peer.ChaincodeSpec {
165
+ ChaincodeId : & peer.ChaincodeID {Name : "lscc" },
166
+ Input : & peer.ChaincodeInput {
167
+ Args : [][]byte {[]byte (f ), []byte ("barf" ), cdsBytes },
168
+ },
169
+ Type : peer .ChaincodeSpec_GOLANG ,
159
170
},
160
- Type : peer .ChaincodeSpec_GOLANG ,
161
- },
171
+ }
172
+ } else {
173
+ cis = & peer.ChaincodeInvocationSpec {
174
+ ChaincodeSpec : & peer.ChaincodeSpec {
175
+ ChaincodeId : & peer.ChaincodeID {Name : "lscc" },
176
+ Input : & peer.ChaincodeInput {
177
+ Args : [][]byte {[]byte (f ), []byte ("barf" )},
178
+ },
179
+ Type : peer .ChaincodeSpec_GOLANG ,
180
+ },
181
+ }
162
182
}
163
183
164
184
prop , _ , err := utils .CreateProposalFromCIS (common .HeaderType_ENDORSER_TRANSACTION , util .GetTestChainID (), cis , sid )
@@ -217,12 +237,44 @@ func TestInvoke(t *testing.T) {
217
237
t .Fatalf ("vscc invoke should have failed" )
218
238
}
219
239
240
+ // not enough args
220
241
args = [][]byte {[]byte ("dv" ), []byte ("tx" )}
221
242
args [1 ] = nil
222
243
if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
223
244
t .Fatalf ("vscc invoke should have failed" )
224
245
}
225
246
247
+ // nil args
248
+ args = [][]byte {nil , nil , nil }
249
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
250
+ t .Fatalf ("vscc invoke should have failed" )
251
+ }
252
+
253
+ // nil args
254
+ args = [][]byte {[]byte ("a" ), []byte ("a" ), nil }
255
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
256
+ t .Fatalf ("vscc invoke should have failed" )
257
+ }
258
+
259
+ // broken Envelope
260
+ args = [][]byte {[]byte ("a" ), []byte ("a" ), []byte ("a" )}
261
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
262
+ t .Fatalf ("vscc invoke should have failed" )
263
+ }
264
+
265
+ // (still) broken Envelope
266
+ args = [][]byte {[]byte ("a" ), utils .MarshalOrPanic (& common.Envelope {Payload : []byte ("barf" )}), []byte ("a" )}
267
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
268
+ t .Fatalf ("vscc invoke should have failed" )
269
+ }
270
+
271
+ // (still) broken Envelope
272
+ b := utils .MarshalOrPanic (& common.Envelope {Payload : utils .MarshalOrPanic (& common.Payload {Header : & common.Header {ChannelHeader : []byte ("barf" )}})})
273
+ args = [][]byte {[]byte ("a" ), b , []byte ("a" )}
274
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
275
+ t .Fatalf ("vscc invoke should have failed" )
276
+ }
277
+
226
278
tx , _ , err := createTx ()
227
279
if err != nil {
228
280
t .Fatalf ("createTx returned err %s" , err )
@@ -233,12 +285,32 @@ func TestInvoke(t *testing.T) {
233
285
t .Fatalf ("GetBytesEnvelope returned err %s" , err )
234
286
}
235
287
236
- // good path: signed by the right MSP
288
+ // broken policy
289
+ args = [][]byte {[]byte ("dv" ), envBytes , []byte ("barf" )}
290
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
291
+ t .Fatalf ("vscc invoke should have failed" )
292
+ }
293
+
237
294
policy , err := getSignedByMSPMemberPolicy (mspid )
238
295
if err != nil {
239
296
t .Fatalf ("failed getting policy, err %s" , err )
240
297
}
241
298
299
+ // broken type
300
+ b = utils .MarshalOrPanic (& common.Envelope {Payload : utils .MarshalOrPanic (& common.Payload {Header : & common.Header {ChannelHeader : utils .MarshalOrPanic (& common.ChannelHeader {Type : int32 (common .HeaderType_ORDERER_TRANSACTION )})}})})
301
+ args = [][]byte {[]byte ("dv" ), b , policy }
302
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
303
+ t .Fatalf ("vscc invoke should have failed" )
304
+ }
305
+
306
+ // broken tx payload
307
+ b = utils .MarshalOrPanic (& common.Envelope {Payload : utils .MarshalOrPanic (& common.Payload {Header : & common.Header {ChannelHeader : utils .MarshalOrPanic (& common.ChannelHeader {Type : int32 (common .HeaderType_ORDERER_TRANSACTION )})}})})
308
+ args = [][]byte {[]byte ("dv" ), b , policy }
309
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
310
+ t .Fatalf ("vscc invoke should have failed" )
311
+ }
312
+
313
+ // good path: signed by the right MSP
242
314
args = [][]byte {[]byte ("dv" ), envBytes , policy }
243
315
res := stub .MockInvoke ("1" , args )
244
316
if res .Status != shim .OK {
@@ -456,11 +528,67 @@ func TestValidateDeployFail(t *testing.T) {
456
528
t .Fatalf ("vscc invoke should have failed" )
457
529
}
458
530
531
+ /**********************/
532
+ /* test bad LSCC args */
533
+ /**********************/
534
+
535
+ res , err := createCCDataRWset (ccname , ccname , ccver , nil )
536
+ assert .NoError (t , err )
537
+
538
+ tx , err = createLSCCTxPutCds (ccname , ccver , lscc .DEPLOY , res , nil , false )
539
+ if err != nil {
540
+ t .Fatalf ("createTx returned err %s" , err )
541
+ }
542
+
543
+ envBytes , err = utils .GetBytesEnvelope (tx )
544
+ if err != nil {
545
+ t .Fatalf ("GetBytesEnvelope returned err %s" , err )
546
+ }
547
+
548
+ // good path: signed by the right MSP
549
+ policy , err = getSignedByMSPMemberPolicy (mspid )
550
+ if err != nil {
551
+ t .Fatalf ("failed getting policy, err %s" , err )
552
+ }
553
+
554
+ args = [][]byte {[]byte ("dv" ), envBytes , policy }
555
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
556
+ t .Fatalf ("vscc invoke should have failed" )
557
+ }
558
+
559
+ /**********************/
560
+ /* test bad LSCC args */
561
+ /**********************/
562
+
563
+ res , err = createCCDataRWset (ccname , ccname , ccver , nil )
564
+ assert .NoError (t , err )
565
+
566
+ tx , err = createLSCCTxPutCds (ccname , ccver , lscc .DEPLOY , res , []byte ("barf" ), true )
567
+ if err != nil {
568
+ t .Fatalf ("createTx returned err %s" , err )
569
+ }
570
+
571
+ envBytes , err = utils .GetBytesEnvelope (tx )
572
+ if err != nil {
573
+ t .Fatalf ("GetBytesEnvelope returned err %s" , err )
574
+ }
575
+
576
+ // good path: signed by the right MSP
577
+ policy , err = getSignedByMSPMemberPolicy (mspid )
578
+ if err != nil {
579
+ t .Fatalf ("failed getting policy, err %s" , err )
580
+ }
581
+
582
+ args = [][]byte {[]byte ("dv" ), envBytes , policy }
583
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
584
+ t .Fatalf ("vscc invoke should have failed" )
585
+ }
586
+
459
587
/***********************/
460
588
/* test bad cc version */
461
589
/***********************/
462
590
463
- res , err : = createCCDataRWset (ccname , ccname , ccver + ".1" , nil )
591
+ res , err = createCCDataRWset (ccname , ccname , ccver + ".1" , nil )
464
592
assert .NoError (t , err )
465
593
466
594
tx , err = createLSCCTx (ccname , ccver , lscc .DEPLOY , res )
@@ -484,6 +612,31 @@ func TestValidateDeployFail(t *testing.T) {
484
612
t .Fatalf ("vscc invoke should have failed" )
485
613
}
486
614
615
+ /*************/
616
+ /* bad rwset */
617
+ /*************/
618
+
619
+ tx , err = createLSCCTx (ccname , ccver , lscc .DEPLOY , []byte ("barf" ))
620
+ if err != nil {
621
+ t .Fatalf ("createTx returned err %s" , err )
622
+ }
623
+
624
+ envBytes , err = utils .GetBytesEnvelope (tx )
625
+ if err != nil {
626
+ t .Fatalf ("GetBytesEnvelope returned err %s" , err )
627
+ }
628
+
629
+ // good path: signed by the right MSP
630
+ policy , err = getSignedByMSPMemberPolicy (mspid )
631
+ if err != nil {
632
+ t .Fatalf ("failed getting policy, err %s" , err )
633
+ }
634
+
635
+ args = [][]byte {[]byte ("dv" ), envBytes , policy }
636
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
637
+ t .Fatalf ("vscc invoke should have failed" )
638
+ }
639
+
487
640
/********************/
488
641
/* test bad cc name */
489
642
/********************/
@@ -652,6 +805,60 @@ func TestAlreadyDeployed(t *testing.T) {
652
805
}
653
806
}
654
807
808
+ func TestValidateDeployNoLedger (t * testing.T ) {
809
+ v := new (ValidatorOneValidSignature )
810
+ stub := shim .NewMockStub ("validatoronevalidsignature" , v )
811
+
812
+ lccc := new (lscc.LifeCycleSysCC )
813
+ stublccc := shim .NewMockStub ("lscc" , lccc )
814
+
815
+ State := make (map [string ]map [string ][]byte )
816
+ State ["lscc" ] = stublccc .State
817
+ sysccprovider .RegisterSystemChaincodeProviderFactory (& scc.MocksccProviderFactory {QErr : fmt .Errorf ("Simulated error" )})
818
+ stub .MockPeerChaincode ("lscc" , stublccc )
819
+
820
+ r1 := stub .MockInit ("1" , [][]byte {})
821
+ if r1 .Status != shim .OK {
822
+ fmt .Println ("Init failed" , string (r1 .Message ))
823
+ t .FailNow ()
824
+ }
825
+
826
+ r := stublccc .MockInit ("1" , [][]byte {})
827
+ if r .Status != shim .OK {
828
+ fmt .Println ("Init failed" , string (r .Message ))
829
+ t .FailNow ()
830
+ }
831
+
832
+ ccname := "mycc"
833
+ ccver := "1"
834
+
835
+ defaultPolicy , err := getSignedByMSPAdminPolicy (mspid )
836
+ assert .NoError (t , err )
837
+ res , err := createCCDataRWset (ccname , ccname , ccver , defaultPolicy )
838
+ assert .NoError (t , err )
839
+
840
+ tx , err := createLSCCTx (ccname , ccver , lscc .DEPLOY , res )
841
+ if err != nil {
842
+ t .Fatalf ("createTx returned err %s" , err )
843
+ }
844
+
845
+ envBytes , err := utils .GetBytesEnvelope (tx )
846
+ if err != nil {
847
+ t .Fatalf ("GetBytesEnvelope returned err %s" , err )
848
+ }
849
+
850
+ // good path: signed by the right MSP
851
+ policy , err := getSignedByMSPMemberPolicy (mspid )
852
+ if err != nil {
853
+ t .Fatalf ("failed getting policy, err %s" , err )
854
+ }
855
+
856
+ args := [][]byte {[]byte ("dv" ), envBytes , policy }
857
+ if res := stub .MockInvoke ("1" , args ); res .Status == shim .OK {
858
+ t .Fatalf ("vscc invoke should have failed" )
859
+ }
860
+ }
861
+
655
862
func TestValidateDeployOK (t * testing.T ) {
656
863
v := new (ValidatorOneValidSignature )
657
864
stub := shim .NewMockStub ("validatoronevalidsignature" , v )
0 commit comments