types
[oweals/gnunet.git] / src / fs / test_fs_search_persistence.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006 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  * @file applications/fs/fsui/search_persistence_test.c
23  * @brief testcase for fsui download persistence for search
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util.h"
29 #include "gnunet_fsui_lib.h"
30
31 #define DEBUG_VERBOSE GNUNET_NO
32
33 #define UPLOAD_PREFIX "/tmp/gnunet-fsui-search_persistence_test"
34
35 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; }
36
37 static struct GNUNET_GE_Context *ectx;
38
39 static struct GNUNET_FSUI_Context *ctx;
40 static struct GNUNET_FSUI_SearchList *search;
41 static int have_error;
42
43 static void *
44 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
45 {
46   switch (event->type)
47     {
48     case GNUNET_FSUI_search_suspended:
49       search = NULL;
50       break;
51     case GNUNET_FSUI_search_resumed:
52 #if DEBUG_VERBOSE
53       printf ("Search resuming\n");
54 #endif
55       search = event->data.SearchResumed.sc.pos;
56       break;
57     case GNUNET_FSUI_search_result:
58 #if DEBUG_VERBOSE
59       printf ("Received search result\n");
60 #endif
61       break;
62     case GNUNET_FSUI_upload_progress:
63 #if DEBUG_VERBOSE
64       printf ("Upload is progressing (%llu/%llu)...\n",
65               event->data.UploadProgress.completed,
66               event->data.UploadProgress.total);
67 #endif
68       break;
69     case GNUNET_FSUI_upload_completed:
70 #if DEBUG_VERBOSE
71       printf ("Upload complete.\n");
72 #endif
73       break;
74     case GNUNET_FSUI_unindex_progress:
75 #if DEBUG_VERBOSE
76       printf ("Unindex is progressing (%llu/%llu)...\n",
77               event->data.UnindexProgress.completed,
78               event->data.UnindexProgress.total);
79 #endif
80       break;
81     case GNUNET_FSUI_unindex_completed:
82 #if DEBUG_VERBOSE
83       printf ("Unindex complete.\n");
84 #endif
85       break;
86     case GNUNET_FSUI_unindex_error:
87     case GNUNET_FSUI_upload_error:
88     case GNUNET_FSUI_download_error:
89       fprintf (stderr, "Received ERROR: %d\n", event->type);
90       GNUNET_GE_BREAK (ectx, 0);
91       break;
92     case GNUNET_FSUI_download_aborted:
93 #if DEBUG_VERBOSE
94       printf ("Received download aborted event.\n");
95 #endif
96       break;
97     case GNUNET_FSUI_unindex_suspended:
98     case GNUNET_FSUI_upload_suspended:
99 #if DEBUG_VERBOSE
100       fprintf (stderr, "Received SUSPENDING: %d\n", event->type);
101 #endif
102       break;
103     case GNUNET_FSUI_upload_started:
104     case GNUNET_FSUI_upload_stopped:
105     case GNUNET_FSUI_search_started:
106     case GNUNET_FSUI_search_aborted:
107     case GNUNET_FSUI_search_stopped:
108     case GNUNET_FSUI_search_update:
109     case GNUNET_FSUI_unindex_started:
110     case GNUNET_FSUI_unindex_stopped:
111       break;
112     default:
113       printf ("Unexpected event: %d\n", event->type);
114       break;
115     }
116   return NULL;
117 }
118
119 #define FILESIZE (1024)
120
121 #define START_DAEMON 1
122
123 int
124 main (int argc, char *argv[])
125 {
126 #if START_DAEMON
127   pid_t daemon;
128 #endif
129   int ok;
130   struct GNUNET_ECRS_URI *uri = NULL;
131   char *keywords[] = {
132     "down_foo",
133     "down_bar",
134   };
135   char keyword[40];
136   int prog;
137   struct GNUNET_GC_Configuration *cfg;
138   int suspendRestart = 0;
139
140
141   ok = GNUNET_YES;
142   cfg = GNUNET_GC_create ();
143   if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
144     {
145       GNUNET_GC_free (cfg);
146       return -1;
147     }
148 #if START_DAEMON
149   daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
150   GNUNET_GE_ASSERT (NULL, daemon > 0);
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
189           suspendRestart--;
190         }
191       if (GNUNET_shutdown_test () == GNUNET_YES)
192         break;
193     }
194   GNUNET_FSUI_search_abort (search);
195   GNUNET_FSUI_search_stop (search);
196   search = NULL;
197   /* END OF TEST CODE */
198 FAILURE:
199   if (ctx != NULL)
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
207   GNUNET_GC_free (cfg);
208   if (have_error)
209     ok = GNUNET_NO;
210   return (ok == GNUNET_YES) ? 0 : 1;
211 }
212
213 /* end of search_persistence_test.c */