From 748d25f473d05076e1f1de0d75d939c54a41c160 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Jun 2015 05:13:04 -0700 Subject: [PATCH 1/9] t0060-daemon: test transport is encrypted License: MIT Signed-off-by: Juan Batiz-Benet --- test/sharness/t0060-daemon.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/sharness/t0060-daemon.sh b/test/sharness/t0060-daemon.sh index e0aff7b5edf..eaffe6384c1 100755 --- a/test/sharness/t0060-daemon.sh +++ b/test/sharness/t0060-daemon.sh @@ -8,6 +8,8 @@ test_description="Test daemon command" . lib/test-lib.sh +# TODO: randomize ports here once we add --config to ipfs daemon + # this needs to be in a different test than "ipfs daemon --init" below test_expect_success "setup IPFS_PATH" ' IPFS_PATH="$(pwd)/.ipfs" && @@ -91,6 +93,16 @@ test_expect_success "ipfs help output looks good" ' test_fsh cat help.txt ' +# check transport is encrypted + +test_expect_success 'transport should be encrypted' ' + nc localhost 4001 >swarmnc & + go-sleep 0.1s && + grep -q "AES-256,AES-128" swarmnc && + ! grep -q "/ipfs/identify" swarmnc || + test_fsh cat swarmnc +' + # end same as in t0010 test_expect_success "daemon is still running" ' From c5caccba14c6ca6b1fa9d5624bdfccbbf70ca089 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Jun 2015 04:47:52 -0700 Subject: [PATCH 2/9] ipfs id -f= and \n \t - added field to `ipfs id -f` - added \n and \t conversion in `ipfs id -f` License: MIT Signed-off-by: Juan Batiz-Benet --- core/commands/id.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/commands/id.go b/core/commands/id.go index ea5e4cd549a..31af0806773 100644 --- a/core/commands/id.go +++ b/core/commands/id.go @@ -46,6 +46,7 @@ ipfs id supports the format option for output with the following keys: : agent version : protocol version : public key +: addresses (newline delimited) `, }, Arguments: []cmds.Argument{ @@ -119,6 +120,9 @@ ipfs id supports the format option for output with the following keys: output = strings.Replace(output, "", val.AgentVersion, -1) output = strings.Replace(output, "", val.ProtocolVersion, -1) output = strings.Replace(output, "", val.PublicKey, -1) + output = strings.Replace(output, "", strings.Join(val.Addresses, "\n"), -1) + output = strings.Replace(output, "\\n", "\n", -1) + output = strings.Replace(output, "\\t", "\t", -1) return strings.NewReader(output), nil } else { From 500f51300d26f5928bb0f3a9a63e454f698b45cb Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Jun 2015 04:20:43 -0700 Subject: [PATCH 3/9] ipfs swarm addrs local - show local addrs Add a command to return local addresses. License: MIT Signed-off-by: Juan Batiz-Benet --- core/commands/swarm.go | 48 ++++++++++++++++++++++++++++++++++++ test/sharness/t0140-swarm.sh | 33 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100755 test/sharness/t0140-swarm.sh diff --git a/core/commands/swarm.go b/core/commands/swarm.go index bd7b943243a..efb6fc1e7a6 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "path" "sort" cmds "github.com/ipfs/go-ipfs/commands" @@ -91,6 +92,9 @@ var swarmAddrsCmd = &cmds.Command{ ipfs swarm addrs lists all addresses this node is aware of. `, }, + Subcommands: map[string]*cmds.Command{ + "local": swarmAddrsLocalCmd, + }, Run: func(req cmds.Request, res cmds.Response) { n, err := req.Context().GetNode() @@ -144,6 +148,50 @@ ipfs swarm addrs lists all addresses this node is aware of. Type: addrMap{}, } +var swarmAddrsLocalCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "List local addresses.", + ShortDescription: ` +ipfs swarm addrs local lists all local addresses the node is listening on. +`, + }, + Options: []cmds.Option{ + cmds.BoolOption("id", "Show peer ID in addresses"), + }, + Run: func(req cmds.Request, res cmds.Response) { + + n, err := req.Context().GetNode() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + if n.PeerHost == nil { + res.SetError(errNotOnline, cmds.ErrClient) + return + } + + showid, _, _ := req.Option("id").Bool() + id := n.Identity.Pretty() + + var addrs []string + for _, addr := range n.PeerHost.Addrs() { + saddr := addr.String() + if showid { + saddr = path.Join(saddr, "ipfs", id) + } + addrs = append(addrs, saddr) + } + sort.Sort(sort.StringSlice(addrs)) + + res.SetOutput(&stringList{addrs}) + }, + Type: stringList{}, + Marshalers: cmds.MarshalerMap{ + cmds.Text: stringListMarshaler, + }, +} + var swarmConnectCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Open connection to a given address", diff --git a/test/sharness/t0140-swarm.sh b/test/sharness/t0140-swarm.sh new file mode 100755 index 00000000000..229ba1676bb --- /dev/null +++ b/test/sharness/t0140-swarm.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# +# Copyright (c) 2014 Jeromy Johnson +# MIT Licensed; see the LICENSE file in this repository. +# + +test_description="Test ipfs swarm command" + +. lib/test-lib.sh + +test_init_ipfs + +test_launch_ipfs_daemon + +test_expect_success 'disconnected: peers is empty' ' + ipfs swarm peers >actual && + test_must_be_empty actual +' + +test_expect_success 'disconnected: addrs local has localhost' ' + ipfs swarm addrs local >actual && + grep "/ip4/127.0.0.1" actual +' + +test_expect_success 'disconnected: addrs local matches ipfs id' ' + ipfs id -f="\\n" | sort >expected && + ipfs swarm addrs local --id | sort >actual && + test_cmp expected actual +' + +test_kill_ipfs_daemon + +test_done From 9716018ca5bba11d8096ce77fc22a78701b74e9d Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Jun 2015 03:21:01 -0700 Subject: [PATCH 4/9] daemon output includes swarm addresses daemon output now includes initial swarm addresses. this is not a full solution, as a change in network will not trigger re-printing. We need a good way to do that. This made me re-think how we're outputting these messages, perhaps we should be throwing them as log.Events, and capturing some with a special keyword to output to the user on stdout. Things like network addresses being rebound, NATs being holepunched, external network addresses being figured out, connections established, etc may be valuable events to show the user. Of course, these should be very few, as a noisy daemon is an annoying daemon. License: MIT Signed-off-by: Juan Batiz-Benet --- cmd/ipfs/daemon.go | 16 ++++++++++++++++ test/sharness/t0060-daemon.sh | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index c50551f27bb..9778168540e 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -6,6 +6,7 @@ import ( "net/http" _ "net/http/pprof" "os" + "sort" "strings" "sync" @@ -179,6 +180,8 @@ func daemonFunc(req cmds.Request, res cmds.Response) { return } + printSwarmAddrs(node) + defer func() { // We wait for the node to close first, as the node has children // that it will wait for before closing, such as the API server. @@ -305,6 +308,19 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) { return nil, errc } +// printSwarmAddrs prints the addresses of the host +func printSwarmAddrs(node *core.IpfsNode) { + var addrs []string + for _, addr := range node.PeerHost.Addrs() { + addrs = append(addrs, addr.String()) + } + sort.Sort(sort.StringSlice(addrs)) + + for _, addr := range addrs { + fmt.Printf("Swarm listening on %s\n", addr) + } +} + // serveHTTPGateway collects options, creates listener, prints status message and starts serving requests func serveHTTPGateway(req cmds.Request) (error, <-chan error) { cfg, err := req.Context().GetConfig() diff --git a/test/sharness/t0060-daemon.sh b/test/sharness/t0060-daemon.sh index eaffe6384c1..a81d2c9f39d 100755 --- a/test/sharness/t0060-daemon.sh +++ b/test/sharness/t0060-daemon.sh @@ -36,6 +36,11 @@ test_expect_success "'ipfs config Identity.PeerID' works" ' ipfs config Identity.PeerID >config_peerId ' +test_expect_success "'ipfs swarm addrs local' works" ' + ipfs swarm addrs local >local_addrs +' + + # this is lifted straight from t0020-init.sh test_expect_success "ipfs peer id looks good" ' PEERID=$(cat config_peerId) && @@ -60,6 +65,7 @@ test_expect_success "ipfs daemon output looks good" ' echo "peer identity: $PEERID" >>expected_daemon && echo "to get started, enter:" >>expected_daemon && printf "\\n\\t$STARTFILE\\n\\n" >>expected_daemon && + cat local_addrs | sed "s/^/Swarm listening on /" >>expected_daemon && echo "API server listening on /ip4/127.0.0.1/tcp/5001" >>expected_daemon && echo "Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080" >>expected_daemon && test_cmp expected_daemon actual_daemon From e37fefdfd305d96106c177e9d4ba2f3c899c9d62 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Jun 2015 03:05:50 -0700 Subject: [PATCH 5/9] daemon option to optionally disable secio This commit adds an option to turn off all encryption. This is a mode used for tests, debugging, achieving protocol implementation interop, learning about how the protocol works (nc ftw), and worst case networks which _demand_ to be able to snoop on all the traffic. (sadly, there are some private intranets like this...). (We should consider at least _signing_ all this traffic.) Because of the severity of this sort of thing, this is an all-or-nothing deal. Either encryption is ON or OFF _fully_. This way, partially unencrypted nodes cannot be accidentally left running without the user's understanding. Nodes without encrypted connections will simply not be able to speak to any of the global bootstrap nodes, or anybody in the public network. License: MIT Signed-off-by: Juan Batiz-Benet --- cmd/ipfs/daemon.go | 19 +++++++++++++++---- p2p/net/conn/dial.go | 2 +- p2p/net/conn/interface.go | 8 ++++++++ p2p/net/conn/listen.go | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 9778168540e..a49282949e6 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -19,6 +19,7 @@ import ( commands "github.com/ipfs/go-ipfs/core/commands" corehttp "github.com/ipfs/go-ipfs/core/corehttp" "github.com/ipfs/go-ipfs/core/corerouting" + conn "github.com/ipfs/go-ipfs/p2p/net/conn" peer "github.com/ipfs/go-ipfs/p2p/peer" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" util "github.com/ipfs/go-ipfs/util" @@ -32,7 +33,8 @@ const ( writableKwd = "writable" ipfsMountKwd = "mount-ipfs" ipnsMountKwd = "mount-ipns" - unrestrictedApiAccess = "unrestricted-api" + unrestrictedApiAccessKwd = "unrestricted-api" + unencryptTransportKwd = "disable-transport-encryption" // apiAddrKwd = "address-api" // swarmAddrKwd = "address-swarm" ) @@ -76,7 +78,8 @@ the port as you would other services or database (firewall, authenticated proxy, cmds.BoolOption(writableKwd, "Enable writing objects (with POST, PUT and DELETE)"), cmds.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount)"), cmds.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount)"), - cmds.BoolOption(unrestrictedApiAccess, "Allow API access to unlisted hashes"), + cmds.BoolOption(unrestrictedApiAccessKwd, "Allow API access to unlisted hashes"), + cmds.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)"), // TODO: add way to override addresses. tricky part: updating the config if also --init. // cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"), @@ -110,6 +113,14 @@ func daemonFunc(req cmds.Request, res cmds.Response) { } }() + // check transport encryption flag. + unencrypted, _, _ := req.Option(unencryptTransportKwd).Bool() + if unencrypted { + log.Warningf(`Running with --%s: All connections are UNENCRYPTED. + You will not be able to connect to regular encrypted networks.`, unencryptTransportKwd) + conn.EncryptConnections = false + } + // first, whether user has provided the initialization flag. we may be // running in an uninitialized state. initialize, _, err := req.Option(initOptionKwd).Bool() @@ -259,9 +270,9 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) { apiMaddr = apiLis.Multiaddr() fmt.Printf("API server listening on %s\n", apiMaddr) - unrestricted, _, err := req.Option(unrestrictedApiAccess).Bool() + unrestricted, _, err := req.Option(unrestrictedApiAccessKwd).Bool() if err != nil { - return fmt.Errorf("serveHTTPApi: Option(%s) failed: %s", unrestrictedApiAccess, err), nil + return fmt.Errorf("serveHTTPApi: Option(%s) failed: %s", unrestrictedApiAccessKwd, err), nil } apiGw := corehttp.NewGateway(corehttp.GatewayConfig{ diff --git a/p2p/net/conn/dial.go b/p2p/net/conn/dial.go index 43831c3efef..a9a1a7aaf93 100644 --- a/p2p/net/conn/dial.go +++ b/p2p/net/conn/dial.go @@ -60,7 +60,7 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) ( return } - if d.PrivateKey == nil { + if d.PrivateKey == nil || EncryptConnections == false { log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr) connOut = c return diff --git a/p2p/net/conn/interface.go b/p2p/net/conn/interface.go index 3a61911af82..82008593057 100644 --- a/p2p/net/conn/interface.go +++ b/p2p/net/conn/interface.go @@ -93,3 +93,11 @@ type Listener interface { // Any blocked Accept operations will be unblocked and return errors. Close() error } + +// EncryptConnections is a global parameter because it should either be +// enabled or _completely disabled_. I.e. a node should only be able to talk +// to proper (encrypted) networks if it is encrypting all its transports. +// Running a node with disabled transport encryption is useful to debug the +// protocols, achieve implementation interop, or for private networks which +// -- for whatever reason -- _must_ run unencrypted. +var EncryptConnections = true diff --git a/p2p/net/conn/listen.go b/p2p/net/conn/listen.go index ea91e5a56d4..71b89d76703 100644 --- a/p2p/net/conn/listen.go +++ b/p2p/net/conn/listen.go @@ -107,7 +107,7 @@ func (l *listener) Accept() (net.Conn, error) { return nil, err } - if l.privk == nil { + if l.privk == nil || EncryptConnections == false { log.Warning("listener %s listening INSECURELY!", l) return c, nil } From f300e97c508640d40917d07b2d70f6e8754394bd Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Jun 2015 05:53:00 -0700 Subject: [PATCH 6/9] added sharness/t0061-daemon-opts Test odd daemon options, like: - unrestricted-api - disable-transport-encryption (known breakage atm) License: MIT Signed-off-by: Juan Batiz-Benet --- test/sharness/lib/test-lib.sh | 4 ++- test/sharness/t0061-daemon-opts.sh | 39 ++++++++++++++++++++++++++++++ test/sharness/t0110-gateway.sh | 4 +++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100755 test/sharness/t0061-daemon-opts.sh diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh index 73b55b343c8..e257ba1e5c8 100644 --- a/test/sharness/lib/test-lib.sh +++ b/test/sharness/lib/test-lib.sh @@ -193,8 +193,10 @@ test_config_ipfs_gateway_writable() { test_launch_ipfs_daemon() { + args=$1 + test_expect_success "'ipfs daemon' succeeds" ' - ipfs daemon >actual_daemon 2>daemon_err & + ipfs daemon $args >actual_daemon 2>daemon_err & ' # we say the daemon is ready when the API server is ready. diff --git a/test/sharness/t0061-daemon-opts.sh b/test/sharness/t0061-daemon-opts.sh new file mode 100755 index 00000000000..d01fb8c8057 --- /dev/null +++ b/test/sharness/t0061-daemon-opts.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# +# Copyright (c) 2014 Juan Batiz-Benet +# MIT Licensed; see the LICENSE file in this repository. +# + +test_description="Test daemon command" + +. lib/test-lib.sh + + +test_init_ipfs + +test_launch_ipfs_daemon '--unrestricted-api --disable-transport-encryption' + +gwyport=$PORT_GWAY +apiport=$PORT_API + +test_expect_success 'api gateway should be unrestricted' ' + echo "hello mars :$gwyport :$apiport" >expected && + HASH=$(ipfs add -q expected) && + curl -sfo actual1 "http://127.0.0.1:$gwyport/ipfs/$HASH" && + curl -sfo actual2 "http://127.0.0.1:$apiport/ipfs/$HASH" && + test_cmp expected actual1 && + test_cmp expected actual2 +' + +# Odd. this fails here, but the inverse works on t0060-daemon. +test_expect_failure 'transport should be unencrypted' ' + nc 127.0.0.1 "$PORT_SWARM" >swarmnc & + go-sleep 0.1s && + ! grep -q "AES-256,AES-128" swarmnc && + grep -q "/ipfs/identify" swarmnc || + test_fsh cat swarmnc +' + +test_kill_ipfs_daemon + +test_done diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index 36a94c671c1..fbaa4cd2e7a 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -33,6 +33,10 @@ test_expect_success "GET IPFS path output looks good" ' rm actual ' +test_expect_success "GET IPFS path on API forbidden" ' + test_curl_resp_http_code "http://127.0.0.1:$apiport/ipfs/$HASH" "HTTP/1.1 403 Forbidden" +' + test_expect_success "GET IPFS directory path succeeds" ' mkdir dir && echo "12345" >dir/test && From 6b8e9f51fe1566293a5a795b1c331cb4acfa17d9 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 26 Jun 2015 23:22:40 -0700 Subject: [PATCH 7/9] fix t0061-daemon-opts.sh nc wait fix the nc wait. the issue was that stdin needs to remain _open_ but not receive any input for some time. If stdin receives (invalid) input or closes, the other side terminates the connection before writing out the muxer frames + identify handshake. This commit also changes the use of `!` for `test_must_fail` License: MIT Signed-off-by: Juan Batiz-Benet --- test/sharness/t0060-daemon.sh | 7 +++---- test/sharness/t0061-daemon-opts.sh | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/test/sharness/t0060-daemon.sh b/test/sharness/t0060-daemon.sh index a81d2c9f39d..13a56fc1c28 100755 --- a/test/sharness/t0060-daemon.sh +++ b/test/sharness/t0060-daemon.sh @@ -19,7 +19,7 @@ test_expect_success "setup IPFS_PATH" ' # NOTE: this should remove bootstrap peers (needs a flag) # TODO(cryptix): # - we won't see daemon startup failure because we put the daemon in the background - fix: fork with exit code after api listen -# - also default ports: might clash with local clients. Failure in that case isn't clear as well because pollEndpoint just uses the already running node +# - also default ports: might clash with local clients. Failure in that case isn't clear as well because pollEndpoint just uses the already running node test_expect_success "ipfs daemon --init launches" ' ipfs daemon --init >actual_daemon 2>daemon_err & ' @@ -102,10 +102,9 @@ test_expect_success "ipfs help output looks good" ' # check transport is encrypted test_expect_success 'transport should be encrypted' ' - nc localhost 4001 >swarmnc & - go-sleep 0.1s && + nc -w 5 localhost 4001 >swarmnc && grep -q "AES-256,AES-128" swarmnc && - ! grep -q "/ipfs/identify" swarmnc || + test_must_fail grep -q "/ipfs/identify" swarmnc || test_fsh cat swarmnc ' diff --git a/test/sharness/t0061-daemon-opts.sh b/test/sharness/t0061-daemon-opts.sh index d01fb8c8057..549fa3e8fd3 100755 --- a/test/sharness/t0061-daemon-opts.sh +++ b/test/sharness/t0061-daemon-opts.sh @@ -26,10 +26,9 @@ test_expect_success 'api gateway should be unrestricted' ' ' # Odd. this fails here, but the inverse works on t0060-daemon. -test_expect_failure 'transport should be unencrypted' ' - nc 127.0.0.1 "$PORT_SWARM" >swarmnc & - go-sleep 0.1s && - ! grep -q "AES-256,AES-128" swarmnc && +test_expect_success 'transport should be unencrypted' ' + go-sleep 0.5s | nc localhost "$PORT_SWARM" >swarmnc && + test_must_fail grep -q "AES-256,AES-128" swarmnc && grep -q "/ipfs/identify" swarmnc || test_fsh cat swarmnc ' From f796615e10348520111edf1566b17b3b812192d3 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 26 Jun 2015 23:57:01 -0700 Subject: [PATCH 8/9] mock: fix notif test License: MIT Signed-off-by: Juan Batiz-Benet --- p2p/net/mock/mock_notif_test.go | 36 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/p2p/net/mock/mock_notif_test.go b/p2p/net/mock/mock_notif_test.go index 1ef9fc6aaf3..d91403f8b2c 100644 --- a/p2p/net/mock/mock_notif_test.go +++ b/p2p/net/mock/mock_notif_test.go @@ -43,24 +43,30 @@ func TestNotifications(t *testing.T) { for i, s := range nets { n := notifiees[i] for _, s2 := range nets { - cos := s.ConnsToPeer(s2.LocalPeer()) - func() { - for i := 0; i < len(cos); i++ { - var c inet.Conn - select { - case c = <-n.connected: - case <-time.After(timeout): - t.Fatal("timeout") - } - for _, c2 := range cos { - if c == c2 { - t.Log("got notif for conn") - return - } + var actual []inet.Conn + for len(s.ConnsToPeer(s2.LocalPeer())) != len(actual) { + select { + case c := <-n.connected: + actual = append(actual, c) + case <-time.After(timeout): + t.Fatal("timeout") + } + } + + expect := s.ConnsToPeer(s2.LocalPeer()) + for _, c1 := range actual { + found := false + for _, c2 := range expect { + if c1 == c2 { + found = true + break } + } + if !found { t.Error("connection not found") } - }() + } + } } From 59d6a9a19cc2c32c4221d68c5df336efcca404c0 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 27 Jun 2015 00:32:34 -0700 Subject: [PATCH 9/9] fix swarm notif tests they were failing intermittently License: MIT Signed-off-by: Juan Batiz-Benet --- p2p/net/swarm/swarm_notif_test.go | 40 +++++++++++++++++-------------- p2p/net/swarm/swarm_test.go | 2 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/p2p/net/swarm/swarm_notif_test.go b/p2p/net/swarm/swarm_notif_test.go index d8b9bc2d2b7..b44b030d80f 100644 --- a/p2p/net/swarm/swarm_notif_test.go +++ b/p2p/net/swarm/swarm_notif_test.go @@ -11,8 +11,6 @@ import ( ) func TestNotifications(t *testing.T) { - t.Parallel() - ctx := context.Background() swarms := makeSwarms(ctx, t, 5) defer func() { @@ -44,24 +42,30 @@ func TestNotifications(t *testing.T) { continue } - cos := s.ConnectionsToPeer(s2.LocalPeer()) - func() { - for i := 0; i < len(cos); i++ { - var c inet.Conn - select { - case c = <-n.connected: - case <-time.After(timeout): - t.Fatal("timeout") - } - for _, c2 := range cos { - if c == c2 { - t.Log("got notif for conn", c) - return - } + var actual []inet.Conn + for len(s.ConnectionsToPeer(s2.LocalPeer())) != len(actual) { + select { + case c := <-n.connected: + actual = append(actual, c) + case <-time.After(timeout): + t.Fatal("timeout") + } + } + + expect := s.ConnectionsToPeer(s2.LocalPeer()) + for _, c1 := range actual { + found := false + for _, c2 := range expect { + if c1 == c2 { + found = true + break } - t.Error("connection not found", c) } - }() + if !found { + t.Error("connection not found") + } + } + } } diff --git a/p2p/net/swarm/swarm_test.go b/p2p/net/swarm/swarm_test.go index 4c12a1b5d3b..6e53cc339ed 100644 --- a/p2p/net/swarm/swarm_test.go +++ b/p2p/net/swarm/swarm_test.go @@ -325,6 +325,6 @@ func TestFilterBounds(t *testing.T) { case <-time.After(time.Second): t.Fatal("should have gotten connection") case <-conns: - fmt.Println("got connect") + t.Log("got connect") } }