invite_tinc_up.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. """Test inviting tinc nodes through tinc-up script."""
  3. import os
  4. import typing as T
  5. from testlib import check, util
  6. from testlib.log import log
  7. from testlib.proc import Tinc, Script
  8. from testlib.test import Test
  9. IFCONFIG = "93.184.216.34/24"
  10. ROUTES_IPV6 = ("2606:2800:220:1::/64", "2606:2800:220:1:248:1893:25c8:1946")
  11. BAD_IPV4 = "1234::"
  12. ED_PUBKEY = "Ed25519PublicKey"
  13. def make_inv_created(export_output: str) -> str:
  14. """Generate script for invitation-created script."""
  15. return f'''
  16. node, invite = os.environ['NODE'], os.environ['INVITATION_FILE']
  17. log.info('writing to invitation file %s, node %s', invite, node)
  18. script = f"""
  19. Name = {{node}}
  20. Ifconfig = {IFCONFIG}
  21. Route = {' '.join(ROUTES_IPV6)}
  22. Route = 1.2.3.4 {BAD_IPV4}
  23. {export_output}
  24. """.strip()
  25. with open(invite, 'w', encoding='utf-8') as f:
  26. f.write(script)
  27. '''
  28. def init(ctx: Test) -> T.Tuple[Tinc, Tinc]:
  29. """Initialize new test nodes."""
  30. foo, bar = ctx.node(), ctx.node()
  31. stdin = f"""
  32. init {foo}
  33. set Port 0
  34. set DeviceType dummy
  35. set Address localhost
  36. """
  37. foo.cmd(stdin=stdin)
  38. foo.start()
  39. return foo, bar
  40. def run_tests(ctx: Test) -> None:
  41. """Run all tests."""
  42. foo, bar = init(ctx)
  43. log.info("run export")
  44. export, _ = foo.cmd("export")
  45. assert export
  46. log.info("adding invitation-created script")
  47. code = make_inv_created(export)
  48. foo.add_script(Script.INVITATION_CREATED, code)
  49. log.info("inviting %s", bar)
  50. url, _ = foo.cmd("invite", bar.name)
  51. url = url.strip()
  52. assert url
  53. log.info('joining %s to %s with "%s"', bar, foo, url)
  54. bar.cmd("--batch", "join", url)
  55. bar.cmd("set", "Port", "0")
  56. log.info("comparing host configs")
  57. check.files_eq(foo.sub("hosts", foo.name), bar.sub("hosts", foo.name))
  58. log.info("comparing public keys")
  59. foo_key = util.find_line(foo.sub("hosts", bar.name), ED_PUBKEY)
  60. bar_key = util.find_line(bar.sub("hosts", bar.name), ED_PUBKEY)
  61. check.equals(foo_key, bar_key)
  62. log.info("bar.tinc-up must not exist")
  63. assert not os.path.exists(bar.sub("tinc-up"))
  64. inv = bar.sub("tinc-up.invitation")
  65. log.info("testing %s", inv)
  66. content = util.read_text(inv)
  67. check.is_in(IFCONFIG, content)
  68. check.not_in(BAD_IPV4, content)
  69. for route in ROUTES_IPV6:
  70. check.is_in(route, content)
  71. if os.name != "nt":
  72. assert not os.access(inv, os.X_OK)
  73. with Test("invite-tinc-up") as context:
  74. run_tests(context)