fix compiler warnings
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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                 (unsigned long long) FILESIZE);
69   }
70   if (NULL != fn)
71   {
72     GNUNET_DISK_directory_remove (fn);
73     GNUNET_free (fn);
74   }
75   GNUNET_SCHEDULER_shutdown ();
76 }
77
78
79 static void
80 do_download (void *cls,
81              const struct GNUNET_FS_Uri *uri,
82              const char *fn)
83 {
84   if (NULL == uri)
85   {
86     GNUNET_break (0);
87     GNUNET_SCHEDULER_shutdown ();
88     ret = 1;
89     return;
90   }
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92               "Downloading %llu bytes\n",
93               (unsigned long long) FILESIZE);
94   start_time = GNUNET_TIME_absolute_get ();
95   GNUNET_FS_TEST_download (the_peers[0],
96                            TIMEOUT, 1, SEED,
97                            uri,
98                            VERBOSE,
99                            &do_stop,
100                            (NULL == fn) ? NULL : GNUNET_strdup (fn));
101 }
102
103
104 static void
105 do_publish (void *cls,
106             struct GNUNET_TESTBED_Operation *op,
107             const char *emsg)
108 {
109   GNUNET_TESTBED_operation_done (op);
110   if (NULL != emsg)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect peers: %s\n", emsg);
113     GNUNET_break (0);
114     ret = 1;
115     GNUNET_SCHEDULER_shutdown ();
116     return;
117   }
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
119               (unsigned long long) FILESIZE);
120   GNUNET_FS_TEST_publish (the_peers[0], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
121                           VERBOSE, &do_download, NULL);
122
123 }
124
125
126 /**
127  * Actual main function for the test.
128  *
129  * @param cls closure
130  * @param h the run handle
131  * @param num_peers number of peers in 'peers'
132  * @param peers handle to peers run in the testbed
133  * @param links_succeeded the number of overlay link connection attempts that
134  *          succeeded
135  * @param links_failed the number of overlay link connection attempts that
136  *          failed
137  */
138 static void
139 run (void *cls,
140      struct GNUNET_TESTBED_RunHandle *h,
141      unsigned int num_peers,
142      struct GNUNET_TESTBED_Peer **peers,
143      unsigned int links_succeeded,
144      unsigned int links_failed)
145 {
146   unsigned int i;
147
148   GNUNET_assert (NUM_DAEMONS == num_peers);
149   for (i=0;i<num_peers;i++)
150     the_peers[i] = peers[i];
151   GNUNET_TESTBED_overlay_connect (NULL,
152                                   &do_publish,
153                                   NULL,
154                                   peers[0],
155                                   peers[1]);
156 }
157
158
159 /**
160  * Main function that initializes the testbed.
161  *
162  * @param argc ignored
163  * @param argv ignored
164  * @return 0 on success
165  */
166 int
167 main (int argc, char *argv[])
168 {
169   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
170   (void) GNUNET_TESTBED_test_run ("test_fs_test_lib",
171                                   "fs_test_lib_data.conf",
172                                   NUM_DAEMONS,
173                                   0, NULL, NULL,
174                                   &run, NULL);
175   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
176   return ret;
177 }
178
179 /* end of test_fs_test_lib.c */