cwtch/peer/connections/peerpeerconnection_test.go

52 lines
1.3 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"
"net"
"testing"
"time"
)
func PeerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
return true, true
}
func runtestserver(t *testing.T) {
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)
}
}
func TestPeerPeerConnection(t *testing.T) {
ppc := NewPeerPeerConnection("127.0.0.1:5452|kwke2hntvyfqm7dr")
//numcalls := 0
go runtestserver(t)
state := ppc.GetState()
if state != DISCONNECTED {
t.Errorf("new connections should start in disconnected state")
}
go ppc.Run()
time.Sleep(time.Second * 1)
state = ppc.GetState()
if state != CONNECTED {
t.Errorf("connection state should be connected, was instead %v", state)
}
}