2018-03-11 18:49:10 +00:00
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
2018-10-05 03:18:34 +00:00
|
|
|
"crypto/rand"
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
|
|
|
"cwtch.im/cwtch/protocol"
|
2019-01-04 21:44:21 +00:00
|
|
|
"cwtch.im/cwtch/protocol/connections/peer"
|
2018-11-10 22:14:12 +00:00
|
|
|
"fmt"
|
2018-06-23 16:15:36 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/connection"
|
2018-11-10 22:14:12 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
2018-06-23 16:15:36 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
|
2018-10-05 03:18:34 +00:00
|
|
|
"golang.org/x/crypto/ed25519"
|
2018-03-11 18:49:10 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-10-05 03:18:34 +00:00
|
|
|
func PeerAuthValid(hostname string, key ed25519.PublicKey) (allowed, known bool) {
|
2018-03-11 18:49:10 +00:00
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
2018-11-22 22:29:05 +00:00
|
|
|
func runtestpeer(t *testing.T, tp *TestPeer, identity identity.Identity, listenChan chan bool) {
|
2018-03-11 18:49:10 +00:00
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:5452")
|
2018-11-22 22:29:05 +00:00
|
|
|
listenChan <- true
|
2018-03-11 18:49:10 +00:00
|
|
|
conn, _ := ln.Accept()
|
|
|
|
defer conn.Close()
|
2018-05-25 22:37:44 +00:00
|
|
|
|
2018-03-11 18:49:10 +00:00
|
|
|
rc, err := goricochet.NegotiateVersionInbound(conn)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Negotiate Version Error: %v", err)
|
|
|
|
}
|
2018-10-05 03:18:34 +00:00
|
|
|
err = connection.HandleInboundConnection(rc).ProcessAuthAsV3Server(identity, PeerAuthValid)
|
2018-03-11 18:49:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ServerAuth Error: %v", err)
|
|
|
|
}
|
2018-03-11 19:05:35 +00:00
|
|
|
tp.RegisterChannelHandler("im.cwtch.peer", func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
cpc := new(peer.CwtchPeerChannel)
|
|
|
|
cpc.Handler = tp
|
|
|
|
return cpc
|
2018-03-11 19:05:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
rc.Process(tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestPeer struct {
|
2018-03-14 22:03:53 +00:00
|
|
|
connection.AutoConnectionHandler
|
|
|
|
ReceivedIdentityPacket bool
|
|
|
|
ReceivedGroupInvite bool
|
2018-03-11 19:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 18:43:51 +00:00
|
|
|
func (tp *TestPeer) HandleGroupInvite(gci *protocol.GroupChatInvite) {
|
2018-03-14 22:03:53 +00:00
|
|
|
tp.ReceivedGroupInvite = true
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerPeerConnection(t *testing.T) {
|
2018-10-05 03:18:34 +00:00
|
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
identity := identity.InitializeV3("", &priv, &pub)
|
2018-05-25 22:37:44 +00:00
|
|
|
|
2018-06-27 15:14:59 +00:00
|
|
|
profile := model.GenerateNewProfile("alice")
|
2018-10-05 03:18:34 +00:00
|
|
|
hostname := identity.Hostname()
|
2019-01-04 21:44:21 +00:00
|
|
|
ppc := NewPeerPeerConnection("127.0.0.1:5452|"+hostname, &Engine{ACN: connectivity.LocalProvider(), Identity: identity})
|
2018-10-05 03:18:34 +00:00
|
|
|
|
2018-03-11 19:05:35 +00:00
|
|
|
tp := new(TestPeer)
|
|
|
|
tp.Init()
|
2018-11-22 22:29:05 +00:00
|
|
|
listenChan := make(chan bool)
|
|
|
|
go runtestpeer(t, tp, identity, listenChan)
|
|
|
|
<-listenChan
|
2018-03-11 18:49:10 +00:00
|
|
|
state := ppc.GetState()
|
|
|
|
if state != DISCONNECTED {
|
2018-11-10 22:14:12 +00:00
|
|
|
fmt.Println("ERROR state should be disconnected")
|
2018-03-11 18:49:10 +00:00
|
|
|
t.Errorf("new connections should start in disconnected state")
|
|
|
|
}
|
|
|
|
go ppc.Run()
|
2018-03-11 19:05:35 +00:00
|
|
|
time.Sleep(time.Second * 5)
|
2018-03-11 18:49:10 +00:00
|
|
|
state = ppc.GetState()
|
2018-03-11 19:05:35 +00:00
|
|
|
if state != AUTHENTICATED {
|
|
|
|
t.Errorf("connection state should be authenticated(3), was instead %v", state)
|
|
|
|
}
|
2018-11-21 22:15:43 +00:00
|
|
|
_, invite, _ := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
|
2018-03-14 22:03:53 +00:00
|
|
|
ppc.SendGroupInvite(invite)
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
if tp.ReceivedGroupInvite == false {
|
|
|
|
t.Errorf("should have received an group invite packet")
|
|
|
|
}
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|