2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-11-21 16:19:22 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-11-21 16:19:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testMembershipUpdateMessageStruct = MembershipUpdateMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatID: "chat-id",
|
|
|
|
Events: []MembershipUpdateEvent{
|
2019-11-21 16:19:22 +00:00
|
|
|
{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_CHAT_CREATED,
|
|
|
|
Name: "thathata",
|
|
|
|
ChatID: "chat-id",
|
|
|
|
ClockValue: 156897373998501,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: protobuf.MembershipUpdateEvent_MEMBERS_ADDED,
|
|
|
|
Members: []string{"0x04aebe2bb01a988abe7d978662f21de7760486119876c680e5a559e38e086a2df6dad41c4e4d9079c03db3bced6cb70fca76afc5650e50ea19b81572046a813534"},
|
|
|
|
ChatID: "chat-id",
|
|
|
|
ClockValue: 156897373998502,
|
2019-11-21 16:19:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignMembershipUpdate(t *testing.T) {
|
|
|
|
key, err := crypto.HexToECDSA("838fbdd1b670209a258b90af25653a018bc582c44c56e6290a973eebbeb15732")
|
|
|
|
require.NoError(t, err)
|
2019-12-02 15:34:05 +00:00
|
|
|
event := &testMembershipUpdateMessageStruct.Events[0]
|
|
|
|
err = event.Sign(key)
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
encodedEvent, err := proto.Marshal(event.ToProtobuf())
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
var signatureMaterial []byte
|
|
|
|
signatureMaterial = append(signatureMaterial, []byte(testMembershipUpdateMessageStruct.ChatID)...)
|
|
|
|
signatureMaterial = crypto.Keccak256(append(signatureMaterial, encodedEvent...))
|
|
|
|
expected, err := crypto.Sign(signatureMaterial, key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, encodedEvent, event.RawPayload)
|
|
|
|
require.Equal(t, expected, event.Signature)
|
|
|
|
|
|
|
|
// Sign the other event
|
|
|
|
err = testMembershipUpdateMessageStruct.Events[1].Sign(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Encode message
|
2020-07-28 07:53:32 +00:00
|
|
|
encodedMessage, err := testMembershipUpdateMessageStruct.ToProtobuf()
|
|
|
|
require.NoError(t, err)
|
2019-12-02 15:34:05 +00:00
|
|
|
// Verify it
|
|
|
|
verifiedMessage, err := MembershipUpdateMessageFromProtobuf(encodedMessage)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, verifiedMessage, &testMembershipUpdateMessageStruct)
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupCreator(t *testing.T) {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
2020-02-29 08:26:08 +00:00
|
|
|
g, err := NewGroupWithCreator("abc", 20, key)
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
creator, err := g.creator()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, publicKeyToString(&key.PublicKey), creator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupProcessEvent(t *testing.T) {
|
2019-12-02 15:34:05 +00:00
|
|
|
createGroup := func(admins, members, joined []string, name string) Group {
|
2019-11-21 16:19:22 +00:00
|
|
|
return Group{
|
|
|
|
name: name,
|
|
|
|
admins: newStringSetFromSlice(admins),
|
2019-12-02 15:34:05 +00:00
|
|
|
joined: newStringSetFromSlice(joined),
|
2019-11-21 16:19:22 +00:00
|
|
|
members: newStringSetFromSlice(members),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Group Group
|
|
|
|
Result Group
|
|
|
|
From string
|
|
|
|
Event MembershipUpdateEvent
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "chat-created event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup(nil, nil, nil, ""),
|
|
|
|
Result: createGroup([]string{"0xabc"}, []string{"0xabc"}, []string{"0xabc"}, "some-name"),
|
2019-11-21 16:19:22 +00:00
|
|
|
From: "0xabc",
|
2019-12-02 15:34:05 +00:00
|
|
|
Event: NewChatCreatedEvent("some-name", 0),
|
2019-11-21 16:19:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "name-changed event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup(nil, nil, nil, ""),
|
|
|
|
Result: createGroup(nil, nil, nil, "some-name"),
|
2019-11-21 16:19:22 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewNameChangedEvent("some-name", 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admins-added event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup(nil, nil, nil, ""),
|
|
|
|
Result: createGroup([]string{"0xabc", "0x123"}, nil, nil, ""),
|
2019-11-21 16:19:22 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewAdminsAddedEvent([]string{"0xabc", "0x123"}, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admin-removed event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup([]string{"0xabc", "0xdef"}, nil, nil, ""),
|
|
|
|
Result: createGroup([]string{"0xdef"}, nil, nil, ""),
|
2019-11-21 16:19:22 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewAdminRemovedEvent("0xabc", 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "members-added event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup(nil, nil, nil, ""),
|
|
|
|
Result: createGroup(nil, []string{"0xabc", "0xdef"}, nil, ""),
|
2019-11-21 16:19:22 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewMembersAddedEvent([]string{"0xabc", "0xdef"}, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-removed event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup(nil, []string{"0xabc", "0xdef"}, []string{"0xdef", "0xabc"}, ""),
|
|
|
|
Result: createGroup(nil, []string{"0xdef"}, []string{"0xdef"}, ""),
|
2019-11-21 16:19:22 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewMemberRemovedEvent("0xabc", 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-joined event",
|
2019-12-02 15:34:05 +00:00
|
|
|
Group: createGroup(nil, []string{"0xabc", "0xdef"}, []string{"0xabc"}, ""),
|
|
|
|
Result: createGroup(nil, []string{"0xabc", "0xdef"}, []string{"0xabc", "0xdef"}, ""),
|
|
|
|
From: "0xdef",
|
|
|
|
Event: NewMemberJoinedEvent(0),
|
2019-11-21 16:19:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
g := tc.Group
|
2019-12-02 15:34:05 +00:00
|
|
|
tc.Event.From = tc.From
|
|
|
|
g.processEvent(tc.Event)
|
2019-11-21 16:19:22 +00:00
|
|
|
require.EqualValues(t, tc.Result, g)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupValidateEvent(t *testing.T) {
|
|
|
|
createGroup := func(admins, members []string) Group {
|
|
|
|
return Group{
|
|
|
|
admins: newStringSetFromSlice(admins),
|
|
|
|
members: newStringSetFromSlice(members),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
From string
|
|
|
|
Group Group
|
|
|
|
Event MembershipUpdateEvent
|
|
|
|
Result bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "chat-created with empty admins and members",
|
|
|
|
Group: createGroup(nil, nil),
|
2019-12-02 15:34:05 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewChatCreatedEvent("test", 0),
|
2019-11-21 16:19:22 +00:00
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "chat-created with existing admins",
|
|
|
|
Group: createGroup([]string{"0xabc"}, nil),
|
2019-12-02 15:34:05 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewChatCreatedEvent("test", 0),
|
2019-11-21 16:19:22 +00:00
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "chat-created with existing members",
|
|
|
|
Group: createGroup(nil, []string{"0xabc"}),
|
2019-12-02 15:34:05 +00:00
|
|
|
From: "0xabc",
|
|
|
|
Event: NewChatCreatedEvent("test", 0),
|
2019-11-21 16:19:22 +00:00
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "name-changed allowed because from is admin",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc"}, nil),
|
|
|
|
Event: NewNameChangedEvent("new-name", 0),
|
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "name-changed not allowed for non-admins",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup(nil, nil),
|
|
|
|
Event: NewNameChangedEvent("new-name", 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "members-added allowed because from is admin",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc"}, nil),
|
|
|
|
Event: NewMembersAddedEvent([]string{"0x123"}, 0),
|
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "members-added not allowed for non-admins",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup(nil, nil),
|
|
|
|
Event: NewMembersAddedEvent([]string{"0x123"}, 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-removed allowed because removing themselves",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup(nil, nil),
|
|
|
|
Event: NewMemberRemovedEvent("0xabc", 0),
|
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-removed allowed because from is admin",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc"}, nil),
|
|
|
|
Event: NewMemberRemovedEvent("0x123", 0),
|
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-removed not allowed for non-admins",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup(nil, nil),
|
|
|
|
Event: NewMemberRemovedEvent("0x123", 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-joined must be in members",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup(nil, []string{"0xabc"}),
|
2019-12-02 15:34:05 +00:00
|
|
|
Event: NewMemberJoinedEvent(0),
|
2019-11-21 16:19:22 +00:00
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-joined not valid because not in members",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup(nil, nil),
|
2019-12-02 15:34:05 +00:00
|
|
|
Event: NewMemberJoinedEvent(0),
|
2019-11-21 16:19:22 +00:00
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "member-joined not valid because from differs from the event",
|
|
|
|
From: "0xdef",
|
|
|
|
Group: createGroup(nil, nil),
|
2019-12-02 15:34:05 +00:00
|
|
|
Event: NewMemberJoinedEvent(0),
|
2019-11-21 16:19:22 +00:00
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admins-added allowed because originating from other admin",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc", "0x123"}, []string{"0xdef", "0xghi"}),
|
|
|
|
Event: NewAdminsAddedEvent([]string{"0xdef"}, 0),
|
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admins-added not allowed because not from admin",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0x123"}, []string{"0xdef", "0xghi"}),
|
|
|
|
Event: NewAdminsAddedEvent([]string{"0xdef"}, 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admins-added not allowed because not in members",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc", "0x123"}, []string{"0xghi"}),
|
|
|
|
Event: NewAdminsAddedEvent([]string{"0xdef"}, 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admin-removed allowed because is admin and removes themselves",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc"}, nil),
|
|
|
|
Event: NewAdminRemovedEvent("0xabc", 0),
|
|
|
|
Result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admin-removed not allowed because not themselves",
|
|
|
|
From: "0xabc",
|
|
|
|
Group: createGroup([]string{"0xabc", "0xdef"}, nil),
|
|
|
|
Event: NewAdminRemovedEvent("0xdef", 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "admin-removed not allowed because not admin",
|
|
|
|
From: "0xdef",
|
|
|
|
Group: createGroup([]string{"0xabc"}, nil),
|
|
|
|
Event: NewAdminRemovedEvent("0xabc", 0),
|
|
|
|
Result: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2019-12-02 15:34:05 +00:00
|
|
|
tc.Event.From = tc.From
|
|
|
|
result := tc.Group.validateEvent(tc.Event)
|
2019-11-21 16:19:22 +00:00
|
|
|
assert.Equal(t, tc.Result, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMembershipUpdateEventEqual(t *testing.T) {
|
|
|
|
u1 := MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_CHAT_CREATED,
|
2019-11-21 16:19:22 +00:00
|
|
|
ClockValue: 1,
|
|
|
|
Members: []string{"0xabc"},
|
|
|
|
Name: "abc",
|
2019-12-02 15:34:05 +00:00
|
|
|
Signature: []byte("signature"),
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
require.True(t, u1.Equal(u1))
|
|
|
|
|
|
|
|
// Verify equality breaking.
|
|
|
|
u2 := u1
|
2019-12-02 15:34:05 +00:00
|
|
|
u2.Signature = []byte("different-signature")
|
2019-11-21 16:19:22 +00:00
|
|
|
require.False(t, u1.Equal(u2))
|
|
|
|
}
|