-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for inactivity claim signing member
- Loading branch information
1 parent
82dc68a
commit 6eaf756
Showing
1 changed file
with
103 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package inactivity | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/keep-network/keep-core/internal/testutils" | ||
"github.com/keep-network/keep-core/pkg/chain" | ||
"github.com/keep-network/keep-core/pkg/chain/local_v1" | ||
"github.com/keep-network/keep-core/pkg/operator" | ||
"github.com/keep-network/keep-core/pkg/protocol/group" | ||
) | ||
|
||
func TestShouldAcceptMessage(t *testing.T) { | ||
groupSize := 5 | ||
honestThreshold := 3 | ||
|
||
localChain := local_v1.Connect(groupSize, honestThreshold) | ||
|
||
operatorsAddresses := make([]chain.Address, groupSize) | ||
operatorsPublicKeys := make([][]byte, groupSize) | ||
for i := range operatorsAddresses { | ||
_, operatorPublicKey, err := operator.GenerateKeyPair( | ||
local_v1.DefaultCurve, | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
operatorAddress, err := localChain.Signing().PublicKeyToAddress( | ||
operatorPublicKey, | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
operatorsAddresses[i] = operatorAddress | ||
operatorsPublicKeys[i] = operator.MarshalUncompressed(operatorPublicKey) | ||
} | ||
|
||
tests := map[string]struct { | ||
senderIndex group.MemberIndex | ||
senderPublicKey []byte | ||
inactiveMembersIDs []group.MemberIndex | ||
expectedResult bool | ||
}{ | ||
"message from another valid and operating member": { | ||
senderIndex: group.MemberIndex(2), | ||
senderPublicKey: operatorsPublicKeys[1], | ||
inactiveMembersIDs: []group.MemberIndex{}, | ||
expectedResult: true, | ||
}, | ||
"message from another valid but non-operating member": { | ||
senderIndex: group.MemberIndex(2), | ||
senderPublicKey: operatorsPublicKeys[1], | ||
inactiveMembersIDs: []group.MemberIndex{2}, | ||
expectedResult: false, | ||
}, | ||
"message from self": { | ||
senderIndex: group.MemberIndex(1), | ||
senderPublicKey: operatorsPublicKeys[0], | ||
inactiveMembersIDs: []group.MemberIndex{}, | ||
expectedResult: false, | ||
}, | ||
"message from another invalid member": { | ||
senderIndex: group.MemberIndex(2), | ||
senderPublicKey: operatorsPublicKeys[3], | ||
inactiveMembersIDs: []group.MemberIndex{}, | ||
expectedResult: false, | ||
}, | ||
} | ||
|
||
for testName, test := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
membershipValdator := group.NewMembershipValidator( | ||
&testutils.MockLogger{}, | ||
operatorsAddresses, | ||
localChain.Signing(), | ||
) | ||
|
||
member := newSigningMember( | ||
&testutils.MockLogger{}, | ||
group.MemberIndex(1), | ||
groupSize, | ||
groupSize-honestThreshold, | ||
membershipValdator, | ||
"session_1", | ||
) | ||
|
||
for _, inactiveMemberID := range test.inactiveMembersIDs { | ||
member.group.MarkMemberAsInactive(inactiveMemberID) | ||
} | ||
|
||
result := member.shouldAcceptMessage(test.senderIndex, test.senderPublicKey) | ||
|
||
testutils.AssertBoolsEqual( | ||
t, | ||
"result from message validator", | ||
test.expectedResult, | ||
result, | ||
) | ||
}) | ||
} | ||
} |