doxygen fix
[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
27 srcdir = "../.."
28 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
29 if gnunet_pyexpect_dir not in sys.path:
30   sys.path.append (gnunet_pyexpect_dir)
31
32 from gnunet_pyexpect import pexpect
33
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
49 if os.name == "nt":
50   shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-rec"), True)
51 else:
52   shutil.rmtree ("/tmp/gnunet-test-fs-py-rec", True)
53
54 arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_fs_rec_data.conf'])
55 arm.communicate ()
56
57 # pray that `tar' is in PATH
58 os.system ('tar xfz test_gnunet_fs_rec_data.tgz')
59 # first, basic publish-search-download run
60 try:
61   pub = pexpect ()
62   pub.spawn (None, [publish, '-c', 'test_gnunet_fs_rec_data.conf', '-d', '-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
83   down = pexpect ()
84   down.spawn (None, [download, '-c', 'test_gnunet_fs_rec_data.conf', '-R', '-o', 'rdir.gnd', url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
85   down.expect ("stdout", re.compile (r"Downloading `rdir.gnd' done (.*).\r?\n"))
86
87   d = pexpect ()
88   d.spawn (None, [directory, '-c', 'test_gnunet_fs_rec_data.conf', 'rdir/a.gnd'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
89   d.expect ("stdout", re.compile (r"Directory `a/' meta data:\r?\n"))
90   d.expect ("stdout", re.compile (r"Directory `a/' contents:\r?\n"))
91   d.expect ("stdout", re.compile (r"COPYING (.*)\r?\n"))
92   d.expect ("stdout", re.compile (r"INSTALL (.*)\r?\n"))
93
94   os.remove ("rdir/b.gnd")
95   os.remove ("rdir/a.gnd")
96   if 0 != os.system ("diff -r dir rdir"):
97     raise Exception ("Unexpected difference between source directory and downloaded result")
98   
99
100 finally:
101   arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_fs_rec_data.conf'])
102   arm.communicate ()
103   if os.name == "nt":
104     shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-rec"), True)
105   else:
106     shutil.rmtree ("/tmp/gnunet-test-fs-py-rec", True)
107   shutil.rmtree ("dir", True)
108   shutil.rmtree ("rdir", True)
109   shutil.rmtree ("rdir.gnd", True)