-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-8944] Add more unit tests for channel config cache
Change-Id: Ic1263079e95793407cd1ac2ebc7d0540a26856c4 Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
- Loading branch information
Showing
9 changed files
with
275 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package membership | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref" | ||
mb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type badKey struct { | ||
s string | ||
} | ||
|
||
func (b *badKey) String() string { | ||
return b.s | ||
} | ||
|
||
func TestMembershipCache(t *testing.T) { | ||
testChannelID := "test" | ||
goodMSPID := "GoodMSP" | ||
|
||
cfg := mocks.NewMockChannelCfg(testChannelID) | ||
cfg.MockMSPs = []*mb.MSPConfig{buildMSPConfig(goodMSPID, []byte(validRootCA))} | ||
|
||
ctx := mocks.NewMockProviderContext() | ||
|
||
cache := NewRefCache(time.Millisecond * 10) | ||
assert.NotNil(t, cache) | ||
|
||
key, err := NewCacheKey(Context{ctx}, lazyref.New(func() (interface{}, error) { return cfg, nil }), testChannelID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, key) | ||
|
||
ch := key.ChannelID() | ||
assert.Equal(t, testChannelID, ch) | ||
|
||
r, err := cache.Get(key) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, r) | ||
|
||
mem, ok := r.(fab.ChannelMembership) | ||
assert.True(t, ok) | ||
|
||
sID := &mb.SerializedIdentity{Mspid: goodMSPID, IdBytes: []byte(certPem)} | ||
goodEndorser, err := proto.Marshal(sID) | ||
assert.Nil(t, err) | ||
|
||
err = mem.Validate(goodEndorser) | ||
assert.Nil(t, err) | ||
|
||
err = mem.Verify(goodEndorser, []byte("test"), []byte("test1")) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestMembershipCacheBad(t *testing.T) { | ||
testChannelID := "test" | ||
testErr := fmt.Errorf("bad initializer") | ||
|
||
ctx := mocks.NewMockProviderContext() | ||
|
||
cache := NewRefCache(time.Millisecond * 10) | ||
assert.NotNil(t, cache) | ||
|
||
r, err := cache.Get(&badKey{s: "test"}) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "unexpected cache key", err.Error()) | ||
assert.Nil(t, r) | ||
|
||
key, err := NewCacheKey(Context{ctx}, lazyref.New(func() (interface{}, error) { return nil, testErr }), testChannelID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, key) | ||
|
||
r, err = cache.Get(key) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, r) | ||
|
||
mem, ok := r.(fab.ChannelMembership) | ||
assert.True(t, ok) | ||
|
||
err = mem.Validate([]byte("MSP")) | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), testErr.Error()) | ||
|
||
err = mem.Verify([]byte("MSP"), []byte("test"), []byte("test1")) | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), testErr.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chconfig | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" | ||
"github.com/stretchr/testify/assert" | ||
|
||
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" | ||
) | ||
|
||
const ( | ||
badProviderErrMessage = "bad provider" | ||
) | ||
|
||
func TestChannelConfigCache(t *testing.T) { | ||
user := mspmocks.NewMockSigningIdentity("user", "user") | ||
clientCtx := mocks.NewMockContext(user) | ||
|
||
cache := NewRefCache(time.Millisecond * 10) | ||
assert.NotNil(t, cache) | ||
|
||
key, err := NewCacheKey(clientCtx, mockProvider, "test") | ||
assert.Nil(t, err) | ||
assert.NotNil(t, key) | ||
|
||
r, err := cache.Get(key) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, r) | ||
} | ||
|
||
func TestChannelConfigCacheBad(t *testing.T) { | ||
user := mspmocks.NewMockSigningIdentity("user", "user") | ||
clientCtx := mocks.NewMockContext(user) | ||
|
||
cache := NewRefCache(time.Millisecond * 10) | ||
assert.NotNil(t, cache) | ||
|
||
r, err := cache.Get(&badKey{s: "test"}) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "unexpected cache key", err.Error()) | ||
assert.Nil(t, r) | ||
|
||
key, err := NewCacheKey(clientCtx, badProvider, "test") | ||
assert.Nil(t, err) | ||
assert.NotNil(t, key) | ||
|
||
cache = NewRefCache(time.Millisecond * 10) | ||
assert.NotNil(t, cache) | ||
|
||
r, err = cache.Get(key) | ||
assert.NotNil(t, r) | ||
assert.Nil(t, err) | ||
|
||
c, err := r.(*Ref).Get() | ||
assert.NotNil(t, err) | ||
assert.Nil(t, c) | ||
assert.Contains(t, err.Error(), badProviderErrMessage) | ||
} | ||
|
||
type badKey struct { | ||
s string | ||
} | ||
|
||
func (b *badKey) String() string { | ||
return b.s | ||
} | ||
|
||
func mockProvider(channelID string) (fab.ChannelConfig, error) { | ||
return mocks.NewMockChannelConfig(nil, channelID) | ||
} | ||
|
||
func badProvider(channelID string) (fab.ChannelConfig, error) { | ||
return nil, fmt.Errorf(badProviderErrMessage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// +build testing | ||
|
||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package fabpvdr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" | ||
"github.com/stretchr/testify/assert" | ||
|
||
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" | ||
) | ||
|
||
func TestCreateChannelCfg(t *testing.T) { | ||
p := newInfraProvider(t) | ||
testChannelID := "test" | ||
p.chCfgCache = newMockChCfgCache(chconfig.NewChannelCfg(testChannelID)) | ||
ctx := mocks.NewMockProviderContext() | ||
user := mspmocks.NewMockSigningIdentity("user", "user") | ||
clientCtx := &mockClientContext{ | ||
Providers: ctx, | ||
SigningIdentity: user, | ||
} | ||
|
||
m, err := p.CreateChannelCfg(clientCtx, "") | ||
assert.Nil(t, err) | ||
assert.NotNil(t, m) | ||
|
||
m, err = p.CreateChannelCfg(clientCtx, testChannelID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, m) | ||
|
||
p.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.