2 # This file is part of GNUnet.
3 # (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
20 # Testcase for gnunet-peerinfo
22 from __future__ import print_function
31 class pexpect (object):
33 super (pexpect, self).__init__ ()
35 def spawn (self, stdin, arglist, *pargs, **kwargs):
36 self.proc = subprocess.Popen (arglist, *pargs, **kwargs)
38 print ("Failed to spawn a process {0}".format (arglist))
41 self.stdo, self.stde = self.proc.communicate (stdin)
43 self.stdo, self.stde = self.proc.communicate ()
46 def expect (self, s, r, flags=0):
47 stream = self.stdo if s == 'stdout' else self.stde
48 if isinstance (r, str):
53 print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r, stream))
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)
58 print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r.pattern, stream))
60 stream = stream[m.end ():]
67 def read (self, s, size=-1):
68 stream = self.stdo if s == 'stdout' else self.stde
74 result = stream[0:size]
75 new_stream = stream[size:]
77 self.stdo = new_stream
79 self.stde = new_stream
82 if os.name == 'posix':
83 peerinfo = 'gnunet-peerinfo'
84 gnunetarm = 'gnunet-arm'
86 peerinfo = 'gnunet-peerinfo.exe'
87 gnunetarm = 'gnunet-arm.exe'
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")
97 shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
99 shutil.rmtree ("/tmp/gnunet-test-peerinfo", True)
100 arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'])
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")
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")
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")
116 arm = subprocess.Popen ([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf'])
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")
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")
134 pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
135 pid = pinfo.read ("stdout")
139 arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'])
142 shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
144 shutil.rmtree ("/tmp/gnunet-test-peerinfo", True)