glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_p2p.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file fs/test_gnunet_service_fs_p2p.c
18  * @brief test P2P routing using simple publish + download operation
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "fs_test_lib.h"
23
24 #define VERBOSE GNUNET_NO
25
26 /**
27  * File-size we use for testing.
28  */
29 #define FILESIZE (1024 * 1024 * 1)
30
31 /**
32  * How long until we give up on the download?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36 #define NUM_DAEMONS 2
37
38 #define SEED 42
39
40 static const char *progname;
41
42 static unsigned int anonymity_level;
43
44 static struct GNUNET_TESTBED_Peer *daemons[NUM_DAEMONS];
45
46 static int ok;
47
48 static struct GNUNET_TIME_Absolute start_time;
49
50
51 static void
52 do_stop (void *cls)
53 {
54   char *fn = cls;
55   struct GNUNET_TIME_Relative del;
56   char *fancy;
57
58   GNUNET_SCHEDULER_shutdown ();
59   if (0 ==
60       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
61                                                                     TIMEOUT)).rel_value_us)
62   {
63     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
64                 "Timeout during download, shutting down with error\n");
65     ok = 1;
66   }
67   else
68   {
69     del = GNUNET_TIME_absolute_get_duration (start_time);
70     if (0 == del.rel_value_us)
71       del.rel_value_us = 1;
72     fancy =
73       GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
74                                       1000000LL / del.rel_value_us);
75     FPRINTF (stdout,
76              "Download speed was %s/s\n",
77              fancy);
78     GNUNET_free (fancy);
79     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80                 "Finished download, shutting down\n");
81   }
82   if (NULL != fn)
83   {
84     GNUNET_DISK_directory_remove (fn);
85     GNUNET_free (fn);
86   }
87 }
88
89
90 static void
91 do_download (void *cls, const struct GNUNET_FS_Uri *uri,
92              const char *fn)
93 {
94   if (NULL == uri)
95   {
96     GNUNET_SCHEDULER_shutdown ();
97     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98                 "Timeout during upload attempt, shutting down with error\n");
99     ok = 1;
100     return;
101   }
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
103               (unsigned long long) FILESIZE);
104   start_time = GNUNET_TIME_absolute_get ();
105   GNUNET_FS_TEST_download (daemons[0], TIMEOUT,
106                            anonymity_level, SEED, uri,
107                            VERBOSE, &do_stop,
108                            (NULL == fn)
109                            ? NULL
110                            : GNUNET_strdup (fn));
111 }
112
113
114 static void
115 do_publish (void *cls,
116             struct GNUNET_TESTBED_RunHandle *h,
117             unsigned int num_peers,
118             struct GNUNET_TESTBED_Peer **peers,
119             unsigned int links_succeeded,
120             unsigned int links_failed)
121 {
122   unsigned int i;
123
124   if (NULL != strstr (progname, "cadet"))
125     anonymity_level = 0;
126   else
127     anonymity_level = 1;
128   GNUNET_assert (NUM_DAEMONS == num_peers);
129   for (i=0;i<num_peers;i++)
130     daemons[i] = peers[i];
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
132               (unsigned long long) FILESIZE);
133   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT,
134                           anonymity_level, GNUNET_NO,
135                           FILESIZE, SEED,
136                           VERBOSE, &do_download, NULL);
137 }
138
139
140 int
141 main (int argc, char *argv[])
142 {
143   const char *config;
144
145   progname = argv[0];
146   if (NULL != strstr (progname, "cadet"))
147     config = "test_gnunet_service_fs_p2p_cadet.conf";
148   else
149     config = "fs_test_lib_data.conf";
150   (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-p2p",
151                                   config,
152                                   NUM_DAEMONS,
153                                   0, NULL, NULL,
154                                   &do_publish, NULL);
155   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
156   return ok;
157 }
158
159 /* end of test_gnunet_service_fs_p2p.c */