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_store_test.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package storage
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "cwtch.im/cwtch/protocol"
  5. "github.com/golang/protobuf/proto"
  6. "os"
  7. "testing"
  8. "time"
  9. )
  10. const testProfileName = "Alice"
  11. const testKey = "key"
  12. const testVal = "value"
  13. const testInitialMessage = "howdy"
  14. const testMessage = "Hello from storage"
  15. func TestProfileStoreWriteRead(t *testing.T) {
  16. os.RemoveAll(testingDir)
  17. eventBus := event.NewEventManager()
  18. profile := NewProfile(testProfileName)
  19. ps1 := NewProfileWriterStore(eventBus, testingDir, password, profile)
  20. eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{event.Key: testKey, event.Data: testVal}))
  21. time.Sleep(1 * time.Second)
  22. groupid, invite, err := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  23. if err != nil {
  24. t.Errorf("Creating group: %v\n", err)
  25. }
  26. if err != nil {
  27. t.Errorf("Creating group invite: %v\n", err)
  28. }
  29. packet := protocol.CwtchPeerPacket{}
  30. proto.Unmarshal(invite, &packet)
  31. invite, _ = proto.Marshal(packet.GetGroupChatInvite())
  32. eventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().Format(time.RFC3339Nano), event.RemotePeer: ps1.GetProfileCopy().Onion, event.GroupInvite: string(invite)}))
  33. time.Sleep(1 * time.Second)
  34. eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{
  35. event.GroupID: groupid,
  36. event.TimestampSent: time.Now().Format(time.RFC3339Nano),
  37. event.TimestampReceived: time.Now().Format(time.RFC3339Nano),
  38. event.RemotePeer: ps1.GetProfileCopy().Onion,
  39. event.Data: testMessage,
  40. }))
  41. time.Sleep(1 * time.Second)
  42. ps1.Shutdown()
  43. ps2 := NewProfileWriterStore(eventBus, testingDir, password, nil)
  44. err = ps2.Load()
  45. if err != nil {
  46. t.Errorf("Error createing profileStore: %v\n", err)
  47. }
  48. profile = ps2.GetProfileCopy()
  49. if profile.Name != testProfileName {
  50. t.Errorf("Profile name from loaded profile incorrect. Expected: '%v' Actual: '%v'\n", testProfileName, profile.Name)
  51. }
  52. v, _ := profile.GetAttribute(testKey)
  53. if v != testVal {
  54. t.Errorf("Profile attribute '%v' inccorect. Expected: '%v' Actual: '%v'\n", testKey, testVal, v)
  55. }
  56. group2 := ps2.GetProfileCopy().Groups[groupid]
  57. if group2 == nil {
  58. t.Errorf("Group not loaded\n")
  59. }
  60. }