refactor testSession setup
This commit is contained in:
parent
a872ddf2c0
commit
51ad16ccd5
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.cryptoscope.co/muxrpc/v2"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/ssb-ngi-pointer/go-ssb-room/internal/aliases"
|
||||
"github.com/ssb-ngi-pointer/go-ssb-room/internal/maybemod/keys"
|
||||
|
@ -26,8 +25,6 @@ import (
|
|||
func TestAliasRegister(t *testing.T) {
|
||||
testInit(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
botgroup, ctx := errgroup.WithContext(ctx)
|
||||
bs := newBotServer(ctx, mainLog)
|
||||
|
||||
r := require.New(t)
|
||||
a := assert.New(t)
|
||||
|
@ -41,55 +38,45 @@ func TestAliasRegister(t *testing.T) {
|
|||
roomsrv.WithContext(ctx),
|
||||
}
|
||||
|
||||
theBots := []*roomsrv.Server{}
|
||||
theBots := []*testSession{}
|
||||
|
||||
srvMembers, serv := makeNamedTestBot(t, "srv", netOpts)
|
||||
botgroup.Go(bs.Serve(serv))
|
||||
theBots = append(theBots, serv)
|
||||
session := makeNamedTestBot(t, "srv", ctx, netOpts)
|
||||
theBots = append(theBots, session)
|
||||
|
||||
// we need bobs key to create the signature
|
||||
bobsKey, err := keys.NewKeyPair(nil)
|
||||
r.NoError(err)
|
||||
|
||||
bobsMembers, bob := makeNamedTestBot(t, "bob", append(netOpts,
|
||||
bobSession := makeNamedTestBot(t, "bob", ctx, append(netOpts,
|
||||
roomsrv.WithKeyPair(bobsKey),
|
||||
))
|
||||
botgroup.Go(bs.Serve(bob))
|
||||
theBots = append(theBots, bob)
|
||||
|
||||
t.Cleanup(func() {
|
||||
for _, bot := range theBots {
|
||||
bot.Shutdown()
|
||||
r.NoError(bot.Close())
|
||||
}
|
||||
r.NoError(botgroup.Wait())
|
||||
})
|
||||
theBots = append(theBots, bobSession)
|
||||
|
||||
// adds
|
||||
_, err = srvMembers.Add(ctx, bob.Whoami(), roomdb.RoleMember)
|
||||
_, err = session.srv.Members.Add(ctx, bobSession.srv.Whoami(), roomdb.RoleMember)
|
||||
r.NoError(err)
|
||||
|
||||
// allow bots to dial the remote
|
||||
// side-effect of re-using a room-server as the client
|
||||
_, err = bobsMembers.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
_, err = bobSession.srv.Members.Add(ctx, session.srv.Whoami(), roomdb.RoleMember)
|
||||
r.NoError(err)
|
||||
|
||||
// should work (we allowed A)
|
||||
err = bob.Network.Connect(ctx, serv.Network.GetListenAddr())
|
||||
err = bobSession.srv.Network.Connect(ctx, session.srv.Network.GetListenAddr())
|
||||
r.NoError(err, "connect A to the Server")
|
||||
|
||||
t.Log("letting handshaking settle..")
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
clientForServer, ok := bob.Network.GetEndpointFor(serv.Whoami())
|
||||
clientForServer, ok := bobSession.srv.Network.GetEndpointFor(session.srv.Whoami())
|
||||
r.True(ok)
|
||||
|
||||
t.Log("got endpoint")
|
||||
|
||||
var testReg aliases.Registration
|
||||
testReg.Alias = "bob"
|
||||
testReg.RoomID = serv.Whoami()
|
||||
testReg.UserID = bob.Whoami()
|
||||
testReg.RoomID = session.srv.Whoami()
|
||||
testReg.UserID = bobSession.srv.Whoami()
|
||||
|
||||
confirmation := testReg.Sign(bobsKey.Pair.Secret)
|
||||
t.Logf("signature created: %x...", confirmation.Signature[:16])
|
||||
|
@ -109,7 +96,7 @@ func TestAliasRegister(t *testing.T) {
|
|||
a.Equal("", resolveURL.Path)
|
||||
|
||||
// server should have the alias now
|
||||
alias, err := serv.Aliases.Resolve(ctx, "bob")
|
||||
alias, err := session.srv.Aliases.Resolve(ctx, "bob")
|
||||
r.NoError(err)
|
||||
|
||||
a.Equal(confirmation.Alias, alias.Name)
|
||||
|
@ -118,5 +105,10 @@ func TestAliasRegister(t *testing.T) {
|
|||
|
||||
t.Log("alias stored")
|
||||
|
||||
for _, bot := range theBots {
|
||||
bot.srv.Shutdown()
|
||||
r.NoError(bot.srv.Close())
|
||||
r.NoError(bot.serveGroup.Wait())
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
|
|
@ -32,21 +32,21 @@ func TestConnEstablishmentDeniedKey(t *testing.T) {
|
|||
indexB
|
||||
)
|
||||
|
||||
serv := theBots[indexSrv].Server
|
||||
botA := theBots[indexA].Server
|
||||
botB := theBots[indexB].Server
|
||||
serv := theBots[indexSrv].srv
|
||||
botA := theBots[indexA].srv
|
||||
botB := theBots[indexB].srv
|
||||
|
||||
// allow A, deny B
|
||||
theBots[indexSrv].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexSrv].srv.Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
||||
// since we want to verify denied keys in particular, let us both:
|
||||
// a) add B as a member
|
||||
theBots[indexSrv].Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexSrv].srv.Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
|
||||
// b) ban B by adding them to the DeniedKeys database
|
||||
theBots[indexSrv].Server.DeniedKeys.Add(ctx, botB.Whoami(), "rude")
|
||||
theBots[indexSrv].srv.DeniedKeys.Add(ctx, botB.Whoami(), "rude")
|
||||
|
||||
// hack: allow bots to dial the server
|
||||
theBots[indexA].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexB].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexA].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexB].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
|
||||
// dial up B->A and C->A
|
||||
// should work (we allowed A)
|
||||
|
|
|
@ -29,16 +29,16 @@ func TestTunnelServerSimple(t *testing.T) {
|
|||
indexB
|
||||
)
|
||||
|
||||
serv := theBots[indexSrv].Server
|
||||
botA := theBots[indexA].Server
|
||||
botB := theBots[indexB].Server
|
||||
serv := theBots[indexSrv].srv
|
||||
botA := theBots[indexA].srv
|
||||
botB := theBots[indexB].srv
|
||||
|
||||
// only allow A
|
||||
theBots[indexSrv].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexSrv].srv.Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
||||
|
||||
// allow bots to dial the remote
|
||||
theBots[indexA].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexB].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexA].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexB].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
|
||||
// dial up B->A and C->A
|
||||
|
||||
|
@ -105,17 +105,17 @@ func TestRoomAnnounce(t *testing.T) {
|
|||
indexB
|
||||
)
|
||||
|
||||
serv := theBots[indexSrv].Server
|
||||
botA := theBots[indexA].Server
|
||||
botB := theBots[indexB].Server
|
||||
serv := theBots[indexSrv].srv
|
||||
botA := theBots[indexA].srv
|
||||
botB := theBots[indexB].srv
|
||||
|
||||
// allow both clients
|
||||
theBots[indexSrv].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexSrv].Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexSrv].srv.Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexSrv].srv.Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)
|
||||
|
||||
// allow bots to dial the remote
|
||||
theBots[indexA].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexB].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexA].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
theBots[indexB].srv.Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
|
||||
|
||||
// should work (we allowed A)
|
||||
err := botA.Network.Connect(ctx, serv.Network.GetListenAddr())
|
||||
|
|
|
@ -71,7 +71,16 @@ func (bs botServer) Serve(s *roomsrv.Server) func() error {
|
|||
}
|
||||
}
|
||||
|
||||
func makeNamedTestBot(t testing.TB, name string, opts []roomsrv.Option) (roomdb.MembersService, *roomsrv.Server) {
|
||||
type testSession struct {
|
||||
t testing.TB
|
||||
|
||||
srv *roomsrv.Server
|
||||
|
||||
ctx context.Context
|
||||
serveGroup *errgroup.Group
|
||||
}
|
||||
|
||||
func makeNamedTestBot(t testing.TB, name string, ctx context.Context, opts []roomsrv.Option) *testSession {
|
||||
r := require.New(t)
|
||||
testPath := filepath.Join("testrun", t.Name(), "bot-"+name)
|
||||
os.RemoveAll(testPath)
|
||||
|
@ -108,22 +117,26 @@ func makeNamedTestBot(t testing.TB, name string, opts []roomsrv.Option) (roomdb.
|
|||
sb := signinwithssb.NewSignalBridge()
|
||||
theBot, err := roomsrv.New(db.Members, db.DeniedKeys, db.Aliases, db.AuthWithSSB, sb, db.Config, netInfo, botOptions...)
|
||||
r.NoError(err)
|
||||
return db.Members, theBot
|
||||
|
||||
ts := testSession{
|
||||
t: t,
|
||||
srv: theBot,
|
||||
}
|
||||
|
||||
ts.serveGroup, ts.ctx = errgroup.WithContext(ctx)
|
||||
|
||||
ts.serveGroup.Go(func() error {
|
||||
return theBot.Network.Serve(ts.ctx)
|
||||
})
|
||||
|
||||
return &ts
|
||||
}
|
||||
|
||||
type testBot struct {
|
||||
Server *roomsrv.Server
|
||||
Members roomdb.MembersService
|
||||
}
|
||||
|
||||
func createServerAndBots(t *testing.T, ctx context.Context, count uint) []testBot {
|
||||
// TODO: refactor for single test session and use makeTestClient()
|
||||
func createServerAndBots(t *testing.T, ctx context.Context, count uint) []*testSession {
|
||||
testInit(t)
|
||||
r := require.New(t)
|
||||
|
||||
botgroup, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
bs := newBotServer(ctx, mainLog)
|
||||
|
||||
appKey := make([]byte, 32)
|
||||
rand.Read(appKey)
|
||||
|
||||
|
@ -131,31 +144,25 @@ func createServerAndBots(t *testing.T, ctx context.Context, count uint) []testBo
|
|||
roomsrv.WithAppKey(appKey),
|
||||
roomsrv.WithContext(ctx),
|
||||
}
|
||||
theBots := []testBot{}
|
||||
|
||||
srvsMembers, serv := makeNamedTestBot(t, "srv", netOpts)
|
||||
botgroup.Go(bs.Serve(serv))
|
||||
theBots = append(theBots, testBot{
|
||||
Server: serv,
|
||||
Members: srvsMembers,
|
||||
})
|
||||
theBots := []*testSession{}
|
||||
|
||||
session := makeNamedTestBot(t, "srv", ctx, netOpts)
|
||||
|
||||
theBots = append(theBots, session)
|
||||
|
||||
for i := uint(1); i < count+1; i++ {
|
||||
botMembers, botSrv := makeNamedTestBot(t, fmt.Sprintf("%d", i), netOpts)
|
||||
botgroup.Go(bs.Serve(botSrv))
|
||||
theBots = append(theBots, testBot{
|
||||
Server: botSrv,
|
||||
Members: botMembers,
|
||||
})
|
||||
// TODO: replace with makeClient?!
|
||||
clientSession := makeNamedTestBot(t, fmt.Sprintf("%d", i), ctx, netOpts)
|
||||
theBots = append(theBots, clientSession)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
for _, bot := range theBots {
|
||||
bot.Server.Shutdown()
|
||||
r.NoError(bot.Server.Close())
|
||||
bot.srv.Shutdown()
|
||||
r.NoError(bot.srv.Close())
|
||||
}
|
||||
r.NoError(botgroup.Wait())
|
||||
})
|
||||
|
||||
return theBots
|
||||
|
|
|
@ -36,7 +36,8 @@ func TestWebsocketDialing(t *testing.T) {
|
|||
t.Cleanup(cancel)
|
||||
|
||||
// create the roomsrv
|
||||
serverMembers, server := makeNamedTestBot(t, "server", nil)
|
||||
session := makeNamedTestBot(t, "server", ctx, nil)
|
||||
server := session.srv
|
||||
|
||||
// open a TCP listener for HTTP
|
||||
l, err := net.Listen("tcp4", "localhost:0")
|
||||
|
@ -53,7 +54,7 @@ func TestWebsocketDialing(t *testing.T) {
|
|||
r.NoError(err)
|
||||
|
||||
// add it as a memeber
|
||||
memberID, err := serverMembers.Add(ctx, client.Feed, roomdb.RoleMember)
|
||||
memberID, err := server.Members.Add(ctx, client.Feed, roomdb.RoleMember)
|
||||
r.NoError(err)
|
||||
t.Log("client member:", memberID)
|
||||
|
||||
|
|
Loading…
Reference in New Issue