458d4606b16237c9be8e65c1bfe8098ea12e753b
[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 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_FS_TestDaemon *daemons[NUM_DAEMONS];
46
47 static int ret;
48
49 static void
50 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
53   {
54     GNUNET_break (0);
55     ret = 1;
56   }
57   else
58   {
59     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
60                 (unsigned long long) FILESIZE);
61   }
62   GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
63 }
64
65
66 static void
67 do_download (void *cls, const struct GNUNET_FS_Uri *uri)
68 {
69   if (NULL == uri)
70   {
71     GNUNET_break (0);
72     GNUNET_SCHEDULER_add_now (&do_stop, NULL);
73     ret = 1;
74     return;
75   }
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
77               (unsigned long long) FILESIZE);
78   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
79                            NULL);
80 }
81
82
83 static void
84 do_publish (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
87   {
88     GNUNET_break (0);
89     ret = 1;
90     GNUNET_SCHEDULER_add_now (&do_stop, NULL);
91     return;
92   }
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
94               (unsigned long long) FILESIZE);
95   GNUNET_FS_TEST_publish (daemons[0], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
96                           VERBOSE, &do_download, NULL);
97 }
98
99
100 static void
101 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
104   {
105     GNUNET_break (0);
106     ret = 1;
107     GNUNET_SCHEDULER_add_now (&do_stop, NULL);
108     return;
109   }
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111               "Daemons started, will now try to connect them\n");
112   GNUNET_FS_TEST_daemons_connect (daemons[0], daemons[1], TIMEOUT, &do_publish,
113                                   NULL);
114 }
115
116
117 static void
118 run (void *cls, char *const *args, const char *cfgfile,
119      const struct GNUNET_CONFIGURATION_Handle *cfg)
120 {
121   GNUNET_FS_TEST_daemons_start ("fs_test_lib_data.conf", TIMEOUT, NUM_DAEMONS,
122                                 daemons, &do_connect, NULL);
123 }
124
125
126 int
127 main (int argc, char *argv[])
128 {
129   char *const argvx[] = {
130     "test-fs-test-lib",
131     "-c",
132     "fs_test_lib_data.conf",
133 #if VERBOSE
134     "-L", "DEBUG",
135 #endif
136     NULL
137   };
138   struct GNUNET_GETOPT_CommandLineOption options[] = {
139     GNUNET_GETOPT_OPTION_END
140   };
141
142   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
143   GNUNET_log_setup ("test_fs_test_lib",
144 #if VERBOSE
145                     "DEBUG",
146 #else
147                     "WARNING",
148 #endif
149                     NULL);
150   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
151                       "test-fs-test-lib", "nohelp", options, &run, NULL);
152   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
153   return ret;
154 }
155
156 /* end of test_fs_test_lib.c */