- fix
[oweals/gnunet.git] / src / ats-test / 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, "ATS Result callback\n");
42 }
43
44 struct TransportConfiguration
45 {
46   int peers;
47   int mechanisms;
48
49   struct ATS_peer *p_head;
50   struct ATS_peer *p_tail;
51
52   struct ATS_mechanism *m_head;
53   struct ATS_mechanism *m_tail;
54 };
55
56
57 static void
58 create_ats_information (struct ATS_peer **p, int *c_p, struct ATS_mechanism **m,
59                         int *c_m)
60 {
61   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS needs addresses\n");
62
63   (*p) = tc->p_head;
64   (*c_p) = tc->mechanisms;
65   (*m) = tc->m_head;
66   (*c_m) = tc->mechanisms;
67 }
68
69
70 static int
71 run_ats ()
72 {
73   int ret = 0;
74
75   ats_calculate_bandwidth_distribution (ats);
76   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running ATS: %s \n",
77               (ret == 0) ? "SUCCESSFUL" : "FAILED");
78   return ret;
79 }
80
81
82 static int
83 init_ats ()
84 {
85   int ret = 0;
86
87   ats =
88       ats_init (1.0, 1.0, 1.0, 50000, 5, 10, ATS_MAX_EXEC_DURATION,
89                 create_ats_information, ats_result_cb);
90   //GNUNET_assert (ats != NULL);
91
92   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Initializing ATS: %s \n",
93               (ret == 0) ? "SUCCESSFUL" : "FAILED");
94   return ret;
95 }
96
97
98 static int
99 shutdown_ats ()
100 {
101   int ret = 0;
102
103   ats_delete_problem (ats);
104   ats_shutdown (ats);
105   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Shutdown ATS: %s \n",
106               (ret == 0) ? "SUCCESSFUL" : "FAILED");
107   return ret;
108 }
109
110
111 /* To make compiler happy */
112 void
113 dummy ()
114 {
115   struct ATS_quality_metric *q = qm;
116
117   q = NULL;
118   struct ATS_ressource *r = ressources;
119
120   r = NULL;
121   q++;
122   r++;
123 }
124
125
126 static void
127 iterate_peer_values (void *cls, const char *section, const char *option,
128                      const char *value)
129 {
130   if (strcmp (option, "f") == 0)
131     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "\t %s %s\n", option, value);
132 }
133
134 static void
135 iterate_mech_values (void *cls, const char *section, const char *option,
136                      const char *value)
137 {
138   if (strcmp (option, "f") == 0)
139     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "\t %s %s\n", option, value);
140 }
141
142 static void
143 iterate_sections (void *cls, const char *section)
144 {
145   struct TransportConfiguration *tc = cls;
146
147   /* Peer definition */
148   if (99 == strlen (section))
149   {
150     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer '%s`\n", section);
151     GNUNET_HashCode h;
152     int res = GNUNET_CRYPTO_hash_from_string (section, &h);
153
154     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "HASH '%s` %i\n", GNUNET_h2s (&h), res);
155     GNUNET_CONFIGURATION_iterate_section_values (cfg, section,
156                                                  iterate_peer_values, NULL);
157     tc->peers++;
158   }
159   if (10 == strlen (section))
160   {
161     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Mechanism '%s`\n", section);
162     GNUNET_CONFIGURATION_iterate_section_values (cfg, section,
163                                                  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 =
173       GNUNET_malloc (sizeof (struct TransportConfiguration));
174
175   cfg = GNUNET_CONFIGURATION_create ();
176   GNUNET_assert (GNUNET_OK == 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*/