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