Skip to content

Commit

Permalink
[FAB-9286] Fix index out of range error
Browse files Browse the repository at this point in the history
When calling ProviderTypeToString with an id equals to the
length of mspTypeStrings, it will raise index out of range
error. This tries to fix that.

Change-Id: I7fb8e9242ba23210d7f0a4a306302ee38021850e
Signed-off-by: Zhenguo Niu <Niu.ZGlinux@gmail.com>
  • Loading branch information
niuzhenguo authored and ale-linux committed Aug 29, 2018
1 parent f81ca9e commit 7c02b03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
13 changes: 8 additions & 5 deletions msp/msp.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,19 @@ const (
OTHER // MSP is of OTHER TYPE

// NOTE: as new types are added to this set,
// the mspTypes array below must be extended
// the mspTypes map below must be extended
)

var mspTypeStrings []string = []string{"bccsp", "idemix"}
var mspTypeStrings = map[ProviderType]string{
FABRIC: "bccsp",
IDEMIX: "idemix",
}

// ProviderTypeToString returns a string that represents the ProviderType integer
func ProviderTypeToString(id ProviderType) string {
if int(id) < 0 || int(id) > len(mspTypeStrings) {
return ""
if res, found := mspTypeStrings[id]; found {
return res
}

return mspTypeStrings[id]
return ""
}
14 changes: 14 additions & 0 deletions msp/msp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,3 +1341,17 @@ func TestAnonymityIdentityFail(t *testing.T) {
err = id.SatisfiesPrincipal(principal)
assert.Error(t, err)
}

func TestProviderTypeToString(t *testing.T) {
// Check that the provider type is found for FABRIC
pt := ProviderTypeToString(FABRIC)
assert.Equal(t, "bccsp", pt)

// Check that the provider type is found for IDEMIX
pt = ProviderTypeToString(IDEMIX)
assert.Equal(t, "idemix", pt)

// Check that the provider type is not found
pt = ProviderTypeToString(OTHER)
assert.Equal(t, "", pt)
}

0 comments on commit 7c02b03

Please sign in to comment.