tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / fs / test_fs_test_lib.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2012 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_fs_test_lib.c
23  * @brief test fs test library
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "fs_test_lib.h"
28
29 #define VERBOSE GNUNET_NO
30
31 /**
32  * File-size we use for testing.
33  */
34 #define FILESIZE (1024 * 1024 * 2)
35
36 /**
37  * How long until we give up on transmitting the message?
38  */
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40
41 #define NUM_DAEMONS 2
42
43 #define SEED 42
44
45 static struct GNUNET_TESTBED_Peer *the_peers[NUM_DAEMONS];
46
47 static struct GNUNET_TIME_Absolute start_time;
48
49 static int ret;
50
51
52 static void
53 do_stop (void *cls)
54 {
55   char *fn = cls;
56
57   if (0 ==
58       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
59                                                                     TIMEOUT)).rel_value_us)
60   {
61     GNUNET_break (0);
62     ret = 1;
63   }
64   else
65   {
66     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67                 "Finished download, shutting down\n");
68   }
69   if (NULL != fn)
70   {
71     GNUNET_DISK_directory_remove (fn);
72     GNUNET_free (fn);
73   }
74   GNUNET_SCHEDULER_shutdown ();
75 }
76
77
78 static void
79 do_download (void *cls,
80              const struct GNUNET_FS_Uri *uri,
81              const char *fn)
82 {
83   if (NULL == uri)
84   {
85     GNUNET_break (0);
86     GNUNET_SCHEDULER_shutdown ();
87     ret = 1;
88     return;
89   }
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91               "Downloading %llu bytes\n",
92               (unsigned long long) FILESIZE);
93   start_time = GNUNET_TIME_absolute_get ();
94   GNUNET_FS_TEST_download (the_peers[0],
95                            TIMEOUT, 1, SEED,
96                            uri,
97                            VERBOSE,
98                            &do_stop,
99                            (NULL == fn) ? NULL : GNUNET_strdup (fn));
100 }
101
102
103 static void
104 do_publish (void *cls,
105             struct GNUNET_TESTBED_Operation *op,
106             const char *emsg)
107 {
108   GNUNET_TESTBED_operation_done (op);
109   if (NULL != emsg)
110   {
111     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect peers: %s\n", emsg);
112     GNUNET_break (0);
113     ret = 1;
114     GNUNET_SCHEDULER_shutdown ();
115     return;
116   }
117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
118               (unsigned long long) FILESIZE);
119   GNUNET_FS_TEST_publish (the_peers[0], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
120                           VERBOSE, &do_download, NULL);
121
122 }
123
124
125 /**
126  * Actual main function for the test.
127  *
128  * @param cls closure
129  * @param h the run handle
130  * @param num_peers number of peers in 'peers'
131  * @param peers handle to peers run in the testbed
132  * @param links_succeeded the number of overlay link connection attempts that
133  *          succeeded
134  * @param links_failed the number of overlay link connection attempts that
135  *          failed
136  */
137 static void
138 run (void *cls,
139      struct GNUNET_TESTBED_RunHandle *h,
140      unsigned int num_peers,
141      struct GNUNET_TESTBED_Peer **peers,
142      unsigned int links_succeeded,
143      unsigned int links_failed)
144 {
145   unsigned int i;
146
147   GNUNET_assert (NUM_DAEMONS == num_peers);
148   for (i=0;i<num_peers;i++)
149     the_peers[i] = peers[i];
150   GNUNET_TESTBED_overlay_connect (NULL,
151                                   &do_publish,
152                                   NULL,
153                                   peers[0],
154                                   peers[1]);
155 }
156
157
158 /**
159  * Main function that initializes the testbed.
160  *
161  * @param argc ignored
162  * @param argv ignored
163  * @return 0 on success
164  */
165 int
166 main (int argc, char *argv[])
167 {
168   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
169   (void) GNUNET_TESTBED_test_run ("test_fs_test_lib",
170                                   "fs_test_lib_data.conf",
171                                   NUM_DAEMONS,
172                                   0, NULL, NULL,
173                                   &run, NULL);
174   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
175   return ret;
176 }
177
178 /* end of test_fs_test_lib.c */