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.

profile_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package model
  2. import (
  3. "testing"
  4. )
  5. func TestProfileIdentity(t *testing.T) {
  6. sarah := GenerateNewProfile("Sarah")
  7. alice := GenerateNewProfile("Alice")
  8. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  9. if alice.Contacts[sarah.Onion].Name != "Sarah" {
  10. t.Errorf("alice should have added sarah as a contact %v", alice.Contacts)
  11. }
  12. if len(alice.GetContacts()) != 1 {
  13. t.Errorf("alice should be only contact: %v", alice.GetContacts())
  14. }
  15. alice.SetAttribute("test", "hello world")
  16. value, _ := alice.GetAttribute("test")
  17. if value != "hello world" {
  18. t.Errorf("value from custom attribute should have been 'hello world', instead was: %v", value)
  19. }
  20. t.Logf("%v", alice)
  21. }
  22. func TestTrustPeer(t *testing.T) {
  23. sarah := GenerateNewProfile("Sarah")
  24. alice := GenerateNewProfile("Alice")
  25. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  26. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  27. alice.TrustPeer(sarah.Onion)
  28. if alice.IsBlocked(sarah.Onion) {
  29. t.Errorf("peer should not be blocked")
  30. }
  31. if alice.TrustPeer("") == nil {
  32. t.Errorf("trusting a non existent peer should error")
  33. }
  34. }
  35. func TestBlockPeer(t *testing.T) {
  36. sarah := GenerateNewProfile("Sarah")
  37. alice := GenerateNewProfile("Alice")
  38. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  39. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  40. alice.BlockPeer(sarah.Onion)
  41. if !alice.IsBlocked(sarah.Onion) {
  42. t.Errorf("peer should not be blocked")
  43. }
  44. if alice.BlockPeer("") == nil {
  45. t.Errorf("blocking a non existent peer should error")
  46. }
  47. }
  48. func TestAcceptNonExistentGroup(t *testing.T) {
  49. sarah := GenerateNewProfile("Sarah")
  50. sarah.AcceptInvite("doesnotexist")
  51. }
  52. func TestRejectGroupInvite(t *testing.T) {
  53. sarah := GenerateNewProfile("Sarah")
  54. alice := GenerateNewProfile("Alice")
  55. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  56. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  57. gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  58. sarah.ProcessInvite(string(invite), alice.Onion)
  59. group := alice.GetGroup(gid)
  60. if len(sarah.Groups) == 1 {
  61. if sarah.GetGroup(group.GroupID).Accepted {
  62. t.Errorf("Group should not be accepted")
  63. }
  64. sarah.RejectInvite(group.GroupID)
  65. if len(sarah.Groups) != 0 {
  66. t.Errorf("Group %v should have been deleted", group.GroupID)
  67. }
  68. return
  69. }
  70. t.Errorf("Group should exist in map")
  71. }
  72. func TestProfileGroup(t *testing.T) {
  73. sarah := GenerateNewProfile("Sarah")
  74. alice := GenerateNewProfile("Alice")
  75. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  76. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  77. gid, invite, _ := alice.StartGroupWithMessage("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd", []byte("Hello World"))
  78. sarah.ProcessInvite(string(invite), alice.Onion)
  79. if len(sarah.GetGroups()) != 1 {
  80. t.Errorf("sarah should only be in 1 group instead: %v", sarah.GetGroups())
  81. }
  82. group := alice.GetGroup(gid)
  83. sarah.AcceptInvite(group.GroupID)
  84. c, s1, _ := sarah.EncryptMessageToGroup("Hello World", group.GroupID)
  85. alice.AttemptDecryption(c, s1)
  86. gid2, invite2, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  87. sarah.ProcessInvite(string(invite2), alice.Onion)
  88. group2 := alice.GetGroup(gid2)
  89. c2, s2, _ := sarah.EncryptMessageToGroup("Hello World", group2.GroupID)
  90. alice.AttemptDecryption(c2, s2)
  91. sarahGroup := sarah.GetGroup(group.GroupID)
  92. im := sarahGroup.GetInitialMessage()
  93. if string(im) != "Hello World" {
  94. t.Errorf("Initial Message was not stored properly: %v", im)
  95. }
  96. _, _, err := sarah.EncryptMessageToGroup(string(make([]byte, MaxGroupMessageLength*2)), group2.GroupID)
  97. if err == nil {
  98. t.Errorf("Overly long message should have returned an error")
  99. }
  100. bob := GenerateNewProfile("bob")
  101. bob.AddContact(alice.Onion, &alice.PublicProfile)
  102. bob.ProcessInvite(string(invite2), alice.Onion)
  103. c3, s3, err := bob.EncryptMessageToGroup("Bobs Message", group2.GroupID)
  104. if err == nil {
  105. ok, _, message, _ := alice.AttemptDecryption(c3, s3)
  106. if !ok {
  107. t.Errorf("Bobs message to the group should be decrypted %v %v", message, ok)
  108. }
  109. eve := GenerateNewProfile("eve")
  110. ok, _, _, _ = eve.AttemptDecryption(c3, s3)
  111. if ok {
  112. t.Errorf("Eves hould not be able to decrypt Messages!")
  113. }
  114. } else {
  115. t.Errorf("Bob failed to encrypt a message to the group")
  116. }
  117. }