-remove trailing whitespace
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_p2p.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_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_NO
30
31 /**
32  * File-size we use for testing.
33  */
34 #define FILESIZE (1024 * 1024 * 1)
35
36 /**
37  * How long until we give up on the download?
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 const char *progname;
46
47 static unsigned int anonymity_level;
48
49 static struct GNUNET_TESTBED_Peer *daemons[NUM_DAEMONS];
50
51 static int ok;
52
53 static struct GNUNET_TIME_Absolute start_time;
54
55
56 static void
57 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
58 {
59   char *fn = cls;
60   struct GNUNET_TIME_Relative del;
61   char *fancy;
62
63   GNUNET_SCHEDULER_shutdown ();
64   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
65   {
66     del = GNUNET_TIME_absolute_get_duration (start_time);
67     if (del.rel_value_us == 0)
68       del.rel_value_us = 1;
69     fancy =
70       GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
71                                       1000000LL / del.rel_value_us);
72     FPRINTF (stdout, "Download speed was %s/s\n", fancy);
73     GNUNET_free (fancy);
74     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
75                 (unsigned long long) FILESIZE);
76   }
77   else
78   {
79     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80                 "Timeout during download, shutting down with error\n");
81     ok = 1;
82   }
83   if (NULL != fn)
84   {
85     GNUNET_DISK_directory_remove (fn);
86     GNUNET_free (fn);
87   }
88 }
89
90
91 static void
92 do_download (void *cls, const struct GNUNET_FS_Uri *uri,
93              const char *fn)
94 {
95   if (NULL == uri)
96   {
97     GNUNET_SCHEDULER_shutdown ();
98     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99                 "Timeout during upload attempt, shutting down with error\n");
100     ok = 1;
101     return;
102   }
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
104               (unsigned long long) FILESIZE);
105   start_time = GNUNET_TIME_absolute_get ();
106   GNUNET_FS_TEST_download (daemons[0], TIMEOUT,
107                            anonymity_level, SEED, uri,
108                            VERBOSE, &do_stop,
109                            (NULL == fn)
110                            ? NULL
111                            : GNUNET_strdup (fn));
112 }
113
114
115 static void
116 do_publish (void *cls,
117             struct GNUNET_TESTBED_RunHandle *h,
118             unsigned int num_peers,
119             struct GNUNET_TESTBED_Peer **peers,
120             unsigned int links_succeeded,
121             unsigned int links_failed)
122 {
123   unsigned int i;
124
125   if (NULL != strstr (progname, "mesh"))
126     anonymity_level = 0;
127   else
128     anonymity_level = 1;
129   GNUNET_assert (NUM_DAEMONS == num_peers);
130   for (i=0;i<num_peers;i++)
131     daemons[i] = peers[i];
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
133               (unsigned long long) FILESIZE);
134   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT,
135                           anonymity_level, GNUNET_NO,
136                           FILESIZE, SEED,
137                           VERBOSE, &do_download, NULL);
138 }
139
140
141 int
142 main (int argc, char *argv[])
143 {
144   const char *config;
145
146   progname = argv[0];
147   if (NULL != strstr (progname, "mesh"))
148     config = "test_gnunet_service_fs_p2p_mesh.conf";
149   else
150     config = "fs_test_lib_data.conf";
151   (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-p2p",
152                                   config,
153                                   NUM_DAEMONS,
154                                   0, NULL, NULL,
155                                   &do_publish, NULL);
156   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
157   return ok;
158 }
159
160 /* end of test_gnunet_service_fs_p2p.c */