fix
[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 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 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, 60)
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, 5)
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       ok = 1;
105       return;
106     }
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "Downloading %llu bytes\n",
109               (unsigned long long) FILESIZE);
110   start_time = GNUNET_TIME_absolute_get ();
111   GNUNET_FS_TEST_download (sched,
112                            daemons[0],
113                            TIMEOUT,
114                            1, SEED, uri, 
115                            VERBOSE, 
116                            &do_stop, NULL);
117 }
118
119
120 static void
121 stop_source_peer (void *cls,
122                   const struct GNUNET_SCHEDULER_TaskContext *tc)
123 {
124   struct GNUNET_FS_Uri *uri = cls;
125   struct GNUNET_TESTING_PeerGroup *pg;
126
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Stopping source peer\n");
129   pg = GNUNET_FS_TEST_get_group (daemons);
130   GNUNET_TESTING_daemons_vary (pg, 1, GNUNET_NO, TIMEOUT,
131                                &do_download,
132                                uri);
133 }
134
135
136 static void
137 do_wait (void *cls,
138          const struct GNUNET_FS_Uri *uri)
139 {
140   struct GNUNET_FS_Uri *d;
141
142   if (NULL == uri)
143     {
144       GNUNET_FS_TEST_daemons_stop (sched,
145                                    2,
146                                    daemons);
147       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148                   "Timeout during upload attempt, shutting down with error\n");
149       ok = 1;
150       return;
151     }
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Waiting to allow content to migrate\n"); 
154   d = GNUNET_FS_uri_dup (uri);
155   GNUNET_SCHEDULER_add_delayed (sched,
156                                 MIGRATION_DELAY,
157                                 &stop_source_peer,
158                                 d);
159 }
160
161
162 static void
163 do_publish (void *cls,
164             const struct GNUNET_SCHEDULER_TaskContext *tc)
165 {
166   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
167     {
168       GNUNET_FS_TEST_daemons_stop (sched,
169                                    2,
170                                    daemons);
171       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172                   "Timeout during connect attempt, shutting down with error\n");
173       ok = 1;
174       return;
175     }
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177               "Publishing %llu bytes\n",
178               (unsigned long long) FILESIZE);
179   GNUNET_FS_TEST_publish (sched,
180                           daemons[1],
181                           TIMEOUT,
182                           1, GNUNET_NO, FILESIZE, SEED, 
183                           VERBOSE, 
184                           &do_wait, NULL);
185 }
186
187
188 static void
189 do_connect (void *cls,
190             const struct GNUNET_SCHEDULER_TaskContext *tc)
191 {
192   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194               "Daemons started, will now try to connect them\n");
195   GNUNET_FS_TEST_daemons_connect (sched,
196                                   daemons[0],
197                                   daemons[1],
198                                   TIMEOUT,
199                                   &do_publish,
200                                   NULL);  
201 }
202
203
204 static void
205 run (void *cls,
206      struct GNUNET_SCHEDULER_Handle *s,
207      char *const *args,
208      const char *cfgfile,
209      const struct GNUNET_CONFIGURATION_Handle *cfg)
210 {
211   sched = s;
212   GNUNET_FS_TEST_daemons_start (sched,
213                                 "test_gnunet_service_fs_migration_data.conf",
214                                 TIMEOUT,
215                                 2,
216                                 daemons,
217                                 &do_connect,
218                                 NULL);
219 }
220
221
222 int
223 main (int argc, char *argv[])
224 {
225   char *const argvx[] = { 
226     "test-gnunet-service-fs-migration",
227     "-c",
228     "fs_test_lib_data.conf",
229 #if VERBOSE
230     "-L", "DEBUG",
231 #endif
232     NULL
233   };
234   struct GNUNET_GETOPT_CommandLineOption options[] = {
235     GNUNET_GETOPT_OPTION_END
236   };
237
238   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
239   GNUNET_log_setup ("test_gnunet_service_fs_migration", 
240 #if VERBOSE
241                     "DEBUG",
242 #else
243                     "WARNING",
244 #endif
245                     NULL);
246   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
247                       argvx, "test-gnunet-service-fs-migration",
248                       "nohelp", options, &run, NULL);
249   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
250   return ok;
251 }
252
253 /* end of test_gnunet_service_fs_migration.c */