Skip to content

Commit 0115d5b

Browse files
author
Matthias Neugschwandtner
committed
[FAB-10528] collection config validation tests
Improve test coverage on the tests for collection configuration validation and fix wrong test matchers. Change-Id: I294c9fb2b8ca570afbca6638fb708e2be86146f3 Signed-off-by: Matthias Neugschwandtner <eug@zurich.ibm.com>
1 parent 78ae92c commit 0115d5b

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

core/handlers/validation/builtin/validation_logic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func validateNewCollectionConfigs(newCollectionConfigs []*common.CollectionConfi
199199

200200
}
201201
if requiredPeerCount < 0 {
202-
return fmt.Errorf("collection-name: %s -- requiredPeerCount (%d) cannot be lesser than zero (%d)",
202+
return fmt.Errorf("collection-name: %s -- requiredPeerCount (%d) cannot be less than zero (%d)",
203203
collectionName, maximumPeerCount, requiredPeerCount)
204204

205205
}
@@ -327,8 +327,8 @@ func (vscc *ValidatorOneValidSignature) validateRWSetAndCollection(
327327
}
328328

329329
if !bytes.Equal(collectionsConfigArg, collectionsConfigLedger) {
330-
return policyErr(fmt.Errorf("collection configuration mismatch for chaincode %s:%s arg: %s writeset: %s",
331-
cdRWSet.Name, cdRWSet.Version, collectionsConfigArg, collectionsConfigLedger))
330+
return policyErr(fmt.Errorf("collection configuration arguments supplied for chaincode %s:%s do not match the configuration in the lscc writeset",
331+
cdRWSet.Name, cdRWSet.Version))
332332

333333
}
334334

core/handlers/validation/builtin/validation_logic_test.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,13 +1752,12 @@ func TestValidateRWSetAndCollectionForDeploy(t *testing.T) {
17521752
// Test 1: More than two entries in the rwset -> error
17531753
rwset := &kvrwset.KVRWSet{Writes: []*kvrwset.KVWrite{{Key: ccid}, {Key: "b"}, {Key: "c"}}}
17541754
err = v.validateRWSetAndCollection(rwset, cdRWSet, nil, lsccFunc, ac, chid)
1755-
assert.Errorf(t, err, "LSCC can only issue one or two putState upon deploy")
1755+
assert.EqualError(t, err, "LSCC can only issue one or two putState upon deploy")
17561756

17571757
// Test 2: Invalid key for the collection config package -> error
17581758
rwset = &kvrwset.KVRWSet{Writes: []*kvrwset.KVWrite{{Key: ccid}, {Key: "b"}}}
17591759
err = v.validateRWSetAndCollection(rwset, cdRWSet, nil, lsccFunc, ac, chid)
1760-
assert.Errorf(t, err, "invalid key for the collection of chaincode %s:%s; expected '%s', received '%s'",
1761-
cdRWSet.Name, cdRWSet.Version, privdata.BuildCollectionKVSKey(rwset.Writes[1].Key), rwset.Writes[1].Key)
1760+
assert.EqualError(t, err, "invalid key for the collection of chaincode mycc:1.0; expected 'mycc~collection', received 'b'")
17621761

17631762
// Test 3: No collection config package -> success
17641763
rwset = &kvrwset.KVRWSet{Writes: []*kvrwset.KVWrite{{Key: ccid}}}
@@ -1774,15 +1773,15 @@ func TestValidateRWSetAndCollectionForDeploy(t *testing.T) {
17741773
err = v.validateRWSetAndCollection(rwset, cdRWSet, lsccargs, lsccFunc, ac, chid)
17751774
assert.NoError(t, err)
17761775

1777-
// Test 5: Invalid collection config package -> error
1776+
// Test 5: Collection configuration of the lscc args doesn't match the rwset
17781777
lsccargs = [][]byte{nil, nil, nil, nil, nil, []byte("barf")}
17791778
err = v.validateRWSetAndCollection(rwset, cdRWSet, lsccargs, lsccFunc, ac, chid)
1780-
assert.Errorf(t, err, "invalid collection configuration supplied for chaincode %s:%s", cdRWSet.Name, cdRWSet.Version)
1779+
assert.EqualError(t, err, "collection configuration arguments supplied for chaincode mycc:1.0 do not match the configuration in the lscc writeset")
17811780

17821781
// Test 6: Invalid collection config package -> error
17831782
rwset = &kvrwset.KVRWSet{Writes: []*kvrwset.KVWrite{{Key: ccid}, {Key: privdata.BuildCollectionKVSKey("mycc"), Value: []byte("barf")}}}
17841783
err = v.validateRWSetAndCollection(rwset, cdRWSet, lsccargs, lsccFunc, ac, chid)
1785-
assert.Errorf(t, err, "invalid collection configuration supplied for chaincode %s:%s", cdRWSet.Name, cdRWSet.Version)
1784+
assert.EqualError(t, err, "invalid collection configuration supplied for chaincode mycc:1.0")
17861785

17871786
// Test 7: Valid collection config package -> success
17881787
collName1 := "mycollection1"
@@ -1820,16 +1819,24 @@ func TestValidateRWSetAndCollectionForDeploy(t *testing.T) {
18201819

18211820
// Test 10: Duplicate collections in the collection config package -> error
18221821
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1, coll2, coll1}, cdRWSet, lsccFunc, ac, chid)
1823-
assert.Errorf(t, err, "collection-name: %s -- found duplicate collection configuration", collName1)
1822+
assert.EqualError(t, err, "collection-name: mycollection1 -- found duplicate collection configuration")
1823+
1824+
// Test 11: requiredPeerCount < 0 -> error
1825+
requiredPeerCount = -2
1826+
maximumPeerCount = 1
1827+
blockToLive = 10000
1828+
coll3 = createCollectionConfig(collName3, policyEnvelope, requiredPeerCount, maximumPeerCount, blockToLive)
1829+
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1, coll2, coll3}, cdRWSet, lsccFunc, ac, chid)
1830+
assert.EqualError(t, err, "collection-name: mycollection3 -- requiredPeerCount (1) cannot be less than zero (-2)",
1831+
collName3, maximumPeerCount, requiredPeerCount)
18241832

