-fix #2598
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_migration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2012 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_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 static void
59 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   struct GNUNET_TIME_Relative del;
62   char *fancy;
63
64   GNUNET_SCHEDULER_shutdown ();
65   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
66   {
67     del = GNUNET_TIME_absolute_get_duration (start_time);
68     if (del.rel_value == 0)
69       del.rel_value = 1;
70     fancy =
71         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
72                                         1000LL / del.rel_value);
73     FPRINTF (stdout, "Download speed was %s/s\n", fancy);
74     GNUNET_free (fancy);
75     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
76                 (unsigned long long) FILESIZE);
77   }
78   else
79   {
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81                 "Timeout during download, shutting down with error\n");
82     ok = 1;
83   }
84 }
85
86
87 static void
88 do_download (void *cls, 
89              const char *emsg)
90 {
91   struct GNUNET_FS_Uri *uri = cls;
92
93   GNUNET_TESTBED_operation_done (op);
94   op = NULL;
95   if (NULL != emsg)
96   {
97     GNUNET_SCHEDULER_shutdown ();
98     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to stop source daemon: %s\n",
99                 emsg);
100     GNUNET_FS_uri_destroy (uri);
101     ok = 1;
102     return;
103   }
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
105               (unsigned long long) FILESIZE);
106   start_time = GNUNET_TIME_absolute_get ();
107   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
108                            NULL);
109   GNUNET_FS_uri_destroy (uri);
110 }
111
112
113 static void
114 stop_source_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   struct GNUNET_FS_Uri *uri = cls;
117
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping source peer\n");
119   op = GNUNET_TESTBED_peer_stop (daemons[1], &do_download, uri);
120   GNUNET_assert (NULL != op);
121 }
122
123
124 static void
125 do_wait (void *cls, const struct GNUNET_FS_Uri *uri)
126 {
127   struct GNUNET_FS_Uri *d;
128
129   if (NULL == uri)
130   {
131     GNUNET_SCHEDULER_shutdown ();
132     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133                 "Timeout during upload attempt, shutting down with error\n");
134     ok = 1;
135     return;
136   }
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting to allow content to migrate\n");
138   d = GNUNET_FS_uri_dup (uri);
139   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY, &stop_source_peer, d);
140 }
141
142
143 static void
144 do_publish (void *cls, 
145             struct GNUNET_TESTBED_Operation *oparg,
146             const char *emsg)
147 {
148   GNUNET_assert (op == oparg);
149   GNUNET_TESTBED_operation_done (op);
150   op = NULL;
151   if (NULL != emsg)
152   {
153     GNUNET_SCHEDULER_shutdown ();
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155                 "Error connecting peers: %s\n",
156                 emsg);
157     ok = 1;
158     return;
159   }
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
161               (unsigned long long) FILESIZE);
162   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
163                           VERBOSE, &do_wait, NULL);
164 }
165
166
167 static void
168 do_connect (void *cls, 
169             unsigned int num_peers,
170             struct GNUNET_TESTBED_Peer **peers)
171 {
172   unsigned int i;
173
174   GNUNET_assert (2 == num_peers);
175   for (i=0;i<num_peers;i++)
176     daemons[i] = peers[i];
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Daemons started, will now try to connect them\n");
179   op = GNUNET_TESTBED_overlay_connect (NULL,
180                                        &do_publish, NULL,
181                                        daemons[0], daemons[1]);
182 }
183
184
185 int
186 main (int argc, char *argv[])
187 {
188   GNUNET_TESTBED_test_run ("test-gnunet-service-fs-migration",
189                            "fs_test_lib_data.conf",
190                            2,
191                            0, NULL, NULL,
192                            &do_connect,
193                            NULL);
194   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
195   return ok;
196 }
197
198 /* end of test_gnunet_service_fs_migration.c */