Official cwtch.im peer and server implementations. https://cwtch.im
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

106 строки
2.9KB

  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 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, 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. t.Logf("Launching Server....\n")
  60. ts := new(TestServer)
  61. ts.Init()
  62. ts.Received = make(chan bool)
  63. listenChan := make(chan bool)
  64. go runtestserver(t, ts, identity, listenChan)
  65. <-listenChan
  66. onionAddr := identity.Hostname()
  67. manager := event.NewEventManager()
  68. engine := NewProtocolEngine(identity, priv, connectivity.LocalProvider(), manager, nil)
  69. psc := NewPeerServerConnection(engine, "127.0.0.1:5451|"+onionAddr)
  70. numcalls := 0
  71. psc.GroupMessageHandler = func(s string, gm *protocol.GroupMessage) {
  72. numcalls++
  73. }
  74. state := psc.GetState()
  75. if state != DISCONNECTED {
  76. t.Errorf("new connections should start in disconnected state")
  77. }
  78. time.Sleep(time.Second * 1)
  79. go psc.Run()
  80. psc.WaitTilSynced()
  81. gm := &protocol.GroupMessage{Ciphertext: []byte("hello"), Signature: []byte{}}
  82. psc.SendGroupMessage(gm)
  83. // Wait until message is received
  84. <-ts.Received
  85. if numcalls != 2 {
  86. t.Errorf("Should have received 2 calls from fetch request, instead received %v", numcalls)
  87. }
  88. }