Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.2KB

  1. package model
  2. import (
  3. "cwtch.im/cwtch/protocol"
  4. "github.com/golang/protobuf/proto"
  5. "testing"
  6. )
  7. func TestProfile(t *testing.T) {
  8. profile := GenerateNewProfile("Sarah")
  9. err := profile.Save("./profile_test")
  10. if err != nil {
  11. t.Errorf("Should have saved profile, but got error: %v", err)
  12. }
  13. loadedProfile, err := LoadProfile("./profile_test")
  14. if err != nil || loadedProfile.Name != "Sarah" {
  15. t.Errorf("Issue loading profile from file %v %v", err, loadedProfile)
  16. }
  17. }
  18. func TestProfileIdentity(t *testing.T) {
  19. sarah := GenerateNewProfile("Sarah")
  20. alice := GenerateNewProfile("Alice")
  21. message := sarah.GetCwtchIdentityPacket()
  22. ci := &protocol.CwtchPeerPacket{}
  23. err := proto.Unmarshal(message, ci)
  24. if err != nil {
  25. t.Errorf("alice should have added sarah as a contact %v", err)
  26. }
  27. alice.AddCwtchIdentity("sarah.onion", ci.GetCwtchIdentify())
  28. if alice.Contacts["sarah.onion"].Name != "Sarah" {
  29. t.Errorf("alice should have added sarah as a contact %v", alice.Contacts)
  30. }
  31. t.Logf("%v", alice)
  32. }
  33. func TestTrustPeer(t *testing.T) {
  34. sarah := GenerateNewProfile("Sarah")
  35. alice := GenerateNewProfile("Alice")
  36. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  37. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  38. alice.TrustPeer(sarah.Onion)
  39. if alice.IsBlocked(sarah.Onion) {
  40. t.Errorf("peer should not be blocked")
  41. }
  42. if alice.TrustPeer("") == nil {
  43. t.Errorf("trusting a non existent peer should error")
  44. }
  45. }
  46. func TestBlockPeer(t *testing.T) {
  47. sarah := GenerateNewProfile("Sarah")
  48. alice := GenerateNewProfile("Alice")
  49. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  50. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  51. alice.BlockPeer(sarah.Onion)
  52. if !alice.IsBlocked(sarah.Onion) {
  53. t.Errorf("peer should not be blocked")
  54. }
  55. if alice.BlockPeer("") == nil {
  56. t.Errorf("blocking a non existent peer should error")
  57. }
  58. }
  59. func TestAcceptNonExistentGroup(t *testing.T) {
  60. sarah := GenerateNewProfile("Sarah")
  61. sarah.AcceptInvite("doesnotexist")
  62. }
  63. func TestRejectGroupInvite(t *testing.T) {
  64. sarah := GenerateNewProfile("Sarah")
  65. alice := GenerateNewProfile("Alice")
  66. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  67. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  68. gid, invite, _ := alice.StartGroup("aaa.onion")
  69. gci := &protocol.CwtchPeerPacket{}
  70. proto.Unmarshal(invite, gci)
  71. sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
  72. group := alice.GetGroupByGroupID(gid)
  73. if len(sarah.Groups) == 1 {
  74. if sarah.GetGroupByGroupID(group.GroupID).Accepted {
  75. t.Errorf("Group should not be accepted")
  76. }
  77. sarah.RejectInvite(group.GroupID)
  78. if len(sarah.Groups) != 0 {
  79. t.Errorf("Group %v should have been deleted", group.GroupID)
  80. }
  81. return
  82. }
  83. t.Errorf("Group should exist in map")
  84. }
  85. func TestProfileGroup(t *testing.T) {
  86. sarah := GenerateNewProfile("Sarah")
  87. alice := GenerateNewProfile("Alice")
  88. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  89. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  90. gid, invite, _ := alice.StartGroup("aaa.onion")
  91. gci := &protocol.CwtchPeerPacket{}
  92. proto.Unmarshal(invite, gci)
  93. sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
  94. group := alice.GetGroupByGroupID(gid)
  95. sarah.AcceptInvite(group.GroupID)
  96. c, _ := sarah.EncryptMessageToGroup("Hello World", group.GroupID)
  97. alice.AttemptDecryption(c)
  98. gid2, invite2, _ := alice.StartGroup("bbb.onion")
  99. gci2 := &protocol.CwtchPeerPacket{}
  100. proto.Unmarshal(invite2, gci2)
  101. sarah.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
  102. group2 := alice.GetGroupByGroupID(gid2)
  103. c2, _ := sarah.EncryptMessageToGroup("Hello World", group2.GroupID)
  104. alice.AttemptDecryption(c2)
  105. bob := GenerateNewProfile("bob")
  106. bob.AddContact(alice.Onion, &alice.PublicProfile)
  107. bob.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
  108. c3, err := bob.EncryptMessageToGroup("Bobs Message", group2.GroupID)
  109. if err == nil {
  110. ok, message := alice.AttemptDecryption(c3)
  111. if ok != true || message.Verified == true {
  112. t.Errorf("Bobs message to the group should be decrypted but not verified by alice instead %v %v", message, ok)
  113. }
  114. eve := GenerateNewProfile("eve")
  115. ok, _ = eve.AttemptDecryption(c3)
  116. if ok {
  117. t.Errorf("Eves hould not be able to decrypt messages!")
  118. }
  119. } else {
  120. t.Errorf("Bob failed to encrypt a message to the group")
  121. }
  122. }