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