fs hackery
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_p2p.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_gnunet_service_fs_p2p.c
23  * @brief test P2P routing using simple publish + download operation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "fs_test_lib.h"
28
29 #define VERBOSE GNUNET_YES
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, 600)
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 ok;
50
51
52 static void
53 do_stop (void *cls,
54          const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   GNUNET_FS_TEST_daemons_stop (sched,
57                                NUM_DAEMONS,
58                                daemons);
59   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
60     {
61       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62                   "Finished download, shutting down\n",
63                   (unsigned long long) FILESIZE);
64     }
65   else
66     {
67       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68                   "Timeout during download, shutting down with error\n");
69       ok = 1;
70     }
71 }
72
73
74 static void
75 do_download (void *cls,
76              const struct GNUNET_FS_Uri *uri)
77 {
78   if (NULL == uri)
79     {
80       GNUNET_FS_TEST_daemons_stop (sched,
81                                    NUM_DAEMONS,
82                                    daemons);
83       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84                   "Timeout during upload attempt, shutting down with error\n");
85       ok = 1;
86       return;
87     }
88   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89               "Downloading %llu bytes\n",
90               (unsigned long long) FILESIZE);
91   GNUNET_FS_TEST_download (sched,
92                            daemons[0],
93                            TIMEOUT,
94                            1, SEED, uri, 
95                            VERBOSE, 
96                            &do_stop, NULL);
97 }
98
99
100 static void
101 do_publish (void *cls,
102             const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
105     {
106       GNUNET_FS_TEST_daemons_stop (sched,
107                                    NUM_DAEMONS,
108                                    daemons);
109       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110                   "Timeout during connect attempt, shutting down with error\n");
111       ok = 1;
112       return;
113     }
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115               "Publishing %llu bytes\n",
116               (unsigned long long) FILESIZE);
117   GNUNET_FS_TEST_publish (sched,
118                           daemons[1],
119                           TIMEOUT,
120                           1, GNUNET_NO, FILESIZE, SEED, 
121                           VERBOSE, 
122                           &do_download, NULL);
123 }
124
125
126 static void
127 do_connect (void *cls,
128             const struct GNUNET_SCHEDULER_TaskContext *tc)
129 {
130   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132               "Daemons started, will now try to connect them\n");
133   GNUNET_FS_TEST_daemons_connect (sched,
134                                   daemons[0],
135                                   daemons[1],
136                                   TIMEOUT,
137                                   &do_publish,
138                                   NULL);  
139 }
140
141
142 static void
143 run (void *cls,
144      struct GNUNET_SCHEDULER_Handle *s,
145      char *const *args,
146      const char *cfgfile,
147      const struct GNUNET_CONFIGURATION_Handle *cfg)
148 {
149   sched = s;
150   GNUNET_FS_TEST_daemons_start (sched,
151                                 TIMEOUT,
152                                 NUM_DAEMONS,
153                                 daemons,
154                                 &do_connect,
155                                 NULL);
156 }
157
158
159 int
160 main (int argc, char *argv[])
161 {
162   char *const argvx[] = { 
163     "test-gnunet-service-fs-p2p",
164     "-c",
165     "fs_test_lib_data.conf",
166 #if VERBOSE
167     "-L", "DEBUG",
168 #endif
169     NULL
170   };
171   struct GNUNET_GETOPT_CommandLineOption options[] = {
172     GNUNET_GETOPT_OPTION_END
173   };
174
175   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
176   GNUNET_log_setup ("test_gnunet_service_fs_p2p", 
177 #if VERBOSE
178                     "DEBUG",
179 #else
180                     "WARNING",
181 #endif
182                     NULL);
183   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
184                       argvx, "test-gnunet-service-fs-p2p",
185                       "nohelp", options, &run, NULL);
186   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
187   return ok;
188 }
189
190 /* end of test_gnunet_service_fs_p2p.c */