From 7c02b033b40578b65b8fce7b68e8c7272fdb874c Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Sat, 26 May 2018 15:47:54 +0800 Subject: [PATCH] [FAB-9286] Fix index out of range error 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 --- msp/msp.go | 13 ++++++++----- msp/msp_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/msp/msp.go b/msp/msp.go index 4cf63a39fea..d21f8f6929b 100644 --- a/msp/msp.go +++ b/msp/msp.go @@ -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 "" } diff --git a/msp/msp_test.go b/msp/msp_test.go index 94ee133522a..12e17e9b147 100644 --- a/msp/msp_test.go +++ b/msp/msp_test.go @@ -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) +}