Skip to content

Commit 68b455e

Browse files
committed
[FAB-3452] peer/gossip test-coverage
This change-set addresses the first step defined in the FAB. Namely, the peer/gossip package has been refactored to remove the subfolders and a new folder called mocks has been introduce to contain the mock objects used for testing the package Change-Id: Ifd37905f511bb6348db5c2363bd060e757465b27 Signed-off-by: Angelo De Caro <[email protected]>
1 parent ca3a1a2 commit 68b455e

File tree

11 files changed

+230
-226
lines changed

11 files changed

+230
-226
lines changed

core/peer/peer_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import (
3232
"github.com/hyperledger/fabric/gossip/service"
3333
"github.com/hyperledger/fabric/msp/mgmt"
3434
"github.com/hyperledger/fabric/msp/mgmt/testtools"
35-
"github.com/hyperledger/fabric/peer/gossip/mcs"
35+
peergossip "github.com/hyperledger/fabric/peer/gossip"
36+
"github.com/hyperledger/fabric/peer/gossip/mocks"
3637
"github.com/spf13/viper"
3738
"github.com/stretchr/testify/assert"
3839
"google.golang.org/grpc"
@@ -94,7 +95,7 @@ func TestCreateChainFromBlock(t *testing.T) {
9495
msptesttools.LoadMSPSetupForTesting()
9596

9697
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
97-
messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
98+
messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
9899
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{}, messageCryptoService)
99100

100101
err = CreateChainFromBlock(block)

core/scc/cscc/configure_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import (
3737
"github.com/hyperledger/fabric/gossip/service"
3838
"github.com/hyperledger/fabric/msp/mgmt"
3939
"github.com/hyperledger/fabric/msp/mgmt/testtools"
40-
"github.com/hyperledger/fabric/peer/gossip/mcs"
40+
peergossip "github.com/hyperledger/fabric/peer/gossip"
41+
"github.com/hyperledger/fabric/peer/gossip/mocks"
4142
"github.com/hyperledger/fabric/protos/common"
4243
pb "github.com/hyperledger/fabric/protos/peer"
4344
"github.com/hyperledger/fabric/protos/utils"
@@ -164,7 +165,7 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
164165
)
165166

166167
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
167-
messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
168+
messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
168169
service.InitGossipServiceCustomDeliveryFactory(identity, peerEndpoint, nil, &mockDeliveryClientFactory{}, messageCryptoService)
169170

170171
// Successful path for JoinChain

gossip/service/gossip_service.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/hyperledger/fabric/gossip/integration"
3232
"github.com/hyperledger/fabric/gossip/state"
3333
"github.com/hyperledger/fabric/gossip/util"
34-
"github.com/hyperledger/fabric/peer/gossip/sa"
34+
peergossip "github.com/hyperledger/fabric/peer/gossip"
3535
"github.com/hyperledger/fabric/protos/common"
3636
proto "github.com/hyperledger/fabric/protos/gossip"
3737
"github.com/spf13/viper"
@@ -141,7 +141,7 @@ func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string
141141
dialOpts = append(dialOpts, grpc.WithInsecure())
142142
}
143143

144-
secAdv := sa.NewSecurityAdvisor()
144+
secAdv := peergossip.NewSecurityAdvisor()
145145

146146
if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" {
147147
endpoint = overrideEndpoint

