summaryrefslogtreecommitdiff
path: root/src/peerinfo-tool/test_gnunet_peerinfo.py.in
blob: d07224b14d6ed0d48644e25783e86338491aaff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!@PYTHON@
#    This file is part of GNUnet.
#    (C) 2010, 2018 Christian Grothoff (and other contributing authors)
#
#    GNUnet is free software: you can redistribute it and/or modify it
#    under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License,
#    or (at your option) any later version.
#
#    GNUnet is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Affero General Public License for more details.
#   
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    SPDX-License-Identifier: AGPL3.0-or-later
#
# Testcase for gnunet-peerinfo
import sys
import os
import subprocess
import re
import shutil
import time

srcdir = "../.."
gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
if gnunet_pyexpect_dir not in sys.path:
    sys.path.append(gnunet_pyexpect_dir)

from gnunet_pyexpect import pexpect

if os.name == 'posix':
    peerinfo = './gnunet-peerinfo'
    gnunetarm = 'gnunet-arm'
    gnunettesting = 'gnunet-testing'
elif os.name == 'nt':
    peerinfo = './gnunet-peerinfo.exe'
    gnunetarm = 'gnunet-arm.exe'
    gnunettesting = 'gnunet-testing.exe'

pinfo = pexpect()


if os.name == "nt":
    shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True)
else:
    shutil.rmtree("/tmp/gnunet-test-peerinfo", True)

# create hostkey via testing lib  # FIXME: The /tmp/ location needs to be adjusted to the TMP variable!
hkk = subprocess.Popen([gnunettesting, '-n', '1', '-c', 'test_gnunet_peerinfo_data.conf', '-k', '/tmp/gnunet-test-peerinfo/.hostkey'])
hkk.communicate()

arm = subprocess.Popen([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'])
arm.communicate()

try:
    pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    pinfo.expect("stdout", re.compile(r'I am peer `.*\'.\r?\n'))

    pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    pinfo.expect("stdout", re.compile(r'....................................................\r?\n'))

    pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    pinfo.expect("stdout", re.compile(r'Invalid command line argument `invalid\'\r?\n'))

    arm = subprocess.Popen([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf'])
    arm.communicate()
    time.sleep(1)

    pinfo.spawn(None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    pinfo.expect("stdout", re.compile("Peer `.*'\r?\n"))
    m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n"))
    while len(m.group(0)) > 0:
        m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)"))

    pinfo.spawn(None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    pinfo.expect("stdout", re.compile("Peer `.*'\r?\n"))
    m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n"))
    while len(m.group(0)) > 0:
        m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)"))

    pinfo.spawn(None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    pid = pinfo.read("stdout")
    pid.strip()

finally:
    arm = subprocess.Popen([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'])
    arm.communicate()
    if os.name == "nt":
        shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True)
    else:
        shutil.rmtree("/tmp/gnunet-test-peerinfo", True)