2018-03-11 18:49:10 +00:00
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
2018-03-14 22:03:53 +00:00
|
|
|
"git.mascherari.press/cwtch/model"
|
|
|
|
"git.mascherari.press/cwtch/peer/peer"
|
|
|
|
"git.mascherari.press/cwtch/protocol"
|
2018-03-11 18:49:10 +00:00
|
|
|
"github.com/s-rah/go-ricochet"
|
2018-03-14 22:03:53 +00:00
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
2018-03-11 18:49:10 +00:00
|
|
|
"github.com/s-rah/go-ricochet/connection"
|
|
|
|
"github.com/s-rah/go-ricochet/identity"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-03-14 22:23:35 +00:00
|
|
|
func PeerAuthValid(string, rsa.PublicKey) (allowed, known bool) {
|
2018-03-11 18:49:10 +00:00
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
2018-05-25 22:37:44 +00:00
|
|
|
func runtestpeer(t *testing.T, tp *TestPeer, privateKey *rsa.PrivateKey) {
|
2018-03-11 18:49:10 +00:00
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:5452")
|
|
|
|
conn, _ := ln.Accept()
|
|
|
|
defer conn.Close()
|
2018-05-25 22:37:44 +00:00
|
|
|
|
2018-03-11 18:49:10 +00:00
|
|
|
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 {
|
2018-03-14 22:23:35 +00:00
|
|
|
cpc := new(peer.CwtchPeerChannel)
|
|
|
|
cpc.Handler = tp
|
|
|
|
return cpc
|
2018-03-11 19:05:35 +00:00
|
|
|
})
|
|
|
|
|
2018-03-14 22:03:53 +00:00
|
|
|
go func() {
|
|
|
|
alice := model.GenerateNewProfile("alice")
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
rc.Do(func() error {
|
|
|
|
channel := rc.Channel("im.cwtch.peer", channels.Inbound)
|
|
|
|
if channel != nil {
|
|
|
|
peerchannel, ok := channel.Handler.(*peer.CwtchPeerChannel)
|
|
|
|
if ok {
|
|
|
|
peerchannel.SendMessage(alice.GetCwtchIdentityPacket())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2018-03-11 19:05:35 +00:00
|
|
|
rc.Process(tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestPeer struct {
|
2018-03-14 22:03:53 +00:00
|
|
|
connection.AutoConnectionHandler
|
|
|
|
ReceivedIdentityPacket bool
|
|
|
|
ReceivedGroupInvite bool
|
2018-03-11 19:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *TestPeer) ClientIdentity(ci *protocol.CwtchIdentity) {
|
2018-03-14 22:03:53 +00:00
|
|
|
tp.ReceivedIdentityPacket = true
|
2018-03-11 19:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 18:43:51 +00:00
|
|
|
func (tp *TestPeer) HandleGroupInvite(gci *protocol.GroupChatInvite) {
|
2018-03-14 22:03:53 +00:00
|
|
|
tp.ReceivedGroupInvite = true
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:44:45 +00:00
|
|
|
func (tp *TestPeer) GetClientIdentityPacket() []byte {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:49:10 +00:00
|
|
|
func TestPeerPeerConnection(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)
|
|
|
|
}
|
|
|
|
onionAddr, err := utils.GetOnionAddress(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Onion address error %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:03:53 +00:00
|
|
|
profile := model.GenerateNewProfile("sarah")
|
2018-05-25 22:37:44 +00:00
|
|
|
ppc := NewPeerPeerConnection("127.0.0.1:5452|" + onionAddr, profile)
|
2018-03-11 18:49:10 +00:00
|
|
|
//numcalls := 0
|
2018-03-11 19:05:35 +00:00
|
|
|
tp := new(TestPeer)
|
|
|
|
tp.Init()
|
2018-05-25 22:37:44 +00:00
|
|
|
go runtestpeer(t, tp, privateKey)
|
2018-03-11 18:49:10 +00:00
|
|
|
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)
|
2018-03-11 18:49:10 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-03-14 22:03:53 +00:00
|
|
|
|
2018-03-11 19:05:35 +00:00
|
|
|
if tp.ReceivedIdentityPacket == false {
|
2018-03-14 22:03:53 +00:00
|
|
|
t.Errorf("should have received an identity packet")
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|
2018-03-14 22:03:53 +00:00
|
|
|
|
2018-05-16 20:20:46 +00:00
|
|
|
_, invite, _ := profile.StartGroup("aaa.onion")
|
2018-03-14 22:03:53 +00:00
|
|
|
ppc.SendGroupInvite(invite)
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
if tp.ReceivedGroupInvite == false {
|
|
|
|
t.Errorf("should have received an group invite packet")
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|