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.

peerpeerconnection_test.go 2.6KB

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