3 """Test import/export error conditions."""
7 from testlib import check, cmd, util
8 from testlib.log import log
9 from testlib.const import RUN_ACCESS_CHECKS
10 from testlib.proc import Tinc
11 from testlib.test import Test
13 SEPARATOR = f"#{'-' * 63}#"
24 MAX_PATH = 255 if os.name == "nt" else os.pathconf("/", "PC_PATH_MAX")
25 LONG_NAME = MAX_PATH * "x"
28 def init(ctx: Test) -> Tinc:
29 """Initialize a node."""
30 return ctx.node(init="set AutoConnect no")
33 def test_import(foo: Tinc) -> None:
34 """Run tests for command 'import'."""
36 _, err = foo.cmd("import", "foo", code=1)
37 check.is_in("Too many arguments", err)
39 _, err = foo.cmd("import", code=1)
40 check.is_in("No host configuration files imported", err)
42 for prefix in "fred", "Name fred", "name = fred", "name=fred":
43 log.info("testing prefix '%s'", prefix)
44 _, err = foo.cmd("import", stdin=prefix, code=1)
45 check.is_in("Junk at the beginning", err)
47 _, err = foo.cmd("import", stdin="Name = !@#", code=1)
48 check.is_in("Invalid Name in input", err)
50 _, err = foo.cmd("import", stdin=f"Name = {LONG_NAME}", code=1)
51 check.is_in("Filename too long", err)
53 log.info("make sure no address for imported nodes is present")
54 for node in "node0", "node1":
55 foo.cmd("get", f"{node}.Address", code=1)
57 _, err = foo.cmd("import", stdin=MULTI_HOST)
58 check.is_in("Imported 2 host configuration files", err)
60 log.info("check imported nodes addresses")
61 check.equals("sun", cmd.get(foo, "node0.Address"))
62 check.equals("moon", cmd.get(foo, "node1.Address"))
64 _, err = foo.cmd("import", stdin="Name = node0", code=1)
65 check.is_in("node0 already exists", err)
68 log.info("import to inaccessible hosts subdirectory")
69 os.chmod(foo.sub("hosts"), 0)
70 _, err = foo.cmd("import", stdin="Name = vinny", code=1)
71 check.is_in("Error creating configuration", err)
74 def test_export(foo: Tinc) -> None:
75 """Run tests for command 'export'."""
77 _, err = foo.cmd("export", "foo", code=1)
78 check.is_in("Too many arguments", err)
80 os.remove(foo.sub(f"hosts/{foo}"))
81 _, err = foo.cmd("export", code=1)
82 check.is_in("Could not open configuration", err)
84 util.write_text(foo.sub("tinc.conf"), "")
85 _, err = foo.cmd("export", code=1)
86 check.is_in("Could not find Name", err)
88 os.remove(foo.sub("tinc.conf"))
89 _, err = foo.cmd("export", code=1)
90 check.is_in("Could not open", err)
93 def test_exchange(foo: Tinc) -> None:
94 """Run tests for command 'exchange'."""
96 log.info("make sure exchange does not import if export fails")
97 util.write_text(foo.sub("tinc.conf"), "")
98 host_foo = "Name = foo\nAddress = 1.1.1.1"
99 _, err = foo.cmd("exchange", stdin=host_foo, code=1)
100 assert "Imported" not in err
103 def test_exchange_all(foo: Tinc) -> None:
104 """Run tests for command 'exchange'."""
106 log.info("make sure exchange-all does not import if export fails")
107 host_bar = foo.sub("hosts/bar")
108 util.write_text(host_bar, "")
109 os.chmod(host_bar, 0)
110 host_foo = "Name = foo\nAddress = 1.1.1.1"
111 _, err = foo.cmd("exchange-all", stdin=host_foo, code=1)
112 assert "Imported" not in err
115 def test_export_all(foo: Tinc) -> None:
116 """Run tests for command 'export-all'."""
118 _, err = foo.cmd("export-all", "foo", code=1)
119 check.is_in("Too many arguments", err)
121 host_foo = foo.sub("hosts/foo")
122 util.write_text(host_foo, "Name = foo")
123 os.chmod(host_foo, 0)
125 host_bar = foo.sub("hosts/bar")
126 util.write_text(host_bar, "Host = bar\nAddress = 1.1.1.1")
128 host_invalid = foo.sub("hosts/xi-Eb-Vx-k3")
129 util.write_text(host_invalid, "Host = invalid")
131 out, err = foo.cmd("export-all", code=1)
132 check.is_in("Could not open configuration", err)
134 log.info("checking bad node name in export")
135 assert "xi-Eb-Vx-k3" not in out
137 for want in "Host = bar", "Address = 1.1.1.1", SEPARATOR:
138 check.is_in(want, out)
140 log.info("verify that separators are used on separate lines")
141 lines = out.splitlines()
142 separators = list(filter(lambda line: line == SEPARATOR, lines))
143 if len(separators) != 2:
144 log.info("unexpected number of separators: %s", lines)
147 if RUN_ACCESS_CHECKS:
148 os.chmod(foo.sub("hosts"), 0)
149 _, err = foo.cmd("export-all", code=1)
150 check.is_in("Could not open host configuration", err)
153 with Test("test 'import' command") as context:
154 test_import(init(context))
156 with Test("test 'export' command") as context:
157 test_export(init(context))
159 with Test("test 'exchange' command") as context:
160 test_exchange(init(context))
162 if RUN_ACCESS_CHECKS:
163 with Test("test 'exchange-all' command") as context:
164 test_exchange_all(init(context))
166 with Test("test 'export-all' command") as context:
167 test_export_all(init(context))