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.
 
 
 
 

153 lines
4.8 KiB

  1. // Known race issue with event bus channel closure
  2. package storage
  3. import (
  4. "cwtch.im/cwtch/event"
  5. "fmt"
  6. "log"
  7. "os"
  8. "testing"
  9. "time"
  10. )
  11. const testProfileName = "Alice"
  12. const testKey = "key"
  13. const testVal = "value"
  14. const testInitialMessage = "howdy"
  15. const testMessage = "Hello from storage"
  16. func TestProfileStoreWriteRead(t *testing.T) {
  17. log.Println("profile store test!")
  18. os.RemoveAll(testingDir)
  19. eventBus := event.NewEventManager()
  20. profile := NewProfile(testProfileName)
  21. ps1 := NewProfileWriterStore(eventBus, testingDir, password, profile)
  22. eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{event.Key: testKey, event.Data: testVal}))
  23. time.Sleep(1 * time.Second)
  24. groupid, invite, err := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  25. if err != nil {
  26. t.Errorf("Creating group: %v\n", err)
  27. }
  28. if err != nil {
  29. t.Errorf("Creating group invite: %v\n", err)
  30. }
  31. eventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().Format(time.RFC3339Nano), event.RemotePeer: ps1.GetProfileCopy(true).Onion, event.GroupInvite: string(invite)}))
  32. time.Sleep(1 * time.Second)
  33. eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{
  34. event.GroupID: groupid,
  35. event.TimestampSent: time.Now().Format(time.RFC3339Nano),
  36. event.TimestampReceived: time.Now().Format(time.RFC3339Nano),
  37. event.RemotePeer: ps1.GetProfileCopy(true).Onion,
  38. event.Data: testMessage,
  39. }))
  40. time.Sleep(1 * time.Second)
  41. ps1.Shutdown()
  42. ps2 := NewProfileWriterStore(eventBus, testingDir, password, nil)
  43. err = ps2.Load()
  44. if err != nil {
  45. t.Errorf("Error createing profileStore: %v\n", err)
  46. }
  47. profile = ps2.GetProfileCopy(true)
  48. if profile.Name != testProfileName {
  49. t.Errorf("Profile name from loaded profile incorrect. Expected: '%v' Actual: '%v'\n", testProfileName, profile.Name)
  50. }
  51. v, _ := profile.GetAttribute(testKey)
  52. if v != testVal {
  53. t.Errorf("Profile attribute '%v' inccorect. Expected: '%v' Actual: '%v'\n", testKey, testVal, v)
  54. }
  55. group2 := ps2.GetProfileCopy(true).Groups[groupid]
  56. if group2 == nil {
  57. t.Errorf("Group not loaded\n")
  58. }
  59. }
  60. func TestProfileStoreChangePassword(t *testing.T) {
  61. os.RemoveAll(testingDir)
  62. eventBus := event.NewEventManager()
  63. queue := event.NewQueue()
  64. eventBus.Subscribe(event.ChangePasswordSuccess, queue)
  65. profile := NewProfile(testProfileName)
  66. ps1 := NewProfileWriterStore(eventBus, testingDir, password, profile)
  67. groupid, invite, err := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  68. if err != nil {
  69. t.Errorf("Creating group: %v\n", err)
  70. }
  71. if err != nil {
  72. t.Errorf("Creating group invite: %v\n", err)
  73. }
  74. eventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().Format(time.RFC3339Nano), event.RemotePeer: ps1.GetProfileCopy(true).Onion, event.GroupInvite: string(invite)}))
  75. time.Sleep(1 * time.Second)
  76. fmt.Println("Sending 200 messages...")
  77. for i := 0; i < 200; i++ {
  78. eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{
  79. event.GroupID: groupid,
  80. event.TimestampSent: time.Now().Format(time.RFC3339Nano),
  81. event.TimestampReceived: time.Now().Format(time.RFC3339Nano),
  82. event.RemotePeer: ps1.GetProfileCopy(true).Onion,
  83. event.Data: testMessage,
  84. }))
  85. }
  86. newPass := "qwerty123"
  87. fmt.Println("Sending Change Passwords event...")
  88. eventBus.Publish(event.NewEventList(event.ChangePassword, event.Password, password, event.NewPassword, newPass))
  89. ev := queue.Next()
  90. if ev.EventType != event.ChangePasswordSuccess {
  91. t.Errorf("Unexpected event response detected %v\n", ev.EventType)
  92. return
  93. }
  94. fmt.Println("Sending 10 more messages...")
  95. for i := 0; i < 10; i++ {
  96. eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{
  97. event.GroupID: groupid,
  98. event.TimestampSent: time.Now().Format(time.RFC3339Nano),
  99. event.TimestampReceived: time.Now().Format(time.RFC3339Nano),
  100. event.RemotePeer: ps1.GetProfileCopy(true).Onion,
  101. event.Data: testMessage,
  102. }))
  103. }
  104. time.Sleep(3 * time.Second)
  105. fmt.Println("Shutdown profile store...")
  106. ps1.Shutdown()
  107. fmt.Println("New Profile store...")
  108. ps2 := NewProfileWriterStore(eventBus, testingDir, newPass, nil)
  109. err = ps2.Load()
  110. if err != nil {
  111. t.Errorf("Error createing new profileStore with new password: %v\n", err)
  112. return
  113. }
  114. profile2 := ps2.GetProfileCopy(true)
  115. if profile2.Groups[groupid] == nil {
  116. t.Errorf("Failed to load group %v\n", groupid)
  117. return
  118. }
  119. if len(profile2.Groups[groupid].Timeline.Messages) != 210 {
  120. t.Errorf("Failed to load group's 210 messages, instead got %v\n", len(profile2.Groups[groupid].Timeline.Messages))
  121. }
  122. }