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