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