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

96 lines
2.2 KiB
Go
Raw Normal View History

2021-01-25 12:23:03 +00:00
package go_test
import (
"context"
"crypto/rand"
"testing"
2021-01-25 15:35:22 +00:00
"time"
2021-01-25 12:23:03 +00:00
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-01-25 12:23:03 +00:00
"golang.org/x/sync/errgroup"
2021-01-25 15:35:22 +00:00
refs "go.mindeco.de/ssb-refs"
2021-01-25 12:23:03 +00:00
"go.mindeco.de/ssb-rooms/roomsrv"
)
func TestTunnelServerSimple(t *testing.T) {
r := require.New(t)
2021-01-25 15:35:22 +00:00
a := assert.New(t)
// defer leakcheck.Check(t)
testInit(t)
2021-01-25 12:23:03 +00:00
2021-01-25 15:35:22 +00:00
ctx, cancel := context.WithCancel(context.Background())
botgroup, ctx := errgroup.WithContext(ctx)
2021-01-25 12:23:03 +00:00
2021-01-25 15:35:22 +00:00
bs := newBotServer(ctx, mainLog)
2021-01-25 12:23:03 +00:00
appKey := make([]byte, 32)
rand.Read(appKey)
2021-01-25 15:35:22 +00:00
netOpts := []roomsrv.Option{
2021-01-25 12:23:03 +00:00
roomsrv.WithAppKey(appKey),
roomsrv.WithContext(ctx),
}
2021-01-25 15:35:22 +00:00
serv := makeNamedTestBot(t, "srv", netOpts)
botgroup.Go(bs.Serve(serv))
2021-01-25 12:23:03 +00:00
2021-01-25 15:35:22 +00:00
botA := makeNamedTestBot(t, "B", netOpts)
botgroup.Go(bs.Serve(botA))
2021-01-25 12:23:03 +00:00
2021-01-25 15:35:22 +00:00
botB := makeNamedTestBot(t, "C", netOpts)
botgroup.Go(bs.Serve(botB))
2021-01-25 12:23:03 +00:00
2021-01-25 15:35:22 +00:00
theBots := []*roomsrv.Server{serv, botA, botB}
// only allow B to dial A
serv.Allow(botA.Whoami(), true)
// allow bots to dial the remote
botA.Allow(serv.Whoami(), true)
botB.Allow(serv.Whoami(), true)
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
edpOfA, has := botA.Network.GetEndpointFor(serv.Whoami())
r.True(has, "botA has no endpoint for the server")
var srvWho struct {
ID refs.FeedRef
}
err = edpOfA.Async(ctx, &srvWho, muxrpc.TypeJSON, muxrpc.Method{"whoami"})
r.NoError(err)
t.Log("server whoami:", srvWho.ID.Ref())
a.True(serv.Whoami().Equal(&srvWho.ID))
edpOfB, has := botB.Network.GetEndpointFor(serv.Whoami())
r.False(has, "botB has an endpoint for the server!")
if edpOfB != nil {
t.Error("should not have an endpoint on B")
err = edpOfB.Async(ctx, &srvWho, muxrpc.TypeJSON, muxrpc.Method{"whoami"})
r.Error(err)
t.Log(srvWho.ID.Ref())
}
// cleanup
cancel()
time.Sleep(1 * time.Second)
for _, bot := range theBots {
bot.Shutdown()
r.NoError(bot.Close())
}
r.NoError(botgroup.Wait())
2021-01-25 12:23:03 +00:00
}