From ad1dc5f405e0470906447b13efa93a822f258d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 26 May 2017 14:00:19 +0200 Subject: [PATCH] Corenet API: Use simple util instead of socat for tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- test/bin/Rules.mk | 4 ++ test/dependencies/ma-pipe-unidir/LICENSE | 22 +++++++ test/dependencies/ma-pipe-unidir/main.go | 73 ++++++++++++++++++++++++ test/sharness/Rules.mk | 2 +- test/sharness/t0180-corenet.sh | 25 ++++---- 5 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 test/dependencies/ma-pipe-unidir/LICENSE create mode 100644 test/dependencies/ma-pipe-unidir/main.go diff --git a/test/bin/Rules.mk b/test/bin/Rules.mk index 08f1a295c..279b59f21 100644 --- a/test/bin/Rules.mk +++ b/test/bin/Rules.mk @@ -22,6 +22,10 @@ $(d)/go-timeout: test/dependencies/go-timeout $(go-build) TGTS_$(d) += $(d)/go-timeout +$(d)/ma-pipe-unidir: test/dependencies/ma-pipe-unidir + $(go-build) +TGTS_$(d) += $(d)/ma-pipe-unidir + TGTS_GX_$(d) := hang-fds iptb TGTS_GX_$(d) := $(addprefix $(d)/,$(TGTS_GX_$(d))) diff --git a/test/dependencies/ma-pipe-unidir/LICENSE b/test/dependencies/ma-pipe-unidir/LICENSE new file mode 100644 index 000000000..e1159e5d4 --- /dev/null +++ b/test/dependencies/ma-pipe-unidir/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2017 Łukasz Magiera + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/test/dependencies/ma-pipe-unidir/main.go b/test/dependencies/ma-pipe-unidir/main.go new file mode 100644 index 000000000..0eb0a7217 --- /dev/null +++ b/test/dependencies/ma-pipe-unidir/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "flag" + "fmt" + + ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr-net" + "io" + "os" +) + +const USAGE = "ma-pipe-unidir [-l|--listen] [-h|--help] \n" + +type Opts struct { + Listen bool +} + +func main() { + opts := Opts{} + flag.BoolVar(&opts.Listen, "l", false, "") + flag.BoolVar(&opts.Listen, "listen", false, "") + flag.Usage = func() { + fmt.Print(USAGE) + } + flag.Parse() + args := flag.Args() + + if len(args) < 2 { // + fmt.Print(USAGE) + return + } + + mode := args[0] + addr := args[1] + + maddr, err := ma.NewMultiaddr(addr) + if err != nil { + return + } + + var conn manet.Conn + + if opts.Listen { + listener, err := manet.Listen(maddr) + if err != nil { + return + } + + conn, err = listener.Accept() + if err != nil { + return + } + } else { + var err error + conn, err = manet.Dial(maddr) + if err != nil { + return + } + } + + defer conn.Close() + switch mode { + case "recv": + io.Copy(os.Stdout, conn) + case "send": + io.Copy(conn, os.Stdin) + default: + //TODO: a bit late + fmt.Print(USAGE) + return + } +} diff --git a/test/sharness/Rules.mk b/test/sharness/Rules.mk index 5832fb9b8..916f376c1 100644 --- a/test/sharness/Rules.mk +++ b/test/sharness/Rules.mk @@ -7,7 +7,7 @@ T_$(d) = $(sort $(wildcard $(d)/t[0-9][0-9][0-9][0-9]-*.sh)) DEPS_$(d) := test/bin/random test/bin/multihash test/bin/pollEndpoint \ test/bin/iptb test/bin/go-sleep test/bin/random-files \ - test/bin/go-timeout test/bin/hang-fds + test/bin/go-timeout test/bin/hang-fds test/bin/ma-pipe-unidir DEPS_$(d) += cmd/ipfs/ipfs DEPS_$(d) += $(d)/clean-test-results DEPS_$(d) += $(SHARNESS_$(d)) diff --git a/test/sharness/t0180-corenet.sh b/test/sharness/t0180-corenet.sh index 0fd7e4e79..c11df7409 100755 --- a/test/sharness/t0180-corenet.sh +++ b/test/sharness/t0180-corenet.sh @@ -21,11 +21,6 @@ test_expect_success 'peer ids' ' PEERID_1=$(iptb get id 1) ' -# netcat (nc) is needed for the following tests -test_expect_success "socat is available" ' - type socat >/dev/null -' - test_expect_success "test ports are closed" ' (! (netstat -ln | grep "LISTEN" | grep ":10101 ")) && (! (netstat -ln | grep "LISTEN" | grep ":10102 ")) @@ -36,22 +31,22 @@ test_expect_success 'start ipfs listener' ' ' test_expect_success 'Test server to client communications' ' - socat FILE:corenet0.bin TCP-LISTEN:10101,reuseaddr & - NC_SERVER_PID=$! + ma-pipe-unidir --listen send /ip4/127.0.0.1/tcp/10101 < corenet0.bin & + SERVER_PID=$! ipfsi 1 exp corenet dial $PEERID_0 corenet-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log && - socat TCP4:127.0.0.1:10102 OPEN:client.out,creat,trunc && - wait $NC_SERVER_PID + ma-pipe-unidir recv /ip4/127.0.0.1/tcp/10102 > client.out && + wait $SERVER_PID ' -test_expect_success 'Test server to client communications' ' - socat TCP-LISTEN:10101,reuseaddr OPEN:server.out,creat,trunc & - NC_SERVER_PID=$! +test_expect_success 'Test client to server communications' ' + ma-pipe-unidir --listen recv /ip4/127.0.0.1/tcp/10101 > server.out & + SERVER_PID=$! + #sleep 0.5 && ipfsi 1 exp corenet dial $PEERID_0 corenet-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log && - socat FILE:corenet1.bin TCP4:127.0.0.1:10102 && - - wait $NC_SERVER_PID + ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < corenet1.bin + wait $SERVER_PID ' test_expect_success 'server to client output looks good' '