3 """Test import/export error conditions."""
7 from testlib import check, cmd, util
8 from testlib.log import log
9 from testlib.proc import Tinc
10 from testlib.test import Test
12 SEPARATOR = f"#{'-' * 63}#"
23 MAX_PATH = 255 if os.name == "nt" else os.pathconf("/", "PC_PATH_MAX")
24 LONG_NAME = MAX_PATH * "x"
27 def init(ctx: Test) -> Tinc:
28 """Initialize a node."""
42 def test_import(foo: Tinc) -> None:
43 """Run tests for command 'import'."""
45 _, err = foo.cmd("import", "foo", code=1)
46 check.is_in("Too many arguments", err)
48 _, err = foo.cmd("import", code=1)
49 check.is_in("No host configuration files imported", err)
51 for prefix in "fred", "Name fred", "name = fred", "name=fred":
52 log.info("testing prefix '%s'", prefix)
53 _, err = foo.cmd("import", stdin=prefix, code=1)
54 check.is_in("Junk at the beginning", err)
56 _, err = foo.cmd("import", stdin="Name = !@#", code=1)
57 check.is_in("Invalid Name in input", err)
59 _, err = foo.cmd("import", stdin=f"Name = {LONG_NAME}", code=1)
60 check.is_in("Filename too long", err)
62 log.info("make sure no address for imported nodes is present")
63 for node in "node0", "node1":
64 foo.cmd("get", f"{node}.Address", code=1)
66 _, err = foo.cmd("import", stdin=MULTI_HOST)
67 check.is_in("Imported 2 host configuration files", err)
69 log.info("check imported nodes addresses")
70 check.equals("sun", cmd.get(foo, "node0.Address"))
71 check.equals("moon", cmd.get(foo, "node1.Address"))
73 _, err = foo.cmd("import", stdin="Name = node0", code=1)
74 check.is_in("node0 already exists", err)
77 log.info("import to inaccessible hosts subdirectory")
78 os.chmod(foo.sub("hosts"), 0)
79 _, err = foo.cmd("import", stdin="Name = vinny", code=1)
80 check.is_in("Error creating configuration", err)
83 def test_export(foo: Tinc) -> None:
84 """Run tests for command 'export'."""
86 _, err = foo.cmd("export", "foo", code=1)
87 check.is_in("Too many arguments", err)
89 os.remove(foo.sub(f"hosts/{foo}"))
90 _, err = foo.cmd("export", code=1)
91 check.is_in("Could not open configuration", err)
93 util.write_text(foo.sub("tinc.conf"), "")
94 _, err = foo.cmd("export", code=1)
95 check.is_in("Could not find Name", err)
97 os.remove(foo.sub("tinc.conf"))
98 _, err = foo.cmd("export", code=1)
99 check.is_in("Could not open", err)
102 def test_exchange(foo: Tinc) -> None:
103 """Run tests for command 'exchange'."""
105 log.info("make sure exchange does not import if export fails")
106 util.write_text(foo.sub("tinc.conf"), "")
107 host_foo = "Name = foo\nAddress = 1.1.1.1"
108 _, err = foo.cmd("exchange", stdin=host_foo, code=1)
109 assert "Imported" not in err
112 def test_exchange_all(foo: Tinc) -> None:
113 """Run tests for command 'exchange'."""
115 log.info("make sure exchange-all does not import if export fails")
116 host_bar = foo.sub("hosts/bar")
117 util.write_text(host_bar, "")
118 os.chmod(host_bar, 0)
119 host_foo = "Name = foo\nAddress = 1.1.1.1"
120 _, err = foo.cmd("exchange-all", stdin=host_foo, code=1)
121 assert "Imported" not in err
124 def test_export_all(foo: Tinc) -> None:
125 """Run tests for command 'export-all'."""
127 _, err = foo.cmd("export-all", "foo", code=1)
128 check.is_in("Too many arguments", err)
130 host_foo = foo.sub("hosts/foo")
131 util.write_text(host_foo, "Name = foo")
132 os.chmod(host_foo, 0)
134 host_bar = foo.sub("hosts/bar")
135 util.write_text(host_bar, "Host = bar\nAddress = 1.1.1.1")
137 host_invalid = foo.sub("hosts/xi-Eb-Vx-k3")
138 util.write_text(host_invalid, "Host = invalid")
140 out, err = foo.cmd("export-all", code=1)
141 check.is_in("Could not open configuration", err)
143 log.info("checking bad node name in export")
144 assert "xi-Eb-Vx-k3" not in out
146 for want in "Host = bar", "Address = 1.1.1.1", SEPARATOR:
147 check.is_in(want, out)
149 log.info("verify that separators are used on separate lines")
150 lines = out.splitlines()
151 separators = list(filter(lambda line: line == SEPARATOR, lines))
152 if len(separators) != 2:
153 log.info("unexpected number of separators: %s", lines)
157 os.chmod(foo.sub("hosts"), 0)
158 _, err = foo.cmd("export-all", code=1)
159 check.is_in("Could not open host configuration", err)
162 with Test("test 'import' command") as context:
163 test_import(init(context))
165 with Test("test 'export' command") as context:
166 test_export(init(context))
168 with Test("test 'exchange' command") as context:
169 test_exchange(init(context))
172 with Test("test 'exchange-all' command") as context:
173 test_exchange_all(init(context))
175 with Test("test 'export-all' command") as context:
176 test_export_all(init(context))