2018-03-10 21:26:19 +00:00
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
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/identity"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
2018-03-10 21:26:19 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-03-14 22:23:35 +00:00
|
|
|
func ServerAuthValid(string, rsa.PublicKey) (allowed, known bool) {
|
2018-03-10 21:26:19 +00:00
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestServer struct {
|
2018-03-10 22:05:48 +00:00
|
|
|
connection.AutoConnectionHandler
|
|
|
|
Received bool
|
2018-03-10 21:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TestServer) HandleGroupMessage(gm *protocol.GroupMessage) {
|
2018-03-10 22:05:48 +00:00
|
|
|
ts.Received = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TestServer) HandleFetchRequest() []*protocol.GroupMessage {
|
2018-06-22 18:11:23 +00:00
|
|
|
return []*protocol.GroupMessage{{Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}, {Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}}
|
2018-03-10 21:26:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 22:37:44 +00:00
|
|
|
func runtestserver(t *testing.T, ts *TestServer, privateKey *rsa.PrivateKey) {
|
2018-03-10 21:26:19 +00:00
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:5451")
|
|
|
|
conn, _ := ln.Accept()
|
|
|
|
defer conn.Close()
|
2018-05-25 22:37:44 +00:00
|
|
|
|
2018-03-10 21:26:19 +00:00
|
|
|
rc, err := goricochet.NegotiateVersionInbound(conn)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Negotiate Version Error: %v", err)
|
|
|
|
}
|
2018-03-10 22:05:48 +00:00
|
|
|
rc.TraceLog(true)
|
2018-03-10 21:26:19 +00:00
|
|
|
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 {
|
2018-03-10 22:05:48 +00:00
|
|
|
server := new(send.CwtchServerSendChannel)
|
|
|
|
server.Handler = ts
|
|
|
|
return server
|
2018-03-10 21:26:19 +00:00
|
|
|
})
|
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)
|
2018-03-10 21:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerServerConnection(t *testing.T) {
|
2018-05-25 22:37:44 +00:00
|
|
|
privateKey, err := utils.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Private Key Error %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-10 21:26:19 +00:00
|
|
|
ts := new(TestServer)
|
|
|
|
ts.Init()
|
2018-05-25 22:37:44 +00:00
|
|
|
go runtestserver(t, ts, privateKey)
|
|
|
|
onionAddr, err := utils.GetOnionAddress(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting onion address: %v", err)
|
|
|
|
}
|
|
|
|
psc := NewPeerServerConnection("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
|
|
|
}
|
2018-03-10 21:26:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-06-22 18:11:23 +00:00
|
|
|
gm := &protocol.GroupMessage{Ciphertext: []byte("hello"), Signature: []byte{}}
|
2018-03-10 21:26:19 +00:00
|
|
|
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 21:26:19 +00:00
|
|
|
}
|
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 21:26:19 +00:00
|
|
|
}
|