48dfa61cd2862f12b60990917a58d70eeb02827b
[oweals/gnunet.git] / src / integration-tests / test_integration_clique_nat.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 #
22 # This test starts 3 peers (1 bootstrap server, 1 not nat'ed peer, 1 nat'ed peer)
23 # and expects bootstrap and a connected clique
24 #
25 # Conditions for successful exit:
26 # Both peers have 1 connected peer in transport, core, topology, fs 
27
28 import sys
29 import os
30 import subprocess
31 import re
32 import shutil
33 import time
34 from gnunet_testing import Peer
35 from gnunet_testing import Test
36 from gnunet_testing import Check
37 from gnunet_testing import Condition
38 from gnunet_testing import * 
39  
40
41 #definitions
42 testname = "test_integration_clique_nat"
43 verbose = True
44 check_timeout = 180
45
46
47 def cleanup ():
48         if os.name == "nt":
49             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_bootstrap_server"), True)
50             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_no_nat_client"), True)
51             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_nat_client"), True)
52         else:
53             shutil.rmtree ("/tmp/c_bootstrap_server/", True)
54             shutil.rmtree ("/tmp/c_no_nat_client/", True)
55             shutil.rmtree ("/tmp/c_nat_client/", True)
56
57
58 def success_cont (check):
59         global success 
60         success = True;
61
62 def fail_cont (check):    
63     global success 
64     success= False;
65     check.evaluate(True)        
66
67
68 def check_disconnect_client ():
69   test.p ('Shutting down bootstrap client')
70   client.stop ()
71   check = Check (test)
72   
73   check.add (StatisticsCondition (client2, 'transport', '# peers connected',0))
74   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',0))  
75   check.add (StatisticsCondition (client2, 'core', '# peers connected',0))
76   check.add (StatisticsCondition (client2, 'topology', '# peers connected',0))
77   check.add (StatisticsCondition (client2, 'fs', '# peers connected',0))
78   
79   check.run_blocking (check_timeout, success_cont, fail_cont)
80
81                 
82 def success_disconnect_server_cont (check):
83     check_disconnect_client ()
84
85
86 def fail_disconnect_server_cont (check):    
87     global success 
88     success= False;
89     check.evaluate(False)        
90
91         
92 def check_disconnect_server ():
93   test.p ('Shutting down bootstrap server')
94   server.stop ()
95   check = Check (test)
96   check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
97   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))  
98   check.add (StatisticsCondition (client, 'core', '# peers connected',1))
99   check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
100   check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
101   
102   check.add (StatisticsCondition (client2, 'transport', '# peers connected',1))
103   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',1))  
104   check.add (StatisticsCondition (client2, 'core', '# peers connected',1))
105   check.add (StatisticsCondition (client2, 'topology', '# peers connected',1))
106   check.add (StatisticsCondition (client2, 'fs', '# peers connected',1))
107   
108   check.run_blocking (check_timeout, success_disconnect_server_cont, fail_disconnect_server_cont)
109
110
111 def success_connect_cont (check):  
112     check_disconnect_server ()
113
114
115 def fail_connect_cont (check):    
116     global success 
117     success= False;
118     check.evaluate(False)
119
120
121 def check_connect ():
122   check = Check (test)
123   check.add (StatisticsCondition (client, 'transport', '# peers connected',2))
124   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',2))  
125   check.add (StatisticsCondition (client, 'core', '# peers connected',2))
126   check.add (StatisticsCondition (client, 'topology', '# peers connected',2))
127   check.add (StatisticsCondition (client, 'fs', '# peers connected',2))
128
129   check.add (StatisticsCondition (client2, 'transport', '# peers connected',2))
130   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',2))  
131   check.add (StatisticsCondition (client2, 'core', '# peers connected',2))
132   check.add (StatisticsCondition (client2, 'topology', '# peers connected',2))
133   check.add (StatisticsCondition (client2, 'fs', '# peers connected',2))
134   
135   check.add (StatisticsCondition (server, 'transport', '# peers connected',2))
136   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',2))  
137   check.add (StatisticsCondition (server, 'core', '# peers connected',2))
138   check.add (StatisticsCondition (server, 'topology', '# peers connected',2))
139   check.add (StatisticsCondition (server, 'fs', '# peers connected',2))  
140   
141   check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
142
143
144 # Test execution
145
146 def run ():
147         global success
148         global test
149         global server
150         global client
151         global client2  
152         
153         success = False
154         
155         test = Test ('test_integration_disconnect', verbose)
156         
157         server = Peer(test, './confs/c_bootstrap_server.conf');
158         server.start();
159         
160         client = Peer(test, './confs/c_no_nat_client.conf');
161         client.start();
162         
163         client2 = Peer(test, './confs/c_nat_client.conf');
164         client2.start();
165         
166         if ((client.started == True) and (client2.started == True) and (server.started == True)):
167             test.p ('Peers started, running check')
168             check_connect ()
169             
170         server.stop ()    
171         client.stop ()
172         client2.stop ()
173         
174         cleanup ()
175         
176         if (success == False):
177                 print ('Test failed')
178                 return False 
179         else:
180                 return True
181
182     
183 try:
184     run ()
185 except (KeyboardInterrupt, SystemExit):    
186     print 'Test interrupted'
187     server.stop ()
188     client.stop ()
189     client2.stop ()
190     cleanup ()
191 if (success == False):
192         sys.exit(1)   
193 else:
194         sys.exit(0)    
195             
196