bf7bc0fe5ab43a5cb4986e5ed8c1b6e612bdfe45
[oweals/gnunet.git] / src / transport / test_transport_ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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  * @file testing/test_transport_ats.c
22  * @brief testcase for ats functionality without starting peers
23  */
24 #include "platform.h"
25 #include "gnunet-service-transport_ats.h"
26 #include "gnunet_configuration_lib.h"
27 #include "gnunet_crypto_lib.h"
28
29 #define VERBOSE GNUNET_YES
30
31 static struct ATS_Handle * ats;
32
33 static struct GNUNET_CONFIGURATION_Handle * cfg;
34
35 static struct TransportConfiguration *tc;
36
37
38 static void 
39 ats_result_cb ()
40 {
41   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
42       "ATS Result callback\n");
43 }
44
45 struct TransportConfiguration
46 {
47   int peers;
48   int mechanisms;
49
50   struct ATS_peer * p_head;
51   struct ATS_peer * p_tail;
52
53   struct ATS_mechanism * m_head;
54   struct ATS_mechanism * m_tail;
55 };
56
57
58 static void 
59 create_ats_information (struct ATS_peer **p, int * c_p,
60                         struct ATS_mechanism ** m, int * c_m)
61
62   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
63       "ATS needs addresses\n");
64
65  (*p) = tc->p_head;
66  (*c_p) = tc->mechanisms;
67  (*m) = tc->m_head;
68  (*c_m) = tc->mechanisms;
69 }
70
71
72 static
73 int run_ats ()
74 {
75   int ret = 0;
76   ats_calculate_bandwidth_distribution (ats);
77   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
78               "Running ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
79   return ret;
80 }
81
82
83 static int
84 init_ats ()
85 {
86   int ret = 0;
87   ats = ats_init(1.0, 1.0, 1.0, 50000, 5, 10, ATS_MAX_EXEC_DURATION,
88                 create_ats_information,
89                 ats_result_cb);
90   //GNUNET_assert (ats != NULL);
91
92   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
93               "Initializing ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
94   return ret;
95 }
96
97
98 static int 
99 shutdown_ats ()
100 {
101   int ret = 0;
102   ats_delete_problem (ats);
103   ats_shutdown (ats);
104   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105               "Shutdown ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
106   return ret;
107 }
108
109
110 /* To make compiler happy */
111 void 
112 dummy()
113 {
114   struct ATS_quality_metric * q = qm;
115   q = NULL;
116   struct ATS_ressource * r = ressources;
117   r = NULL;
118   q++; 
119   r++;
120 }
121
122
123 static void 
124 iterate_peer_values (void *cls,
125                      const char *section,
126                      const char *option,
127                      const char *value)
128 {
129   if (strcmp (option, "f") == 0)
130     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131                 "\t %s %s\n", option, value);
132 }
133
134 static void
135 iterate_mech_values (void *cls,
136                      const char *section,
137                      const char *option,
138                      const char *value)
139 {
140   if (strcmp (option, "f") == 0)
141     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
142                 "\t %s %s\n", option, value);
143 }
144
145 static void 
146 iterate_sections (void *cls,
147                   const char *section)
148 {
149   struct TransportConfiguration * tc = cls;
150   /* Peer definition */
151   if (99 == strlen(section))
152     {
153       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer '%s`\n", section);
154       GNUNET_HashCode h;
155       int res =GNUNET_CRYPTO_hash_from_string(section, &h);
156       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "HASH '%s` %i\n", GNUNET_h2s (&h), res);
157       GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_peer_values, NULL);
158       tc->peers++;
159     }
160   if (10 == strlen(section))
161     {
162       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Mechanism '%s`\n",section);
163       GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_mech_values, NULL);
164       tc->peers++;
165     }
166 }
167
168
169 static struct TransportConfiguration * 
170 load_transport_configuration (char * filename)
171 {
172   struct TransportConfiguration * ret = GNUNET_malloc(sizeof (struct TransportConfiguration));
173
174   cfg = GNUNET_CONFIGURATION_create();
175   GNUNET_assert (GNUNET_OK ==
176                  GNUNET_CONFIGURATION_load(cfg, filename));
177   GNUNET_CONFIGURATION_iterate_sections (cfg, iterate_sections, ret);
178   GNUNET_CONFIGURATION_destroy (cfg);
179   cfg = NULL;
180   return ret;
181 }
182
183
184 int
185 main (int argc, char *argv[])
186 {
187   int ret = 0;
188
189   GNUNET_log_setup ("test-transport-ats",
190 #if VERBOSE
191                     "DEBUG",
192 #else
193                     "INFO",
194 #endif
195                     NULL);
196   tc = load_transport_configuration ("test_transport_ats.data");
197   ats = NULL;
198   ret += init_ats ();
199   ret += run_ats ();
200   ret += shutdown_ats ();
201   return ret;
202
203 }
204
205 /* end of test_transport_ats.c*/