glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / integration-tests / test_integration_disconnect_nat.py.in
1 #!@PYTHON@
2 #    This file is part of GNUnet.
3 #    (C) 2010, 2018 Christian Grothoff (and other contributing authors)
4 #
5 #    GNUnet is free software: you can redistribute it and/or modify it
6 #    under the terms of the GNU Affero General Public License as published
7 #    by the Free Software Foundation, either version 3 of the License,
8 #    or (at your 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 #    Affero General Public License for more details.
14 #
15 #
16 import sys
17 import signal
18 import os
19 import subprocess
20 import re
21 import shutil
22 import time
23 from gnunet_testing import Peer
24 from gnunet_testing import Test
25 from gnunet_testing import Check
26 from gnunet_testing import Condition
27 from gnunet_testing import *
28
29
30 #
31 # This test tests if a fresh peer bootstraps from a hostlist server and then
32 # successfully connects to the server. When both peers are connected
33 # in transport, core, topology, fs, the server is shutdown
34 #
35 # Conditions for successful exit:
36 # Client peer has 0 connected peer in transport, core, topology, dht, fs
37
38 # definitions
39
40 testname = "test_integration_disconnect"
41 verbose = True
42 check_timeout = 180
43
44 if os.name == "nt":
45     tmp = os.getenv("TEMP")
46     signals = [signal.SIGTERM, signal.SIGINT]
47 else:
48     tmp = "/tmp"
49     signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
50
51
52 def cleanup_onerror(function, path, excinfo):
53     import stat
54     if not os.path.exists(path):
55         pass
56     elif not os.access(path, os.W_OK):
57         # Is the error an access error ?
58         os.chmod(path, stat.S_IWUSR)
59         function(path)
60     else:
61         raise
62
63
64 def cleanup():
65     shutil.rmtree(os.path.join(tmp, "c_bootstrap_server"), False, cleanup_onerror)
66     shutil.rmtree(os.path.join(tmp, "c_nat_client"), False, cleanup_onerror)
67
68
69 def success_disconnect_cont(check):
70     print('Peers disconnected successfully')
71     global success
72     success = True
73
74
75 def fail_disconnect_cont(check):
76     global success
77     success = False;
78     print('Peers failed to disconnect')
79     check.evaluate(True)
80
81
82 def check_disconnect():
83     global server
84     global nat_client
85     test.p('Shutting down nat client')
86     nat_client.stop()
87     check = Check(test)
88     check.add(StatisticsCondition(server, 'transport', '# peers connected', 0))
89     check.add(StatisticsCondition(server, 'core', '# peers connected', 0))
90     check.add(StatisticsCondition(server, 'topology', '# peers connected', 0))
91     check.add(StatisticsCondition(server, 'dht', '# peers connected', 0))
92     check.add(StatisticsCondition(server, 'fs', '# peers connected', 0))
93     check.run_blocking(check_timeout, success_disconnect_cont, fail_disconnect_cont)
94
95
96 def success_connect_cont(check):
97     print('Peers connected successfully')
98     check_disconnect()
99
100
101 def fail_connect_cont(check):
102     global success
103     success = False
104     print('Peers failed to connected!')
105     check.evaluate(True)
106
107
108 def check_connect():
109     global server
110     global nat_client
111     check = Check(test)
112     check.add(StatisticsCondition(nat_client, 'transport', '# peers connected', 1))
113     check.add(StatisticsCondition(nat_client, 'core', '# peers connected', 1))
114     check.add(StatisticsCondition(nat_client, 'topology', '# peers connected', 1))
115     check.add(StatisticsCondition(nat_client, 'dht', '# peers connected', 1))
116     check.add(StatisticsCondition(nat_client, 'fs', '# peers connected', 1))
117
118     check.add(StatisticsCondition(server, 'transport', '# peers connected', 1))
119     check.add(StatisticsCondition(server, 'core', '# peers connected', 1))
120     check.add(StatisticsCondition(server, 'topology', '# peers connected', 1))
121     check.add(StatisticsCondition(server, 'dht', '# peers connected', 1))
122     check.add(StatisticsCondition(server, 'fs', '# peers connected', 1))
123
124     check.run_blocking(check_timeout, success_connect_cont, fail_connect_cont)
125
126
127 #
128 # Test execution
129 #
130
131 def SigHandler(signum=None, frame=None):
132     global success
133     global server
134     global nat_client
135
136     print('Test was aborted!')
137     if (None != server):
138         server.stop()
139     if (None != nat_client):
140         nat_client.stop()
141     cleanup()
142     sys.exit(success)
143
144
145 def run():
146     global success
147     global test
148     global server
149     global nat_client
150
151     server = None
152     nat_client = None
153     success = False
154
155     for sig in signals:
156         signal.signal(sig, SigHandler)
157
158     test = Test('test_integration_bootstrap_and_connect.py', verbose)
159     cleanup()
160
161     server = Peer(test, './confs/c_bootstrap_server.conf')
162     nat_client = Peer(test, './confs/c_nat_client.conf')
163
164     if (True != server.start()):
165         print('Failed to start server')
166         if (None != server):
167             server.stop()
168         cleanup()
169         sys.exit(success)
170
171     # Give the server time to start
172     time.sleep(5)
173
174     if (True != nat_client.start()):
175         print('Failed to start nat_client')
176         if (None != server):
177             server.stop()
178         if (None != nat_client):
179             nat_client.stop()
180         cleanup()
181         sys.exit(success)
182
183     if ((nat_client.started == True) and (server.started == True)):
184         test.p('Peers started, running check')
185         time.sleep(5)
186         check_connect()
187     server.stop()
188     nat_client.stop()
189
190     cleanup()
191
192     if (success == False):
193         print('Test failed')
194         return False
195     else:
196         return True
197
198
199 try:
200     run()
201 except(KeyboardInterrupt, SystemExit):
202     print('Test interrupted')
203     server.stop()
204     nat_client.stop()
205     cleanup()
206 if (success == False):
207     sys.exit(1)
208 else:
209     sys.exit(0)