f54da35644bbe7e8e5b1ebb6d43a1203a48ae4f3
[oweals/gnunet.git] / src / peerinfo-tool / test_gnunet_peerinfo.py.in
1 #!@PYTHON@
2 #    This file is part of GNUnet.
3 #    (C) 2010, 2018 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 # Testcase for gnunet-peerinfo
19 import sys
20 import os
21 import subprocess
22 import re
23 import shutil
24 import time
25
26 srcdir = "../.."
27 gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
28 if gnunet_pyexpect_dir not in sys.path:
29     sys.path.append(gnunet_pyexpect_dir)
30
31 from gnunet_pyexpect import pexpect
32
33 if os.name == 'posix':
34     peerinfo = './gnunet-peerinfo'
35     gnunetarm = 'gnunet-arm'
36     gnunettesting = 'gnunet-testing'
37 elif os.name == 'nt':
38     peerinfo = './gnunet-peerinfo.exe'
39     gnunetarm = 'gnunet-arm.exe'
40     gnunettesting = 'gnunet-testing.exe'
41
42 pinfo = pexpect()
43
44
45 if os.name == "nt":
46     shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True)
47 else:
48     shutil.rmtree("/tmp/gnunet-test-peerinfo", True)
49
50 # create hostkey via testing lib  # FIXME: The /tmp/ location needs to be adjusted to the TMP variable!
51 hkk = subprocess.Popen([gnunettesting, '-n', '1', '-c', 'test_gnunet_peerinfo_data.conf', '-k', '/tmp/gnunet-test-peerinfo/.hostkey'])
52 hkk.communicate()
53
54 arm = subprocess.Popen([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'])
55 arm.communicate()
56
57 try:
58     pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
59     pinfo.expect("stdout", re.compile(r'I am peer `.*\'.\r?\n'))
60
61     pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
62     pinfo.expect("stdout", re.compile(r'....................................................\r?\n'))
63
64     pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
65     pinfo.expect("stdout", re.compile(r'Invalid command line argument `invalid\'\r?\n'))
66
67     arm = subprocess.Popen([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf'])
68     arm.communicate()
69     time.sleep(1)
70
71     pinfo.spawn(None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
72     pinfo.expect("stdout", re.compile("Peer `.*'\r?\n"))
73     m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n"))
74     while len(m.group(0)) > 0:
75         m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)"))
76
77     pinfo.spawn(None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
78     pinfo.expect("stdout", re.compile("Peer `.*'\r?\n"))
79     m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n"))
80     while len(m.group(0)) > 0:
81         m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)"))
82
83     pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
84     pid = pinfo.read("stdout")
85     pid.strip()
86
87 finally:
88     arm = subprocess.Popen([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'])
89     arm.communicate()
90     if os.name == "nt":
91         shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True)
92     else:
93         shutil.rmtree("/tmp/gnunet-test-peerinfo", True)