added ATS addresstype information to unix
[oweals/gnunet.git] / src / integration-tests / test_integration_disconnect.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
21 import sys
22 import os
23 import subprocess
24 import re
25 import shutil
26 import time
27 import pexpect
28
29 #
30 # This test tests if a fresh peer bootstraps from a hostlist server and then
31 # successfully connects to the server. When both peers are connected
32 # in transport, core, topology, fs, the server is shutdown
33 #
34 # Conditions for successful exit:
35 # Both peers have 0 connected peer in transport, core, topology, fs
36
37 #definitions
38
39 testname = "test_integration_disconnect"
40 verbose = False
41 gnunetarm = ""
42 gnunetstatistics = ""
43 success = False
44 timeout = 100
45
46 #test conditions
47
48
49 def vprintf (msg):
50     if verbose == True:
51         print msg
52
53 def setup ():
54   srcdir = "../.."
55   gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
56   if gnunet_pyexpect_dir not in sys.path:
57     sys.path.append (gnunet_pyexpect_dir)
58   from gnunet_pyexpect import pexpect
59   global gnunetarm      
60   global gnunetstatistics
61   if os.name == 'posix':
62     gnunetarm = 'gnunet-arm'
63     gnunetstatistics = 'gnunet-statistics'
64   elif os.name == 'nt':
65     gnunetarm = 'gnunet-arm.exe'
66     gnunetstatistics = 'gnunet-statistics.exe'    
67   if os.name == "nt":
68     shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
69   else:
70     shutil.rmtree ("/tmp/" + testname, True)
71
72 def start ():
73         vprintf ("Starting bootstrap server & client")
74         try:
75             server = subprocess.Popen ([gnunetarm, '-sq', '-c', './confs/c_bootstrap_server.conf'])
76             server.communicate ()    
77         except OSError:
78             print "Can not start bootstrap server, exiting..."
79             exit (1)
80         try:
81             client = subprocess.Popen ([gnunetarm, '-sq', '-c', 'confs/c_no_nat_client.conf'])
82             client.communicate ()    
83         except OSError:
84             print "Can not start bootstrap client, exiting..."
85             exit (1)
86         vprintf ("Bootstrap server & client started")
87
88 def stop ():
89         try:
90             client = subprocess.Popen ([gnunetarm, '-eq', '-c', 'confs/c_no_nat_client.conf'])
91             client.communicate ()    
92         except OSError:
93             print "Can not stop bootstrap client, exiting..."
94             exit (1)
95         vprintf ("Bootstrap client stopped")
96
97
98 def cleanup ():
99         if os.name == "nt":
100             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-ns"), True)
101             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_no_nat_client"), True)
102         else:
103             shutil.rmtree ("/tmp/c_bootstrap_server/", True)
104             shutil.rmtree ("/tmp/c_no_nat_client/", True)
105
106 def check_statistics (conf, subsystem, name, value):
107     from gnunet_pyexpect import pexpect
108     server = pexpect ()
109     server.spawn (None, [gnunetstatistics, '-c', conf ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
110     #server.expect ("stdout", re.compile (r""))
111     test = server.read("stdout", 10240)
112     if (test.find(str(value)) == -1): 
113         return False
114     else:
115         return True 
116     
117     
118
119 def check_connect ():
120   server_transport_connected = False
121   server_topology_connected = False
122   server_core_connected = False
123   server_core_session_map = False
124   server_fs_connected = False
125   client_transport_connected = False
126   client_topology_connected = False
127   client_core_connected = False
128   client_core_session_map = False
129   client_fs_connected = False
130   
131   connected = False
132   count = 1
133   while ((connected == False) and (count <= timeout)):
134         # Perform checks        
135         if ((False == server_transport_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'transport', '# peers connected',1))):
136           server_transport_connected = True
137           vprintf ('Server transport services is connected')
138           
139         if ((False == client_transport_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'transport', '# peers connected',1))):
140           client_transport_connected = True
141           vprintf ('Client transport services is connected')
142
143         if ((False == server_core_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'core', '# neighbour entries allocated',1))):
144           server_core_connected = True
145           vprintf ('Server core services is connected')
146           
147         if ((False == client_core_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'core', '# neighbour entries allocated',1))):
148           client_core_connected = True
149           vprintf ('Client core services is connected')
150           
151         if ((False == client_core_session_map) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'core', '# entries in session map',1))):
152           client_core_session_map = True
153           vprintf ('Server core services is connected')
154           
155         if ((False == server_core_session_map) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'core', '# entries in session map',1))):
156           server_core_session_map = True
157           vprintf ('Client core notifies about connected')
158
159         if ((False == server_topology_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'topology', '# peers connected',1))):
160           server_topology_connected = True
161           vprintf ('Server topology services is connected')
162           
163         if ((False == client_topology_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'topology', '# peers connected',1))):
164           client_topology_connected = True
165           vprintf ('Client topology services is connected')
166           
167         if ((False == client_fs_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'fs', '# peers connected',1))):
168           client_fs_connected = True
169           vprintf ('Client fs services is connected')
170         if ((False == server_fs_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'fs', '# peers connected',1))):
171           server_fs_connected = True
172           vprintf ('Server fs services is connected')     
173             
174         # Check if conditions fulfilled
175         if ((True == client_transport_connected) and (True == server_transport_connected) and 
176         (True == client_topology_connected) and (True == server_topology_connected) and 
177         (True == client_core_connected) and (True == server_core_connected) and
178         (True == client_core_session_map) and (True == server_core_session_map) and
179         (True == client_fs_connected) and (True == server_fs_connected)):
180                 connected = True
181                 break 
182         print '.',
183         time.sleep(1)
184         count += 1
185   if (connected == False):
186                 print ''
187                 if (client_transport_connected == False):
188                         print ('Client transport was NOT connected')
189                 if (server_transport_connected == False):
190                         print ('Server transport was NOT connected')
191                 if (client_topology_connected == False):
192                         print ('Client topology was NOT connected')
193                 if (server_topology_connected == False):
194                         print ('Server topology was NOT connected')
195                 if (client_core_connected == False):
196                         print ('Client core was NOT connected')
197                 if (server_core_connected == False):
198                         print ('Server core was NOT connected')
199                 if (client_core_session_map == False):
200                         print ('Client core sessions did NOT increase')
201                 if (server_core_session_map == False):
202                         print ('Server core sessions did NOT increase')
203                 return False
204   else:
205         return True
206
207 def check_disconnect ():
208         vprintf ("Shutting down bootstrap server")
209         try:
210             server = subprocess.Popen ([gnunetarm, '-eq', '-c', './confs/c_bootstrap_server.conf'])
211             server.communicate ()    
212         except OSError:
213             print "Can not stop bootstrap server, exiting..."
214             exit (1)
215             
216         client_transport_disconnected = False
217         client_topology_disconnected = False
218         client_core_disconnected = False
219         client_core_session_map = False
220         client_fs_disconnected = False
221             
222         disconnected = False
223         count = 1
224         while ((disconnected == False) and (count <= timeout)):                   
225                 if ((False == client_transport_disconnected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'transport', '# peers connected',0))):
226                   client_transport_disconnected = True
227                   vprintf ('Client transport services is disconnected')
228                   
229                 if ((False == client_core_disconnected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'core', '# neighbour entries allocated',0))):
230                   client_core_disconnected = True
231                   vprintf ('Client core services is disconnected')
232                   
233                 if ((False == client_core_session_map) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'core', '# entries in session map',0))):
234                   client_core_session_map = True
235                   vprintf ('Client core notifies about disconnected')
236                   
237                 if ((False == client_topology_disconnected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'topology', '# peers connected',0))):
238                   client_topology_disconnected = True
239                   vprintf ('Client topology services is disconnected')
240                   
241                 if ((False == client_fs_disconnected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'fs', '# peers connected',0))):
242                   client_fs_disconnected = True
243                   vprintf ('Client fs services is disconnected')
244                     
245                 # Check if conditions fulfilled
246                 if ((True == client_transport_disconnected) and
247                 (True == client_topology_disconnected) and 
248                 (True == client_core_disconnected) and 
249                 (True == client_core_session_map) and 
250                 (True == client_fs_disconnected)):
251                         disconnected = True
252                         break 
253                 print '.'
254                 time.sleep(1)
255                 count += 1
256         if (disconnected == False):
257                 print ''
258                 if (client_transport_disconnected == False):
259                         print ('Client transport was NOT disconnected')
260                 if (client_topology_disconnected == False):
261                         print ('Client topology was NOT disconnected')
262                 if (client_core_disconnected == False):
263                         print ('Client core was NOT disconnected')
264                 if (client_core_session_map == False):
265                         print ('Server core sessions did NOT decrease')
266                 return False
267         else:
268                 return True         
269
270
271 # Test execution
272
273
274 vprintf ("Running " + testname)
275 setup ()
276 start ()
277
278 ret = check_connect ()
279 if (ret == True):
280         vprintf ('Peers connected')
281         if (True == check_disconnect ()):
282                 success = True
283
284 stop ()
285
286
287 cleanup ()
288
289 if (success == False):
290         print ('Test failed')
291         exit (1)
292 else:
293         exit (0)
294
295
296