LRN: 0004-Remove-pyexpect-code-import-it-from-the-module.patch
[oweals/gnunet.git] / src / peerinfo-tool / test_gnunet_peerinfo.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 gnunet-peerinfo
21 import sys
22 import os
23 import subprocess
24 import re
25 import shutil
26 import time
27
28 srcdir = "../.."
29 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib", "gnunet_pyexpect")
30 if gnunet_pyexpect_dir not in sys.path:
31   sys.path.append (gnunet_pyexpect_dir)
32
33 from gnunet_pyexpect import pexpect
34
35 if os.name == 'posix':
36   peerinfo = 'gnunet-peerinfo'
37   gnunetarm = 'gnunet-arm'
38 elif os.name == 'nt':
39   peerinfo = 'gnunet-peerinfo.exe'
40   gnunetarm = 'gnunet-arm.exe'
41
42
43
44 pinfo = pexpect ()
45 pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-L', 'ERROR'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
46 pinfo.expect ("stdout", re.compile (r'Error in communication with PEERINFO service\r?\n'))
47 pinfo.expect ("stdout", "EOF")
48
49 if os.name == "nt":
50   shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-peerinfo"), True)
51 else:
52   shutil.rmtree ("/tmp/gnunet-test-peerinfo", True)
53 arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'])
54 arm.communicate ()
55
56 try:
57   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
58   pinfo.expect ("stdout", re.compile (r'I am peer `.*\'.\r?\n'))
59
60   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
61   pinfo.expect ("stdout", re.compile (r'.......................................................................................................\r?\n'))
62
63   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
64   pinfo.expect ("stdout", re.compile (r'Invalid command line argument `invalid\'\r?\n'))
65
66   arm = subprocess.Popen ([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf'])
67   arm.communicate ()
68   time.sleep (1)
69
70   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
71   pinfo.expect ("stdout", re.compile ("Peer `.*'\r?\n"))
72   m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n"))
73   while len (m.group (0)) > 0:
74     m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)"))
75
76   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
77   pinfo.expect ("stdout", re.compile ("Peer `.*'\r?\n"))
78   m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n"))
79   while len (m.group (0)) > 0:
80     m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)"))
81
82   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
83   pid = pinfo.read ("stdout")
84   pid.strip ()
85   
86 finally:
87   arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'])
88   arm.communicate ()
89   if os.name == "nt":
90     shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-peerinfo"), True)
91   else:
92     shutil.rmtree ("/tmp/gnunet-test-peerinfo", True)
93