2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-01-25 12:23:03 +00:00
|
|
|
package go_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2021-01-25 15:35:22 +00:00
|
|
|
"time"
|
2021-01-25 12:23:03 +00:00
|
|
|
|
2021-03-19 10:51:06 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
|
|
|
|
|
2021-01-25 15:35:22 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-01-25 12:23:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-25 15:35:22 +00:00
|
|
|
"go.cryptoscope.co/muxrpc/v2"
|
2021-02-11 15:43:19 +00:00
|
|
|
refs "go.mindeco.de/ssb-refs"
|
2021-01-25 12:23:03 +00:00
|
|
|
)
|
|
|
|
|
2021-01-26 17:33:29 +00:00
|
|
|
func TestTunnelServerSimple(t *testing.T) {
|
|
|
|
// defer leakcheck.Check(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
theBots := createServerAndBots(t, ctx, 2)
|
|
|
|
|
|
|
|
r := require.New(t)
|
|
|
|
a := assert.New(t)
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
const (
|
|
|
|
indexSrv = iota
|
|
|
|
indexA
|
|
|
|
indexB
|
|
|
|
)
|
|
|
|
|
|
|
|
serv := theBots[indexSrv].Server
|
|
|
|
botA := theBots[indexA].Server
|
|
|
|
botB := theBots[indexB].Server
|
2021-01-25 15:35:22 +00:00
|
|
|
|
2021-03-19 10:51:06 +00:00
|
|
|
// allow both clients
|
2021-04-12 13:00:34 +00:00
|
|
|
theBots[indexSrv].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
|
|
|
theBots[indexSrv].Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
|
2021-01-25 15:35:22 +00:00
|
|
|
|
|
|
|
// allow bots to dial the remote
|
2021-04-12 13:00:34 +00:00
|
|
|
theBots[indexA].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
|
|
|
theBots[indexB].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
2021-01-25 12:23:03 +00:00
|
|
|
|
2021-01-25 15:35:22 +00:00
|
|
|
// dial up B->A and C->A
|
2021-01-25 12:23:03 +00:00
|
|
|
|
2021-01-25 15:35:22 +00:00
|
|
|
// should work (we allowed A)
|
|
|
|
err := botA.Network.Connect(ctx, serv.Network.GetListenAddr())
|
|
|
|
r.NoError(err, "connect A to the Server")
|
2021-01-25 12:23:03 +00:00
|
|
|
|
2021-01-25 15:35:22 +00:00
|
|
|
// shouldn't work (we did not allowed A)
|
|
|
|
err = botB.Network.Connect(ctx, serv.Network.GetListenAddr())
|
|
|
|
r.NoError(err, "connect B to the Server") // we dont see an error because it just establishes the tcp connection
|
|
|
|
|
2021-01-25 17:31:09 +00:00
|
|
|
// t.Log("letting handshaking settle..")
|
|
|
|
// time.Sleep(1 * time.Second)
|
2021-01-25 15:35:22 +00:00
|
|
|
|
|
|
|
var srvWho struct {
|
|
|
|
ID refs.FeedRef
|
|
|
|
}
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
endpointB, has := botB.Network.GetEndpointFor(serv.Whoami())
|
2021-01-25 15:35:22 +00:00
|
|
|
r.False(has, "botB has an endpoint for the server!")
|
2021-04-12 13:00:34 +00:00
|
|
|
if endpointB != nil {
|
|
|
|
a.Nil(endpointB, "should not have an endpoint on B")
|
|
|
|
err = endpointB.Async(ctx, &srvWho, muxrpc.TypeJSON, muxrpc.Method{"whoami"})
|
2021-01-25 15:35:22 +00:00
|
|
|
r.Error(err)
|
|
|
|
t.Log(srvWho.ID.Ref())
|
|
|
|
}
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
endpointA, has := botA.Network.GetEndpointFor(serv.Whoami())
|
2021-01-25 17:31:09 +00:00
|
|
|
r.True(has, "botA has no endpoint for the server")
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointA.Async(ctx, &srvWho, muxrpc.TypeJSON, muxrpc.Method{"whoami"})
|
2021-01-25 17:31:09 +00:00
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
t.Log("server whoami:", srvWho.ID.Ref())
|
|
|
|
a.True(serv.Whoami().Equal(&srvWho.ID))
|
|
|
|
|
|
|
|
// start testing basic room stuff
|
|
|
|
var yes bool
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointA.Async(ctx, &yes, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "isRoom"})
|
2021-01-25 17:31:09 +00:00
|
|
|
r.NoError(err)
|
|
|
|
a.True(yes, "srv is not a room?!")
|
|
|
|
|
|
|
|
var ts int
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointA.Async(ctx, &ts, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "ping"})
|
2021-01-25 17:31:09 +00:00
|
|
|
r.NoError(err)
|
|
|
|
t.Log("ping:", ts)
|
|
|
|
|
2021-01-25 15:35:22 +00:00
|
|
|
// cleanup
|
|
|
|
cancel()
|
2021-01-26 17:33:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoomAnnounce(t *testing.T) {
|
|
|
|
// defer leakcheck.Check(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
theBots := createServerAndBots(t, ctx, 2)
|
|
|
|
|
|
|
|
r := require.New(t)
|
|
|
|
a := assert.New(t)
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
const (
|
|
|
|
indexSrv = iota
|
|
|
|
indexA
|
|
|
|
indexB
|
|
|
|
)
|
|
|
|
|
|
|
|
serv := theBots[indexSrv].Server
|
|
|
|
botA := theBots[indexA].Server
|
|
|
|
botB := theBots[indexB].Server
|
2021-01-26 17:33:29 +00:00
|
|
|
|
2021-03-19 10:51:06 +00:00
|
|
|
// allow both clients
|
2021-04-12 13:00:34 +00:00
|
|
|
theBots[indexSrv].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
|
|
|
theBots[indexSrv].Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
|
2021-01-26 17:33:29 +00:00
|
|
|
|
|
|
|
// allow bots to dial the remote
|
2021-04-12 13:00:34 +00:00
|
|
|
theBots[indexA].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
|
|
|
theBots[indexB].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
2021-01-26 17:33:29 +00:00
|
|
|
|
|
|
|
// should work (we allowed A)
|
|
|
|
err := botA.Network.Connect(ctx, serv.Network.GetListenAddr())
|
|
|
|
r.NoError(err, "connect A to the Server")
|
|
|
|
|
|
|
|
// shouldn't work (we did not allowed A)
|
|
|
|
err = botB.Network.Connect(ctx, serv.Network.GetListenAddr())
|
|
|
|
r.NoError(err, "connect B to the Server") // we dont see an error because it just establishes the tcp connection
|
|
|
|
|
|
|
|
t.Log("letting handshaking settle..")
|
2021-01-25 15:35:22 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2021-01-26 17:33:29 +00:00
|
|
|
|
|
|
|
var srvWho struct {
|
|
|
|
ID refs.FeedRef
|
2021-01-25 15:35:22 +00:00
|
|
|
}
|
2021-04-12 13:00:34 +00:00
|
|
|
endpointA, has := botA.Network.GetEndpointFor(serv.Whoami())
|
2021-01-26 17:33:29 +00:00
|
|
|
r.True(has, "botA has no endpoint for the server")
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
endpointB, has := botB.Network.GetEndpointFor(serv.Whoami())
|
2021-01-26 17:33:29 +00:00
|
|
|
r.True(has, "botB has no endpoint for the server!")
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointA.Async(ctx, &srvWho, muxrpc.TypeJSON, muxrpc.Method{"whoami"})
|
2021-01-26 17:33:29 +00:00
|
|
|
r.NoError(err)
|
|
|
|
a.True(serv.Whoami().Equal(&srvWho.ID))
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointB.Async(ctx, &srvWho, muxrpc.TypeJSON, muxrpc.Method{"whoami"})
|
2021-01-26 17:33:29 +00:00
|
|
|
r.NoError(err)
|
|
|
|
a.True(serv.Whoami().Equal(&srvWho.ID))
|
|
|
|
|
|
|
|
// let B listen for changes
|
2021-04-12 13:00:34 +00:00
|
|
|
newRoomMember, err := endpointB.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
|
2021-01-26 17:33:29 +00:00
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
newMemberChan := make(chan string)
|
|
|
|
|
|
|
|
// read all the messages from endpoints and throw them over the channel
|
|
|
|
go func() {
|
|
|
|
for newRoomMember.Next(ctx) {
|
|
|
|
|
|
|
|
body, err := newRoomMember.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newMemberChan <- string(body)
|
|
|
|
}
|
|
|
|
close(newMemberChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// announce A
|
2021-01-27 10:41:08 +00:00
|
|
|
var ret bool
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointA.Async(ctx, &ret, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "announce"})
|
2021-01-26 17:33:29 +00:00
|
|
|
r.NoError(err)
|
2021-01-27 10:41:08 +00:00
|
|
|
a.False(ret) // <ascii-shrugg>
|
2021-01-26 17:33:29 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Error("timeout")
|
|
|
|
case got := <-newMemberChan:
|
|
|
|
t.Log("received join?")
|
|
|
|
t.Log(got)
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2021-04-12 13:00:34 +00:00
|
|
|
err = endpointA.Async(ctx, &ret, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "leave"})
|
2021-01-26 17:33:29 +00:00
|
|
|
r.NoError(err)
|
2021-01-27 10:41:08 +00:00
|
|
|
a.False(ret) // <ascii-shrugg>
|
2021-01-26 17:33:29 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Error("timeout")
|
|
|
|
case got := <-newMemberChan:
|
|
|
|
t.Log("received leave?")
|
|
|
|
t.Log(got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
cancel()
|
2021-01-25 12:23:03 +00:00
|
|
|
}
|