82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
|
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"
|
||
|
"github.com/s-rah/go-ricochet/channels"
|
||
|
"git.mascherari.press/cwtch/server/send"
|
||
|
"git.mascherari.press/cwtch/protocol"
|
||
|
"net"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func ServerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
|
||
|
return true, true
|
||
|
}
|
||
|
|
||
|
type TestServer struct {
|
||
|
connection.AutoConnectionHandler
|
||
|
Received bool
|
||
|
}
|
||
|
|
||
|
func (ts *TestServer) HandleGroupMessage(gm *protocol.GroupMessage) {
|
||
|
ts.Received = true
|
||
|
}
|
||
|
|
||
|
func runtestserver(t *testing.T, ts *TestServer) {
|
||
|
ln, _ := net.Listen("tcp", "127.0.0.1:5451")
|
||
|
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)
|
||
|
}
|
||
|
err = connection.HandleInboundConnection(rc).ProcessAuthAsServer(identity.Initialize("", privateKey), ServerAuthValid)
|
||
|
if err != nil {
|
||
|
t.Errorf("ServerAuth Error: %v", err)
|
||
|
}
|
||
|
|
||
|
|
||
|
ts.RegisterChannelHandler("im.cwtch.server.send", func() channels.Handler {
|
||
|
server := new(send.CwtchServerSendChannel)
|
||
|
server.Handler = ts
|
||
|
return server
|
||
|
})
|
||
|
|
||
|
rc.Process(ts)
|
||
|
}
|
||
|
|
||
|
func TestPeerServerConnection(t *testing.T) {
|
||
|
ts := new(TestServer)
|
||
|
ts.Init()
|
||
|
go runtestserver(t, ts)
|
||
|
psc := NewPeerServerConnection("127.0.0.1:5451|kwke2hntvyfqm7dr")
|
||
|
state := psc.GetState()
|
||
|
if state != DISCONNECTED {
|
||
|
t.Errorf("new connections should start in disconnected state")
|
||
|
}
|
||
|
time.Sleep(time.Second * 1)
|
||
|
go psc.Run()
|
||
|
time.Sleep(time.Second * 2)
|
||
|
state = psc.GetState()
|
||
|
if state != AUTHENTICATED {
|
||
|
t.Errorf("connection should now be authed(%v), instead was %v", AUTHENTICATED, state)
|
||
|
}
|
||
|
|
||
|
gm := &protocol.GroupMessage{Ciphertext: []byte("hello")}
|
||
|
psc.SendGroupMessage(gm)
|
||
|
time.Sleep(time.Second * 2)
|
||
|
if ts.Received == false {
|
||
|
t.Errorf("Should have received a group message in test server")
|
||
|
}
|
||
|
|
||
|
}
|