gossip/service/gossip_service_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import (
3737
"github.com/hyperledger/fabric/gossip/util"
3838
"github.com/hyperledger/fabric/msp/mgmt"
3939
"github.com/hyperledger/fabric/msp/mgmt/testtools"
40-
"github.com/hyperledger/fabric/peer/gossip/mcs"
40+
peergossip "github.com/hyperledger/fabric/peer/gossip"
41+
"github.com/hyperledger/fabric/peer/gossip/mocks"
4142
"github.com/hyperledger/fabric/protos/common"
4243
"github.com/hyperledger/fabric/protos/peer"
4344
"github.com/op/go-logging"
@@ -66,7 +67,7 @@ func TestInitGossipService(t *testing.T) {
6667
wg.Add(10)
6768
for i := 0; i < 10; i++ {
6869
go func() {
69-
messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
70+
messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
7071
InitGossipService(identity, "localhost:5611", grpcServer, messageCryptoService)
7172

7273
wg.Done()

peer/gossip/mcs/mcs.go peer/gossip/mcs.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package mcs
17+
package gossip
1818

1919
import (
2020
"bytes"
@@ -35,7 +35,7 @@ import (
3535
"github.com/hyperledger/fabric/protos/utils"
3636
)
3737

38-
var logger = flogging.MustGetLogger("peer/gossip/mcs")
38+
var mcsLogger = flogging.MustGetLogger("peer/gossip/mcs")
3939

4040
// mspMessageCryptoService implements the MessageCryptoService interface
4141
// using the peer MSPs (local and channel-related)
@@ -52,13 +52,13 @@ type mspMessageCryptoService struct {
5252
deserializer mgmt.DeserializersManager
5353
}
5454

55-
// New creates a new instance of mspMessageCryptoService
55+
// NewMCS creates a new instance of mspMessageCryptoService
5656
// that implements MessageCryptoService.
5757
// The method takes in input:
5858
// 1. a policies.ChannelPolicyManagerGetter that gives access to the policy manager of a given channel via the Manager method.
5959
// 2. an instance of crypto.LocalSigner
6060
// 3. an identity deserializer manager
61-
func New(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localSigner crypto.LocalSigner, deserializer mgmt.DeserializersManager) api.MessageCryptoService {
61+
func NewMCS(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localSigner crypto.LocalSigner, deserializer mgmt.DeserializersManager) api.MessageCryptoService {
6262
return &mspMessageCryptoService{channelPolicyManagerGetter: channelPolicyManagerGetter, localSigner: localSigner, deserializer: deserializer}
6363
}
6464

@@ -83,14 +83,14 @@ func (s *mspMessageCryptoService) ValidateIdentity(peerIdentity api.PeerIdentity
8383
func (s *mspMessageCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
8484
// Validate arguments
8585
if len(peerIdentity) == 0 {
86-
logger.Error("Invalid Peer Identity. It must be different from nil.")
86+
mcsLogger.Error("Invalid Peer Identity. It must be different from nil.")
8787

8888
return nil
8989
}
9090

9191
sid, err := s.deserializer.Deserialize(peerIdentity)
9292
if err != nil {
93-
logger.Errorf("Failed getting validated identity from peer identity [% x]: [%s]", peerIdentity, err)
93+
mcsLogger.Errorf("Failed getting validated identity from peer identity [% x]: [%s]", peerIdentity, err)
9494

9595
return nil
9696
}
@@ -105,7 +105,7 @@ func (s *mspMessageCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityTy
105105
// Hash
106106
digest, err := factory.GetDefault().Hash(raw, &bccsp.SHA256Opts{})
107107
if err != nil {
108-
logger.Errorf("Failed computing digest of serialized identity [% x]: [%s]", peerIdentity, err)
108+
mcsLogger.Errorf("Failed computing digest of serialized identity [% x]: [%s]", peerIdentity, err)
109109

110110
return nil
111111
}
@@ -160,12 +160,12 @@ func (s *mspMessageCryptoService) VerifyBlock(chainID common.ChainID, signedBloc
160160
return fmt.Errorf("Could not acquire policy manager for channel %s", channelID)
161161
}
162162
// ok is true if it was the manager requested, or false if it is the default manager
163-
logger.Debugf("Got policy manager for channel [%s] with flag [%s]", channelID, ok)
163+
mcsLogger.Debugf("Got policy manager for channel [%s] with flag [%s]", channelID, ok)
164164

165165
// Get block validation policy
166166
policy, ok := cpm.GetPolicy(policies.BlockValidation)
167167
// ok is true if it was the policy requested, or false if it is the default policy
168-
logger.Debugf("Got block validation policy for channel [%s] with flag [%s]", channelID, ok)
168+
mcsLogger.Debugf("Got block validation policy for channel [%s] with flag [%s]", channelID, ok)
169169

170170
// - Prepare SignedData
171171
signatureSet := []*pcommon.SignedData{}
@@ -200,7 +200,7 @@ func (s *mspMessageCryptoService) Sign(msg []byte) ([]byte, error) {
200200
func (s *mspMessageCryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
201201
identity, chainID, err := s.getValidatedIdentity(peerIdentity)
202202
if err != nil {
203-
logger.Errorf("Failed getting validated identity from peer identity [%s]", err)
203+
mcsLogger.Errorf("Failed getting validated identity from peer identity [%s]", err)
204204

205205
return err
206206
}
@@ -234,11 +234,11 @@ func (s *mspMessageCryptoService) VerifyByChannel(chainID common.ChainID, peerId
234234
if cpm == nil {
235235
return fmt.Errorf("Could not acquire policy manager for channel %s", string(chainID))
236236
}
237-
logger.Debugf("Got policy manager for channel [%s] with flag [%s]", string(chainID), flag)
237+
mcsLogger.Debugf("Got policy manager for channel [%s] with flag [%s]", string(chainID), flag)
238238

239239
// Get channel reader policy
240240
policy, flag := cpm.GetPolicy(policies.ChannelApplicationReaders)
241-
logger.Debugf("Got reader policy for channel [%s] with flag [%s]", string(chainID), flag)
241+
mcsLogger.Debugf("Got reader policy for channel [%s] with flag [%s]", string(chainID), flag)
242242

243243
return policy.Evaluate(
244244
[]*pcommon.SignedData{{
@@ -290,7 +290,7 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden
290290
// Deserialize identity
291291
identity, err := mspManager.DeserializeIdentity([]byte(peerIdentity))
292292
if err != nil {
293-
logger.Debugf("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err)
293+
mcsLogger.Debugf("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err)
294294
continue
295295
}
296296

@@ -300,11 +300,11 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden
300300
// This will be done by the caller function, if needed.
301301

302302
if err := identity.Validate(); err != nil {
303-
logger.Debugf("Failed validating identity [% x] on [%s]: [%s]", peerIdentity, chainID, err)
303+
mcsLogger.Debugf("Failed validating identity [% x] on [%s]: [%s]", peerIdentity, chainID, err)
304304
continue
305305
}
306306

307-
logger.Debugf("Validation succeeded [% x] on [%s]", peerIdentity, chainID)
307+
mcsLogger.Debugf("Validation succeeded [% x] on [%s]", peerIdentity, chainID)
308308

309309
return identity, common.ChainID(chainID), nil
310310
}

peer/gossip/mcs/mocks.go

-163
This file was deleted.

0 commit comments

Comments
 (0)