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.

peerserverconnection_test.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package connections
  2. import (
  3. "crypto/rand"
  4. "cwtch.im/cwtch/event"
  5. "cwtch.im/cwtch/protocol"
  6. "cwtch.im/cwtch/server/fetch"
  7. "cwtch.im/cwtch/server/send"
  8. "git.openprivacy.ca/openprivacy/libricochet-go"
  9. "git.openprivacy.ca/openprivacy/libricochet-go/channels"
  10. "git.openprivacy.ca/openprivacy/libricochet-go/connection"
  11. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  12. "git.openprivacy.ca/openprivacy/libricochet-go/identity"
  13. "golang.org/x/crypto/ed25519"
  14. "net"
  15. "testing"
  16. "time"
  17. )
  18. func ServerAuthValid(hostname string, key ed25519.PublicKey) (allowed, known bool) {
  19. return true, true
  20. }
  21. type TestServer struct {
  22. connection.AutoConnectionHandler
  23. Received bool
  24. }
  25. func (ts *TestServer) HandleGroupMessage(gm *protocol.GroupMessage) {
  26. ts.Received = true
  27. }
  28. func (ts *TestServer) HandleFetchRequest() []*protocol.GroupMessage {
  29. return []*protocol.GroupMessage{{Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}, {Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}}
  30. }
  31. func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity, listenChan chan bool) {
  32. ln, _ := net.Listen("tcp", "127.0.0.1:5451")
  33. listenChan <- true
  34. conn, _ := ln.Accept()
  35. defer conn.Close()
  36. rc, err := goricochet.NegotiateVersionInbound(conn)
  37. if err != nil {
  38. t.Errorf("Negotiate Version Error: %v", err)
  39. }
  40. err = connection.HandleInboundConnection(rc).ProcessAuthAsV3Server(identity, ServerAuthValid)
  41. if err != nil {
  42. t.Errorf("ServerAuth Error: %v", err)
  43. }
  44. ts.RegisterChannelHandler("im.cwtch.server.send", func() channels.Handler {
  45. server := new(send.CwtchServerSendChannel)
  46. server.Handler = ts
  47. return server
  48. })
  49. ts.RegisterChannelHandler("im.cwtch.server.fetch", func() channels.Handler {
  50. server := new(fetch.CwtchServerFetchChannel)
  51. server.Handler = ts
  52. return server
  53. })
  54. rc.Process(ts)
  55. }
  56. func TestPeerServerConnection(t *testing.T) {
  57. pub, priv, _ := ed25519.GenerateKey(rand.Reader)
  58. identity := identity.InitializeV3("", &priv, &pub)
  59. ts := new(TestServer)
  60. ts.Init()
  61. listenChan := make(chan bool)
  62. go runtestserver(t, ts, identity, listenChan)
  63. <-listenChan
  64. onionAddr := identity.Hostname()
  65. manager := event.NewEventManager()
  66. engine := NewProtocolEngine(identity, priv, connectivity.LocalProvider(), manager, nil)
  67. psc := NewPeerServerConnection(engine, "127.0.0.1:5451|"+onionAddr)
  68. numcalls := 0
  69. psc.GroupMessageHandler = func(s string, gm *protocol.GroupMessage) {
  70. numcalls++
  71. }
  72. closedCalls := 0
  73. psc.CloseHandler = func(s string) {
  74. closedCalls++
  75. }
  76. state := psc.GetState()
  77. if state != DISCONNECTED {
  78. t.Errorf("new connections should start in disconnected state")
  79. }
  80. time.Sleep(time.Second * 1)
  81. go psc.Run()
  82. time.Sleep(time.Second * 2)
  83. state = psc.GetState()
  84. if state != AUTHENTICATED {
  85. t.Errorf("connection should now be authed(%v), instead was %v", AUTHENTICATED, state)
  86. }
  87. gm := &protocol.GroupMessage{Ciphertext: []byte("hello"), Signature: []byte{}}
  88. psc.SendGroupMessage(gm)
  89. time.Sleep(time.Second * 2)
  90. if ts.Received == false {
  91. t.Errorf("Should have received a group message in test server")
  92. }
  93. if numcalls != 2 {
  94. t.Errorf("Should have received 2 calls from fetch request, instead received %v", numcalls)
  95. }
  96. if closedCalls != 1 {
  97. t.Errorf("Should have closed connection 1 time, instead of %v", closedCalls)
  98. }
  99. }