From 2f2bda4d1953617b945f1222e008cb53edee162a Mon Sep 17 00:00:00 2001 From: Kirill Isakov Date: Wed, 25 May 2022 21:13:25 +0600 Subject: [PATCH] Add tests for network commands --- src/net_socket.c | 4 +- test/integration/cmd_net.py | 100 +++++++++++++++++++++++++++++++++++ test/integration/meson.build | 1 + 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100755 test/integration/cmd_net.py diff --git a/src/net_socket.c b/src/net_socket.c index 92dcbedb..4c76c30a 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -869,12 +869,12 @@ void try_outgoing_connections(void) { node_add(n); } - free(name); - outgoing->node = n; list_insert_tail(&outgoing_list, outgoing); setup_outgoing_connection(outgoing, true); } + + free(name); } /* Terminate any connections whose outgoing_t is to be deleted. */ diff --git a/test/integration/cmd_net.py b/test/integration/cmd_net.py new file mode 100755 index 00000000..d773839e --- /dev/null +++ b/test/integration/cmd_net.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 + +"""Test network control commands.""" + +from testlib import check, cmd +from testlib.log import log +from testlib.proc import Tinc +from testlib.test import Test + + +def init(ctx: Test) -> Tinc: + """Initialize a node.""" + + node = ctx.node() + stdin = f""" + init {node} + set Port 0 + set Address localhost + set DeviceType dummy + set AutoConnect no + """ + node.cmd(stdin=stdin) + return node + + +def test_network(foo: Tinc) -> None: + """Test command 'network'.""" + + _, err = foo.cmd("network", "foo", "bar", code=1) + check.is_in("Too many arguments", err) + + _, err = foo.cmd("network", "foo./", code=1) + check.is_in("Invalid character in netname", err) + + _, err = foo.cmd("network", "foo.<") + check.is_in("unsafe character in netname", err) + + +def run_tests(foo: Tinc, bar: Tinc) -> None: + """Run tests.""" + + log.info("start nodes") + foo.start() + bar.start() + check.nodes(foo, 1) + + log.info("test failing commands") + _, err = foo.cmd("connect", code=1) + check.is_in("Invalid number of arguments", err) + + _, err = foo.cmd("connect", "foo", "bar", code=1) + check.is_in("Invalid number of arguments", err) + + _, err = foo.cmd("connect", f"{bar}@", code=1) + check.is_in("Invalid name for node", err) + + log.info("connect nodes") + foo.add_script(bar.script_up) + cmd.exchange(foo, bar) + + # Implement REQ_CONNECT and update this + log.info("test connect") + _, err = foo.cmd("connect", bar.name, code=1) + check.is_in("Could not connect to", err) + + log.info("connect nodes") + foo.cmd("add", "ConnectTo", bar.name) + foo.cmd("retry") + foo[bar.script_up].wait() + check.nodes(foo, 2) + + log.info("disconnect nodes") + foo.add_script(bar.script_down) + foo.cmd("disconnect", bar.name) + foo[bar.script_down].wait() + check.nodes(foo, 1) + + log.info("second disconnect must fail") + _, err = foo.cmd("disconnect", bar.name, code=1) + check.is_in("Could not disconnect", err) + + log.info("retry connections") + foo.cmd("retry") + foo[bar.script_up].wait() + check.nodes(foo, 2) + + log.info("purge old connections") + bar.cmd("stop") + foo[bar.script_down].wait() + foo.cmd("purge") + + for command, result in ("nodes", 1), ("edges", 0), ("subnets", 4): + out, _ = foo.cmd("dump", command) + check.lines(out, result) + + test_network(foo) + + +with Test("run network tests") as context: + run_tests(init(context), init(context)) diff --git a/test/integration/meson.build b/test/integration/meson.build index 2b9f4030..9c061af6 100644 --- a/test/integration/meson.build +++ b/test/integration/meson.build @@ -2,6 +2,7 @@ tests = [ 'basic.py', 'cmd_dump.py', 'cmd_fsck.py', + 'cmd_net.py', 'cmd_sign_verify.py', 'commandline.py', 'executables.py', -- 2.20.1