3 """Test device configuration variables."""
9 from testlib import check
10 from testlib.feature import Feature
11 from testlib.log import log
12 from testlib.proc import Script
13 from testlib.test import Test
15 system = platform.system()
18 def unknown_device_types(
19 features: T.Container[Feature],
20 ) -> T.Generator[str, T.Any, None]:
21 """Get devices unsupported by current OS."""
25 if Feature.UML not in features:
28 if Feature.TUNEMU not in features:
31 if system != "Darwin":
32 if not system.endswith("BSD"):
38 if system == "Windows":
43 def test_unknown_types(ctx: Test) -> None:
44 """Test unknown device types."""
46 foo = ctx.node(init=True)
48 for dev_type in unknown_device_types(foo.features):
49 log.info("testing unknown device type %s", dev_type)
50 _, err = foo.cmd("start", "-o", f"DeviceType={dev_type}", code=1)
51 check.is_in(f"Unknown device type {dev_type}", err)
54 def test_device_standby(ctx: Test) -> None:
55 """Test DeviceStandby."""
57 foo, bar, baz = ctx.node(init=True), ctx.node(), ctx.node()
59 log.info("configure %s", foo)
60 foo.cmd("set", "DeviceStandby", "yes")
61 foo.add_script(Script.TINC_UP)
62 foo.add_script(Script.TINC_DOWN)
64 log.info("starting tincd must not call tinc-up")
66 assert not foo[Script.TINC_UP].wait(timeout=1)
68 log.info("invite %s", bar)
69 url, _ = foo.cmd("invite", bar.name)
70 bar.cmd("join", url.strip())
71 bar.cmd("set", "DeviceType", "dummy")
72 bar.cmd("set", "Port", "0")
74 log.info("invite %s", baz)
75 url, _ = foo.cmd("invite", baz.name)
76 baz.cmd("join", url.strip())
77 baz.cmd("set", "DeviceType", "dummy")
78 baz.cmd("set", "Port", "0")
80 log.info("starting first client must call tinc-up")
82 foo[Script.TINC_UP].wait()
84 log.info("starting second client must not call tinc-up")
86 assert not foo[Script.TINC_UP].wait(timeout=1)
88 log.info("stopping next-to-last client must not call tinc-down")
89 bar.add_script(Script.TINC_DOWN)
91 bar[Script.TINC_DOWN].wait()
92 assert not foo[Script.TINC_DOWN].wait(timeout=1)
94 log.info("stopping last client must call tinc-down")
96 foo[Script.TINC_DOWN].wait()
98 log.info("stopping tincd must not call tinc-down")
100 assert not foo[Script.TINC_DOWN].wait(timeout=1)
103 # Device types are not checked on Windows.
104 # /dev/net/tun is not available in Docker containers.
105 if system != "Windows" and (system != "Linux" or os.path.exists("/dev/net/tun")):
106 with Test("unknown device types") as context:
107 test_unknown_types(context)
109 if system != "Windows":
110 with Test("test DeviceStandby = yes") as context:
111 test_device_standby(context)