go-ssb-room/muxrpc/test/go/simple_test.go

204 lines
5.2 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
//
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
"github.com/ssbc/go-muxrpc/v2"
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-04-22 14:19:53 +00:00
refs "github.com/ssbc/go-ssb-refs"
tunserv "github.com/ssbc/go-ssb-room/v2/muxrpc/handlers/tunnel/server"
"github.com/ssbc/go-ssb-room/v2/roomdb"
2021-01-25 12:23:03 +00:00
)
2021-01-26 17:33:29 +00:00
func TestTunnelServerSimple(t *testing.T) {
2021-04-22 14:19:53 +00:00
testInit(t)
2021-01-26 17:33:29 +00:00
// 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
)
2021-04-20 10:14:24 +00:00
serv := theBots[indexSrv].srv
botA := theBots[indexA].srv
botB := theBots[indexB].srv
2021-01-25 15:35:22 +00:00
// only allow A
2021-04-20 10:14:24 +00:00
theBots[indexSrv].srv.Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
2021-01-25 15:35:22 +00:00
// allow bots to dial the remote
2021-04-20 10:14:24 +00:00
theBots[indexA].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
theBots[indexB].srv.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
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())
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.String())
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-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.String())
a.True(serv.Whoami().Equal(srvWho.ID))
2021-01-25 17:31:09 +00:00
// start testing basic room stuff
var meta tunserv.MetadataReply
err = endpointA.Async(ctx, &meta, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "isRoom"})
2021-01-25 17:31:09 +00:00
r.NoError(err)
r.Equal("srv", meta.Name)
r.True(meta.Membership, "not a member?")
2021-01-25 17:31:09 +00:00
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
)
2021-04-20 10:14:24 +00:00
serv := theBots[indexSrv].srv
botA := theBots[indexA].srv
botB := theBots[indexB].srv
2021-01-26 17:33:29 +00:00
2021-03-19 10:51:06 +00:00
// allow both clients
2021-04-20 10:14:24 +00:00
theBots[indexSrv].srv.Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
theBots[indexSrv].srv.Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
2021-01-26 17:33:29 +00:00
// allow bots to dial the remote
2021-04-20 10:14:24 +00:00
theBots[indexA].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
theBots[indexB].srv.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-01-26 17:33:29 +00:00
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))
2021-01-26 17:33:29 +00:00
// 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
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-04-21 06:28:36 +00:00
a.True(ret)
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-04-21 06:28:36 +00:00
a.True(ret)
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
}