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.

104 lines
2.8KB

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