Skip to content

Commit

Permalink
PLAT-559: sender test (#543)
Browse files Browse the repository at this point in the history
* PLAT-559: sender test

Co-authored-by: mariavasilenko <[email protected]>
Co-authored-by: Eugene Blikh <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2020
1 parent 627dd54 commit 9141aec
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 12 deletions.
12 changes: 0 additions & 12 deletions ledger-core/virtual/integration/delegationtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,6 @@ type testCase struct {
func TestDelegationToken_IsMessageFromVirtualLegitimate(t *testing.T) {
fixedVe := gen.UniqueGlobalRef()
cases := []testCase{
{
name: "Fail if DT is zero and sender not eq expectedVE",
testRailID: "C5196",
zeroToken: true,
expectedVE: veSetFake,
approverVE: veSetNone, // don't need due to zeroToken
errorMessages: []string{
"unexpected sender",
"illegitimate msg",
},
errorSeverity: 0,
},
{
name: "Fail if sender eq approver",
testRailID: "C5193",
Expand Down
144 changes: 144 additions & 0 deletions ledger-core/virtual/integration/sender_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2020 Insolar Network Ltd.
// All rights reserved.
// This material is licensed under the Insolar License version 1.0,
// available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.

package integration

import (
"strings"
"testing"

"github.com/gojuno/minimock/v3"
"github.com/stretchr/testify/assert"

"github.com/insolar/assured-ledger/ledger-core/insolar/jet"
"github.com/insolar/assured-ledger/ledger-core/insolar/payload"
"github.com/insolar/assured-ledger/ledger-core/instrumentation/inslogger"
"github.com/insolar/assured-ledger/ledger-core/reference"
commontestutils "github.com/insolar/assured-ledger/ledger-core/testutils"
"github.com/insolar/assured-ledger/ledger-core/virtual/authentication"
"github.com/insolar/assured-ledger/ledger-core/virtual/integration/utils"
)

var messagesWithoutToken = []struct {
name string
msg interface{}
ignoreSenderCheck bool
}{
{
name: "VCallRequest",
msg: &payload.VCallRequest{},
},
{
name: "VCallResult",
msg: &payload.VCallResult{},
},
{
name: "VStateRequest",
msg: &payload.VStateRequest{},
},
{
name: "VStateReport",
msg: &payload.VStateReport{},
},
{
name: "VDelegatedCallRequest",
msg: &payload.VDelegatedCallRequest{},
},
{
name: "VDelegatedCallResponse",
msg: &payload.VDelegatedCallResponse{},
},
{
name: "VFindCallRequest",
msg: &payload.VFindCallRequest{},
},
{
name: "VFindCallResponse",
msg: &payload.VFindCallResponse{},
ignoreSenderCheck: true,
},
}

type reseter interface {
Reset()
}

func TestVirtual_SenderCheck_With_ExpectedVE(t *testing.T) {
testCases := []struct {
name string
caseId string
senderIsEqualExpectedVE bool
}{
{"Sender is equal expectedVE", "C5188", true},
{"Sender is not equal expectedVE", "C5196", false},
}

for _, cases := range testCases {
t.Run(cases.name, func(t *testing.T) {
t.Log(cases.caseId)
for _, testMsg := range messagesWithoutToken {
t.Run(testMsg.name, func(t *testing.T) {
defer commontestutils.LeakTester(t)

mc := minimock.NewController(t)

server, ctx := utils.NewUninitializedServerWithErrorFilter(nil, t, func(s string) bool {
return false
})

jetCoordinatorMock := jet.NewAffinityHelperMock(mc)
auth := authentication.NewService(ctx, jetCoordinatorMock)
server.ReplaceAuthenticationService(auth)

var (
unexpectedError error
errorFound bool
)
{
logHandler := func(arg interface{}) {
err, ok := arg.(error)
if !ok {
return
}

errorMsg := err.Error()
if strings.Contains(errorMsg, "unexpected sender") &&
strings.Contains(errorMsg, "illegitimate msg") {
errorFound = true
} else {
unexpectedError = err
}
}
logger := utils.InterceptLog(inslogger.FromContext(ctx), logHandler)
server.OverrideConveyorFactoryLogContext(inslogger.SetLogger(ctx, logger))
}

server.Init(ctx)
server.IncrementPulseAndWaitIdle(ctx)

if !testMsg.ignoreSenderCheck {
rv := server.RandomGlobalWithPulse()
if cases.senderIsEqualExpectedVE {
rv = server.GlobalCaller()
}
jetCoordinatorMock.QueryRoleMock.Return([]reference.Global{rv}, nil)
}

testMsg.msg.(reseter).Reset()
server.SendPayload(ctx, testMsg.msg.(payload.Marshaler)) // default caller == server.GlobalCaller()

server.WaitIdleConveyor()

expectNoError := cases.senderIsEqualExpectedVE || testMsg.ignoreSenderCheck == true
assert.Equal(t, !expectNoError, errorFound, "Fail "+testMsg.name)
assert.NoError(t, unexpectedError)

server.Stop()
mc.Finish()
})
}
})
}
}

0 comments on commit 9141aec

Please sign in to comment.