diff --git a/handlers/tunnel/state.go b/handlers/tunnel/state.go index e80d6b5..9718026 100644 --- a/handlers/tunnel/state.go +++ b/handlers/tunnel/state.go @@ -25,7 +25,7 @@ type roomState struct { } func (rs *roomState) stateTicker(ctx context.Context) { - tick := time.NewTicker(1 * time.Second) + tick := time.NewTicker(10 * time.Second) for { select { case <-ctx.Done(): diff --git a/test/nodejs/announce_test.go b/test/nodejs/announce_test.go index a8d1b4e..5f82ea8 100644 --- a/test/nodejs/announce_test.go +++ b/test/nodejs/announce_test.go @@ -1,9 +1,12 @@ package nodejs_test import ( + "bytes" "context" + "io/ioutil" "net" "os" + "path/filepath" "testing" "time" @@ -16,21 +19,36 @@ import ( func TestJSClient(t *testing.T) { // defer leakcheck.Check(t) - // r := require.New(t) + r := require.New(t) ts := newRandomSession(t) // ts := newSession(t, nil) srv := ts.startGoServer() - alice := ts.startJSBot("./testscripts/simple_client.js", + alice := ts.startJSClient("alice", "./testscripts/simple_client.js", srv.Network.GetListenAddr(), srv.Whoami(), ) srv.Allow(alice, true) + var roomHandle bytes.Buffer + roomHandle.WriteString("tunnel:") + roomHandle.WriteString(srv.Whoami().Ref()) + roomHandle.WriteString(":") + roomHandle.WriteString(alice.Ref()) + // nasty tunnel~shs: hack + // roomHandle.WriteString("~shs:") + // roomHandle.WriteString(base64.StdEncoding.EncodeToString(srv.Whoami().ID)) + + // write the handle to the testrun folder of the bot + handleFile := filepath.Join("testrun", t.Name(), "bob", "endpoint_through_room.txt") + os.MkdirAll(filepath.Dir(handleFile), 0700) + err := ioutil.WriteFile(handleFile, roomHandle.Bytes(), 0700) + r.NoError(err) + time.Sleep(1500 * time.Millisecond) - bob := ts.startJSBot("./testscripts/simple_client.js", + bob := ts.startJSClient("bob", "./testscripts/simple_client_opening_tunnel.js", srv.Network.GetListenAddr(), srv.Whoami(), ) @@ -61,21 +79,38 @@ func TestJSServer(t *testing.T) { Port: port, } - bob := ts.startJSBot("./testscripts/simple_client.js", + // now connect our go client + client := ts.startGoServer() + client.Allow(*alice, true) + + var roomHandle bytes.Buffer + roomHandle.WriteString("tunnel:") + roomHandle.WriteString(alice.Ref()) + roomHandle.WriteString(":") + roomHandle.WriteString(client.Whoami().Ref()) + // nasty tunnel~shs: hack + // roomHandle.WriteString("~shs:") + // roomHandle.WriteString(base64.StdEncoding.EncodeToString(alice.ID)) + + // write the handle to the testrun folder of the bot + handleFile := filepath.Join("testrun", t.Name(), "bob", "endpoint_through_room.txt") + os.MkdirAll(filepath.Dir(handleFile), 0700) + err := ioutil.WriteFile(handleFile, roomHandle.Bytes(), 0700) + r.NoError(err) + + bob := ts.startJSClient("bob", "./testscripts/simple_client_opening_tunnel.js", aliceAddr, *alice, ) t.Log("started bob:", bob.Ref()) - // now connect our go client - client := ts.startGoServer() - client.Allow(*alice, true) + client.Allow(bob, true) // connect to alice aliceShsAddr := netwrap.WrapAddr(aliceAddr, secretstream.Addr{PubKey: alice.ID}) ctx, connCancel := context.WithCancel(context.TODO()) - err := client.Network.Connect(ctx, aliceShsAddr) + err = client.Network.Connect(ctx, aliceShsAddr) defer connCancel() r.NoError(err, "connect #1 failed") diff --git a/test/nodejs/sbot_client.js b/test/nodejs/sbot_client.js index 7e32808..f033ddf 100644 --- a/test/nodejs/sbot_client.js +++ b/test/nodejs/sbot_client.js @@ -60,7 +60,8 @@ tape(testName, function (t) { process.exit(0) } - const tempRepo = Path.join('testrun', testName) + const tempRepo = process.env['TEST_REPO'] + console.warn(tempRepo) const keys = loadOrCreateSync(Path.join(tempRepo, 'secret')) const opts = { allowPrivate: true, @@ -68,6 +69,18 @@ tape(testName, function (t) { keys: keys } + opts.connections = { + incoming: { + tunnel: [{scope: 'public', transform: 'shs'}], + }, + outgoing: { + net: [{transform: 'shs'}], + // ws: [{transform: 'shs'}], + tunnel: [{transform: 'shs'}], + }, + } + + if (testSHSappKey !== false) { opts.caps = opts.caps ? opts.caps : {} opts.caps.shs = testSHSappKey @@ -75,8 +88,8 @@ tape(testName, function (t) { const sbot = createSbot(opts) const alice = sbot.whoami() - - t.comment('sbot spawned, running before') + t.comment('client spawned. I am:' + alice.id) + console.log(alice.id) // tell go process who's incoming testSession.before(sbot, ready) }) diff --git a/test/nodejs/sbot_serv.js b/test/nodejs/sbot_serv.js index 0038d09..e0c61ec 100644 --- a/test/nodejs/sbot_serv.js +++ b/test/nodejs/sbot_serv.js @@ -14,11 +14,10 @@ if (testSHSappKey !== false) { // stackOpts = {appKey: require('ssb-caps').shs} stackOpts = {caps: {shs: testAppkey } } const createSbot = theStack(stackOpts) - .use(require('ssb-db')) - .use(require('ssb-master')) - .use(require('ssb-logging')) + .use(require('ssb-db')) .use(require('ssb-conn')) .use(require('ssb-room/tunnel/server')) + // .use(require('ssb-logging')) const testName = process.env['TEST_NAME'] const testPort = process.env['TEST_PORT'] @@ -31,8 +30,6 @@ tape(testName, function (t) { // t.comment("test timeout") // process.exit(1) // }, 50000) - - function exit() { // call this when you're done sbot.close() @@ -41,7 +38,8 @@ tape(testName, function (t) { t.end() } - const tempRepo = Path.join('testrun', testName) + const tempRepo = process.env['TEST_REPO'] + console.warn("my repo:", tempRepo) const keys = loadOrCreateSync(Path.join(tempRepo, 'secret')) const sbot = createSbot({ port: testPort, @@ -55,7 +53,7 @@ tape(testName, function (t) { t.comment("sbot spawned, running before") function ready() { - console.warn('ready!', alice.id) + t.comment('server spawned. I am:' + alice.id) console.log(alice.id) // tell go process who our pubkey } testSession.before(sbot, ready) diff --git a/test/nodejs/setup_test.go b/test/nodejs/setup_test.go index 0644e24..74556fb 100644 --- a/test/nodejs/setup_test.go +++ b/test/nodejs/setup_test.go @@ -127,33 +127,33 @@ func (ts *testSession) startGoServer(opts ...roomsrv.Option) *roomsrv.Server { var jsBotCnt = 0 -func (ts *testSession) startJSBot(testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef { - return ts.startJSBotWithName("", testScript, peerAddr, peerRef) -} - // returns the jsbots pubkey -func (ts *testSession) startJSBotWithName(name, testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef { +func (ts *testSession) startJSClient(name, testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef { ts.t.Log("starting client", name) r := require.New(ts.t) - cmd := exec.CommandContext(ts.ctx, "node", "./sbot_client.js") + cmd := exec.CommandContext(ts.ctx, "node", "../../../sbot_client.js") cmd.Stderr = os.Stderr - outrc, err := cmd.StdoutPipe() r.NoError(err) if name == "" { - name = fmt.Sprint(ts.t.Name(), jsBotCnt) + name = fmt.Sprintf("jsbot-%d", jsBotCnt) } jsBotCnt++ - // ts.gobot.Network.GetListenAddr() - // ts.gobot.Whoami() + // copy test scripts (maybe later with templates if we need to) + cmd.Dir = filepath.Join("testrun", ts.t.Name(), name) + os.MkdirAll(cmd.Dir, 0700) + err = exec.Command("cp", "-r", "testscripts", cmd.Dir).Run() + r.NoError(err) env := []string{ "TEST_NAME=" + name, + "TEST_REPO=" + cmd.Dir, "TEST_PEERADDR=" + netwrap.GetAddr(peerAddr, "tcp").String(), "TEST_PEERREF=" + peerRef.Ref(), "TEST_SESSIONSCRIPT=" + testScript, + "DEBUG=ssb:room:tunnel:*", } if ts.keySHS != nil { @@ -184,11 +184,9 @@ func (ts *testSession) startJSBotWithName(name, testScript string, peerAddr net. } func (ts *testSession) startJSBotAsServer(name, testScriptFileName string) (*refs.FeedRef, int) { - ts.t.Log("starting srv", name) r := require.New(ts.t) - cmd := exec.CommandContext(ts.ctx, "node", "./sbot_serv.js") + cmd := exec.CommandContext(ts.ctx, "node", "../../../sbot_serv.js") cmd.Stderr = os.Stderr - outrc, err := cmd.StdoutPipe() r.NoError(err) @@ -197,12 +195,20 @@ func (ts *testSession) startJSBotAsServer(name, testScriptFileName string) (*ref } jsBotCnt++ + // copy test scripts (maybe later with templates if we need to) + cmd.Dir = filepath.Join("testrun", ts.t.Name(), name) + os.MkdirAll(cmd.Dir, 0700) + err = exec.Command("cp", "-r", "testscripts", cmd.Dir).Run() + r.NoError(err) + var port = 1024 + mrand.Intn(23000) env := []string{ - "TEST_NAME=" + filepath.Join(ts.t.Name(), "jsbot-"+name), + "TEST_NAME=jsbot-" + name, + "TEST_REPO=" + cmd.Dir, fmt.Sprintf("TEST_PORT=%d", port), "TEST_SESSIONSCRIPT=" + testScriptFileName, + "DEBUG=ssb:room:tunnel:*", } if ts.keySHS != nil { env = append(env, "TEST_APPKEY="+base64.StdEncoding.EncodeToString(ts.keySHS)) diff --git a/test/nodejs/testscripts/server.js b/test/nodejs/testscripts/server.js index 788a662..59a00d1 100644 --- a/test/nodejs/testscripts/server.js +++ b/test/nodejs/testscripts/server.js @@ -3,9 +3,9 @@ const pull = require('pull-stream') module.exports = { before: (sbot, ready) => { pull( - sbot.conn.peers(), + sbot.conn.hub().listen(), pull.drain((p) => { - console.warn('peer change:',p) + console.warn('peer change:',p.type, p.key) }) ) @@ -14,8 +14,9 @@ module.exports = { }, after: (sbot, client, exit) => { - console.warn('after:', sbot.id, client.id) + // hrm.. this runs twice (for each connection) + console.warn('server new connection:', client.id) - setTimeout(exit, 5000) + setTimeout(exit, 10000) } } \ No newline at end of file diff --git a/test/nodejs/testscripts/simple_client.js b/test/nodejs/testscripts/simple_client.js index 70944d0..5a111b5 100644 --- a/test/nodejs/testscripts/simple_client.js +++ b/test/nodejs/testscripts/simple_client.js @@ -1,4 +1,5 @@ const pull = require('pull-stream') +const { readFileSync } = require('fs') module.exports = { before: (sbot, ready) => { @@ -29,12 +30,13 @@ module.exports = { console.warn('left failed') throw err }) - }, 4000) + }, 9000) }) ready() }, after: (sbot, exit) => { + // now connected to the room console.warn('after connect... exiting in 10s') setTimeout(exit, 10000) } diff --git a/test/nodejs/testscripts/simple_client_opening_tunnel.js b/test/nodejs/testscripts/simple_client_opening_tunnel.js new file mode 100644 index 0000000..174bb83 --- /dev/null +++ b/test/nodejs/testscripts/simple_client_opening_tunnel.js @@ -0,0 +1,67 @@ +const pull = require('pull-stream') +const { readFileSync } = require('fs') + +module.exports = { + before: (sbot, ready) => { + sbot.on('rpc:connect', rpc => { + // log all new endpoints + pull( + rpc.tunnel.endpoints(), + pull.drain(el => { + console.warn("from roomsrv:",el) + }) + ) + + rpc.tunnel.isRoom().then((yes) => { + if (!yes) throw new Error("expected isRoom to be true!") + console.warn("peer is indeed a room!") + + // announce ourselves to the room/tunnel + rpc.tunnel.announce().then((ret) => { + console.warn('announced!') + console.warn(ret) + + setTimeout(() => { + // put there by the go test process + let roomHandle = readFileSync('endpoint_through_room.txt').toString() + console.warn("connecting to room handle:", roomHandle) + + sbot.connect(roomHandle, (err, tunneldRpc) => { + if (err) throw err + console.warn("got tunnel to:", tunneldRpc.id) + }) + }, 5000) + }).catch((err) => { + console.warn('announce failed') + throw err + }) + + + + }).catch((err) => { + console.warn('isRoom failed') + throw err + }) + + + + // leave after 5 seconds + setTimeout(() => { + rpc.tunnel.leave().then((ret) => { + console.warn('left') + console.warn(ret) + }).catch((err) => { + console.warn('left failed') + throw err + }) + }, 9000) + }) + ready() + }, + + after: (sbot, exit) => { + // now connected to the room + console.warn('after connect... exiting in 10s') + setTimeout(exit, 10000) + } +} \ No newline at end of file