more client code
[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 pexpect
22 from __future__ import print_function
23 import os
24 #import signal
25 import re
26 import subprocess
27 import sys
28 import shutil
29 import time
30
31 class pexpect (object):
32   def __init__ (self):
33     super (pexpect, self).__init__ ()
34
35   def spawn (self, stdin, arglist, *pargs, **kwargs):
36     self.proc = subprocess.Popen (arglist, *pargs, **kwargs)
37     if self.proc is None:
38       print ("Failed to spawn a process {0}".format (arglist))
39       sys.exit (1)
40     if stdin is not None:
41       self.stdo, self.stde = self.proc.communicate (stdin)
42     else:
43       self.stdo, self.stde = self.proc.communicate ()
44     return self.proc
45
46   def expect (self, s, r, flags=0):
47     stream = self.stdo if s == 'stdout' else self.stde
48     if isinstance (r, str):
49       if r == "EOF":
50         if len (stream) == 0:
51           return True
52         else:
53           print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r, stream))
54           sys.exit (2)
55       raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r))
56     m = r.match (stream, flags)
57     if not m:
58       print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r.pattern, stream))
59       sys.exit (2)
60     stream = stream[m.end ():]
61     if s == 'stdout':
62       self.stdo = stream
63     else:
64       self.stde = stream
65     return m
66
67   def read (self, s, size=-1):
68     stream = self.stdo if s == 'stdout' else self.stde
69     result = ""
70     if size < 0:
71       result = stream
72       new_stream = ""
73     else:
74       result = stream[0:size]
75       new_stream = stream[size:]
76     if s == 'stdout':
77       self.stdo = new_stream
78     else:
79       self.stde = new_stream
80     return result
81
82 if os.name == 'posix':
83   peerinfo = 'gnunet-peerinfo'
84   gnunetarm = 'gnunet-arm'
85 elif os.name == 'nt':
86   peerinfo = 'gnunet-peerinfo.exe'
87   gnunetarm = 'gnunet-arm.exe'
88
89
90
91 pinfo = pexpect ()
92 pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-L', 'ERROR'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
93 pinfo.expect ("stdout", re.compile (r'Error in communication with PEERINFO service\r?\n'))
94 pinfo.expect ("stdout", "EOF")
95
96 if os.name == "nt":
97   shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
98 else:
99   shutil.rmtree ("/tmp/gnunet-test-peerinfo", True)
100 arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'])
101 arm.communicate ()
102
103 try:
104   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
105   pinfo.expect ("stdout", re.compile (r'I am peer `.*\'.\r?\n'))
106   pinfo.expect ("stdout", "EOF")
107
108   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
109   pinfo.expect ("stdout", re.compile (r'.......................................................................................................\r?\n'))
110   pinfo.expect ("stdout", "EOF")
111
112   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
113   pinfo.expect ("stdout", re.compile (r'Invalid command line argument `invalid\'\r?\n'))
114   pinfo.expect ("stdout", "EOF")
115
116   arm = subprocess.Popen ([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf'])
117   arm.communicate ()
118   time.sleep (1)
119
120   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
121   pinfo.expect ("stdout", re.compile ("Peer `.*'\r?\n"))
122   m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n"))
123   while len (m.group (0)) > 0:
124     m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)"))
125   pinfo.expect ("stdout", "EOF")
126
127   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
128   pinfo.expect ("stdout", re.compile ("Peer `.*'\r?\n"))
129   m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n"))
130   while len (m.group (0)) > 0:
131     m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)"))
132   pinfo.expect ("stdout", "EOF")
133
134   pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
135   pid = pinfo.read ("stdout")
136   pid.strip ()
137   
138 finally:
139   arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'])
140   arm.communicate ()
141   if os.name == "nt":
142     shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
143   else:
144     shutil.rmtree ("/tmp/gnunet-test-peerinfo", True)
145