09e55c144e63f94e748917cc77a5235e969500fb
[oweals/gnunet.git] / src / fs / test_gnunet_fs_rec.py.in
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
6 #    it under the terms of the GNU General Public License as published
7 #    by the Free Software Foundation; either version 2, or (at your
8 #    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 #    General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with GNUnet; see the file COPYING.  If not, write to the
17 #    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 #    Boston, MA 02111-1307, USA.
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 import pydiffer
29
30 srcdir = "../.."
31 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
32 if gnunet_pyexpect_dir not in sys.path:
33   sys.path.append (gnunet_pyexpect_dir)
34
35 from gnunet_pyexpect import pexpect
36 from pydiffer import dcdiff
37
38 if os.name == 'posix':
39   download = 'gnunet-download'
40   gnunetarm = 'gnunet-arm'
41   publish = 'gnunet-publish'
42   unindex = 'gnunet-unindex'
43   search = 'gnunet-search'
44   directory = 'gnunet-directory'
45 elif os.name == 'nt':
46   download = 'gnunet-download.exe'
47   gnunetarm = 'gnunet-arm.exe'
48   publish = 'gnunet-publish.exe'
49   unindex = 'gnunet-unindex.exe'
50   search = 'gnunet-search.exe'
51   directory = 'gnunet-directory.exe'
52
53 if os.name == "nt":
54   shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-rec"), True)
55 else:
56   shutil.rmtree ("/tmp/gnunet-test-fs-py-rec", True)
57
58 arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_fs_rec_data.conf'])
59 arm.communicate ()
60
61 # pray that `tar' is in PATH
62 tar = tarfile.open ('test_gnunet_fs_rec_data.tgz')
63 tar.extractall ()
64 # first, basic publish-search-download run
65 try:
66   pub = pexpect ()
67   pub.spawn (None, [publish, '-c', 'test_gnunet_fs_rec_data.conf', '-d', '-k', 'testdir', 'dir/'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
68   # Can't say much for publishing, except that the last one is the toplevel directory
69   pub.expect ("stdout", re.compile (r"Publishing `.+' done\.\r?\n"))
70   pub.expect ("stdout", re.compile (r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
71   pub.expect ("stdout", re.compile (r"Publishing `.+' done\.\r?\n"))
72   pub.expect ("stdout", re.compile (r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
73   pub.expect ("stdout", re.compile (r"Publishing `.+' done\.\r?\n"))
74   pub.expect ("stdout", re.compile (r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
75   pub.expect ("stdout", re.compile (r"Publishing `.+' done\.\r?\n"))
76   pub.expect ("stdout", re.compile (r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
77   pub.expect ("stdout", re.compile (r"Publishing `.+' done\.\r?\n"))
78   pub.expect ("stdout", re.compile (r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
79   pub.expect ("stdout", re.compile (r"Publishing `.+' done\.\r?\n"))
80   pub.expect ("stdout", re.compile (r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"))
81   pub.expect ("stdout", re.compile (r"Publishing `.+[\\/]dir[\\/]' done\.\r?\n"))
82   m = pub.expect ("stdout", re.compile (r".+\r?\n"))
83   if not m:
84     sys.exit (3)
85   output = m.string
86   url = output[output.find ("`")+1:output.find("'")]
87
88   down = pexpect ()
89   down.spawn (None, [download, '-c', 'test_gnunet_fs_rec_data.conf', '-R', '-o', 'rdir.gnd', url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
90   down.expect ("stdout", re.compile (r"Downloading `rdir.gnd' done (.*).\r?\n"))
91
92   d = pexpect ()
93   d.spawn (None, [directory, '-c', 'test_gnunet_fs_rec_data.conf', 'rdir/a.gnd'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
94   d.expect ("stdout", re.compile (r"Directory `a/' meta data:\r?\n"))
95   d.expect ("stdout", re.compile (r"Directory `a/' contents:\r?\n"))
96   d.expect ("stdout", re.compile (r"COPYING (.*)\r?\n"))
97   d.expect ("stdout", re.compile (r"INSTALL (.*)\r?\n"))
98
99   os.remove ("rdir/b.gnd")
100   os.remove ("rdir/a.gnd")
101   diff = dcdiff ('dir', 'rdir')
102   if len (diff) != 0:
103     raise Exception ("Unexpected difference between source directory and downloaded result:\n{}".format (diff))
104   
105
106 finally:
107   arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_fs_rec_data.conf'])
108   arm.communicate ()
109   if os.name == "nt":
110     shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-rec"), True)
111   else:
112     shutil.rmtree ("/tmp/gnunet-test-fs-py-rec", True)
113   shutil.rmtree ("dir", True)
114   shutil.rmtree ("rdir", True)
115   shutil.rmtree ("rdir.gnd", True)