test_gnunet_fs_rec.py.in 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!@PYTHON@
  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(os.path.join(os.getenv("TEMP"), "gnunet-test-fs-py-rec"), True)
  50. else:
  51. shutil.rmtree("/tmp/gnunet-test-fs-py-rec", True)
  52. arm = subprocess.Popen([gnunetarm, '-sq', '-c', 'test_gnunet_fs_rec_data.conf'])
  53. arm.communicate()
  54. # pray that `tar' is in PATH.
  55. # FIXME: Actually we should check for that and output
  56. # a message if it isn't found.
  57. tar = tarfile.open('test_gnunet_fs_rec_data.tgz')
  58. tar.extractall()
  59. # first, basic publish-search-download run
  60. try:
  61. pub = pexpect()
  62. pub.spawn(None, [publish, '-c', 'test_gnunet_fs_rec_data.conf', '-k', 'testdir', 'dir/'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  63. # Can't say much for publishing, except that the last one is the toplevel directory
  64. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  65. pub.expect("stdout", re.compile(r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
  66. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  67. pub.expect("stdout", re.compile(r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
  68. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  69. pub.expect("stdout", re.compile(r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
  70. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  71. pub.expect("stdout", re.compile(r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
  72. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  73. pub.expect("stdout", re.compile(r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
  74. pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
  75. pub.expect("stdout", re.compile(r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
  76. pub.expect("stdout", re.compile(r"Publishing `.+[\\/]dir[\\/]' done\.\r?\n"))
  77. m = pub.expect("stdout", re.compile(r".+\r?\n"))
  78. if not m:
  79. sys.exit(3)
  80. output = m.string
  81. url = output[output.find("`")+1:output.find("'")]
  82. down = pexpect()
  83. down.spawn(None, [download, '-c', 'test_gnunet_fs_rec_data.conf', '-R', '-o', 'rdir.gnd', url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  84. down.expect("stdout", re.compile(r"Downloading `rdir.gnd' done (.*).\r?\n"))
  85. d = pexpect()
  86. d.spawn(None, [directory, '-c', 'test_gnunet_fs_rec_data.conf', 'rdir/a.gnd'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  87. d.expect("stdout", re.compile(r"Directory `a/' meta data:\r?\n"))
  88. d.expect("stdout", re.compile(r"Directory `a/' contents:\r?\n"))
  89. d.expect("stdout", re.compile(r"COPYING (.*)\r?\n"))
  90. d.expect("stdout", re.compile(r"INSTALL (.*)\r?\n"))
  91. os.remove("rdir/b.gnd")
  92. os.remove("rdir/a.gnd")
  93. diff = dcdiff('dir', 'rdir')
  94. if len(diff) != 0:
  95. raise Exception("Unexpected difference between source directory and downloaded result:\n{}".format(diff))
  96. finally:
  97. arm = subprocess.Popen([gnunetarm, '-eq', '-c', 'test_gnunet_fs_rec_data.conf'])
  98. arm.communicate()
  99. if os.name == "nt":
  100. shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-fs-py-rec"), True)
  101. else:
  102. shutil.rmtree("/tmp/gnunet-test-fs-py-rec", True)
  103. shutil.rmtree("dir", True)
  104. shutil.rmtree("rdir", True)
  105. shutil.rmtree("rdir.gnd", True)