authentication of ciphertexts (+ seed)
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_migration.c
1 /*
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 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 /**
22  * @file fs/test_gnunet_service_fs_migration.c
23  * @brief test content migration between two peers
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "fs_test_lib.h"
28 #include "gnunet_testing_lib.h"
29
30 #define VERBOSE GNUNET_NO
31
32 /**
33  * File-size we use for testing.
34  */
35 #define FILESIZE (2 * 32 * 1024)
36
37 /**
38  * How long until we give up on transmitting the message?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
41
42 /**
43  * How long do we give the peers for content migration?
44  */
45 #define MIGRATION_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
46
47 #define SEED 42
48
49 static struct GNUNET_FS_TestDaemon *daemons[2];
50
51 static struct GNUNET_SCHEDULER_Handle *sched;
52
53 static int ok;
54
55 static struct GNUNET_TIME_Absolute start_time;
56
57 static void
58 do_stop (void *cls,
59          const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   struct GNUNET_TIME_Relative del;
62   char *fancy;
63
64   GNUNET_FS_TEST_daemons_stop (sched,
65                                2,
66                                daemons);
67   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
68     {
69       del = GNUNET_TIME_absolute_get_duration (start_time);
70       if (del.value == 0)
71         del.value = 1;
72       fancy = GNUNET_STRINGS_byte_size_fancy (((unsigned long long)FILESIZE) * 1000LL / del.value);
73       fprintf (stdout,
74                "Download speed was %s/s\n",
75                fancy);
76       GNUNET_free (fancy);
77       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78                   "Finished download, shutting down\n",
79                   (unsigned long long) FILESIZE);
80     }
81   else
82     {
83       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84                   "Timeout during download, shutting down with error\n");
85       ok = 1;
86     }
87 }
88
89
90 static void
91 do_download (void *cls,
92              const char *emsg)
93 {
94   struct GNUNET_FS_Uri *uri = cls;
95
96   if (emsg != NULL)
97     {
98       GNUNET_FS_TEST_daemons_stop (sched,
99                                    2,
100                                    daemons);
101       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
102                   "Failed to stop source daemon: %s\n",
103                   emsg);
104       GNUNET_FS_uri_destroy (uri);
105       ok = 1;
106       return;
107     }
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "Downloading %llu bytes\n",
110               (unsigned long long) FILESIZE);
111   start_time = GNUNET_TIME_absolute_get ();
112   GNUNET_FS_TEST_download (sched,
113                            daemons[0],
114                            TIMEOUT,
115                            1, SEED, uri, 
116                            VERBOSE, 
117                            &do_stop, NULL);
118   GNUNET_FS_uri_destroy (uri);
119 }
120
121
122 static void
123 stop_source_peer (void *cls,
124                   const struct GNUNET_SCHEDULER_TaskContext *tc)
125 {
126   struct GNUNET_FS_Uri *uri = cls;
127   struct GNUNET_TESTING_PeerGroup *pg;
128
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Stopping source peer\n");
131   pg = GNUNET_FS_TEST_get_group (daemons);
132   GNUNET_TESTING_daemons_vary (pg, 1, GNUNET_NO, TIMEOUT,
133                                &do_download,
134                                uri);
135 }
136
137
138 static void
139 do_wait (void *cls,
140          const struct GNUNET_FS_Uri *uri)
141 {
142   struct GNUNET_FS_Uri *d;
143
144   if (NULL == uri)
145     {
146       GNUNET_FS_TEST_daemons_stop (sched,
147                                    2,
148                                    daemons);
149       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150                   "Timeout during upload attempt, shutting down with error\n");
151       ok = 1;
152       return;
153     }
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155               "Waiting to allow content to migrate\n"); 
156   d = GNUNET_FS_uri_dup (uri);
157   (void) GNUNET_SCHEDULER_add_delayed (sched,
158                                        MIGRATION_DELAY,
159                                        &stop_source_peer,
160                                        d);
161 }
162
163
164 static void
165 do_publish (void *cls,
166             const struct GNUNET_SCHEDULER_TaskContext *tc)
167 {
168   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
169     {
170       GNUNET_FS_TEST_daemons_stop (sched,
171                                    2,
172                                    daemons);
173       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174                   "Timeout during connect attempt, shutting down with error\n");
175       ok = 1;
176       return;
177     }
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179               "Publishing %llu bytes\n",
180               (unsigned long long) FILESIZE);
181   GNUNET_FS_TEST_publish (sched,
182                           daemons[1],
183                           TIMEOUT,
184                           1, GNUNET_NO, FILESIZE, SEED, 
185                           VERBOSE, 
186                           &do_wait, NULL);
187 }
188
189
190 static void
191 do_connect (void *cls,
192             const struct GNUNET_SCHEDULER_TaskContext *tc)
193 {
194   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
195     {
196       fprintf (stderr,
197                "Daemons failed to start!\n");
198       GNUNET_break (0);
199       ok = 1;
200       return;
201     }
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203               "Daemons started, will now try to connect them\n");
204   GNUNET_FS_TEST_daemons_connect (sched,
205                                   daemons[0],
206                                   daemons[1],
207                                   TIMEOUT,
208                                   &do_publish,
209                                   NULL);  
210 }
211
212
213 static void
214 run (void *cls,
215      struct GNUNET_SCHEDULER_Handle *s,
216      char *const *args,
217      const char *cfgfile,
218      const struct GNUNET_CONFIGURATION_Handle *cfg)
219 {
220   sched = s;
221   GNUNET_FS_TEST_daemons_start (sched,
222                                 "test_gnunet_service_fs_migration_data.conf",
223                                 TIMEOUT,
224                                 2,
225                                 daemons,
226                                 &do_connect,
227                                 NULL);
228 }
229
230
231 int
232 main (int argc, char *argv[])
233 {
234   char *const argvx[] = { 
235     "test-gnunet-service-fs-migration",
236     "-c",
237     "fs_test_lib_data.conf",
238 #if VERBOSE
239     "-L", "DEBUG",
240 #endif
241     NULL
242   };
243   struct GNUNET_GETOPT_CommandLineOption options[] = {
244     GNUNET_GETOPT_OPTION_END
245   };
246
247   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
248   GNUNET_log_setup ("test_gnunet_service_fs_migration", 
249 #if VERBOSE
250                     "DEBUG",
251 #else
252                     "WARNING",
253 #endif
254                     NULL);
255   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
256                       argvx, "test-gnunet-service-fs-migration",
257                       "nohelp", options, &run, NULL);
258   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
259   return ok;
260 }
261
262 /* end of test_gnunet_service_fs_migration.c */