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.

message_test.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package model
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. )
  7. func TestMessagePadding(t *testing.T) {
  8. // Setup the Group
  9. sarah := GenerateNewProfile("Sarah")
  10. alice := GenerateNewProfile("Alice")
  11. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  12. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  13. gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  14. sarah.ProcessInvite(string(invite), alice.Onion)
  15. group := alice.GetGroup(gid)
  16. c1, s1, err := sarah.EncryptMessageToGroup("Hello World 1", group.GroupID)
  17. t.Logf("Length of Encrypted Message: %v %v", len(c1), err)
  18. alice.AttemptDecryption(c1, s1)
  19. c2, s2, _ := alice.EncryptMessageToGroup("Hello World 2", group.GroupID)
  20. t.Logf("Length of Encrypted Message: %v", len(c2))
  21. alice.AttemptDecryption(c2, s2)
  22. c3, s3, _ := alice.EncryptMessageToGroup("Hello World 3", group.GroupID)
  23. t.Logf("Length of Encrypted Message: %v", len(c3))
  24. alice.AttemptDecryption(c3, s3)
  25. c4, s4, _ := alice.EncryptMessageToGroup("Hello World this is a much longer message 3", group.GroupID)
  26. t.Logf("Length of Encrypted Message: %v", len(c4))
  27. alice.AttemptDecryption(c4, s4)
  28. }
  29. func TestTranscriptConsistency(t *testing.T) {
  30. timeline := new(Timeline)
  31. // Setup the Group
  32. sarah := GenerateNewProfile("Sarah")
  33. alice := GenerateNewProfile("Alice")
  34. sarah.AddContact(alice.Onion, &alice.PublicProfile)
  35. alice.AddContact(sarah.Onion, &sarah.PublicProfile)
  36. gid, invite, _ := alice.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  37. sarah.ProcessInvite(string(invite), alice.Onion)
  38. group := alice.GetGroup(gid)
  39. t.Logf("group: %v, sarah %v", group, sarah)
  40. c1, s1, _ := alice.EncryptMessageToGroup("Hello World 1", group.GroupID)
  41. t.Logf("Length of Encrypted Message: %v", len(c1))
  42. alice.AttemptDecryption(c1, s1)
  43. c2, s2, _ := alice.EncryptMessageToGroup("Hello World 2", group.GroupID)
  44. t.Logf("Length of Encrypted Message: %v", len(c2))
  45. alice.AttemptDecryption(c2, s2)
  46. c3, s3, _ := alice.EncryptMessageToGroup("Hello World 3", group.GroupID)
  47. t.Logf("Length of Encrypted Message: %v", len(c3))
  48. alice.AttemptDecryption(c3, s3)
  49. time.Sleep(time.Second * 1)
  50. c4, s4, _ := alice.EncryptMessageToGroup("Hello World 4", group.GroupID)
  51. t.Logf("Length of Encrypted Message: %v", len(c4))
  52. alice.AttemptDecryption(c4, s4)
  53. c5, s5, _ := alice.EncryptMessageToGroup("Hello World 5", group.GroupID)
  54. t.Logf("Length of Encrypted Message: %v", len(c5))
  55. _, _, m1, _ := sarah.AttemptDecryption(c1, s1)
  56. sarah.AttemptDecryption(c1, s1) // Try a duplicate
  57. _, _, m2, _ := sarah.AttemptDecryption(c2, s2)
  58. _, _, m3, _ := sarah.AttemptDecryption(c3, s3)
  59. _, _, m4, _ := sarah.AttemptDecryption(c4, s4)
  60. _, _, m5, _ := sarah.AttemptDecryption(c5, s5)
  61. // Now we simulate a client receiving these Messages completely out of order
  62. timeline.Insert(m1)
  63. timeline.Insert(m5)
  64. timeline.Insert(m4)
  65. timeline.Insert(m3)
  66. timeline.Insert(m2)
  67. for i, m := range group.GetTimeline() {
  68. if m.Message != "Hello World "+strconv.Itoa(i+1) {
  69. t.Fatalf("Timeline Out of Order!: %v %v", i, m)
  70. }
  71. t.Logf("Messages %v: %v %x %x", i, m.Message, m.Signature, m.PreviousMessageSig)
  72. }
  73. }