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.

85 lines
2.3KB

  1. package connections
  2. import (
  3. "crypto/rand"
  4. "cwtch.im/cwtch/model"
  5. "cwtch.im/cwtch/peer/peer"
  6. "cwtch.im/cwtch/protocol"
  7. "git.openprivacy.ca/openprivacy/libricochet-go"
  8. "git.openprivacy.ca/openprivacy/libricochet-go/application"
  9. "git.openprivacy.ca/openprivacy/libricochet-go/channels"
  10. "git.openprivacy.ca/openprivacy/libricochet-go/connection"
  11. "git.openprivacy.ca/openprivacy/libricochet-go/identity"
  12. "golang.org/x/crypto/ed25519"
  13. "net"
  14. "testing"
  15. "time"
  16. )
  17. func PeerAuthValid(hostname string, key ed25519.PublicKey) (allowed, known bool) {
  18. return true, true
  19. }
  20. func runtestpeer(t *testing.T, tp *TestPeer, identity identity.Identity) {
  21. ln, _ := net.Listen("tcp", "127.0.0.1:5452")
  22. conn, _ := ln.Accept()
  23. defer conn.Close()
  24. rc, err := goricochet.NegotiateVersionInbound(conn)
  25. if err != nil {
  26. t.Errorf("Negotiate Version Error: %v", err)
  27. }
  28. rc.TraceLog(true)
  29. err = connection.HandleInboundConnection(rc).ProcessAuthAsV3Server(identity, PeerAuthValid)
  30. if err != nil {
  31. t.Errorf("ServerAuth Error: %v", err)
  32. }
  33. tp.RegisterChannelHandler("im.cwtch.peer", func() channels.Handler {
  34. cpc := new(peer.CwtchPeerChannel)
  35. cpc.Handler = tp
  36. return cpc
  37. })
  38. rc.Process(tp)
  39. }
  40. type TestPeer struct {
  41. connection.AutoConnectionHandler
  42. ReceivedIdentityPacket bool
  43. ReceivedGroupInvite bool
  44. }
  45. func (tp *TestPeer) HandleGroupInvite(gci *protocol.GroupChatInvite) {
  46. tp.ReceivedGroupInvite = true
  47. }
  48. func TestPeerPeerConnection(t *testing.T) {
  49. pub, priv, _ := ed25519.GenerateKey(rand.Reader)
  50. identity := identity.InitializeV3("", &priv, &pub)
  51. profile := model.GenerateNewProfile("alice")
  52. hostname := identity.Hostname()
  53. ppc := NewPeerPeerConnection("127.0.0.1:5452|"+hostname, profile, nil, application.ApplicationInstanceFactory{})
  54. tp := new(TestPeer)
  55. tp.Init()
  56. go runtestpeer(t, tp, identity)
  57. state := ppc.GetState()
  58. if state != DISCONNECTED {
  59. t.Errorf("new connections should start in disconnected state")
  60. }
  61. go ppc.Run()
  62. time.Sleep(time.Second * 5)
  63. state = ppc.GetState()
  64. if state != AUTHENTICATED {
  65. t.Errorf("connection state should be authenticated(3), was instead %v", state)
  66. }
  67. _, invite, _ := profile.StartGroup("aaa.onion")
  68. ppc.SendGroupInvite(invite)
  69. time.Sleep(time.Second * 3)
  70. if tp.ReceivedGroupInvite == false {
  71. t.Errorf("should have received an group invite packet")
  72. }
  73. }