|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ccintf |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/hex" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/hyperledger/fabric/common/util" |
| 24 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetName(t *testing.T) { |
| 29 | + spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: "ccname"}} |
| 30 | + ccid := &CCID{ChaincodeSpec: spec, Version: "ver"} |
| 31 | + name := ccid.GetName() |
| 32 | + assert.Equal(t, "ccname-ver", name, "unexpected name") |
| 33 | + |
| 34 | + ccid.ChainID = GetCCHandlerKey() |
| 35 | + hash := hex.EncodeToString(util.ComputeSHA256([]byte(ccid.ChainID))) |
| 36 | + name = ccid.GetName() |
| 37 | + assert.Equal(t, "ccname-ver-"+hash, name, "unexpected name with hash") |
| 38 | +} |
| 39 | + |
| 40 | +func TestBadCCID(t *testing.T) { |
| 41 | + defer func() { |
| 42 | + if err := recover(); err == nil { |
| 43 | + t.Fatalf("Should never reach here... GetName should have paniced") |
| 44 | + } else { |
| 45 | + assert.Equal(t, "nil chaincode spec", err, "expected 'nil chaincode spec'") |
| 46 | + } |
| 47 | + }() |
| 48 | + |
| 49 | + ccid := &CCID{Version: "ver"} |
| 50 | + ccid.GetName() |
| 51 | +} |
0 commit comments