diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-05-05 12:19:54 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-05-05 12:19:54 +0000 |
commit | 4b7f94c75bcc661940324abcad36a3f5b8081399 (patch) | |
tree | 97d140e5791a087de78a8269563b08af8bf106f0 /src/fs/test_fs_search_persistence.c | |
parent | 1a8f8c24386988cac9c7290161f1dd25b652148d (diff) | |
download | gnunet-4b7f94c75bcc661940324abcad36a3f5b8081399.tar.gz gnunet-4b7f94c75bcc661940324abcad36a3f5b8081399.zip |
tests
Diffstat (limited to 'src/fs/test_fs_search_persistence.c')
-rw-r--r-- | src/fs/test_fs_search_persistence.c | 445 |
1 files changed, 294 insertions, 151 deletions
diff --git a/src/fs/test_fs_search_persistence.c b/src/fs/test_fs_search_persistence.c index ad0e12b4b..af4344515 100644 --- a/src/fs/test_fs_search_persistence.c +++ b/src/fs/test_fs_search_persistence.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors) | 3 | (C) 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -19,195 +19,338 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file applications/fs/fsui/search_persistence_test.c | 22 | * @file fs/test_fs_search_persistence.c |
23 | * @brief testcase for fsui download persistence for search | 23 | * @brief simple testcase for persistence of search operation |
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | */ | 25 | */ |
26 | 26 | ||
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_util.h" | 28 | #include "gnunet_util_lib.h" |
29 | #include "gnunet_fsui_lib.h" | 29 | #include "gnunet_arm_service.h" |
30 | #include "gnunet_fs_service.h" | ||
30 | 31 | ||
31 | #define DEBUG_VERBOSE GNUNET_NO | 32 | #define VERBOSE GNUNET_NO |
32 | 33 | ||
33 | #define UPLOAD_PREFIX "/tmp/gnunet-fsui-search_persistence_test" | 34 | #define START_ARM GNUNET_YES |
35 | |||
36 | /** | ||
37 | * File-size we use for testing. | ||
38 | */ | ||
39 | #define FILESIZE 1024 | ||
40 | |||
41 | /** | ||
42 | * How long until we give up on transmitting the message? | ||
43 | */ | ||
44 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
45 | |||
46 | /** | ||
47 | * How long should our test-content live? | ||
48 | */ | ||
49 | #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) | ||
50 | |||
51 | struct PeerContext | ||
52 | { | ||
53 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
54 | struct GNUNET_PeerIdentity id; | ||
55 | #if START_ARM | ||
56 | pid_t arm_pid; | ||
57 | #endif | ||
58 | }; | ||
59 | |||
60 | static struct PeerContext p1; | ||
61 | |||
62 | static struct GNUNET_TIME_Absolute start; | ||
63 | |||
64 | static struct GNUNET_SCHEDULER_Handle *sched; | ||
65 | |||
66 | static struct GNUNET_FS_Handle *fs; | ||
67 | |||
68 | static struct GNUNET_FS_SearchContext *search; | ||
69 | |||
70 | static struct GNUNET_FS_PublishContext *publish; | ||
71 | |||
72 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
73 | |||
74 | static void | ||
75 | abort_publish_task (void *cls, | ||
76 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
77 | { | ||
78 | GNUNET_FS_publish_stop (publish); | ||
79 | publish = NULL; | ||
80 | } | ||
34 | 81 | ||
35 | #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; } | ||
36 | 82 | ||
37 | static struct GNUNET_GE_Context *ectx; | 83 | static void |
84 | abort_search_task (void *cls, | ||
85 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
86 | { | ||
87 | if (search != NULL) | ||
88 | GNUNET_FS_search_stop (search); | ||
89 | search = NULL; | ||
90 | } | ||
38 | 91 | ||
39 | static struct GNUNET_FSUI_Context *ctx; | ||
40 | static struct GNUNET_FSUI_SearchList *search; | ||
41 | static int have_error; | ||
42 | 92 | ||
43 | static void * | 93 | static void * |
44 | eventCallback (void *cls, const GNUNET_FSUI_Event * event) | 94 | progress_cb (void *cls, |
95 | const struct GNUNET_FS_ProgressInfo *event); | ||
96 | |||
97 | |||
98 | static void | ||
99 | restart_fs_task (void *cls, | ||
100 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
45 | { | 101 | { |
46 | switch (event->type) | 102 | GNUNET_FS_stop (fs); |
103 | fs = GNUNET_FS_start (sched, | ||
104 | cfg, | ||
105 | "test-fs-search-persistence", | ||
106 | &progress_cb, | ||
107 | NULL, | ||
108 | GNUNET_FS_FLAGS_PERSISTENCE, | ||
109 | GNUNET_FS_OPTIONS_END); | ||
110 | } | ||
111 | |||
112 | |||
113 | |||
114 | |||
115 | /** | ||
116 | * Consider scheduling the restart-task. | ||
117 | * Only runs the restart task once per event | ||
118 | * category. | ||
119 | * | ||
120 | * @param ev type of the event to consider | ||
121 | */ | ||
122 | static void | ||
123 | consider_restart (int ev) | ||
124 | { | ||
125 | static int prev[32]; | ||
126 | static int off; | ||
127 | int i; | ||
128 | for (i=0;i<off;i++) | ||
129 | if (prev[i] == ev) | ||
130 | return; | ||
131 | prev[off++] = ev; | ||
132 | GNUNET_SCHEDULER_add_with_priority (sched, | ||
133 | GNUNET_SCHEDULER_PRIORITY_URGENT, | ||
134 | &restart_fs_task, | ||
135 | NULL); | ||
136 | } | ||
137 | |||
138 | |||
139 | static void * | ||
140 | progress_cb (void *cls, | ||
141 | const struct GNUNET_FS_ProgressInfo *event) | ||
142 | { | ||
143 | const char *keywords[] = { | ||
144 | "down_foo" | ||
145 | }; | ||
146 | struct GNUNET_FS_Uri *kuri; | ||
147 | |||
148 | switch (event->status) | ||
47 | { | 149 | { |
48 | case GNUNET_FSUI_search_suspended: | 150 | case GNUNET_FS_STATUS_PUBLISH_PROGRESS: |
49 | search = NULL; | 151 | #if VERBOSE |
152 | printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n", | ||
153 | (unsigned long long) event->value.publish.completed, | ||
154 | (unsigned long long) event->value.publish.size, | ||
155 | event->value.publish.specifics.progress.depth, | ||
156 | (unsigned long long) event->value.publish.specifics.progress.offset); | ||
157 | #endif | ||
50 | break; | 158 | break; |
51 | case GNUNET_FSUI_search_resumed: | 159 | case GNUNET_FS_STATUS_PUBLISH_COMPLETED: |
52 | #if DEBUG_VERBOSE | 160 | kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords); |
53 | printf ("Search resuming\n"); | 161 | start = GNUNET_TIME_absolute_get (); |
54 | #endif | 162 | search = GNUNET_FS_search_start (fs, |
55 | search = event->data.SearchResumed.sc.pos; | 163 | kuri, |
56 | break; | 164 | 1, |
57 | case GNUNET_FSUI_search_result: | 165 | GNUNET_FS_SEARCH_OPTION_NONE, |
58 | #if DEBUG_VERBOSE | 166 | "search"); |
59 | printf ("Received search result\n"); | 167 | GNUNET_FS_uri_destroy (kuri); |
60 | #endif | 168 | GNUNET_assert (search != NULL); |
61 | break; | 169 | break; |
62 | case GNUNET_FSUI_upload_progress: | 170 | case GNUNET_FS_STATUS_SEARCH_RESULT: |
63 | #if DEBUG_VERBOSE | 171 | consider_restart (event->status); |
64 | printf ("Upload is progressing (%llu/%llu)...\n", | 172 | #if VERBOSE |
65 | event->data.UploadProgress.completed, | 173 | printf ("Search complete.\n"); |
66 | event->data.UploadProgress.total); | ||
67 | #endif | 174 | #endif |
175 | GNUNET_SCHEDULER_add_continuation (sched, | ||
176 | &abort_search_task, | ||
177 | NULL, | ||
178 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
68 | break; | 179 | break; |
69 | case GNUNET_FSUI_upload_completed: | 180 | case GNUNET_FS_STATUS_PUBLISH_ERROR: |
70 | #if DEBUG_VERBOSE | 181 | fprintf (stderr, |
71 | printf ("Upload complete.\n"); | 182 | "Error publishing file: %s\n", |
72 | #endif | 183 | event->value.publish.specifics.error.message); |
184 | GNUNET_break (0); | ||
185 | GNUNET_SCHEDULER_add_continuation (sched, | ||
186 | &abort_publish_task, | ||
187 | NULL, | ||
188 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
73 | break; | 189 | break; |
74 | case GNUNET_FSUI_unindex_progress: | 190 | case GNUNET_FS_STATUS_SEARCH_ERROR: |
75 | #if DEBUG_VERBOSE | 191 | fprintf (stderr, |
76 | printf ("Unindex is progressing (%llu/%llu)...\n", | 192 | "Error searching file: %s\n", |
77 | event->data.UnindexProgress.completed, | 193 | event->value.search.specifics.error.message); |
78 | event->data.UnindexProgress.total); | 194 | GNUNET_SCHEDULER_add_continuation (sched, |
79 | #endif | 195 | &abort_search_task, |
196 | NULL, | ||
197 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
80 | break; | 198 | break; |
81 | case GNUNET_FSUI_unindex_completed: | 199 | case GNUNET_FS_STATUS_PUBLISH_START: |
82 | #if DEBUG_VERBOSE | 200 | GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx)); |
83 | printf ("Unindex complete.\n"); | 201 | GNUNET_assert (NULL == event->value.publish.pctx); |
84 | #endif | 202 | GNUNET_assert (FILESIZE == event->value.publish.size); |
203 | GNUNET_assert (0 == event->value.publish.completed); | ||
204 | GNUNET_assert (1 == event->value.publish.anonymity); | ||
85 | break; | 205 | break; |
86 | case GNUNET_FSUI_unindex_error: | 206 | case GNUNET_FS_STATUS_PUBLISH_STOPPED: |
87 | case GNUNET_FSUI_upload_error: | 207 | GNUNET_assert (publish == event->value.publish.sc); |
88 | case GNUNET_FSUI_download_error: | 208 | GNUNET_assert (FILESIZE == event->value.publish.size); |
89 | fprintf (stderr, "Received ERROR: %d\n", event->type); | 209 | GNUNET_assert (1 == event->value.publish.anonymity); |
90 | GNUNET_GE_BREAK (ectx, 0); | 210 | GNUNET_FS_stop (fs); |
211 | fs = NULL; | ||
91 | break; | 212 | break; |
92 | case GNUNET_FSUI_download_aborted: | 213 | case GNUNET_FS_STATUS_SEARCH_START: |
93 | #if DEBUG_VERBOSE | 214 | consider_restart (event->status); |
94 | printf ("Received download aborted event.\n"); | 215 | GNUNET_assert (search == NULL); |
95 | #endif | 216 | GNUNET_assert (0 == strcmp ("search", event->value.search.cctx)); |
217 | GNUNET_assert (1 == event->value.search.anonymity); | ||
96 | break; | 218 | break; |
97 | case GNUNET_FSUI_unindex_suspended: | 219 | case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED: |
98 | case GNUNET_FSUI_upload_suspended: | ||
99 | #if DEBUG_VERBOSE | ||
100 | fprintf (stderr, "Received SUSPENDING: %d\n", event->type); | ||
101 | #endif | ||
102 | break; | 220 | break; |
103 | case GNUNET_FSUI_upload_started: | 221 | case GNUNET_FS_STATUS_SEARCH_STOPPED: |
104 | case GNUNET_FSUI_upload_stopped: | 222 | consider_restart (event->status); |
105 | case GNUNET_FSUI_search_started: | 223 | GNUNET_assert (search == event->value.search.sc); |
106 | case GNUNET_FSUI_search_aborted: | 224 | GNUNET_SCHEDULER_add_continuation (sched, |
107 | case GNUNET_FSUI_search_stopped: | 225 | &abort_publish_task, |
108 | case GNUNET_FSUI_search_update: | 226 | NULL, |
109 | case GNUNET_FSUI_unindex_started: | 227 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); |
110 | case GNUNET_FSUI_unindex_stopped: | ||
111 | break; | 228 | break; |
112 | default: | 229 | default: |
113 | printf ("Unexpected event: %d\n", event->type); | 230 | fprintf (stderr, |
231 | "Unexpected event: %d\n", | ||
232 | event->status); | ||
114 | break; | 233 | break; |
115 | } | 234 | } |
116 | return NULL; | 235 | return NULL; |
117 | } | 236 | } |
118 | 237 | ||
119 | #define FILESIZE (1024) | ||
120 | 238 | ||
121 | #define START_DAEMON 1 | 239 | static void |
240 | setup_peer (struct PeerContext *p, const char *cfgname) | ||
241 | { | ||
242 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
243 | #if START_ARM | ||
244 | p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", | ||
245 | "gnunet-service-arm", | ||
246 | #if VERBOSE | ||
247 | "-L", "DEBUG", | ||
248 | #endif | ||
249 | "-c", cfgname, NULL); | ||
250 | #endif | ||
251 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
252 | } | ||
122 | 253 | ||
123 | int | 254 | |
124 | main (int argc, char *argv[]) | 255 | static void |
256 | stop_arm (struct PeerContext *p) | ||
125 | { | 257 | { |
126 | #if START_DAEMON | 258 | #if START_ARM |
127 | pid_t daemon; | 259 | if (0 != PLIBC_KILL (p->arm_pid, SIGTERM)) |
260 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
261 | if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK) | ||
262 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); | ||
263 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
264 | "ARM process %u stopped\n", p->arm_pid); | ||
128 | #endif | 265 | #endif |
129 | int ok; | 266 | GNUNET_CONFIGURATION_destroy (p->cfg); |
130 | struct GNUNET_ECRS_URI *uri = NULL; | 267 | } |
131 | char *keywords[] = { | 268 | |
269 | |||
270 | static void | ||
271 | run (void *cls, | ||
272 | struct GNUNET_SCHEDULER_Handle *s, | ||
273 | char *const *args, | ||
274 | const char *cfgfile, | ||
275 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
276 | { | ||
277 | const char *keywords[] = { | ||
132 | "down_foo", | 278 | "down_foo", |
133 | "down_bar", | 279 | "down_bar" |
134 | }; | 280 | }; |
135 | char keyword[40]; | 281 | char *buf; |
136 | int prog; | 282 | struct GNUNET_CONTAINER_MetaData *meta; |
137 | struct GNUNET_GC_Configuration *cfg; | 283 | struct GNUNET_FS_Uri *kuri; |
138 | int suspendRestart = 0; | 284 | struct GNUNET_FS_FileInformation *fi; |
285 | size_t i; | ||
139 | 286 | ||
287 | sched = s; | ||
288 | cfg = c; | ||
289 | setup_peer (&p1, "test_fs_search_data.conf"); | ||
290 | fs = GNUNET_FS_start (sched, | ||
291 | cfg, | ||
292 | "test-fs-search-persistence", | ||
293 | &progress_cb, | ||
294 | NULL, | ||
295 | GNUNET_FS_FLAGS_PERSISTENCE, | ||
296 | GNUNET_FS_OPTIONS_END); | ||
297 | GNUNET_assert (NULL != fs); | ||
298 | buf = GNUNET_malloc (FILESIZE); | ||
299 | for (i = 0; i < FILESIZE; i++) | ||
300 | buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256); | ||
301 | meta = GNUNET_CONTAINER_meta_data_create (); | ||
302 | kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords); | ||
303 | fi = GNUNET_FS_file_information_create_from_data (fs, | ||
304 | "publish-context", | ||
305 | FILESIZE, | ||
306 | buf, | ||
307 | kuri, | ||
308 | meta, | ||
309 | GNUNET_NO, | ||
310 | 1, | ||
311 | 42, | ||
312 | GNUNET_TIME_relative_to_absolute (LIFETIME)); | ||
313 | GNUNET_FS_uri_destroy (kuri); | ||
314 | GNUNET_CONTAINER_meta_data_destroy (meta); | ||
315 | GNUNET_assert (NULL != fi); | ||
316 | start = GNUNET_TIME_absolute_get (); | ||
317 | publish = GNUNET_FS_publish_start (fs, | ||
318 | fi, | ||
319 | NULL, NULL, NULL, | ||
320 | GNUNET_FS_PUBLISH_OPTION_NONE); | ||
321 | GNUNET_assert (publish != NULL); | ||
322 | } | ||
140 | 323 | ||
141 | ok = GNUNET_YES; | 324 | |
142 | cfg = GNUNET_GC_create (); | 325 | int |
143 | if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf")) | 326 | main (int argc, char *argv[]) |
144 | { | 327 | { |
145 | GNUNET_GC_free (cfg); | 328 | char *const argvx[] = { |
146 | return -1; | 329 | "test-fs-search-persistence", |
147 | } | 330 | "-c", |
148 | #if START_DAEMON | 331 | "test_fs_search_data.conf", |
149 | daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO); | 332 | #if VERBOSE |
150 | GNUNET_GE_ASSERT (NULL, daemon > 0); | 333 | "-L", "DEBUG", |
151 | CHECK (GNUNET_OK == | ||
152 | GNUNET_wait_for_daemon_running (NULL, cfg, | ||
153 | 30 * GNUNET_CRON_SECONDS)); | ||
154 | GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */ | ||
155 | /* ACTUAL TEST CODE */ | ||
156 | #endif | ||
157 | ctx = GNUNET_FSUI_start (NULL, | ||
158 | cfg, "search_persistence_test", 32, GNUNET_YES, | ||
159 | &eventCallback, NULL); | ||
160 | CHECK (ctx != NULL); | ||
161 | GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]); | ||
162 | uri = GNUNET_ECRS_keyword_string_to_uri (ectx, keyword); | ||
163 | search = GNUNET_FSUI_search_start (ctx, 0, uri); | ||
164 | CHECK (search != NULL); | ||
165 | prog = 0; | ||
166 | suspendRestart = 10; | ||
167 | while (prog < 100) | ||
168 | { | ||
169 | prog++; | ||
170 | GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS); | ||
171 | if ((suspendRestart > 0) | ||
172 | && (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 10) == 0)) | ||
173 | { | ||
174 | #if 1 | ||
175 | #if DEBUG_VERBOSE | ||
176 | printf ("Testing FSUI suspend-resume\n"); | ||
177 | #endif | ||
178 | GNUNET_FSUI_stop (ctx); /* download possibly incomplete | ||
179 | at this point, thus testing resume */ | ||
180 | CHECK (search == NULL); | ||
181 | ctx = GNUNET_FSUI_start (NULL, | ||
182 | cfg, | ||
183 | "search_persistence_test", 32, GNUNET_YES, | ||
184 | &eventCallback, NULL); | ||
185 | #if DEBUG_VERBOSE | ||
186 | printf ("Resumed...\n"); | ||
187 | #endif | ||
188 | #endif | 334 | #endif |
189 | suspendRestart--; | 335 | NULL |
190 | } | 336 | }; |
191 | if (GNUNET_shutdown_test () == GNUNET_YES) | 337 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
192 | break; | 338 | GNUNET_GETOPT_OPTION_END |
193 | } | 339 | }; |
194 | GNUNET_FSUI_search_abort (search); | 340 | |
195 | GNUNET_FSUI_search_stop (search); | 341 | GNUNET_log_setup ("test_fs_search_persistence", |
196 | search = NULL; | 342 | #if VERBOSE |
197 | /* END OF TEST CODE */ | 343 | "DEBUG", |
198 | FAILURE: | 344 | #else |
199 | if (ctx != NULL) | 345 | "WARNING", |
200 | GNUNET_FSUI_stop (ctx); | ||
201 | if (uri != NULL) | ||
202 | GNUNET_ECRS_uri_destroy (uri); | ||
203 | |||
204 | #if START_DAEMON | ||
205 | GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon)); | ||
206 | #endif | 346 | #endif |
207 | GNUNET_GC_free (cfg); | 347 | NULL); |
208 | if (have_error) | 348 | GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, |
209 | ok = GNUNET_NO; | 349 | argvx, "test-fs-search-persistencce", |
210 | return (ok == GNUNET_YES) ? 0 : 1; | 350 | "nohelp", options, &run, NULL); |
351 | stop_arm (&p1); | ||
352 | GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/"); | ||
353 | return 0; | ||
211 | } | 354 | } |
212 | 355 | ||
213 | /* end of search_persistence_test.c */ | 356 | /* end of test_fs_search_persistence.c */ |