diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-03-22 18:20:25 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-03-22 18:20:25 +0000 |
commit | f948a3c0a1b817e3920f58b2b6b19281fa090913 (patch) | |
tree | 8c0cd553ed685e2833953d4d8cc62ed32241a964 /src | |
parent | b57a34c4392be66883830b365b44afa8e1b21f6c (diff) | |
download | gnunet-f948a3c0a1b817e3920f58b2b6b19281fa090913.tar.gz gnunet-f948a3c0a1b817e3920f58b2b6b19281fa090913.zip |
-add test
Diffstat (limited to 'src')
-rw-r--r-- | src/gns/test_gns_max_queries_test.c | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/src/gns/test_gns_max_queries_test.c b/src/gns/test_gns_max_queries_test.c new file mode 100644 index 000000000..a244b3f02 --- /dev/null +++ b/src/gns/test_gns_max_queries_test.c | |||
@@ -0,0 +1,337 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 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 3, 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 | * @file gns/test_gns_max_queries_test.c | ||
22 | * @brief base testcase for testing GNS background queries | ||
23 | * in particular query replacement and clean shutdown | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_testing_lib.h" | ||
27 | #include "gnunet_core_service.h" | ||
28 | #include "block_dns.h" | ||
29 | #include "gnunet_signatures.h" | ||
30 | #include "gnunet_namestore_service.h" | ||
31 | #include "gnunet_dnsparser_lib.h" | ||
32 | #include "gnunet_gns_service.h" | ||
33 | |||
34 | /* DEFINES */ | ||
35 | #define VERBOSE GNUNET_YES | ||
36 | |||
37 | /* Timeout for entire testcase */ | ||
38 | #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) | ||
39 | |||
40 | /* If number of peers not in config file, use this number */ | ||
41 | #define DEFAULT_NUM_PEERS 2 | ||
42 | |||
43 | /* test records to resolve */ | ||
44 | #define TEST_DOMAIN "www.gnunet" | ||
45 | #define TEST_DOMAIN_NACK "doesnotexist.gnunet" | ||
46 | #define TEST_IP "127.0.0.1" | ||
47 | #define TEST_RECORD_NAME "www" | ||
48 | #define TEST_ADDITIONAL_LOOKUPS 10 | ||
49 | |||
50 | /* Globals */ | ||
51 | |||
52 | /** | ||
53 | * Directory to store temp data in, defined in config file | ||
54 | */ | ||
55 | static char *test_directory; | ||
56 | |||
57 | struct GNUNET_TESTING_Daemon *d1; | ||
58 | |||
59 | |||
60 | /* Task handle to use to schedule test failure */ | ||
61 | GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
62 | |||
63 | /* Global return value (0 for success, anything else for failure) */ | ||
64 | static int ok; | ||
65 | |||
66 | static struct GNUNET_NAMESTORE_Handle *namestore_handle; | ||
67 | |||
68 | static struct GNUNET_GNS_Handle *gns_handle; | ||
69 | |||
70 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
71 | |||
72 | static unsigned long long max_parallel_lookups; | ||
73 | |||
74 | /** | ||
75 | * Check whether peers successfully shut down. | ||
76 | */ | ||
77 | void | ||
78 | shutdown_callback (void *cls, const char *emsg) | ||
79 | { | ||
80 | if (emsg != NULL) | ||
81 | { | ||
82 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok); | ||
83 | if (ok == 0) | ||
84 | ok = 2; | ||
85 | } | ||
86 | |||
87 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok); | ||
88 | } | ||
89 | |||
90 | |||
91 | static void | ||
92 | on_lookup_result(void *cls, uint32_t rd_count, | ||
93 | const struct GNUNET_NAMESTORE_RecordData *rd) | ||
94 | { | ||
95 | struct in_addr a; | ||
96 | int i; | ||
97 | char* addr; | ||
98 | |||
99 | if (rd_count == 0) | ||
100 | { | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
102 | "Lookup failed, rp_filtering?\n"); | ||
103 | ok = 2; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | ok = 1; | ||
108 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls); | ||
109 | for (i=0; i<rd_count; i++) | ||
110 | { | ||
111 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type); | ||
112 | if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A) | ||
113 | { | ||
114 | memcpy(&a, rd[i].data, sizeof(a)); | ||
115 | addr = inet_ntoa(a); | ||
116 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr); | ||
117 | if (0 == strcmp(addr, TEST_IP)) | ||
118 | { | ||
119 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
120 | "%s correctly resolved to %s!\n", TEST_DOMAIN, addr); | ||
121 | ok = 0; | ||
122 | } | ||
123 | } | ||
124 | else | ||
125 | { | ||
126 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n"); | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | GNUNET_GNS_disconnect(gns_handle); | ||
131 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n"); | ||
132 | GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL, | ||
133 | GNUNET_YES, GNUNET_NO); | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Function scheduled to be run on the successful start of services | ||
139 | * tries to look up the dns record for TEST_DOMAIN | ||
140 | */ | ||
141 | static void | ||
142 | commence_testing (void *cls, int32_t success, const char *emsg) | ||
143 | { | ||
144 | int i; | ||
145 | |||
146 | GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES); | ||
147 | |||
148 | gns_handle = GNUNET_GNS_connect(cfg); | ||
149 | |||
150 | if (NULL == gns_handle) | ||
151 | { | ||
152 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
153 | "Failed to connect to GNS!\n"); | ||
154 | ok = 2; | ||
155 | } | ||
156 | |||
157 | GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_A, | ||
158 | &on_lookup_result, TEST_DOMAIN); | ||
159 | |||
160 | /* Now lookup some non existing records */ | ||
161 | for (i=0; i<max_parallel_lookups+TEST_ADDITIONAL_LOOKUPS; i++) | ||
162 | { | ||
163 | GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_NACK, GNUNET_GNS_RECORD_TYPE_A, | ||
164 | &on_lookup_result_dummy, NULL); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | |||
169 | /** | ||
170 | * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut | ||
171 | * down the peers without freeing memory associated with GET request. | ||
172 | */ | ||
173 | static void | ||
174 | end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
175 | { | ||
176 | |||
177 | if (d1 != NULL) | ||
178 | GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL, | ||
179 | GNUNET_YES, GNUNET_NO); | ||
180 | GNUNET_SCHEDULER_cancel (die_task); | ||
181 | } | ||
182 | |||
183 | /** | ||
184 | * Check if the get_handle is being used, if so stop the request. Either | ||
185 | * way, schedule the end_badly_cont function which actually shuts down the | ||
186 | * test. | ||
187 | */ | ||
188 | static void | ||
189 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
190 | { | ||
191 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n", | ||
192 | (char *) cls); | ||
193 | GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL); | ||
194 | ok = 1; | ||
195 | } | ||
196 | |||
197 | static void | ||
198 | do_lookup(void *cls, const struct GNUNET_PeerIdentity *id, | ||
199 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
200 | struct GNUNET_TESTING_Daemon *d, const char *emsg) | ||
201 | { | ||
202 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey; | ||
203 | struct GNUNET_CRYPTO_RsaPrivateKey *alice_key; | ||
204 | char* alice_keyfile; | ||
205 | |||
206 | GNUNET_SCHEDULER_cancel (die_task); | ||
207 | |||
208 | /* put records into namestore */ | ||
209 | namestore_handle = GNUNET_NAMESTORE_connect(cfg); | ||
210 | if (NULL == namestore_handle) | ||
211 | { | ||
212 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n"); | ||
213 | ok = -1; | ||
214 | return; | ||
215 | } | ||
216 | |||
217 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns", | ||
218 | "ZONEKEY", | ||
219 | &alice_keyfile)) | ||
220 | { | ||
221 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n"); | ||
222 | ok = -1; | ||
223 | return; | ||
224 | } | ||
225 | |||
226 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "gns", | ||
227 | "MAX_PARALLEL_BACKGROUND_QUERIES", | ||
228 | &max_parallel_lookups)) | ||
229 | { | ||
230 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get max queries from cfg\n"); | ||
231 | ok = -1; | ||
232 | return; | ||
233 | } | ||
234 | |||
235 | alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile); | ||
236 | |||
237 | GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey); | ||
238 | |||
239 | GNUNET_free(alice_keyfile); | ||
240 | |||
241 | struct GNUNET_NAMESTORE_RecordData rd; | ||
242 | char* ip = TEST_IP; | ||
243 | struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr)); | ||
244 | rd.expiration = GNUNET_TIME_absolute_get_forever (); | ||
245 | GNUNET_assert(1 == inet_pton (AF_INET, ip, web)); | ||
246 | rd.data_size = sizeof(struct in_addr); | ||
247 | rd.data = web; | ||
248 | rd.record_type = GNUNET_DNSPARSER_TYPE_A; | ||
249 | |||
250 | GNUNET_NAMESTORE_record_create (namestore_handle, | ||
251 | alice_key, | ||
252 | TEST_RECORD_NAME, | ||
253 | &rd, | ||
254 | &commence_testing, | ||
255 | NULL); | ||
256 | |||
257 | GNUNET_CRYPTO_rsa_key_free(alice_key); | ||
258 | GNUNET_free(web); | ||
259 | |||
260 | } | ||
261 | |||
262 | static void | ||
263 | run (void *cls, char *const *args, const char *cfgfile, | ||
264 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
265 | { | ||
266 | cfg = c; | ||
267 | /* Get path from configuration file */ | ||
268 | if (GNUNET_YES != | ||
269 | GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome", | ||
270 | &test_directory)) | ||
271 | { | ||
272 | ok = 404; | ||
273 | return; | ||
274 | } | ||
275 | |||
276 | |||
277 | /* Set up a task to end testing if peer start fails */ | ||
278 | die_task = | ||
279 | GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, | ||
280 | "didn't start all daemons in reasonable amount of time!!!"); | ||
281 | |||
282 | /* Start alice */ | ||
283 | d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0, | ||
284 | NULL, NULL, NULL, &do_lookup, NULL); | ||
285 | } | ||
286 | |||
287 | static int | ||
288 | check () | ||
289 | { | ||
290 | int ret; | ||
291 | |||
292 | /* Arguments for GNUNET_PROGRAM_run */ | ||
293 | char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */ | ||
294 | "-c", | ||
295 | "test_gns_simple_lookup.conf", /* Config file to use */ | ||
296 | #if VERBOSE | ||
297 | "-L", "DEBUG", | ||
298 | #endif | ||
299 | NULL | ||
300 | }; | ||
301 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
302 | GNUNET_GETOPT_OPTION_END | ||
303 | }; | ||
304 | /* Run the run function as a new program */ | ||
305 | ret = | ||
306 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, | ||
307 | "test-gns-simple-lookup", "nohelp", options, &run, | ||
308 | &ok); | ||
309 | if (ret != GNUNET_OK) | ||
310 | { | ||
311 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
312 | "`test-gns-simple-lookup': Failed with error code %d\n", ret); | ||
313 | } | ||
314 | return ok; | ||
315 | } | ||
316 | |||
317 | int | ||
318 | main (int argc, char *argv[]) | ||
319 | { | ||
320 | int ret; | ||
321 | |||
322 | GNUNET_log_setup ("test-gns-simple-lookup", | ||
323 | #if VERBOSE | ||
324 | "DEBUG", | ||
325 | #else | ||
326 | "WARNING", | ||
327 | #endif | ||
328 | NULL); | ||
329 | ret = check (); | ||
330 | /** | ||
331 | * Need to remove base directory, subdirectories taken care | ||
332 | * of by the testing framework. | ||
333 | */ | ||
334 | return ret; | ||
335 | } | ||
336 | |||
337 | /* end of test_gns_twopeer.c */ | ||