test_gnunet_fs_rec.py.in 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!@PYTHONEXE@
  2. # This file is part of GNUnet.
  3. # (C) 2010 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 file-sharing command-line tools (recursive publishing & download)
  21. import sys
  22. import os
  23. import subprocess
  24. import re
  25. import shutil
  26. import tarfile
  27. import filecmp
  28. srcdir = "../.."
  29. gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
  30. if gnunet_pyexpect_dir not in sys.path:
  31. sys.path.append(gnunet_pyexpect_dir)
  32. from gnunet_pyexpect import pexpect
  33. from pydiffer import dcdiff
  34. if os.name == 'posix':
  35. download = './gnunet-download'
  36. gnunetarm = 'gnunet-arm'
  37. publish = './gnunet-publish'
  38. unindex = './gnunet-unindex'
  39. search = './gnunet-search'
  40. directory = './gnunet-directory'
  41. elif os.name == 'nt':
  42. download = './gnunet-download.exe'
  43. gnunetarm = 'gnunet-arm.exe'
  44. publish = './gnunet-publish.exe'
  45. unindex = './gnunet-unindex.exe'
  46. search = './gnunet-search.exe'
  47. directory = './gnunet-directory.exe'
  48. if os.name == "nt":
  49. shutil.rmtree(
  50. os.path.join(os.getenv("TEMP"), "gnunet-test-fs-py-rec"), True
  51. )
  52. else:
  53. shutil.rmtree("/tmp/gnunet-test-fs-py-rec", True)
  54. arm = subprocess.Popen([gnunetarm, '-sq', '-c', 'test_gnunet_fs_rec_data.conf'])
  55. arm.communicate()
  56. tar = tarfile.open('test_gnunet_fs_rec_data.tgz')
  57. tar.extractall()
  58. # first, basic publish-search-download run
  59. try:
  60. pub = pexpect()
  61. pub.spawn(
  62. None, [
  63. publish, '-c', 'test_gnunet_fs_rec_data.conf', '-k', 'testdir',
  64. 'dir/'
  65. ],
  66. stdout=subprocess.PIPE,
  67. stderr=subprocess.STDOUT
  68. )
  69. # Can't say much for publishing, except that the last one is the toplevel directory
  70. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  71. pub.expect(
  72. "stdout",
  73. re.compile(
  74. r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
  75. )
  76. )
  77. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  78. pub.expect(
  79. "stdout",
  80. re.compile(
  81. r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
  82. )
  83. )
  84. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  85. pub.expect(
  86. "stdout",
  87. re.compile(
  88. r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
  89. )
  90. )
  91. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  92. pub.expect(
  93. "stdout",
  94. re.compile(
  95. r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
  96. )
  97. )
  98. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  99. pub.expect(
  100. "stdout",
  101. re.compile(
  102. r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
  103. )
  104. )
  105. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  106. pub.expect(
  107. "stdout",
  108. re.compile(
  109. r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
  110. )
  111. )
  112. pub.expect(
  113. "stdout", re.compile(r"Publishing `.+[\\/]dir[\\/]' done\.\r?\n")
  114. )
  115. m = pub.expect("stdout", re.compile(r".+\r?\n"))
  116. if not m:
  117. sys.exit(3)
  118. output = m.string
  119. url = output[output.find("`") + 1:output.find("'")]
  120. down = pexpect()
  121. down.spawn(
  122. None, [
  123. download, '-c', 'test_gnunet_fs_rec_data.conf', '-R', '-o',
  124. 'rdir.gnd', url
  125. ],
  126. stdout=subprocess.PIPE,
  127. stderr=subprocess.STDOUT
  128. )
  129. down.expect("stdout", re.compile(r"Downloading `rdir.gnd' done (.*).\r?\n"))
  130. d = pexpect()
  131. d.spawn(
  132. None, [directory, '-c', 'test_gnunet_fs_rec_data.conf', 'rdir/a.gnd'],
  133. stdout=subprocess.PIPE,
  134. stderr=subprocess.STDOUT
  135. )
  136. d.expect("stdout", re.compile(r"Directory `a/' meta data:\r?\n"))
  137. d.expect("stdout", re.compile(r"Directory `a/' contents:\r?\n"))
  138. d.expect("stdout", re.compile(r"COPYING (.*)\r?\n"))
  139. d.expect("stdout", re.compile(r"INSTALL (.*)\r?\n"))
  140. os.remove("rdir/b.gnd")
  141. os.remove("rdir/a.gnd")
  142. diff = dcdiff('dir', 'rdir')
  143. if len(diff) != 0:
  144. raise Exception(
  145. "Unexpected difference between source directory and downloaded result:\n{}"
  146. .format(diff)
  147. )
  148. finally:
  149. arm = subprocess.Popen([
  150. gnunetarm, '-eq', '-c', 'test_gnunet_fs_rec_data.conf'
  151. ])
  152. arm.communicate()
  153. if os.name == "nt":
  154. shutil.rmtree(
  155. os.path.join(os.getenv("TEMP"), "gnunet-test-fs-py-rec"), True
  156. )
  157. else:
  158. shutil.rmtree("/tmp/gnunet-test-fs-py-rec", True)
  159. shutil.rmtree("dir", True)
  160. shutil.rmtree("rdir", True)
  161. shutil.rmtree("rdir.gnd", True)