dbdc12fecde42ff720e6ea14292bc7f4f67d7dcc
[oweals/gnunet.git] / src / fs / test_fs_download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009 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_download.c
23  * @brief simple testcase for download
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31
32 #define DEBUG_VERBOSE 42 
33 //GNUNET_NO
34
35 #define START_ARM GNUNET_YES
36
37 #define FILESIZE (1024 * 1024 * 2)
38
39 /**
40  * How long until we give up on transmitting the message?
41  */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
43
44 /**
45  * How long should our test-content live?
46  */ 
47 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
48
49 struct PeerContext
50 {
51   struct GNUNET_CONFIGURATION_Handle *cfg;
52   struct GNUNET_PeerIdentity id;   
53 #if START_ARM
54   pid_t arm_pid;
55 #endif
56 };
57
58 static struct PeerContext p1;
59
60
61 static struct GNUNET_SCHEDULER_Handle *sched;
62
63 static struct GNUNET_FS_Handle *fs;
64
65 static struct GNUNET_FS_DownloadContext *download;
66
67 static struct GNUNET_FS_PublishContext *publish;
68
69 static char *fn;
70
71
72 static void *
73 progress_cb (void *cls, 
74              const struct GNUNET_FS_ProgressInfo *event)
75 {
76
77   switch (event->status)
78     {
79     case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
80       GNUNET_break (0);
81       break;
82     case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
83       GNUNET_break (0);
84       break;
85     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
86 #if DEBUG_VERBOSE > 1
87       printf ("Publish is progressing (%llu/%llu)...\n",
88               (unsigned long long) event->value.publish.completed,
89               (unsigned long long) event->value.publish.size);
90 #endif
91       break;
92     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
93 #if DEBUG_VERBOSE
94       printf ("Publish complete.\n");
95 #endif
96       GNUNET_FS_publish_stop (publish);
97       publish = NULL;
98       fn = GNUNET_DISK_mktemp ("gnunet-download-test-dstXXXXXX");
99       download = GNUNET_FS_download_start (fs,
100                                            event->value.publish.specifics.completed.chk_uri,
101                                            NULL,
102                                            fn,
103                                            0,
104                                            FILESIZE,
105                                            1,
106                                            GNUNET_FS_DOWNLOAD_OPTION_NONE,
107                                            NULL);
108       GNUNET_assert (download != NULL);
109       break;
110     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
111 #if DEBUG_VERBOSE
112       printf ("Download complete.\n");
113 #endif
114       GNUNET_FS_download_stop (download, GNUNET_YES);
115       download = NULL;
116       break;
117     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
118 #if DEBUG_VERBOSE > 1
119       printf ("Download is progressing (%llu/%llu)...\n",
120               (unsigned long long) event->value.download.completed,
121               (unsigned long long) event->value.download.size);
122 #endif
123       break;
124     case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
125       GNUNET_break (0);
126       break;
127     case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
128       GNUNET_break (0);
129       break;
130     case GNUNET_FS_STATUS_UNINDEX_ERROR:
131       GNUNET_break (0);
132     case GNUNET_FS_STATUS_PUBLISH_ERROR:
133       GNUNET_break (0);
134     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
135       GNUNET_break (0);
136       break;
137     case GNUNET_FS_STATUS_UNINDEX_SUSPEND:
138       GNUNET_break (0);
139       break;
140     case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
141       GNUNET_break (0);
142       break;
143     case GNUNET_FS_STATUS_PUBLISH_START:
144       /* FIXME: add checks here... */
145     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
146       /* FIXME: add checks here... */
147     case GNUNET_FS_STATUS_DOWNLOAD_START:
148       /* FIXME: add checks here... */
149       break;
150     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
151       /* FIXME: add checks here... */
152       GNUNET_FS_stop (fs);
153       fs = NULL;
154       break;
155     case GNUNET_FS_STATUS_UNINDEX_START:
156       GNUNET_break (0);
157       break;
158     case GNUNET_FS_STATUS_UNINDEX_STOPPED:
159       GNUNET_break (0);
160       break;
161     default:
162       printf ("Unexpected event: %d\n", 
163               event->status);
164       break;
165     }
166   return NULL;
167 }
168
169
170 static void
171 setup_peer (struct PeerContext *p, const char *cfgname)
172 {
173   p->cfg = GNUNET_CONFIGURATION_create ();
174 #if START_ARM
175   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
176                                         "gnunet-service-arm",
177 #if VERBOSE
178                                         "-L", "DEBUG",
179 #endif
180                                         "-c", cfgname, NULL);
181   sleep (1);                    /* allow ARM to start */
182 #endif
183   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
184   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
185 }
186
187
188 static void
189 stop_arm (struct PeerContext *p)
190 {
191 #if START_ARM
192   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
193     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
194   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
195     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "ARM process %u stopped\n", p->arm_pid);
198 #endif
199   GNUNET_CONFIGURATION_destroy (p->cfg);
200 }
201
202
203 static void
204 run (void *cls,
205      struct GNUNET_SCHEDULER_Handle *s,
206      char *const *args,
207      const char *cfgfile,
208      const struct GNUNET_CONFIGURATION_Handle *cfg)
209 {
210   const char *keywords[] = {
211     "down_foo",
212     "down_bar",
213   };
214   char *buf;
215   struct GNUNET_CONTAINER_MetaData *meta;
216   struct GNUNET_FS_Uri *kuri;
217   struct GNUNET_FS_FileInformation *fi;
218   size_t i;
219
220   sched = s;
221   setup_peer (&p1, "test_fs_download_data.conf");
222   fs = GNUNET_FS_start (sched,
223                         cfg,
224                         "test-fs-download",
225                         &progress_cb,
226                         NULL,
227                         GNUNET_FS_FLAGS_NONE,
228                         GNUNET_FS_OPTIONS_END);
229   GNUNET_assert (NULL != fs); 
230   buf = GNUNET_malloc (FILESIZE);
231   for (i = 0; i < FILESIZE; i++)
232     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
233   meta = GNUNET_CONTAINER_meta_data_create ();
234   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
235   fi = GNUNET_FS_file_information_create_from_data ("file-to-publish",
236                                                     FILESIZE,
237                                                     buf,
238                                                     kuri,
239                                                     meta,
240                                                     GNUNET_NO,
241                                                     1,
242                                                     42,
243                                                     GNUNET_TIME_relative_to_absolute (LIFETIME)); 
244   GNUNET_FS_uri_destroy (kuri);
245   GNUNET_CONTAINER_meta_data_destroy (meta);
246   GNUNET_assert (NULL != fi);
247   publish = GNUNET_FS_publish_start (fs,
248                                     "publish-context",
249                                     fi,
250                                     NULL, NULL, NULL,
251                                     GNUNET_FS_PUBLISH_OPTION_NONE);
252   GNUNET_assert (publish != NULL);
253 }
254
255
256 int
257 main (int argc, char *argv[])
258 {
259   char *const argvx[] = { 
260     "test-fs-download",
261     "-c",
262     "test_fs_download_data.conf",
263 #if VERBOSE
264     "-L", "DEBUG",
265 #endif
266     NULL
267   };
268   struct GNUNET_GETOPT_CommandLineOption options[] = {
269     GNUNET_GETOPT_OPTION_END
270   };
271
272   GNUNET_log_setup ("test_fs_download", 
273 #if VERBOSE
274                     "DEBUG",
275 #else
276                     "WARNING",
277 #endif
278                     NULL);
279   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
280                       argvx, "test-fs-download",
281                       "nohelp", options, &run, NULL);
282   stop_arm (&p1);
283   return 0;
284 }
285
286 /* end of test_fs_download.c */