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