40c258c9f42d6634c104a30b3249212d03e5f9a8
[tinc] / test / integration / cmd_import.py
1 #!/usr/bin/env python3
2
3 """Test import/export error conditions."""
4
5 import os
6
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
12
13 SEPARATOR = f"#{'-' * 63}#"
14
15 MULTI_HOST = f"""
16 Name = node0
17 Address = sun
18 {SEPARATOR}
19 Name = node1
20 Address = moon
21 {SEPARATOR}
22 """.strip()
23
24 MAX_PATH = 255 if os.name == "nt" else os.pathconf("/", "PC_PATH_MAX")
25 LONG_NAME = MAX_PATH * "x"
26
27
28 def init(ctx: Test) -> Tinc:
29     """Initialize a node."""
30     return ctx.node(init="set AutoConnect no")
31
32
33 def test_import(foo: Tinc) -> None:
34     """Run tests for command 'import'."""
35
36     _, err = foo.cmd("import", "foo", code=1)
37     check.is_in("Too many arguments", err)
38
39     _, err = foo.cmd("import", code=1)
40     check.is_in("No host configuration files imported", err)
41
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)
46
47     _, err = foo.cmd("import", stdin="Name = !@#", code=1)
48     check.is_in("Invalid Name in input", err)
49
50     _, err = foo.cmd("import", stdin=f"Name = {LONG_NAME}", code=1)
51     check.is_in("Filename too long", err)
52
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)
56
57     _, err = foo.cmd("import", stdin=MULTI_HOST)
58     check.is_in("Imported 2 host configuration files", err)
59
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"))
63
64     _, err = foo.cmd("import", stdin="Name = node0", code=1)
65     check.is_in("node0 already exists", err)
66
67     if RUN_ACCESS_CHECKS:
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)
72
73
74 def test_export(foo: Tinc) -> None:
75     """Run tests for command 'export'."""
76
77     _, err = foo.cmd("export", "foo", code=1)
78     check.is_in("Too many arguments", err)
79
80     os.remove(foo.sub(f"hosts/{foo}"))
81     _, err = foo.cmd("export", code=1)
82     check.is_in("Could not open configuration", err)
83
84     util.write_text(foo.sub("tinc.conf"), "")
85     _, err = foo.cmd("export", code=1)
86     check.is_in("Could not find Name", err)
87
88     os.remove(foo.sub("tinc.conf"))
89     _, err = foo.cmd("export", code=1)
90     check.is_in("Could not open", err)
91
92
93 def test_exchange(foo: Tinc) -> None:
94     """Run tests for command 'exchange'."""
95
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
101
102
103 def test_exchange_all(foo: Tinc) -> None:
104     """Run tests for command 'exchange'."""
105
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
113
114
115 def test_export_all(foo: Tinc) -> None:
116     """Run tests for command 'export-all'."""
117
118     _, err = foo.cmd("export-all", "foo", code=1)
119     check.is_in("Too many arguments", err)
120
121     host_foo = foo.sub("hosts/foo")
122     util.write_text(host_foo, "Name = foo")
123     os.chmod(host_foo, 0)
124
125     host_bar = foo.sub("hosts/bar")
126     util.write_text(host_bar, "Host = bar\nAddress = 1.1.1.1")
127
128     host_invalid = foo.sub("hosts/xi-Eb-Vx-k3")
129     util.write_text(host_invalid, "Host = invalid")
130
131     out, err = foo.cmd("export-all", code=1)
132     check.is_in("Could not open configuration", err)
133
134     log.info("checking bad node name in export")
135     assert "xi-Eb-Vx-k3" not in out
136
137     for want in "Host = bar", "Address = 1.1.1.1", SEPARATOR:
138         check.is_in(want, out)
139
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)
145         assert False
146
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)
151
152
153 with Test("test 'import' command") as context:
154     test_import(init(context))
155
156 with Test("test 'export' command") as context:
157     test_export(init(context))
158
159 with Test("test 'exchange' command") as context:
160     test_exchange(init(context))
161
162 if RUN_ACCESS_CHECKS:
163     with Test("test 'exchange-all' command") as context:
164         test_exchange_all(init(context))
165
166     with Test("test 'export-all' command") as context:
167         test_export_all(init(context))