enhancement/test-send (#8)

* cleaned

* basic send test

* libsecp
This commit is contained in:
Dean Eigenmann 2019-08-10 06:52:53 +02:00 committed by GitHub
parent e7b5548812
commit 01e7ad18db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -95,11 +95,10 @@ func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) (s
c.Lock()
defer c.Unlock()
lastMessage := c.lastMessages[chat]
msg := &protobuf.Message{
MessageType: protobuf.Message_MessageType(t),
Body: body,
PreviousMessage: lastMessage[:],
PreviousMessage: c.lastMessage(chat),
}
err := crypto.Sign(c.identity, msg)
@ -173,3 +172,12 @@ func (c *Client) handlePreviousMessage(group state.GroupID, previousMessage stat
log.Printf("error while requesting message: %s", err.Error())
}
}
func (c *Client) lastMessage(chat Chat) []byte {
last, ok := c.lastMessages[chat]
if !ok {
return nil
}
return last[:]
}

View File

@ -18,6 +18,7 @@ import (
"github.com/vacp2p/dasy/event"
"github.com/vacp2p/dasy/protobuf"
mvdsproto "github.com/vacp2p/mvds/protobuf"
"github.com/vacp2p/mvds/state"
)
func TestMain(m *testing.M) {
@ -27,6 +28,30 @@ func TestMain(m *testing.M) {
// @todo think about turning feed into an interface so we can mock it and ensure its never called when sigs fail
func TestClient_Post(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
node := internal.NewMockDataSyncNode(ctrl)
identity, _ := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
client := Client{
node: node,
lastMessages: make(map[Chat]state.MessageID),
identity: identity,
}
chat := Chat([32]byte{0x01, 0x2, 0x3, 0x4})
msgid := state.MessageID([32]byte{0x01, 0x2, 0x3})
node.EXPECT().AppendMessage(state.GroupID(chat), gomock.Any()).Return(msgid, nil)
ret, _ := client.Post(chat, []byte("string"))
if msgid != ret {
t.Error("returned message ID does not match expected")
}
}
func TestClient_Listen_MessageSentToFeed(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()