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