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.
 
 
 
 

135 rivejä
4.3 KiB

  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.SetContactAuthorization(sarah.Onion, AuthApproved)
  28. if alice.GetContactAuthorization(sarah.Onion) != AuthApproved {
  29. t.Errorf("peer should be approved")
  30. }
  31. }
  32. func TestBlockPeer(t *testing.T) {
  33. sarah := GenerateNewProfile("Sarah")
  34. alice := GenerateNewProfile("Alice")
  35. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  36. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  37. alice.SetContactAuthorization(sarah.Onion, AuthBlocked)
  38. if alice.GetContactAuthorization(sarah.Onion) != AuthBlocked {
  39. t.Errorf("peer should be blocked")
  40. }
  41. if alice.SetContactAuthorization("", AuthUnknown) == nil {
  42. t.Errorf("Seting Auth level of a non existent peer should error")
  43. }
  44. }
  45. func TestAcceptNonExistentGroup(t *testing.T) {
  46. sarah := GenerateNewProfile("Sarah")
  47. sarah.AcceptInvite("doesnotexist")
  48. }
  49. func TestRejectGroupInvite(t *testing.T) {
  50. sarah := GenerateNewProfile("Sarah")
  51. alice := GenerateNewProfile("Alice")
  52. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  53. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  54. gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  55. sarah.ProcessInvite(string(invite), alice.Onion)
  56. group := alice.GetGroup(gid)
  57. if len(sarah.Groups) == 1 {
  58. if sarah.GetGroup(group.GroupID).Accepted {
  59. t.Errorf("Group should not be accepted")
  60. }
  61. sarah.RejectInvite(group.GroupID)
  62. if len(sarah.Groups) != 0 {
  63. t.Errorf("Group %v should have been deleted", group.GroupID)
  64. }
  65. return
  66. }
  67. t.Errorf("Group should exist in map")
  68. }
  69. func TestProfileGroup(t *testing.T) {
  70. sarah := GenerateNewProfile("Sarah")
  71. alice := GenerateNewProfile("Alice")
  72. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  73. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  74. gid, invite, _ := alice.StartGroupWithMessage("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd", []byte("Hello World"))
  75. sarah.ProcessInvite(string(invite), alice.Onion)
  76. if len(sarah.GetGroups()) != 1 {
  77. t.Errorf("sarah should only be in 1 group instead: %v", sarah.GetGroups())
  78. }
  79. group := alice.GetGroup(gid)
  80. sarah.AcceptInvite(group.GroupID)
  81. c, s1, _ := sarah.EncryptMessageToGroup("Hello World", group.GroupID)
  82. alice.AttemptDecryption(c, s1)
  83. gid2, invite2, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  84. sarah.ProcessInvite(string(invite2), alice.Onion)
  85. group2 := alice.GetGroup(gid2)
  86. c2, s2, _ := sarah.EncryptMessageToGroup("Hello World", group2.GroupID)
  87. alice.AttemptDecryption(c2, s2)
  88. sarahGroup := sarah.GetGroup(group.GroupID)
  89. im := sarahGroup.GetInitialMessage()
  90. if string(im) != "Hello World" {
  91. t.Errorf("Initial Message was not stored properly: %v", im)
  92. }
  93. _, _, err := sarah.EncryptMessageToGroup(string(make([]byte, MaxGroupMessageLength*2)), group2.GroupID)
  94. if err == nil {
  95. t.Errorf("Overly long message should have returned an error")
  96. }
  97. bob := GenerateNewProfile("bob")
  98. bob.AddContact(alice.Onion, &alice.PublicProfile)
  99. bob.ProcessInvite(string(invite2), alice.Onion)
  100. c3, s3, err := bob.EncryptMessageToGroup("Bobs Message", group2.GroupID)
  101. if err == nil {
  102. ok, _, message, _ := alice.AttemptDecryption(c3, s3)
  103. if !ok {
  104. t.Errorf("Bobs message to the group should be decrypted %v %v", message, ok)
  105. }
  106. eve := GenerateNewProfile("eve")
  107. ok, _, _, _ = eve.AttemptDecryption(c3, s3)
  108. if ok {
  109. t.Errorf("Eves hould not be able to decrypt Messages!")
  110. }
  111. } else {
  112. t.Errorf("Bob failed to encrypt a message to the group")
  113. }
  114. }