tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_migration.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2012, 2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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_testbed_service.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, 90)
46
47 #define SEED 42
48
49 static struct GNUNET_TESTBED_Peer *daemons[2];
50
51 static int ok;
52
53 static struct GNUNET_TIME_Absolute start_time;
54
55 static struct GNUNET_TESTBED_Operation *op;
56
57
58 struct DownloadContext
59 {
60   char *fn;
61
62   struct GNUNET_FS_Uri *uri;
63 };
64
65
66 static void
67 do_stop (void *cls)
68 {
69   struct GNUNET_TIME_Relative del;
70   char *fancy;
71
72   GNUNET_SCHEDULER_shutdown ();
73   if (0 ==
74       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
75                                                                     TIMEOUT)).rel_value_us)
76   {
77     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78                 "Timeout during download, shutting down with error\n");
79     ok = 1;
80   }
81   else
82   {
83     del = GNUNET_TIME_absolute_get_duration (start_time);
84     if (del.rel_value_us == 0)
85       del.rel_value_us = 1;
86     fancy =
87         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
88                                         1000000LL / del.rel_value_us);
89     FPRINTF (stdout,
90              "Download speed was %s/s\n",
91              fancy);
92     GNUNET_free (fancy);
93     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                 "Finished download, shutting down\n");
95   }
96 }
97
98
99 static void
100 do_download (void *cls,
101              const char *emsg)
102 {
103   struct DownloadContext *dc = cls;
104   struct GNUNET_FS_Uri *uri = dc->uri;
105
106   GNUNET_TESTBED_operation_done (op);
107   op = NULL;
108   if (NULL != dc->fn)
109   {
110     GNUNET_DISK_directory_remove (dc->fn);
111     GNUNET_free (dc->fn);
112   }
113   GNUNET_free (dc);
114   if (NULL != emsg)
115   {
116     GNUNET_SCHEDULER_shutdown ();
117     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118                 "Failed to stop source daemon: %s\n",
119                 emsg);
120     GNUNET_FS_uri_destroy (uri);
121     ok = 1;
122     return;
123   }
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125               "Downloading %llu bytes\n",
126               (unsigned long long) FILESIZE);
127   start_time = GNUNET_TIME_absolute_get ();
128   GNUNET_FS_TEST_download (daemons[0],
129                            TIMEOUT,
130                            1,
131                            SEED,
132                            uri,
133                            VERBOSE,
134                            &do_stop,
135                            NULL);
136   GNUNET_FS_uri_destroy (uri);
137 }
138
139
140 static void
141 stop_source_peer (void *cls)
142 {
143   struct DownloadContext *dc = cls;
144
145   /* FIXME: We should not interact with testbed when shutting down */
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "Stopping source peer\n");
148   op = GNUNET_TESTBED_peer_stop (NULL,
149                                  daemons[1],
150                                  &do_download, dc);
151   GNUNET_assert (NULL != op);
152 }
153
154
155 static void
156 do_wait (void *cls,
157          const struct GNUNET_FS_Uri *uri,
158          const char *fn)
159 {
160   struct DownloadContext *dc;
161
162   if (NULL == uri)
163   {
164     GNUNET_SCHEDULER_shutdown ();
165     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166                 "Timeout during upload attempt, shutting down with error\n");
167     ok = 1;
168     return;
169   }
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171               "Waiting to allow content to migrate\n");
172   dc = GNUNET_new (struct DownloadContext);
173   dc->uri = GNUNET_FS_uri_dup (uri);
174   if (NULL != fn)
175     dc->fn = GNUNET_strdup (fn);
176   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY,
177                                        &stop_source_peer,
178                                        dc);
179 }
180
181
182 static void
183 do_publish (void *cls,
184             struct GNUNET_TESTBED_RunHandle *h,
185             unsigned int num_peers,
186             struct GNUNET_TESTBED_Peer **peers,
187             unsigned int links_succeeded,
188             unsigned int links_failed)
189 {
190   unsigned int i;
191
192   GNUNET_assert (2 == num_peers);
193   for (i=0;i<num_peers;i++)
194     daemons[i] = peers[i];
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196               "Publishing %llu bytes\n",
197               (unsigned long long) FILESIZE);
198   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO,
199                           FILESIZE, SEED,
200                           VERBOSE, &do_wait, NULL);
201 }
202
203
204 int
205 main (int argc,
206       char *argv[])
207 {
208   (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-migration",
209                                   "fs_test_lib_data.conf",
210                                   2,
211                                   0, NULL, NULL,
212                                   &do_publish,
213                                   NULL);
214   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
215   return ok;
216 }
217
218 /* end of test_gnunet_service_fs_migration.c */