splice.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. """Test splicing connection between tinc peers."""
  3. import os
  4. import subprocess as subp
  5. import typing as T
  6. from testlib import check, cmd, path
  7. from testlib.log import log
  8. from testlib.proc import Tinc, Script
  9. from testlib.test import Test
  10. from testlib.feature import SANDBOX_LEVEL
  11. def init(ctx: Test, *options: str) -> T.Tuple[Tinc, Tinc]:
  12. """Initialize new test nodes."""
  13. custom = os.linesep.join(options)
  14. log.info('init two nodes with options "%s"', custom)
  15. foo, bar = ctx.node(), ctx.node()
  16. stdin = f"""
  17. init {foo}
  18. set Port 0
  19. set DeviceType dummy
  20. set Address localhost
  21. set AutoConnect no
  22. set Subnet 10.96.96.1
  23. set Sandbox {SANDBOX_LEVEL}
  24. {custom}
  25. """
  26. foo.cmd("--force", stdin=stdin)
  27. stdin = f"""
  28. init {bar}
  29. set Port 0
  30. set Address localhost
  31. set DeviceType dummy
  32. set AutoConnect no
  33. set Subnet 10.96.96.2
  34. set Sandbox {SANDBOX_LEVEL}
  35. {custom}
  36. """
  37. bar.cmd("--force", stdin=stdin)
  38. foo.add_script(Script.SUBNET_UP)
  39. bar.add_script(Script.SUBNET_UP)
  40. foo.start()
  41. bar.start()
  42. log.info("exchange host configs")
  43. cmd.exchange(foo, bar)
  44. return foo, bar
  45. def splice(foo: Tinc, bar: Tinc, protocol: str) -> subp.Popen:
  46. """Start splice between nodes."""
  47. args = [
  48. path.SPLICE_PATH,
  49. foo.name,
  50. "localhost",
  51. str(foo.port),
  52. bar.name,
  53. "localhost",
  54. str(bar.port),
  55. protocol,
  56. ]
  57. log.info("splice with args %s", args)
  58. return subp.Popen(args)
  59. def test_splice(ctx: Test, protocol: str, *options: str) -> None:
  60. """Splice connection and check that it fails."""
  61. log.info("no splicing allowed (%s)", protocol)
  62. foo, bar = init(ctx, *options)
  63. log.info("waiting for subnets to come up")
  64. foo[Script.SUBNET_UP].wait()
  65. bar[Script.SUBNET_UP].wait()
  66. splice_proc = splice(foo, bar, protocol)
  67. try:
  68. check.nodes(foo, 1)
  69. check.nodes(bar, 1)
  70. finally:
  71. splice_proc.kill()
  72. with Test("sptps") as context:
  73. test_splice(context, "17.7")
  74. with Test("legacy") as context:
  75. test_splice(context, "17.0", "set ExperimentalProtocol no")