-fixing #2006
[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_EXTRA_LOGGING
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, 90)
46
47 #define SEED 42
48
49 static struct GNUNET_FS_TestDaemon *daemons[2];
50
51 static int ok;
52
53 static struct GNUNET_TIME_Absolute start_time;
54
55 static struct GNUNET_FS_TEST_ConnectContext *cc;
56
57 static void
58 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
59 {
60   struct GNUNET_TIME_Relative del;
61   char *fancy;
62
63   if (NULL != cc)
64   {
65     GNUNET_FS_TEST_daemons_connect_cancel (cc);
66     cc = NULL;
67   }
68   GNUNET_FS_TEST_daemons_stop (2, daemons);
69   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
70   {
71     del = GNUNET_TIME_absolute_get_duration (start_time);
72     if (del.rel_value == 0)
73       del.rel_value = 1;
74     fancy =
75         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
76                                         1000LL / del.rel_value);
77     fprintf (stdout, "Download speed was %s/s\n", fancy);
78     GNUNET_free (fancy);
79     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
80                 (unsigned long long) FILESIZE);
81   }
82   else
83   {
84     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85                 "Timeout during download, shutting down with error\n");
86     ok = 1;
87   }
88 }
89
90
91 static void
92 do_download (void *cls, const char *emsg)
93 {
94   struct GNUNET_FS_Uri *uri = cls;
95
96   if (emsg != NULL)
97   {
98     GNUNET_FS_TEST_daemons_stop (2, daemons);
99     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to stop source daemon: %s\n",
100                 emsg);
101     GNUNET_FS_uri_destroy (uri);
102     ok = 1;
103     return;
104   }
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
106               (unsigned long long) FILESIZE);
107   start_time = GNUNET_TIME_absolute_get ();
108   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
109                            NULL);
110   GNUNET_FS_uri_destroy (uri);
111 }
112
113
114 static void
115 stop_source_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   struct GNUNET_FS_Uri *uri = cls;
118   struct GNUNET_TESTING_PeerGroup *pg;
119
120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping source peer\n");
121   pg = GNUNET_FS_TEST_get_group (daemons);
122   GNUNET_TESTING_daemons_vary (pg, 1, GNUNET_NO, TIMEOUT, &do_download, uri);
123 }
124
125
126 static void
127 do_wait (void *cls, const struct GNUNET_FS_Uri *uri)
128 {
129   struct GNUNET_FS_Uri *d;
130
131   if (NULL == uri)
132   {
133     GNUNET_FS_TEST_daemons_stop (2, daemons);
134     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135                 "Timeout during upload attempt, shutting down with error\n");
136     ok = 1;
137     return;
138   }
139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting to allow content to migrate\n");
140   d = GNUNET_FS_uri_dup (uri);
141   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY, &stop_source_peer, d);
142 }
143
144
145 static void
146 do_publish (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   cc = NULL;
149   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
150   {
151     GNUNET_FS_TEST_daemons_stop (2, daemons);
152     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153                 "Timeout during connect attempt, shutting down with error\n");
154     ok = 1;
155     return;
156   }
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
158               (unsigned long long) FILESIZE);
159   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
160                           VERBOSE, &do_wait, NULL);
161 }
162
163
164 static void
165 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
168   {
169     fprintf (stderr, "Daemons failed to start!\n");
170     GNUNET_break (0);
171     ok = 1;
172     return;
173   }
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175               "Daemons started, will now try to connect them\n");
176   cc = GNUNET_FS_TEST_daemons_connect (daemons[0], daemons[1], TIMEOUT,
177                                        &do_publish, NULL);
178 }
179
180
181 static void
182 run (void *cls, char *const *args, const char *cfgfile,
183      const struct GNUNET_CONFIGURATION_Handle *cfg)
184 {
185   GNUNET_FS_TEST_daemons_start ("test_gnunet_service_fs_migration_data.conf",
186                                 TIMEOUT, 2, daemons, &do_connect, NULL);
187 }
188
189
190 int
191 main (int argc, char *argv[])
192 {
193   char *const argvx[] = {
194     "test-gnunet-service-fs-migration",
195     "-c",
196     "fs_test_lib_data.conf",
197 #if VERBOSE
198     "-L", "DEBUG",
199 #endif
200     NULL
201   };
202   struct GNUNET_GETOPT_CommandLineOption options[] = {
203     GNUNET_GETOPT_OPTION_END
204   };
205
206   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
207   GNUNET_log_setup ("test_gnunet_service_fs_migration",
208 #if VERBOSE
209                     "DEBUG",
210 #else
211                     "WARNING",
212 #endif
213                     NULL);
214   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
215                       "test-gnunet-service-fs-migration", "nohelp", options,
216                       &run, NULL);
217   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
218   return ok;
219 }
220
221 /* end of test_gnunet_service_fs_migration.c */