2018-04-02 21:10:29 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
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")
|
2019-09-19 23:14:35 +00:00
|
|
|
|
2021-05-03 23:32:48 +00:00
|
|
|
sarah.ProcessInvite(invite)
|
2018-05-28 17:44:47 +00:00
|
|
|
|
2019-10-31 21:39:31 +00:00
|
|
|
group := alice.GetGroup(gid)
|
2018-05-28 17:44:47 +00:00
|
|
|
|
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
|
|
|
|
2021-06-09 20:38:11 +00:00
|
|
|
// The lightest weight server entry possible (usually we would import a key bundle...)
|
|
|
|
sarah.AddContact("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd", &PublicProfile{Attributes: map[string]string{string(KeyTypeServerOnion): "2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd"}})
|
|
|
|
|
2018-11-21 22:15:43 +00:00
|
|
|
gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
|
2019-09-19 23:14:35 +00:00
|
|
|
|
2021-05-03 23:32:48 +00:00
|
|
|
sarah.ProcessInvite(invite)
|
2018-04-02 21:10:29 +00:00
|
|
|
|
2019-10-31 21:39:31 +00:00
|
|
|
group := alice.GetGroup(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-02-03 03:24:42 +00:00
|
|
|
_, _, m1, _ := sarah.AttemptDecryption(c1, s1)
|
2018-06-22 18:11:23 +00:00
|
|
|
sarah.AttemptDecryption(c1, s1) // Try a duplicate
|
2019-02-03 03:24:42 +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
|
|
|
}
|
2021-07-02 17:50:05 +00:00
|
|
|
|
|
|
|
// Test message by hash lookup...
|
|
|
|
hash := timeline.calculateHash(*m5)
|
|
|
|
|
|
|
|
t.Logf("Looking up %v ", hash)
|
|
|
|
|
2021-07-08 19:32:43 +00:00
|
|
|
for key, msgs := range timeline.hashCache {
|
2021-07-02 17:50:05 +00:00
|
|
|
t.Logf("%v %v", key, msgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check a real message..
|
|
|
|
msgs, err := timeline.GetMessagesByHash(hash)
|
|
|
|
if err != nil || len(msgs) != 1 {
|
|
|
|
t.Fatalf("looking up message by hash %v should have not errored: %v", hash, err)
|
|
|
|
} else if msgs[0].Message.Message != m5.Message {
|
|
|
|
t.Fatalf("%v != %v", msgs[0].Message, m5.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check a non existed hash... error if there is no error
|
|
|
|
_, err = timeline.GetMessagesByHash("not a real hash")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("looking up message by hash %v should have errored: %v", hash, err)
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:10:29 +00:00
|
|
|
}
|