2018-04-02 21:10:29 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/protocol"
|
2018-04-02 21:10:29 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-05-28 17:44:47 +00:00
|
|
|
"strconv"
|
2018-04-02 21:10:29 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-05-28 17:44:47 +00:00
|
|
|
func TestMessagePadding(t *testing.T) {
|
|
|
|
|
|
|
|
// Setup the Group
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
alice := GenerateNewProfile("Alice")
|
|
|
|
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
|
|
|
|
2018-11-21 22:15:43 +00:00
|
|
|
gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
|
2018-05-28 17:44:47 +00:00
|
|
|
gci := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite, gci)
|
|
|
|
sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
|
|
|
|
|
|
|
|
group := alice.GetGroupByGroupID(gid)
|
|
|
|
|
2018-10-31 23:24:12 +00:00
|
|
|
c1, s1, err := sarah.EncryptMessageToGroup("Hello World 1", group.GroupID)
|
|
|
|
t.Logf("Length of Encrypted Message: %v %v", len(c1), err)
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c1, s1)
|
2018-05-28 17:44:47 +00:00
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c2, s2, _ := alice.EncryptMessageToGroup("Hello World 2", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c2))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c2, s2)
|
2018-05-28 17:44:47 +00:00
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c3, s3, _ := alice.EncryptMessageToGroup("Hello World 3", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c3))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c3, s3)
|
2018-05-28 17:44:47 +00:00
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c4, s4, _ := alice.EncryptMessageToGroup("Hello World this is a much longer message 3", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c4))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c4, s4)
|
2018-05-28 17:44:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-28 17:52:59 +00:00
|
|
|
func TestTranscriptConsistency(t *testing.T) {
|
2018-04-02 21:10:29 +00:00
|
|
|
timeline := new(Timeline)
|
|
|
|
|
|
|
|
// Setup the Group
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
alice := GenerateNewProfile("Alice")
|
2018-05-09 19:09:00 +00:00
|
|
|
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2018-11-21 22:15:43 +00:00
|
|
|
gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
|
2018-04-02 21:10:29 +00:00
|
|
|
gci := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite, gci)
|
|
|
|
sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
|
|
|
|
|
2018-05-16 20:53:09 +00:00
|
|
|
group := alice.GetGroupByGroupID(gid)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2018-05-09 19:09:00 +00:00
|
|
|
t.Logf("group: %v, sarah %v", group, sarah)
|
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c1, s1, _ := alice.EncryptMessageToGroup("Hello World 1", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c1))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c1, s1)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c2, s2, _ := alice.EncryptMessageToGroup("Hello World 2", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c2))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c2, s2)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c3, s3, _ := alice.EncryptMessageToGroup("Hello World 3", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c3))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c3, s3)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c4, s4, _ := alice.EncryptMessageToGroup("Hello World 4", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c4))
|
2018-06-22 18:11:23 +00:00
|
|
|
alice.AttemptDecryption(c4, s4)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
c5, s5, _ := alice.EncryptMessageToGroup("Hello World 5", group.GroupID)
|
2018-05-28 17:44:47 +00:00
|
|
|
t.Logf("Length of Encrypted Message: %v", len(c5))
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2019-01-04 21:44:21 +00:00
|
|
|
_, _, m1 := sarah.AttemptDecryption(c1, s1)
|
2018-06-22 18:11:23 +00:00
|
|
|
sarah.AttemptDecryption(c1, s1) // Try a duplicate
|
2019-01-04 21:44:21 +00:00
|
|
|
_, _, m2 := sarah.AttemptDecryption(c2, s2)
|
|
|
|
_, _, m3 := sarah.AttemptDecryption(c3, s3)
|
|
|
|
_, _, m4 := sarah.AttemptDecryption(c4, s4)
|
|
|
|
_, _, m5 := sarah.AttemptDecryption(c5, s5)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2019-02-03 01:18:33 +00:00
|
|
|
// Now we simulate a client receiving these Messages completely out of order
|
2018-04-02 21:10:29 +00:00
|
|
|
timeline.Insert(m1)
|
|
|
|
timeline.Insert(m5)
|
|
|
|
timeline.Insert(m4)
|
|
|
|
timeline.Insert(m3)
|
|
|
|
timeline.Insert(m2)
|
|
|
|
|
2018-06-03 19:02:42 +00:00
|
|
|
for i, m := range group.GetTimeline() {
|
2018-05-28 17:44:47 +00:00
|
|
|
if m.Message != "Hello World "+strconv.Itoa(i+1) {
|
|
|
|
t.Fatalf("Timeline Out of Order!: %v %v", i, m)
|
|
|
|
}
|
|
|
|
|
2019-02-03 01:18:33 +00:00
|
|
|
t.Logf("Messages %v: %v %x %x", i, m.Message, m.Signature, m.PreviousMessageSig)
|
2018-04-02 21:10:29 +00:00
|
|
|
}
|
|
|
|
}
|