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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package connections
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "cwtch.im/cwtch/protocol"
  5. "cwtch.im/cwtch/server/fetch"
  6. "cwtch.im/cwtch/server/send"
  7. "cwtch.im/tapir/primitives"
  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. identityOld "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 chan 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, priv ed25519.PrivateKey, identity primitives.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. // TODO switch from old identity to new tapir identity.
  41. pub := identity.PublicKey()
  42. err = connection.HandleInboundConnection(rc).ProcessAuthAsV3Server(identityOld.InitializeV3("", &priv, &pub), ServerAuthValid)
  43. if err != nil {
  44. t.Errorf("ServerAuth Error: %v", err)
  45. }
  46. ts.RegisterChannelHandler("im.cwtch.server.send", func() channels.Handler {
  47. server := new(send.CwtchServerSendChannel)
  48. server.Handler = ts
  49. return server
  50. })
  51. ts.RegisterChannelHandler("im.cwtch.server.fetch", func() channels.Handler {
  52. server := new(fetch.CwtchServerFetchChannel)
  53. server.Handler = ts
  54. return server
  55. })
  56. rc.Process(ts)
  57. }
  58. func TestPeerServerConnection(t *testing.T) {
  59. identity, priv := primitives.InitializeEphemeralIdentity()
  60. t.Logf("Launching Server....\n")
  61. ts := new(TestServer)
  62. ts.Init()
  63. ts.Received = make(chan bool)
  64. listenChan := make(chan bool)
  65. go runtestserver(t, ts, priv, identity, listenChan)
  66. <-listenChan
  67. onionAddr := identity.Hostname()
  68. manager := event.NewEventManager()
  69. engine := NewProtocolEngine(identity, priv, connectivity.LocalProvider(), manager, nil)
  70. psc := NewPeerServerConnection(engine, "127.0.0.1:5451|"+onionAddr)
  71. numcalls := 0
  72. psc.GroupMessageHandler = func(s string, gm *protocol.GroupMessage) {
  73. numcalls++
  74. }
  75. state := psc.GetState()
  76. if state != DISCONNECTED {
  77. t.Errorf("new connections should start in disconnected state")
  78. }
  79. time.Sleep(time.Second * 1)
  80. go psc.Run()
  81. psc.WaitTilSynced()
  82. gm := &protocol.GroupMessage{Ciphertext: []byte("hello"), Signature: []byte{}}
  83. psc.SendGroupMessage(gm)
  84. // Wait until message is received
  85. <-ts.Received
  86. if numcalls != 2 {
  87. t.Errorf("Should have received 2 calls from fetch request, instead received %v", numcalls)
  88. }
  89. }