tighten formatting rules
[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, \
46                                                        90)
47
48 #define SEED 42
49
50 static struct GNUNET_TESTBED_Peer *daemons[2];
51
52 static int ok;
53
54 static struct GNUNET_TIME_Absolute start_time;
55
56 static struct GNUNET_TESTBED_Operation *op;
57
58
59 struct DownloadContext
60 {
61   char *fn;
62
63   struct GNUNET_FS_Uri *uri;
64 };
65
66
67 static void
68 do_stop (void *cls)
69 {
70   struct GNUNET_TIME_Relative del;
71   char *fancy;
72
73   GNUNET_SCHEDULER_shutdown ();
74   if (0 ==
75       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
76                                                                     TIMEOUT)).
77       rel_value_us)
78   {
79     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80                 "Timeout during download, shutting down with error\n");
81     ok = 1;
82   }
83   else
84   {
85     del = GNUNET_TIME_absolute_get_duration (start_time);
86     if (del.rel_value_us == 0)
87       del.rel_value_us = 1;
88     fancy =
89       GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE)
90                                       * 1000000LL / del.rel_value_us);
91     fprintf (stdout,
92              "Download speed was %s/s\n",
93              fancy);
94     GNUNET_free (fancy);
95     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96                 "Finished download, shutting down\n");
97   }
98 }
99
100
101 static void
102 do_download (void *cls,
103              const char *emsg)
104 {
105   struct DownloadContext *dc = cls;
106   struct GNUNET_FS_Uri *uri = dc->uri;
107
108   GNUNET_TESTBED_operation_done (op);
109   op = NULL;
110   if (NULL != dc->fn)
111   {
112     GNUNET_DISK_directory_remove (dc->fn);
113     GNUNET_free (dc->fn);
114   }
115   GNUNET_free (dc);
116   if (NULL != emsg)
117   {
118     GNUNET_SCHEDULER_shutdown ();
119     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120                 "Failed to stop source daemon: %s\n",
121                 emsg);
122     GNUNET_FS_uri_destroy (uri);
123     ok = 1;
124     return;
125   }
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127               "Downloading %llu bytes\n",
128               (unsigned long long) FILESIZE);
129   start_time = GNUNET_TIME_absolute_get ();
130   GNUNET_FS_TEST_download (daemons[0],
131                            TIMEOUT,
132                            1,
133                            SEED,
134                            uri,
135                            VERBOSE,
136                            &do_stop,
137                            NULL);
138   GNUNET_FS_uri_destroy (uri);
139 }
140
141
142 static void
143 stop_source_peer (void *cls)
144 {
145   struct DownloadContext *dc = cls;
146
147   /* FIXME: We should not interact with testbed when shutting down */
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "Stopping source peer\n");
150   op = GNUNET_TESTBED_peer_stop (NULL,
151                                  daemons[1],
152                                  &do_download, dc);
153   GNUNET_assert (NULL != op);
154 }
155
156
157 static void
158 do_wait (void *cls,
159          const struct GNUNET_FS_Uri *uri,
160          const char *fn)
161 {
162   struct DownloadContext *dc;
163
164   if (NULL == uri)
165   {
166     GNUNET_SCHEDULER_shutdown ();
167     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168                 "Timeout during upload attempt, shutting down with error\n");
169     ok = 1;
170     return;
171   }
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173               "Waiting to allow content to migrate\n");
174   dc = GNUNET_new (struct DownloadContext);
175   dc->uri = GNUNET_FS_uri_dup (uri);
176   if (NULL != fn)
177     dc->fn = GNUNET_strdup (fn);
178   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY,
179                                        &stop_source_peer,
180                                        dc);
181 }
182
183
184 static void
185 do_publish (void *cls,
186             struct GNUNET_TESTBED_RunHandle *h,
187             unsigned int num_peers,
188             struct GNUNET_TESTBED_Peer **peers,
189             unsigned int links_succeeded,
190             unsigned int links_failed)
191 {
192   unsigned int i;
193
194   GNUNET_assert (2 == num_peers);
195   for (i = 0; i < num_peers; i++)
196     daemons[i] = peers[i];
197   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198               "Publishing %llu bytes\n",
199               (unsigned long long) FILESIZE);
200   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO,
201                           FILESIZE, SEED,
202                           VERBOSE, &do_wait, NULL);
203 }
204
205
206 int
207 main (int argc,
208       char *argv[])
209 {
210   (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-migration",
211                                   "fs_test_lib_data.conf",
212                                   2,
213                                   0, NULL, NULL,
214                                   &do_publish,
215                                   NULL);
216   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
217   return ok;
218 }
219
220
221 /* end of test_gnunet_service_fs_migration.c */