test:improve test coverage
This commit is contained in:
parent
f1000a1fe4
commit
7cd9a03796
|
@ -1372,3 +1372,51 @@ func TestAssetExchange_GetStatus(t *testing.T) {
|
|||
assert.True(t, res.Ok)
|
||||
assert.Equal(t, "1", string(res.Result))
|
||||
}
|
||||
|
||||
func TestInterRelayBroker_InvokeInterRelayContract(t *testing.T) {
|
||||
mockCtl := gomock.NewController(t)
|
||||
mockStub := mock_stub.NewMockStub(mockCtl)
|
||||
|
||||
mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
mockStub.EXPECT().GetObject(gomock.Any(), gomock.Any()).Return(true).AnyTimes()
|
||||
mockStub.EXPECT().CrossInvoke(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&boltvm.Response{
|
||||
Ok: true,
|
||||
Result: nil,
|
||||
}).AnyTimes()
|
||||
|
||||
realArgs := [2][]byte{[]byte("123"), []byte("123454")}
|
||||
args, err := json.Marshal(realArgs)
|
||||
require.Nil(t, err)
|
||||
|
||||
interRelayBroker := InterRelayBroker{mockStub}
|
||||
res := interRelayBroker.InvokeInterRelayContract(constant.DIDRegistryContractAddr.String(), "", args)
|
||||
require.False(t, res.Ok)
|
||||
|
||||
res = interRelayBroker.InvokeInterRelayContract(constant.MethodRegistryContractAddr.String(), "Synchronize", args)
|
||||
require.True(t, res.Ok)
|
||||
|
||||
res = interRelayBroker.GetInCouterMap()
|
||||
require.True(t, res.Ok)
|
||||
|
||||
res = interRelayBroker.GetOutCouterMap()
|
||||
require.True(t, res.Ok)
|
||||
|
||||
res = interRelayBroker.GetOutMessageMap()
|
||||
require.True(t, res.Ok)
|
||||
|
||||
ibtp := &pb.IBTP{
|
||||
From: "123",
|
||||
To: "123",
|
||||
Type: pb.IBTP_INTERCHAIN,
|
||||
}
|
||||
|
||||
ibtps := &pb.IBTPs{Ibtps: []*pb.IBTP{ibtp}}
|
||||
data, err := ibtps.Marshal()
|
||||
require.Nil(t, err)
|
||||
|
||||
res = interRelayBroker.RecordIBTPs(data)
|
||||
require.True(t, res.Ok)
|
||||
|
||||
res = interRelayBroker.GetOutMessage("123", 1)
|
||||
require.True(t, res.Ok)
|
||||
}
|
||||
|
|
|
@ -381,8 +381,8 @@ func (x *InterchainManager) checkUnionIBTP(app *appchainMgr.Appchain, ibtp *pb.I
|
|||
}
|
||||
}
|
||||
|
||||
// _, err := verifyMultiSign(app, ibtp, ibtp.Proof)
|
||||
return nil
|
||||
_, err := verifyMultiSign(app, ibtp, ibtp.Proof)
|
||||
return err
|
||||
}
|
||||
|
||||
// verifyMultiSign .
|
||||
|
|
|
@ -6,10 +6,10 @@ new = false
|
|||
account = ""
|
||||
hosts = ["/ip4/127.0.0.1/tcp/5002/p2p/"]
|
||||
id = 2
|
||||
pid = "QmWzsQzgxNwE9ei2nGk6NxPtNy1BJYrBuiTa4ivRwRuYUJ"
|
||||
pid = "QmNiUbM1wdRKTh9pE5hcZa3muPVaXVmWhwXodHVTJTgyq8"
|
||||
|
||||
[[nodes]]
|
||||
account = ""
|
||||
hosts = ["/ip4/127.0.0.1/tcp/5003/p2p/"]
|
||||
id = 3
|
||||
pid = "QmeR2M39nxULfjkAbLVxr9zVzoYJQ8mwuSz932Rn8rnx7o"
|
||||
pid = "QmWTcHv2KUbwoCB7Lk7hMi6WAAmgzUzDccYPKPoHA3mQpY"
|
||||
|
|
|
@ -8,7 +8,8 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
crypto "github.com/libp2p/go-libp2p-crypto"
|
||||
"github.com/meshplus/bitxhub-kit/crypto"
|
||||
|
||||
appchainMgr "github.com/meshplus/bitxhub-core/appchain-mgr"
|
||||
"github.com/meshplus/bitxhub-core/validator"
|
||||
"github.com/meshplus/bitxhub-kit/crypto/asym"
|
||||
|
|
|
@ -6,6 +6,9 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/meshplus/bitxhub-kit/crypto"
|
||||
"github.com/meshplus/bitxhub-kit/crypto/asym"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
appchainMgr "github.com/meshplus/bitxhub-core/appchain-mgr"
|
||||
"github.com/meshplus/bitxhub-core/validator/mock_validator"
|
||||
|
@ -124,42 +127,64 @@ func TestVerifyPool_CheckProof2(t *testing.T) {
|
|||
mockEngine := mock_validator.NewMockEngine(mockCtl)
|
||||
|
||||
chain := &appchainMgr.Appchain{
|
||||
ID: from,
|
||||
Name: "appchain A",
|
||||
ChainType: "fabric",
|
||||
Status: appchainMgr.APPROVED,
|
||||
ID: from,
|
||||
Name: "appchain" + from,
|
||||
Validators: "",
|
||||
ConsensusType: int32(1),
|
||||
ChainType: "fabric",
|
||||
Desc: "",
|
||||
Version: "",
|
||||
PublicKey: "pubkey",
|
||||
}
|
||||
|
||||
chainData, err := json.Marshal(chain)
|
||||
keys := make([]crypto.PrivateKey, 0, 4)
|
||||
var bv contracts.BxhValidators
|
||||
addrs := make([]string, 0, 4)
|
||||
for i := 0; i < 4; i++ {
|
||||
keyPair, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
||||
require.Nil(t, err)
|
||||
keys = append(keys, keyPair)
|
||||
address, err := keyPair.PublicKey().Address()
|
||||
require.Nil(t, err)
|
||||
addrs = append(addrs, address.String())
|
||||
}
|
||||
|
||||
bv.Addresses = addrs
|
||||
addrsData, err := json.Marshal(bv)
|
||||
require.Nil(t, err)
|
||||
|
||||
rl := &contracts.Rule{
|
||||
Address: contract,
|
||||
}
|
||||
rlData, err := json.Marshal(rl)
|
||||
chainData, err := json.Marshal(chain)
|
||||
require.Nil(t, err)
|
||||
|
||||
mockLedger.EXPECT().GetState(constant.AppchainMgrContractAddr.Address(), gomock.Any()).Return(true, chainData)
|
||||
mockEngine.EXPECT().Validate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil).AnyTimes()
|
||||
|
||||
vp := VerifyPool{
|
||||
ledger: mockLedger,
|
||||
ve: mockEngine,
|
||||
logger: log.NewWithModule("test_verify"),
|
||||
ibtp := getIBTP(t, 1, pb.IBTP_RECEIPT_SUCCESS, nil)
|
||||
ibtpHash := ibtp.Hash()
|
||||
hash := sha256.Sum256([]byte(ibtpHash.String()))
|
||||
sign := &pb.SignResponse{Sign: make(map[string][]byte)}
|
||||
for _, key := range keys {
|
||||
signData, err := key.Sign(hash[:])
|
||||
require.Nil(t, err)
|
||||
|
||||
address, err := key.PublicKey().Address()
|
||||
require.Nil(t, err)
|
||||
ok, err := asym.Verify(crypto.Secp256k1, signData[:], hash[:], *address)
|
||||
require.Nil(t, err)
|
||||
require.True(t, ok)
|
||||
sign.Sign[address.String()] = signData
|
||||
}
|
||||
signData, err := sign.Marshal()
|
||||
require.Nil(t, err)
|
||||
ibtp.Proof = signData
|
||||
|
||||
proof := []byte("test_proof")
|
||||
proofHash := sha256.Sum256(proof)
|
||||
ok, err := verifyMultiSign(chain, ibtp, nil)
|
||||
require.NotNil(t, err)
|
||||
require.False(t, ok)
|
||||
|
||||
txWithIBTP := &pb.Transaction{
|
||||
From: types.NewAddressByStr(from),
|
||||
To: types.NewAddressByStr(to),
|
||||
IBTP: getIBTP(t, 1, pb.IBTP_RECEIPT_SUCCESS, proofHash[:]),
|
||||
Extra: proof,
|
||||
}
|
||||
|
||||
mockLedger.EXPECT().GetState(constant.RuleManagerContractAddr.Address(), gomock.Any()).Return(true, rlData)
|
||||
txWithIBTP.TransactionHash = txWithIBTP.Hash()
|
||||
ok, err := vp.CheckProof(txWithIBTP)
|
||||
chain.Validators = string(addrsData)
|
||||
ok, err = verifyMultiSign(chain, ibtp, nil)
|
||||
require.Nil(t, err)
|
||||
require.True(t, ok)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue