2018-03-09 20:44:13 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.mascherari.press/cwtch/protocol"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProfile(t *testing.T) {
|
|
|
|
profile := GenerateNewProfile("Sarah")
|
|
|
|
err := profile.Save("./profile_test")
|
|
|
|
if err != nil {
|
2018-03-12 18:43:51 +00:00
|
|
|
t.Errorf("Should have saved profile, but got error: %v", err)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
loadedProfile, err := LoadProfile("./profile_test")
|
|
|
|
if err != nil || loadedProfile.Name != "Sarah" {
|
|
|
|
t.Errorf("Issue loading profile from file %v %v", err, loadedProfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-31 19:33:32 +00:00
|
|
|
alice.AddCwtchIdentity("sarah.onion", ci.GetCwtchIdentify())
|
2018-03-09 20:44:13 +00:00
|
|
|
if alice.Contacts["sarah.onion"].Name != "Sarah" {
|
|
|
|
t.Errorf("alice should have added sarah as a contact %v", alice.Contacts)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("%v", alice)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProfileGroup(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-03-30 21:16:51 +00:00
|
|
|
sarah.AddContact(alice.Onion, alice.PublicProfile)
|
|
|
|
alice.AddContact(sarah.Onion, sarah.PublicProfile)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
gid, invite := alice.StartGroup("aaa.onion")
|
|
|
|
gci := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite, gci)
|
|
|
|
sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
group := alice.GetGroupByGroupId(gid)
|
2018-03-31 19:33:32 +00:00
|
|
|
c := sarah.EncryptMessageToGroup("Hello World", group.GroupID)
|
|
|
|
alice.AttemptDecryption(c)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
gid2, invite2 := alice.StartGroup("bbb.onion")
|
|
|
|
gci2 := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite2, gci2)
|
|
|
|
sarah.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
|
|
|
|
group2 := alice.GetGroupByGroupId(gid2)
|
2018-03-31 19:33:32 +00:00
|
|
|
c2 := sarah.EncryptMessageToGroup("Hello World", group2.GroupID)
|
|
|
|
alice.AttemptDecryption(c2)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
bob := GenerateNewProfile("bob")
|
|
|
|
bob.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
|
|
|
|
c3 := bob.EncryptMessageToGroup("Bobs Message", group2.GroupID)
|
|
|
|
ok, message := alice.AttemptDecryption(c3)
|
|
|
|
if ok != true || message.Verified == true {
|
|
|
|
t.Errorf("Bobs message to the group should be decrypted but not verified by alice instead %v %v", message, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
eve := GenerateNewProfile("eve")
|
|
|
|
ok, _ = eve.AttemptDecryption(c3)
|
|
|
|
if ok {
|
|
|
|
t.Errorf("Eves hould not be able to decrypt messages!")
|
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|