18251833
// Test 11: requiredPeerCount > maximumPeerCount -> error
18261834
requiredPeerCount = 2
18271835
maximumPeerCount = 1
18281836
blockToLive = 10000
18291837
coll3 = createCollectionConfig(collName3, policyEnvelope, requiredPeerCount, maximumPeerCount, blockToLive)
18301838
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1, coll2, coll3}, cdRWSet, lsccFunc, ac, chid)
1831-
assert.Errorf(t, err, "collection-name: %s -- maximum peer count (%d) cannot be greater than the required peer count (%d)",
1832-
collName3, maximumPeerCount, requiredPeerCount)
1839+
assert.EqualError(t, err, "collection-name: mycollection3 -- maximum peer count (1) cannot be greater than the required peer count (2)")
18331840

18341841
// Test 12: AND concatenation of orgs in access policy -> error
18351842
requiredPeerCount = 1
@@ -1838,6 +1845,14 @@ func TestValidateRWSetAndCollectionForDeploy(t *testing.T) {
18381845
coll3 = createCollectionConfig(collName3, policyEnvelope, requiredPeerCount, maximumPeerCount, blockToLive)
18391846
err = testValidateCollection(t, v, []*common.CollectionConfig{coll3}, cdRWSet, lsccFunc, ac, chid)
18401847
assert.EqualError(t, err, "collection-name: mycollection3 -- error in member org policy: signature policy is not an OR concatenation, NOutOf 2")
1848+
1849+
// Test 13: deploy with existing collection config on the ledger -> error
1850+
ccp := &common.CollectionConfigPackage{[]*common.CollectionConfig{coll1}}
1851+
ccpBytes, err := proto.Marshal(ccp)
1852+
assert.NoError(t, err)
1853+
state["lscc"][privdata.BuildCollectionKVSKey(ccid)] = ccpBytes
1854+
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1}, cdRWSet, lsccFunc, ac, chid)
1855+
assert.EqualError(t, err, "collection data should not exist for chaincode mycc:1.0")
18411856
}
18421857

18431858
func TestValidateRWSetAndCollectionForUpgrade(t *testing.T) {
@@ -1887,12 +1902,11 @@ func TestValidateRWSetAndCollectionForUpgrade(t *testing.T) {
18871902

18881903
// Test 3: missing one existing collection (check based on the length) -> error
18891904
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1}, cdRWSet, lsccFunc, ac, chid)
1890-
assert.Errorf(t, err, "Some existing collection configurations are missing in the new collection configuration package")
1905+
assert.EqualError(t, err, "Some existing collection configurations are missing in the new collection configuration package")
18911906

18921907
// Test 4: missing one existing collection (check based on the collection names) -> error
18931908
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1, coll3}, cdRWSet, lsccFunc, ac, chid)
1894-
assert.Errorf(t, err, "existing collection named %s is missing in the new collection configuration package",
1895-
collName2)
1909+
assert.EqualError(t, err, "existing collection named mycollection2 is missing in the new collection configuration package")
18961910

18971911
// Test 5: adding a new collection along with the existing collections -> success
18981912
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1, coll2, coll3}, cdRWSet, lsccFunc, ac, chid)
@@ -1903,7 +1917,7 @@ func TestValidateRWSetAndCollectionForUpgrade(t *testing.T) {
19031917

19041918
// Test 6: modify the BlockToLive in an existing collection -> error
19051919
err = testValidateCollection(t, v, []*common.CollectionConfig{coll1, coll2, coll3}, cdRWSet, lsccFunc, ac, chid)
1906-
assert.Errorf(t, err, "BlockToLive in the existing collection named %s cannot be changed", collName2)
1920+
assert.EqualError(t, err, "BlockToLive in the existing collection named mycollection2 cannot be changed")
19071921
}
19081922

19091923
var lccctestpath = "/tmp/lscc-validation-test"

0 commit comments

Comments
 (0)