Skip to content

Commit

Permalink
[FAB-3055] Enhance the url check on chaincode path
Browse files Browse the repository at this point in the history
Now we can detect invalid path like '/' or 'http:///'.

Also add new test case.

Fix https://jira.hyperledger.org/browse/FAB-3055.

Change-Id: I28cca3b11ead61563220cc4eda6cb01a0feb8004
Signed-off-by: Baohua Yang <baohyang@cn.ibm.com>
  • Loading branch information
yeasy committed Apr 12, 2017
1 parent 0640d43 commit e7bbf1c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/chaincode/platforms/golang/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func decodeUrl(spec *pb.ChaincodeSpec) (string, error) {
urlLocation = spec.ChaincodeId.Path
}

if urlLocation == "" {
return "", errors.New("ChaincodeSpec's path/URL cannot be empty")
if len(urlLocation) < 2 {
return "", errors.New("ChaincodeSpec's path/URL invalid")
}

if strings.LastIndex(urlLocation, "/") == len(urlLocation)-1 {
Expand Down
35 changes: 35 additions & 0 deletions core/chaincode/platforms/golang/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,38 @@ func Test_writeGopathSrc(t *testing.T) {
//ioutil.WriteFile("/tmp/chaincode_deployment.tar", inputbuf.Bytes(), 0644)

}

func Test_decodeUrl(t *testing.T) {
cs := &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{
Name: "Test Chaincode",
Path: "http://github.com/hyperledger/fabric/examples/chaincode/go/map",
},
}

if _, err := decodeUrl(cs); err != nil {
t.Fail()
t.Logf("Error to decodeUrl unsuccessfully with valid path: %s, %s", cs.ChaincodeId.Path, err)
}

cs.ChaincodeId.Path = ""

if _, err := decodeUrl(cs); err == nil {
t.Fail()
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
}

cs.ChaincodeId.Path = "/"

if _, err := decodeUrl(cs); err == nil {
t.Fail()
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
}

cs.ChaincodeId.Path = "http:///"

if _, err := decodeUrl(cs); err == nil {
t.Fail()
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
}
}

0 comments on commit e7bbf1c

Please sign in to comment.