Skip to content

Commit 0e7ffae

Browse files
committed
[FAB-4146] chaincode install panics if GOPATH empty
If the GOPATH isn't set, the command peer chaincode install panics because of golang/platform.go I added a test that simulates such a condition. Also added one test which uses an invalid URL path in order to improve the UT coverage for ValidateSpec(). Change-Id: I1c9ed3406078bddf48e866bd0ca9a856a1c0848e Signed-off-by: Yacov Manevich <[email protected]> Signed-off-by: Will Lahti <[email protected]>
1 parent 6c3b730 commit 0e7ffae

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

core/chaincode/platforms/golang/platform.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ import (
2525
"net/url"
2626
"os"
2727
"path/filepath"
28-
"strings"
29-
3028
"regexp"
29+
"strings"
3130

3231
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
3332
cutil "github.com/hyperledger/fabric/core/container/util"
@@ -84,14 +83,18 @@ func (goPlatform *Platform) ValidateSpec(spec *pb.ChaincodeSpec) error {
8483
if path.Scheme == "" {
8584
gopath := os.Getenv("GOPATH")
8685
// Only take the first element of GOPATH
87-
gopath = filepath.SplitList(gopath)[0]
86+
splitGoPath := filepath.SplitList(gopath)
87+
if len(splitGoPath) == 0 {
88+
return fmt.Errorf("invalid GOPATH environment variable value:[%s]", gopath)
89+
}
90+
gopath = splitGoPath[0]
8891
pathToCheck := filepath.Join(gopath, "src", spec.ChaincodeId.Path)
8992
exists, err := pathExists(pathToCheck)
9093
if err != nil {
91-
return fmt.Errorf("Error validating chaincode path: %s", err)
94+
return fmt.Errorf("error validating chaincode path: %s", err)
9295
}
9396
if !exists {
94-
return fmt.Errorf("Path to chaincode does not exist: %s", spec.ChaincodeId.Path)
97+
return fmt.Errorf("path to chaincode does not exist: %s", spec.ChaincodeId.Path)
9598
}
9699
}
97100
return nil
@@ -133,7 +136,7 @@ func (goPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSp
133136
// Check name for conforming path
134137
// --------------------------------------------------------------------------------------
135138
if !re.MatchString(header.Name) {
136-
return fmt.Errorf("Illegal file detected in payload: \"%s\"", header.Name)
139+
return fmt.Errorf("illegal file detected in payload: \"%s\"", header.Name)
137140
}
138141

139142
// --------------------------------------------------------------------------------------
@@ -146,7 +149,7 @@ func (goPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSp
146149
// Anything else is suspect in this context and will be rejected
147150
// --------------------------------------------------------------------------------------
148151
if header.Mode&^0100666 != 0 {
149-
return fmt.Errorf("Illegal file mode detected for file %s: %o", header.Name, header.Mode)
152+
return fmt.Errorf("illegal file mode detected for file %s: %o", header.Name, header.Mode)
150153
}
151154
}
152155

core/chaincode/platforms/golang/platform_test.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ limitations under the License.
1717
package golang
1818

1919
import (
20+
"archive/tar"
21+
"bytes"
22+
"compress/gzip"
2023
"fmt"
2124
"os"
2225
"strings"
2326
"testing"
24-
25-
"archive/tar"
26-
"bytes"
27-
"compress/gzip"
2827
"time"
2928

30-
"github.com/spf13/viper"
31-
3229
"github.com/hyperledger/fabric/core/config"
3330
pb "github.com/hyperledger/fabric/protos/peer"
31+
"github.com/spf13/viper"
32+
"github.com/stretchr/testify/assert"
3433
)
3534

3635
func testerr(err error, succ bool) error {
@@ -108,6 +107,24 @@ func TestValidateCDS(t *testing.T) {
108107
}
109108
}
110109

110+
func TestPlatform_GoPathNotSet(t *testing.T) {
111+
p := &Platform{}
112+
spec := &pb.ChaincodeSpec{
113+
ChaincodeId: &pb.ChaincodeID{
114+
Path: "/opt/gopath/src/github.com/hyperledger/fabric",
115+
},
116+
}
117+
gopath := os.Getenv("GOPATH")
118+
defer os.Setenv("GOPATH", gopath)
119+
os.Setenv("GOPATH", "")
120+
121+
f := func() {
122+
p.ValidateSpec(spec)
123+
}
124+
assert.NotPanics(t, f)
125+
assert.Contains(t, p.ValidateSpec(spec).Error(), "invalid GOPATH environment variable value")
126+
}
127+
111128
func Test_writeGopathSrc(t *testing.T) {
112129

113130
inputbuf := bytes.NewBuffer(nil)
@@ -157,7 +174,7 @@ func Test_decodeUrl(t *testing.T) {
157174
}
158175
}
159176

160-
func TestValidChaincodeSpec(t *testing.T) {
177+
func TestValidateSpec(t *testing.T) {
161178
platform := &Platform{}
162179

163180
var tests = []struct {
@@ -168,12 +185,13 @@ func TestValidChaincodeSpec(t *testing.T) {
168185
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "https://github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: true},
169186
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: true},
170187
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/bad/chaincode/go/map"}}, succ: false},
188+
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: ":github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: false},
171189
}
172190

173191
for _, tst := range tests {
174192
err := platform.ValidateSpec(tst.spec)
175193
if err = testerr(err, tst.succ); err != nil {
176-
t.Errorf("Error to validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
194+
t.Errorf("Error validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
177195
}
178196
}
179197
}

0 commit comments

Comments
 (0)