cwtch/protocol/connections/peerserverconnection_test.go

119 regels
3.3 KiB
Go

package connections
import (
2018-10-05 03:18:34 +00:00
"crypto/rand"
"cwtch.im/cwtch/event"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/protocol"
"cwtch.im/cwtch/server/fetch"
"cwtch.im/cwtch/server/send"
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"
"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"
"net"
"testing"
"time"
)
2018-10-05 03:18:34 +00:00
func ServerAuthValid(hostname string, key ed25519.PublicKey) (allowed, known bool) {
return true, true
}
type TestServer struct {
2018-03-10 22:05:48 +00:00
connection.AutoConnectionHandler
Received bool
}
func (ts *TestServer) HandleGroupMessage(gm *protocol.GroupMessage) {
2018-03-10 22:05:48 +00:00
ts.Received = true
}
func (ts *TestServer) HandleFetchRequest() []*protocol.GroupMessage {
return []*protocol.GroupMessage{{Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}, {Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}}
}
func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity, listenChan chan bool) {
ln, _ := net.Listen("tcp", "127.0.0.1:5451")
listenChan <- true
conn, _ := ln.Accept()
defer conn.Close()
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, ServerAuthValid)
if err != nil {
t.Errorf("ServerAuth Error: %v", err)
}
ts.RegisterChannelHandler("im.cwtch.server.send", func() channels.Handler {
2018-03-10 22:05:48 +00:00
server := new(send.CwtchServerSendChannel)
server.Handler = ts
return server
})
2018-03-10 22:05:48 +00:00
ts.RegisterChannelHandler("im.cwtch.server.fetch", func() channels.Handler {
server := new(fetch.CwtchServerFetchChannel)
server.Handler = ts
return server
})
rc.Process(ts)
}
func TestPeerServerConnection(t *testing.T) {
2018-10-05 03:18:34 +00:00
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
identity := identity.InitializeV3("", &priv, &pub)
ts := new(TestServer)
ts.Init()
listenChan := make(chan bool)
go runtestserver(t, ts, identity, listenChan)
<-listenChan
2018-10-05 03:18:34 +00:00
onionAddr := identity.Hostname()
manager := event.NewEventManager()
engine := NewProtocolEngine(identity, priv, connectivity.LocalProvider(), manager, nil)
psc := NewPeerServerConnection(engine, "127.0.0.1:5451|"+onionAddr)
2018-03-10 22:05:48 +00:00
numcalls := 0
psc.GroupMessageHandler = func(s string, gm *protocol.GroupMessage) {
2018-03-15 16:33:26 +00:00
numcalls++
2018-03-10 22:05:48 +00:00
}
closedCalls := 0
psc.CloseHandler = func(s string) {
closedCalls++
}
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"), Signature: []byte{}}
psc.SendGroupMessage(gm)
time.Sleep(time.Second * 2)
if ts.Received == false {
2018-03-10 22:05:48 +00:00
t.Errorf("Should have received a group message in test server")
}
2018-03-10 22:05:48 +00:00
if numcalls != 2 {
t.Errorf("Should have received 2 calls from fetch request, instead received %v", numcalls)
2018-03-10 22:05:48 +00:00
}
if closedCalls != 1 {
t.Errorf("Should have closed connection 1 time, instead of %v", closedCalls)
}
}