3 """Test recent address cache."""
9 from testlib import check
10 from testlib.log import log
11 from testlib.proc import Tinc, Script
12 from testlib.test import Test
15 def init(ctx: Test) -> T.Tuple[Tinc, Tinc]:
16 """Create test node."""
17 foo, bar = ctx.node(), ctx.node()
31 def connect_nodes(foo: Tinc, bar: Tinc) -> None:
32 """Start second node and wait for connection."""
33 log.info("connect nodes")
35 bar[foo.script_up].wait()
36 foo[bar.script_up].wait()
39 def run_tests(ctx: Test) -> None:
43 log.info("cache directory must exist after init")
44 check.dir_exists(foo.sub("cache"))
46 foo.add_script(Script.TINC_UP)
47 foo.add_script(Script.INVITATION_ACCEPTED)
50 log.info("invite %s to %s", bar, foo)
51 invite, _ = foo.cmd("invite", bar.name)
52 invite = invite.strip()
54 log.info("join %s to %s", bar, foo)
55 bar.cmd("join", invite)
57 log.info("cache directory must exist after join")
58 check.dir_exists(bar.sub("cache"))
60 log.info("invitee address must be cached after invitation is accepted")
61 foo[Script.INVITATION_ACCEPTED].wait()
62 check.file_exists(foo.sub(f"cache/{bar}"))
63 os.remove(foo.sub(f"cache/{bar}"))
65 log.info("configure %s", bar)
66 bar.cmd("set", "DeviceType", "dummy")
67 bar.cmd("set", "Port", "0")
69 log.info("add host-up scripts")
70 foo.add_script(bar.script_up)
71 bar.add_script(foo.script_up)
73 connect_nodes(foo, bar)
75 log.info("%s must cache %s's public address", bar, foo)
76 check.file_exists(bar.sub(f"cache/{foo}"))
78 log.info("%s must not cache %s's outgoing address", foo, bar)
79 assert not os.path.exists(foo.sub(f"cache/{bar}"))
81 log.info("stop node %s", bar)
84 log.info("remove %s cache directory", bar)
85 shutil.rmtree(bar.sub("cache"))
87 connect_nodes(foo, bar)
89 log.info("make sure %s cache was recreated", bar)
90 check.file_exists(bar.sub(f"cache/{foo}"))
92 log.info("stop nodes")
96 log.info("remove Address from all nodes")
98 node.cmd("del", "Address", code=None)
100 node.cmd("del", f"{peer}.Address", code=None)
101 bar.cmd("add", "ConnectTo", foo.name)
103 log.info("make sure connection works using just the cached address")
105 connect_nodes(foo, bar)
108 with Test("run address cache tests") as context: