cmd_join.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python3
  2. """Test invite/join error conditions."""
  3. import os
  4. import shutil
  5. from testlib import check, util
  6. from testlib.log import log
  7. from testlib.proc import Tinc
  8. from testlib.test import Test
  9. FAKE_INVITE = "localhost:65535/pVOZMJGm3MqTvTu0UnhMGb2cfuqygiu79MdnERnGYdga5v8C"
  10. def init(ctx: Test) -> Tinc:
  11. """Initialize a node."""
  12. node = ctx.node()
  13. stdin = f"""
  14. init {node}
  15. set Port 0
  16. set Address localhost
  17. set DeviceType dummy
  18. """
  19. node.cmd(stdin=stdin)
  20. return node
  21. def test_invite(foo: Tinc) -> None:
  22. """Test successful 'invite'."""
  23. foo.cmd("set", "Mode", "switch")
  24. foo.cmd("set", "Broadcast", "mst")
  25. foo.start()
  26. log.info("test successful invitation")
  27. out, _ = foo.cmd("invite", "quux")
  28. check.is_in(f"localhost:{foo.port}/", out)
  29. for filename in os.listdir(foo.sub("invitations")):
  30. content = util.read_text(foo.sub(f"invitations/{filename}"))
  31. if filename == "ed25519_key.priv":
  32. check.is_in("-----BEGIN ED25519 PRIVATE KEY-----", content)
  33. else:
  34. check.is_in("Broadcast = mst", content)
  35. check.is_in("Mode = switch", content)
  36. check.is_in("Address = localhost", content)
  37. check.is_in("Name = quux", content)
  38. check.is_in(f"NetName = {foo}", content)
  39. check.is_in(f"ConnectTo = {foo}", content)
  40. def test_invite_errors(foo: Tinc) -> None:
  41. """Test invite error conditions."""
  42. log.info("invite node with tincd stopped")
  43. _, err = foo.cmd("invite", "foobar", code=1)
  44. check.is_in("Could not open pid file", err)
  45. log.info("start node %s", foo)
  46. foo.start()
  47. log.info("invite without arguments")
  48. _, err = foo.cmd("invite", code=1)
  49. check.is_in("Not enough arguments", err)
  50. log.info("invite with too many arguments")
  51. _, err = foo.cmd("invite", "foo", "bar", code=1)
  52. check.is_in("Too many arguments", err)
  53. log.info("invite with invalid name")
  54. _, err = foo.cmd("invite", "!@#", code=1)
  55. check.is_in("Invalid name for node", err)
  56. log.info("invite existing node")
  57. _, err = foo.cmd("invite", foo.name, code=1)
  58. check.is_in("already exists", err)
  59. if os.name != "nt":
  60. log.info("bad permissions on invitations are fixed")
  61. invites = foo.sub("invitations")
  62. os.chmod(invites, 0)
  63. out, _ = foo.cmd("invite", "foobar")
  64. check.has_prefix(out, "localhost:")
  65. log.info("invitations directory is created with bad permissions on parent")
  66. shutil.rmtree(invites)
  67. os.chmod(foo.work_dir, 0o500)
  68. out, _ = foo.cmd("invite", "foobar")
  69. check.has_prefix(out, "localhost:")
  70. check.true(os.access(invites, os.W_OK))
  71. log.info("fully block access to configuration directory")
  72. work_dir = foo.sub("test_no_access")
  73. os.mkdir(work_dir, mode=0)
  74. _, err = foo.cmd("-c", work_dir, "invite", "foobar", code=1)
  75. check.is_in("Could not open", err)
  76. def test_join_errors(foo: Tinc) -> None:
  77. """Test join error conditions."""
  78. log.info("try joining with redundant arguments")
  79. _, err = foo.cmd("join", "bar", "quux", code=1)
  80. check.is_in("Too many arguments", err)
  81. log.info("try joining with existing configuration")
  82. _, err = foo.cmd("join", FAKE_INVITE, code=1)
  83. check.is_in("already exists", err)
  84. log.info("try running without an invite URL")
  85. work_dir = foo.sub("test_no_invite")
  86. join = foo.tinc("-c", work_dir, "join")
  87. _, err = join.communicate(input="")
  88. check.equals(1, join.returncode)
  89. check.is_in("Error while reading", err)
  90. log.info("try using an invalid invite")
  91. work_dir = foo.sub("test_invalid_invite")
  92. _, err = foo.cmd("-c", work_dir, "join", FAKE_INVITE, code=1)
  93. check.is_in("Could not connect to", err)
  94. if os.name != "nt":
  95. log.info("bad permissions on configuration directory are fixed")
  96. work_dir = foo.sub("wd_access_test")
  97. os.mkdir(work_dir, mode=400)
  98. _, err = foo.cmd("-c", work_dir, "join", FAKE_INVITE, code=1)
  99. check.is_in("Could not connect to", err)
  100. check.true(os.access(work_dir, mode=os.W_OK))
  101. with Test("run invite success tests") as context:
  102. test_invite(init(context))
  103. with Test("run invite error tests") as context:
  104. test_invite_errors(init(context))
  105. with Test("run join tests") as context:
  106. test_join_errors(init(context))