2018-03-09 20:44:13 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/protocol"
|
2018-03-09 20:44:13 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProfileIdentity(t *testing.T) {
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
alice := GenerateNewProfile("Alice")
|
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
message := sarah.GetCwtchIdentityPacket()
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
ci := &protocol.CwtchPeerPacket{}
|
2018-03-09 20:44:13 +00:00
|
|
|
err := proto.Unmarshal(message, ci)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("alice should have added sarah as a contact %v", err)
|
|
|
|
}
|
2018-10-31 23:24:12 +00:00
|
|
|
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
|
|
|
if alice.Contacts[sarah.Onion].Name != "Sarah" {
|
2018-03-09 20:44:13 +00:00
|
|
|
t.Errorf("alice should have added sarah as a contact %v", alice.Contacts)
|
|
|
|
}
|
|
|
|
|
2018-06-19 22:28:44 +00:00
|
|
|
if len(alice.GetContacts()) != 1 {
|
|
|
|
t.Errorf("alice should be only contact: %v", alice.GetContacts())
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:45:25 +00:00
|
|
|
alice.SetCustomAttribute("test", "hello world")
|
|
|
|
value, _ := alice.GetCustomAttribute("test")
|
|
|
|
if value != "hello world" {
|
|
|
|
t.Errorf("value from custom attribute should have been 'hello world', instead was: %v", value)
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
t.Logf("%v", alice)
|
|
|
|
}
|
|
|
|
|
2018-06-03 19:02:42 +00:00
|
|
|
func TestTrustPeer(t *testing.T) {
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
alice := GenerateNewProfile("Alice")
|
|
|
|
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
|
|
|
alice.TrustPeer(sarah.Onion)
|
|
|
|
if alice.IsBlocked(sarah.Onion) {
|
|
|
|
t.Errorf("peer should not be blocked")
|
|
|
|
}
|
|
|
|
|
|
|
|
if alice.TrustPeer("") == nil {
|
|
|
|
t.Errorf("trusting a non existent peer should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockPeer(t *testing.T) {
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
alice := GenerateNewProfile("Alice")
|
|
|
|
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
|
|
|
alice.BlockPeer(sarah.Onion)
|
|
|
|
if !alice.IsBlocked(sarah.Onion) {
|
|
|
|
t.Errorf("peer should not be blocked")
|
|
|
|
}
|
|
|
|
|
|
|
|
if alice.BlockPeer("") == nil {
|
|
|
|
t.Errorf("blocking a non existent peer should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAcceptNonExistentGroup(t *testing.T) {
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
sarah.AcceptInvite("doesnotexist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRejectGroupInvite(t *testing.T) {
|
2018-03-30 21:16:51 +00:00
|
|
|
sarah := GenerateNewProfile("Sarah")
|
2018-03-09 20:44:13 +00:00
|
|
|
alice := GenerateNewProfile("Alice")
|
2018-05-09 19:09:00 +00:00
|
|
|
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-05-16 20:20:46 +00:00
|
|
|
gid, invite, _ := alice.StartGroup("aaa.onion")
|
2018-03-30 21:16:51 +00:00
|
|
|
gci := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite, gci)
|
|
|
|
sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
|
2018-06-03 19:02:42 +00:00
|
|
|
group := alice.GetGroupByGroupID(gid)
|
|
|
|
if len(sarah.Groups) == 1 {
|
|
|
|
if sarah.GetGroupByGroupID(group.GroupID).Accepted {
|
|
|
|
t.Errorf("Group should not be accepted")
|
|
|
|
}
|
|
|
|
sarah.RejectInvite(group.GroupID)
|
|
|
|
if len(sarah.Groups) != 0 {
|
|
|
|
t.Errorf("Group %v should have been deleted", group.GroupID)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Errorf("Group should exist in map")
|
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-06-03 19:02:42 +00:00
|
|
|
func TestProfileGroup(t *testing.T) {
|
|
|
|
sarah := GenerateNewProfile("Sarah")
|
|
|
|
alice := GenerateNewProfile("Alice")
|
|
|
|
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, &sarah.PublicProfile)
|
|
|
|
|
2018-09-21 18:02:46 +00:00
|
|
|
gid, invite, _ := alice.StartGroupWithMessage("aaa.onion", []byte("Hello World"))
|
2018-06-03 19:02:42 +00:00
|
|
|
gci := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite, gci)
|
|
|
|
sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
|
2018-06-19 22:28:44 +00:00
|
|
|
if len(sarah.GetGroups()) != 1 {
|
|
|
|
t.Errorf("sarah should only be in 1 group instead: %v", sarah.GetGroups())
|
|
|
|
}
|
|
|
|
|
2018-05-16 20:53:09 +00:00
|
|
|
group := alice.GetGroupByGroupID(gid)
|
2018-06-03 19:02:42 +00:00
|
|
|
sarah.AcceptInvite(group.GroupID)
|
2018-06-22 18:11:23 +00:00
|
|
|
c, s1, _ := sarah.EncryptMessageToGroup("Hello World", group.GroupID)
|
|
|
|
alice.AttemptDecryption(c, s1)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-05-16 20:20:46 +00:00
|
|
|
gid2, invite2, _ := alice.StartGroup("bbb.onion")
|
2018-03-30 21:16:51 +00:00
|
|
|
gci2 := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite2, gci2)
|
|
|
|
sarah.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
|
2018-05-16 20:53:09 +00:00
|
|
|
group2 := alice.GetGroupByGroupID(gid2)
|
2018-06-22 18:11:23 +00:00
|
|
|
c2, s2, _ := sarah.EncryptMessageToGroup("Hello World", group2.GroupID)
|
|
|
|
alice.AttemptDecryption(c2, s2)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-09-21 18:02:46 +00:00
|
|
|
sarahGroup := sarah.GetGroupByGroupID(group.GroupID)
|
|
|
|
im := sarahGroup.GetInitialMessage()
|
|
|
|
if string(im) != "Hello World" {
|
|
|
|
t.Errorf("Initial Message was not stored properly: %v", im)
|
|
|
|
}
|
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
bob := GenerateNewProfile("bob")
|
2018-05-09 19:09:00 +00:00
|
|
|
bob.AddContact(alice.Onion, &alice.PublicProfile)
|
2018-03-31 19:33:32 +00:00
|
|
|
bob.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
|
2018-06-22 18:11:23 +00:00
|
|
|
c3, s3, err := bob.EncryptMessageToGroup("Bobs Message", group2.GroupID)
|
2018-05-09 19:09:00 +00:00
|
|
|
if err == nil {
|
2018-06-22 18:11:23 +00:00
|
|
|
ok, message := alice.AttemptDecryption(c3, s3)
|
2018-10-05 03:18:34 +00:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("Bobs message to the group should be decrypted %v %v", message, ok)
|
2018-05-09 19:09:00 +00:00
|
|
|
}
|
2018-03-31 19:33:32 +00:00
|
|
|
|
2018-05-09 19:09:00 +00:00
|
|
|
eve := GenerateNewProfile("eve")
|
2018-06-22 18:11:23 +00:00
|
|
|
ok, _ = eve.AttemptDecryption(c3, s3)
|
2018-05-09 19:09:00 +00:00
|
|
|
if ok {
|
|
|
|
t.Errorf("Eves hould not be able to decrypt messages!")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("Bob failed to encrypt a message to the group")
|
2018-03-31 19:33:32 +00:00
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|