Skip to content

Commit

Permalink
[FAB-3452] peer/gossip test-coverage
Browse files Browse the repository at this point in the history
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 <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Apr 27, 2017
1 parent ca3a1a2 commit 68b455e
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 226 deletions.
5 changes: 3 additions & 2 deletions core/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/peer/gossip/mcs"
peergossip "github.com/hyperledger/fabric/peer/gossip"
"github.com/hyperledger/fabric/peer/gossip/mocks"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestCreateChainFromBlock(t *testing.T) {
msptesttools.LoadMSPSetupForTesting()

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

err = CreateChainFromBlock(block)
Expand Down
5 changes: 3 additions & 2 deletions core/scc/cscc/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import (
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/peer/gossip/mcs"
peergossip "github.com/hyperledger/fabric/peer/gossip"
"github.com/hyperledger/fabric/peer/gossip/mocks"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -164,7 +165,7 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
)

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

// Successful path for JoinChain
Expand Down
4 changes: 2 additions & 2 deletions gossip/service/gossip_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/hyperledger/fabric/gossip/integration"
"github.com/hyperledger/fabric/gossip/state"
"github.com/hyperledger/fabric/gossip/util"
"github.com/hyperledger/fabric/peer/gossip/sa"
peergossip "github.com/hyperledger/fabric/peer/gossip"
"github.com/hyperledger/fabric/protos/common"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/spf13/viper"
Expand Down Expand Up @@ -141,7 +141,7 @@ func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string
dialOpts = append(dialOpts, grpc.WithInsecure())
}

secAdv := sa.NewSecurityAdvisor()
secAdv := peergossip.NewSecurityAdvisor()

if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" {
endpoint = overrideEndpoint
Expand Down
5 changes: 3 additions & 2 deletions gossip/service/gossip_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import (
"github.com/hyperledger/fabric/gossip/util"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/peer/gossip/mcs"
peergossip "github.com/hyperledger/fabric/peer/gossip"
"github.com/hyperledger/fabric/peer/gossip/mocks"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/op/go-logging"
Expand Down Expand Up @@ -66,7 +67,7 @@ func TestInitGossipService(t *testing.T) {
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
InitGossipService(identity, "localhost:5611", grpcServer, messageCryptoService)

wg.Done()
Expand Down
30 changes: 15 additions & 15 deletions peer/gossip/mcs/mcs.go → peer/gossip/mcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package mcs
package gossip

import (
"bytes"
Expand All @@ -35,7 +35,7 @@ import (
"github.com/hyperledger/fabric/protos/utils"
)

var logger = flogging.MustGetLogger("peer/gossip/mcs")
var mcsLogger = flogging.MustGetLogger("peer/gossip/mcs")

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

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

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

return nil
}

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

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

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

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

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

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

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

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

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

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

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

return identity, common.ChainID(chainID), nil
}
Expand Down
163 changes: 0 additions & 163 deletions peer/gossip/mcs/mocks.go

This file was deleted.

Loading

0 comments on commit 68b455e

Please sign in to comment.