-LRN: use correct character counting, instead of byte counting
[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 from gnunet_testing import Peer
29 from gnunet_testing import Test
30 from gnunet_testing import Check
31 from gnunet_testing import Condition
32 from gnunet_testing import * 
33  
34
35 #
36 # This test tests if a fresh peer bootstraps from a hostlist server and then
37 # successfully connects to the server. When both peers are connected
38 # in transport, core, topology, fs, the server is shutdown
39 #
40 # Conditions for successful exit:
41 # Both peers have 0 connected peer in transport, core, topology, fs
42
43 #definitions
44
45 testname = "test_integration_disconnect"
46 verbose = True
47 check_timeout = 30
48
49
50 def cleanup ():
51         if os.name == "nt":
52             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_bootstrap_server"), True)
53             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_no_nat_client"), True)
54         else:
55             shutil.rmtree ("/tmp/c_bootstrap_server/", True)
56             shutil.rmtree ("/tmp/c_no_nat_client/", True)    
57
58
59 def success_disconnect_cont (check):
60     global success 
61     success = True;
62
63
64 def fail_disconnect_cont (check):    
65     global success 
66     success = False;
67     check.eval(True)   
68   
69
70 def check_disconnect ():
71   test.p ('Shutting down bootstrap server')
72   server.stop ()
73   check = Check (test)
74   check.add (StatisticsCondition (client, 'transport', '# peers connected',0))
75   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',0))  
76   check.add (StatisticsCondition (client, 'core', '# entries in session map',0))
77   check.add (StatisticsCondition (client, 'topology', '# peers connected',0))
78   check.add (StatisticsCondition (client, 'fs', '# peers connected',0))
79   check.run_blocking (check_timeout, success_disconnect_cont, fail_disconnect_cont)
80
81
82 def success_connect_cont (check):
83     check_disconnect ()
84
85
86 def fail_connect_cont (check):    
87     global success 
88     success= False;
89     check.eval(True)
90
91
92 def check_connect ():
93   check = Check (test)
94   check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
95   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))  
96   check.add (StatisticsCondition (client, 'core', '# entries in session map',1))
97   check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
98   check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
99   
100   check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
101   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))  
102   check.add (StatisticsCondition (server, 'core', '# entries in session map',1))
103   check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
104   check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
105   
106   check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
107
108
109 # Test execution
110
111
112 def run ():
113         global success
114         global test
115         global server
116         global client
117         
118         success = False
119         
120         test = Test ('test_integration_disconnect', verbose)
121         
122         server = Peer(test, './confs/c_bootstrap_server.conf');
123         server.start();
124         
125         client = Peer(test, './confs/c_no_nat_client.conf');
126         client.start();
127         
128         
129         if ((client.started == True) and (server.started == True)):
130             test.p ('Peers started, running check')
131             check_connect ()
132                 
133         server.stop ()    
134         client.stop ()
135         
136         cleanup ()
137         
138         if (success == False):
139                 print ('Test failed')
140                 return True
141         else:
142                 return False
143
144         
145 try:
146     run ()
147 except (KeyboardInterrupt, SystemExit):    
148     print 'Test interrupted'
149     server.stop ()
150     client.stop ()
151     cleanup ()
152 if (success == False):
153         sys.exit(1)   
154 else:
155         sys.exit(0)    
156