2021-10-08 12:39:31 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
|
|
|
|
//
|
2021-05-17 14:05:51 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-04-20 10:14:35 +00:00
|
|
|
package go_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-04-20 10:14:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.cryptoscope.co/muxrpc/v2"
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
refs "go.mindeco.de/ssb-refs"
|
2021-04-20 10:14:35 +00:00
|
|
|
)
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
type announcements map[string]struct{}
|
|
|
|
|
2021-04-20 10:14:35 +00:00
|
|
|
// we will let three clients (alf, bre, crl) join and see that the endpoint output is as expected
|
|
|
|
func TestEndpointClients(t *testing.T) {
|
2021-05-17 14:05:51 +00:00
|
|
|
testInit(t)
|
|
|
|
|
2021-04-20 10:14:35 +00:00
|
|
|
r := require.New(t)
|
2021-04-20 15:02:28 +00:00
|
|
|
a := assert.New(t)
|
2021-04-20 10:14:35 +00:00
|
|
|
|
|
|
|
testPath := filepath.Join("testrun", t.Name())
|
|
|
|
os.RemoveAll(testPath)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
// create the roomsrv
|
|
|
|
ts := makeNamedTestBot(t, "server", ctx, nil)
|
|
|
|
ctx = ts.ctx
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// create three test clients
|
2021-04-22 10:13:34 +00:00
|
|
|
alf := ts.makeTestClient("alf")
|
|
|
|
bre := ts.makeTestClient("bre")
|
|
|
|
carl := ts.makeTestClient("carl")
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// let carl join the room
|
|
|
|
// carl wont announce to emulate manyverse
|
2021-05-17 14:05:51 +00:00
|
|
|
carlEndpointsSrc, err := carl.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
|
2021-04-20 15:02:28 +00:00
|
|
|
r.NoError(err)
|
|
|
|
t.Log("carl opened endpoints")
|
|
|
|
|
|
|
|
announcementsForCarl := make(announcements)
|
2021-05-17 14:05:51 +00:00
|
|
|
go logEndpointsStream(ts, carlEndpointsSrc, "carl", announcementsForCarl)
|
2021-04-20 15:02:28 +00:00
|
|
|
time.Sleep(1 * time.Second) // give some time to process new events
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen := announcementsForCarl[carl.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "carl saw himself")
|
|
|
|
|
|
|
|
// let alf join the room
|
2021-04-20 10:14:35 +00:00
|
|
|
alfEndpointsSerc, err := alf.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
|
|
|
|
r.NoError(err)
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
announcementsForAlf := make(announcements)
|
2021-04-22 08:19:03 +00:00
|
|
|
go logEndpointsStream(ts, alfEndpointsSerc, "alf", announcementsForAlf)
|
2021-04-20 15:02:28 +00:00
|
|
|
time.Sleep(1 * time.Second) // give some time to process new events
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// assert what alf saw
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForAlf[carl.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "alf saw carl")
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForAlf[alf.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "alf saw himself")
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// assert what carl saw
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForCarl[alf.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "carl saw alf")
|
2021-04-20 10:14:35 +00:00
|
|
|
|
|
|
|
// let bre join the room
|
|
|
|
breEndpointsSrc, err := bre.Source(ctx, muxrpc.TypeJSON, muxrpc.Method{"tunnel", "endpoints"})
|
|
|
|
r.NoError(err)
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
announcementsForBre := make(announcements)
|
2021-04-22 08:19:03 +00:00
|
|
|
go logEndpointsStream(ts, breEndpointsSrc, "bre", announcementsForBre)
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
time.Sleep(1 * time.Second) // give some time to process new events
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// assert bre saw the other two and herself
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForBre[carl.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "bre saw carl")
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForBre[alf.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "bre saw alf")
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForBre[bre.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "bre saw herself")
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// assert the others saw bre
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForAlf[bre.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "alf saw bre")
|
2021-04-22 10:13:34 +00:00
|
|
|
_, seen = announcementsForCarl[bre.feed.Ref()]
|
2021-04-20 15:02:28 +00:00
|
|
|
a.True(seen, "carl saw bre")
|
2021-04-20 10:14:35 +00:00
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
// terminate server and the clients
|
|
|
|
ts.srv.Shutdown()
|
2021-04-20 10:14:35 +00:00
|
|
|
alf.Terminate()
|
|
|
|
bre.Terminate()
|
|
|
|
carl.Terminate()
|
2021-04-20 15:02:28 +00:00
|
|
|
ts.srv.Close()
|
2021-04-20 10:14:35 +00:00
|
|
|
|
|
|
|
// wait for all muxrpc serve()s to exit
|
|
|
|
r.NoError(ts.serveGroup.Wait())
|
2021-04-20 15:02:28 +00:00
|
|
|
cancel()
|
2021-04-20 10:14:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 08:19:03 +00:00
|
|
|
// consume endpoint messaes and put each peer on the passed map
|
|
|
|
func logEndpointsStream(ts *testSession, src *muxrpc.ByteSource, who string, a announcements) {
|
2021-04-20 15:02:28 +00:00
|
|
|
var edps []refs.FeedRef
|
2021-04-20 10:14:35 +00:00
|
|
|
|
|
|
|
for src.Next(ts.ctx) {
|
|
|
|
body, err := src.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// ts.t.Log(who, "got body:", string(body))
|
|
|
|
|
2021-04-20 15:02:28 +00:00
|
|
|
err = json.Unmarshal(body, &edps)
|
2021-04-20 10:14:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-20 15:02:28 +00:00
|
|
|
ts.t.Log(who, "got endpoints:", len(edps))
|
|
|
|
for i, f := range edps {
|
|
|
|
ts.t.Log(who, ":", i, f.ShortRef())
|
|
|
|
|
|
|
|
// mark as f is present
|
|
|
|
a[f.Ref()] = struct{}{}
|
|
|
|
}
|
2021-04-20 10:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := src.Err(); err != nil {
|
|
|
|
ts.t.Log("source errored: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.t.Log(who, "stream closed")
|
|
|
|
}
|