b575e68c7b3f4e424d26f3164a443f19007c7370
[oweals/gnunet.git] / src / fs / test_fs_test_lib.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 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 2, 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, 60)
40
41 #define NUM_DAEMONS 2
42
43 #define SEED 42
44
45 static struct GNUNET_FS_TestDaemon *daemons[NUM_DAEMONS];
46
47 static struct GNUNET_SCHEDULER_Handle *sched;
48
49 static int ret;
50
51 static void
52 do_stop (void *cls,
53          const struct GNUNET_SCHEDULER_TaskContext *tc)
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,
63                   "Finished download, shutting down\n",
64                   (unsigned long long) FILESIZE);
65     }
66   GNUNET_FS_TEST_daemons_stop (sched,
67                                NUM_DAEMONS,
68                                daemons);
69 }
70
71
72 static void
73 do_download (void *cls,
74              const struct GNUNET_FS_Uri *uri)
75 {
76   if (NULL == uri)
77     {
78       GNUNET_break (0);
79       GNUNET_SCHEDULER_add_now (sched,
80                                 &do_stop,
81                                 NULL);
82       ret = 1;
83       return;
84     }
85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86               "Downloading %llu bytes\n",
87               (unsigned long long) FILESIZE);
88   GNUNET_FS_TEST_download (sched,
89                            daemons[0],
90                            TIMEOUT,
91                            1, SEED, uri, 
92                            VERBOSE, 
93                            &do_stop, NULL);
94 }
95
96
97 static void
98 do_publish (void *cls,
99             const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
102     {
103       GNUNET_break (0);
104       ret = 1;
105       GNUNET_SCHEDULER_add_now (sched,
106                                 &do_stop,
107                                 NULL);
108     }
109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110               "Publishing %llu bytes\n",
111               (unsigned long long) FILESIZE);
112   GNUNET_FS_TEST_publish (sched,
113                           daemons[0],
114                           TIMEOUT,
115                           1, GNUNET_NO, FILESIZE, SEED, 
116                           VERBOSE, 
117                           &do_download, NULL);
118 }
119
120
121 static void
122 do_connect (void *cls,
123             const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
126     {
127       GNUNET_break (0);
128       ret = 1;
129       GNUNET_SCHEDULER_add_now (sched,
130                                 &do_stop,
131                                 NULL);
132       return;
133     }
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135               "Daemons started, will now try to connect them\n");
136   GNUNET_FS_TEST_daemons_connect (sched,
137                                   daemons[0],
138                                   daemons[1],
139                                   TIMEOUT,
140                                   &do_publish,
141                                   NULL);  
142 }
143
144
145 static void
146 run (void *cls,
147      struct GNUNET_SCHEDULER_Handle *s,
148      char *const *args,
149      const char *cfgfile,
150      const struct GNUNET_CONFIGURATION_Handle *cfg)
151 {
152   sched = s;
153   GNUNET_FS_TEST_daemons_start (sched,
154                                 TIMEOUT,
155                                 NUM_DAEMONS,
156                                 daemons,
157                                 &do_connect,
158                                 NULL);
159 }
160
161
162 int
163 main (int argc, char *argv[])
164 {
165   char *const argvx[] = { 
166     "test-fs-test-lib",
167     "-c",
168     "fs_test_lib_data.conf",
169 #if VERBOSE
170     "-L", "DEBUG",
171 #endif
172     NULL
173   };
174   struct GNUNET_GETOPT_CommandLineOption options[] = {
175     GNUNET_GETOPT_OPTION_END
176   };
177
178   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
179   GNUNET_log_setup ("test_fs_test_lib", 
180 #if VERBOSE
181                     "DEBUG",
182 #else
183                     "WARNING",
184 #endif
185                     NULL);
186   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
187                       argvx, "test-fs-test-lib",
188                       "nohelp", options, &run, NULL);
189   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
190   return ret;
191 }
192
193 /* end of test_fs_test_lib.c */