Skip to content

Commit 28f468c

Browse files
committed
[FAB-3926] Add tests to CSCC to validate inputs
Added new tests cases to validate various cases of incorrect input as well, removed code duplicates, hence increase code coverage of CSCC package to 86%. Change-Id: I3dd2d7b512e07de7fb114208d906b968db7737a1 Signed-off-by: Artem Barger <[email protected]>
1 parent 04eed73 commit 28f468c

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

core/scc/cscc/configure.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
package cscc
2323

2424
import (
25+
"errors"
2526
"fmt"
2627

2728
"github.com/golang/protobuf/proto"
@@ -32,6 +33,7 @@ import (
3233
"github.com/hyperledger/fabric/core/policy"
3334
"github.com/hyperledger/fabric/events/producer"
3435
"github.com/hyperledger/fabric/msp/mgmt"
36+
"github.com/hyperledger/fabric/protos/common"
3537
pb "github.com/hyperledger/fabric/protos/peer"
3638
"github.com/hyperledger/fabric/protos/utils"
3739
)
@@ -145,11 +147,7 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
145147
// Since it is the first block, it is the genesis block containing configuration
146148
// for this chain, so we want to update the Chain object with this info
147149
func joinChain(blockBytes []byte) pb.Response {
148-
if blockBytes == nil {
149-
return shim.Error("Genesis block must not be nil.")
150-
}
151-
152-
block, err := utils.GetBlockFromBlockBytes(blockBytes)
150+
block, err := extractBlock(blockBytes)
153151
if err != nil {
154152
return shim.Error(fmt.Sprintf("Failed to reconstruct the genesis block, %s", err))
155153
}
@@ -173,10 +171,7 @@ func joinChain(blockBytes []byte) pb.Response {
173171
}
174172

175173
func updateConfigBlock(blockBytes []byte) pb.Response {
176-
if blockBytes == nil {
177-
return shim.Error("Configuration block must not be nil.")
178-
}
179-
block, err := utils.GetBlockFromBlockBytes(blockBytes)
174+
block, err := extractBlock(blockBytes)
180175
if err != nil {
181176
return shim.Error(fmt.Sprintf("Failed to reconstruct the configuration block, %s", err))
182177
}
@@ -193,6 +188,19 @@ func updateConfigBlock(blockBytes []byte) pb.Response {
193188
return shim.Success(nil)
194189
}
195190

191+
func extractBlock(bytes []byte) (*common.Block, error) {
192+
if bytes == nil {
193+
return nil, errors.New("Genesis block must not be nil.")
194+
}
195+
196+
block, err := utils.GetBlockFromBlockBytes(bytes)
197+
if err != nil {
198+
return nil, errors.New(fmt.Sprintf("Failed to reconstruct the genesis block, %s", err))
199+
}
200+
201+
return block, nil
202+
}
203+
196204
// Return the current configuration block for the specified chainID. If the
197205
// peer doesn't belong to the chain, return error
198206
func getConfigBlock(chainID []byte) pb.Response {

core/scc/cscc/configure_test.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ func TestConfigerInit(t *testing.T) {
9090
}
9191
}
9292

93+
func TestConfigerInvokeInvalidParameters(t *testing.T) {
94+
e := new(PeerConfiger)
95+
stub := shim.NewMockStub("PeerConfiger", e)
96+
97+
res := stub.MockInit("1", nil)
98+
assert.Equal(t, res.Status, int32(shim.OK), "Init failed")
99+
100+
res = stub.MockInvoke("2", nil)
101+
assert.Equal(t, res.Status, int32(shim.ERROR), "CSCC invoke expected to fail having zero arguments")
102+
assert.Equal(t, res.Message, "Incorrect number of arguments, 0")
103+
104+
args := [][]byte{[]byte("GetChannels")}
105+
res = stub.MockInvokeWithSignedProposal("3", args, nil)
106+
assert.Equal(t, res.Status, int32(shim.ERROR), "CSCC invoke expected to fail no signed proposal provided")
107+
assert.Contains(t, res.Message, "failed authorization check")
108+
109+
args = [][]byte{[]byte("GetConfigBlock"), []byte("testChainID")}
110+
res = stub.MockInvokeWithSignedProposal("4", args, nil)
111+
assert.Equal(t, res.Status, int32(shim.ERROR), "CSCC invoke expected to fail no signed proposal provided")
112+
assert.Contains(t, res.Message, "failed authorization check")
113+
114+
args = [][]byte{[]byte("fooFunction"), []byte("testChainID")}
115+
res = stub.MockInvoke("5", args)
116+
assert.Equal(t, res.Status, int32(shim.ERROR), "CSCC invoke expected wrong function name provided")
117+
assert.Equal(t, res.Message, "Requested function fooFunction not found.")
118+
}
119+
93120
func TestConfigerInvokeJoinChainMissingParams(t *testing.T) {
94121
viper.Set("peer.fileSystemPath", "/tmp/hyperledgertest/")
95122
os.Mkdir("/tmp/hyperledgertest", 0755)
@@ -103,7 +130,7 @@ func TestConfigerInvokeJoinChainMissingParams(t *testing.T) {
103130
t.FailNow()
104131
}
105132

106-
// Failed path: Not enough parameters
133+
// Failed path: expected to have at least one argument
107134
args := [][]byte{[]byte("JoinChain")}
108135
if res := stub.MockInvoke("2", args); res.Status == shim.OK {
109136
t.Fatalf("cscc invoke JoinChain should have failed with invalid number of args: %v", args)
@@ -181,13 +208,19 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
181208
sProp, _ := utils.MockSignedEndorserProposalOrPanic("", &pb.ChaincodeSpec{}, []byte("Alice"), []byte("msg1"))
182209
identityDeserializer.Msg = sProp.ProposalBytes
183210
sProp.Signature = sProp.ProposalBytes
211+
212+
// Try fail path with nil block
213+
res := stub.MockInvokeWithSignedProposal("2", [][]byte{[]byte("JoinChain"), nil}, sProp)
214+
assert.Equal(t, res.Status, int32(shim.ERROR))
215+
216+
// Now, continue with valid execution path
184217
if res := stub.MockInvokeWithSignedProposal("2", args, sProp); res.Status != shim.OK {
185218
t.Fatalf("cscc invoke JoinChain failed with: %v", res.Message)
186219
}
187220

188221
// This call must fail
189222
sProp.Signature = nil
190-
res := stub.MockInvokeWithSignedProposal("3", args, sProp)
223+
res = stub.MockInvokeWithSignedProposal("3", args, sProp)
191224
if res.Status == shim.OK {
192225
t.Fatalf("cscc invoke JoinChain must fail : %v", res.Message)
193226
}

0 commit comments

Comments
 (0)