cwtch/peer/connections/peerpeerconnection_test.go

82 lines
2.1 KiB
Go
Raw Normal View History

package connections
import (
"crypto/rsa"
"github.com/s-rah/go-ricochet"
"github.com/s-rah/go-ricochet/connection"
"github.com/s-rah/go-ricochet/identity"
"github.com/s-rah/go-ricochet/utils"
2018-03-11 19:05:35 +00:00
"github.com/s-rah/go-ricochet/channels"
"net"
"testing"
"time"
2018-03-11 19:05:35 +00:00
"git.mascherari.press/cwtch/model"
"git.mascherari.press/cwtch/protocol"
"git.mascherari.press/cwtch/peer/peer"
)
func PeerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
return true, true
}
2018-03-12 18:43:51 +00:00
func runtestpeer(t *testing.T,tp *TestPeer) {
ln, _ := net.Listen("tcp", "127.0.0.1:5452")
conn, _ := ln.Accept()
defer conn.Close()
privateKey, err := utils.LoadPrivateKeyFromFile("../../testing/private_key")
if err != nil {
t.Errorf("Private Key Error %v", err)
}
rc, err := goricochet.NegotiateVersionInbound(conn)
if err != nil {
t.Errorf("Negotiate Version Error: %v", err)
}
rc.TraceLog(true)
err = connection.HandleInboundConnection(rc).ProcessAuthAsServer(identity.Initialize("", privateKey), PeerAuthValid)
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 {
peer := new(peer.CwtchPeerChannel)
peer.Handler = tp
return peer
})
rc.Process(tp)
}
type TestPeer struct {
connection.AutoConnectionHandler
ReceivedIdentityPacket bool
}
func (tp *TestPeer) ClientIdentity(ci *protocol.CwtchIdentity) {
tp.ReceivedIdentityPacket = true
}
2018-03-12 18:43:51 +00:00
func (tp *TestPeer) HandleGroupInvite(gci *protocol.GroupChatInvite) {
}
func TestPeerPeerConnection(t *testing.T) {
2018-03-11 19:05:35 +00:00
profile := model.GenerateNewProfile("sarah")
ppc := NewPeerPeerConnection("127.0.0.1:5452|kwke2hntvyfqm7dr", profile)
//numcalls := 0
2018-03-11 19:05:35 +00:00
tp := new(TestPeer)
tp.Init()
2018-03-12 18:43:51 +00:00
go runtestpeer(t, tp)
state := ppc.GetState()
if state != DISCONNECTED {
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)
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)
}
if tp.ReceivedIdentityPacket == false {
t.Errorf("should have received an identity packet")
}
}