-fix dht flags
[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 struct GNUNET_TESTBED_Peer *daemons[NUM_DAEMONS];
46
47 static int ok;
48
49 static struct GNUNET_TIME_Absolute start_time;
50
51
52 static void
53 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 {
55   struct GNUNET_TIME_Relative del;
56   char *fancy;
57
58   GNUNET_SCHEDULER_shutdown ();
59   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
60   {
61     del = GNUNET_TIME_absolute_get_duration (start_time);
62     if (del.rel_value == 0)
63       del.rel_value = 1;
64     fancy =
65         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
66                                         1000LL / del.rel_value);
67     FPRINTF (stdout, "Download speed was %s/s\n", fancy);
68     GNUNET_free (fancy);
69     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
70                 (unsigned long long) FILESIZE);
71   }
72   else
73   {
74     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75                 "Timeout during download, shutting down with error\n");
76     ok = 1;
77   }
78 }
79
80
81 static void
82 do_download (void *cls, const struct GNUNET_FS_Uri *uri)
83 {
84   if (NULL == uri)
85   {
86     GNUNET_SCHEDULER_shutdown ();
87     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88                 "Timeout during upload attempt, shutting down with error\n");
89     ok = 1;
90     return;
91   }
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
93               (unsigned long long) FILESIZE);
94   start_time = GNUNET_TIME_absolute_get ();
95   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
96                            NULL);
97 }
98
99
100 static void
101 do_publish (void *cls,
102             struct GNUNET_TESTBED_Operation *op,
103             const char *emsg)
104 {
105   if (NULL != emsg)
106   {
107     GNUNET_SCHEDULER_shutdown ();
108     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
109                 "Timeout during connect attempt, shutting down with error: %s\n",
110                 emsg);
111     ok = 1;
112     return;
113   }
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
115               (unsigned long long) FILESIZE);
116   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
117                           VERBOSE, &do_download, NULL);
118 }
119
120
121 static void
122 do_connect (void *cls,
123             unsigned int num_peers,
124             struct GNUNET_TESTBED_Peer **peers)
125 {
126   unsigned int i;
127  
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,
132               "Daemons started, will now try to connect them\n");
133   GNUNET_TESTBED_overlay_connect (NULL,
134                                   &do_publish, NULL,
135                                   daemons[0], daemons[1]);
136 }
137
138
139 int
140 main (int argc, char *argv[])
141 {
142   GNUNET_TESTBED_test_run ("test-gnunet-service-fs-p2p",
143                            "fs_test_lib_data.conf",
144                            NUM_DAEMONS,
145                            0, NULL, NULL,
146                            &do_connect, NULL);
147   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
148   return ok;
149 }
150
151 /* end of test_gnunet_service_fs_p2p.c */