test_gnunet_peerinfo.py.in 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!@PYTHONEXE@
  2. # This file is part of GNUnet.
  3. # (C) 2010, 2018 Christian Grothoff (and other contributing authors)
  4. #
  5. # GNUnet is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU Affero General Public License as published
  7. # by the Free Software Foundation, either version 3 of the License,
  8. # or (at your option) any later version.
  9. #
  10. # GNUnet is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # SPDX-License-Identifier: AGPL3.0-or-later
  19. #
  20. # Testcase for gnunet-peerinfo
  21. import sys
  22. import os
  23. import subprocess
  24. import re
  25. import shutil
  26. import time
  27. srcdir = "../.."
  28. gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
  29. if gnunet_pyexpect_dir not in sys.path:
  30. sys.path.append(gnunet_pyexpect_dir)
  31. from gnunet_pyexpect import pexpect
  32. #save LANG and set it to C
  33. mylang = os.environ['LANG']
  34. os.environ['LANG'] = 'C'
  35. if os.name == 'posix':
  36. peerinfo = './gnunet-peerinfo'
  37. gnunetarm = 'gnunet-arm'
  38. gnunettesting = 'gnunet-testing'
  39. elif os.name == 'nt':
  40. peerinfo = './gnunet-peerinfo.exe'
  41. gnunetarm = 'gnunet-arm.exe'
  42. gnunettesting = 'gnunet-testing.exe'
  43. pinfo = pexpect()
  44. if os.name == "nt":
  45. shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True)
  46. else:
  47. shutil.rmtree("/tmp/gnunet-test-peerinfo", True)
  48. # create hostkey via testing lib # FIXME: The /tmp/ location needs to be adjusted to the TMP variable!
  49. hkk = subprocess.Popen([
  50. gnunettesting, '-n', '1', '-c', 'test_gnunet_peerinfo_data.conf', '-k',
  51. '/tmp/gnunet-test-peerinfo/.hostkey'
  52. ])
  53. hkk.communicate()
  54. arm = subprocess.Popen([
  55. gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'
  56. ])
  57. arm.communicate()
  58. try:
  59. pinfo.spawn(
  60. None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'],
  61. stdout=subprocess.PIPE,
  62. stderr=subprocess.STDOUT
  63. )
  64. pinfo.expect("stdout", re.compile(r'I am peer `.*\'.\r?\n'))
  65. pinfo.spawn(
  66. None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'],
  67. stdout=subprocess.PIPE,
  68. stderr=subprocess.STDOUT
  69. )
  70. pinfo.expect(
  71. "stdout",
  72. re.
  73. compile(r'....................................................\r?\n')
  74. )
  75. pinfo.spawn(
  76. None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'],
  77. stdout=subprocess.PIPE,
  78. stderr=subprocess.STDOUT
  79. )
  80. pinfo.expect(
  81. "stdout", re.compile(r'Invalid command line argument `invalid\'\r?\n')
  82. )
  83. arm = subprocess.Popen([
  84. gnunetarm, '-q', '-i', 'transport', '-c',
  85. 'test_gnunet_peerinfo_data.conf'
  86. ])
  87. arm.communicate()
  88. time.sleep(1)
  89. pinfo.spawn(
  90. None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf'],
  91. stdout=subprocess.PIPE,
  92. stderr=subprocess.STDOUT
  93. )
  94. pinfo.expect("stdout", re.compile("Peer `.*'\r?\n"))
  95. m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n"))
  96. while len(m.group(0)) > 0:
  97. m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)"))
  98. pinfo.spawn(
  99. None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf', '-n'],
  100. stdout=subprocess.PIPE,
  101. stderr=subprocess.STDOUT
  102. )
  103. pinfo.expect("stdout", re.compile("Peer `.*'\r?\n"))
  104. m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n"))
  105. while len(m.group(0)) > 0:
  106. m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)"))
  107. pinfo.spawn(
  108. None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'],
  109. stdout=subprocess.PIPE,
  110. stderr=subprocess.STDOUT
  111. )
  112. pid = pinfo.read("stdout")
  113. pid.strip()
  114. finally:
  115. arm = subprocess.Popen([
  116. gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'
  117. ])
  118. arm.communicate()
  119. if os.name == "nt":
  120. shutil.rmtree(
  121. os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True
  122. )
  123. else:
  124. shutil.rmtree("/tmp/gnunet-test-peerinfo", True)
  125. #Reset LANG
  126. os.environ['LANG'] = mylang