cwtch/model/profile_test.go

82 lines
2.5 KiB
Go
Raw Normal View History

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")
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-03-09 20:44:13 +00:00
2018-03-30 21:16:51 +00:00
group := alice.GetGroupByGroupId(gid)
c, _ := sarah.EncryptMessageToGroup("Hello World", group.GroupID)
2018-03-31 19:33:32 +00:00
alice.AttemptDecryption(c)
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)
group2 := alice.GetGroupByGroupId(gid2)
c2, _ := sarah.EncryptMessageToGroup("Hello World", group2.GroupID)
2018-03-31 19:33:32 +00:00
alice.AttemptDecryption(c2)
2018-03-09 20:44:13 +00:00
2018-03-31 19:33:32 +00:00
bob := GenerateNewProfile("bob")
bob.AddContact(alice.Onion, &alice.PublicProfile)
2018-03-31 19:33:32 +00:00
bob.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
c3, err := bob.EncryptMessageToGroup("Bobs Message", group2.GroupID)
if err == nil {
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)
}
2018-03-31 19:33:32 +00:00
eve := GenerateNewProfile("eve")
ok, _ = eve.AttemptDecryption(c3)
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
}