changes
[oweals/gnunet.git] / src / ats / test_ats_api_common.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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 ats/test_ats_api_common.c
22  * @brief shared functions for ats test
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26
27 #include "test_ats_api_common.h"
28
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
30 #define BIG_M_STRING "unlimited"
31
32
33 #define PEERID0 "2AK99KD8RM9UA9LC3QKA0IQ5UBFC0FBB50EBGCFQT8448DGGACNAC4CJQDD1CPFS494O41U88DJD1FLIG8VA5CQR9IN4L96GP104MVO"
34 #define PEERID1 "5ED7I0AR3MSTAL7FQN04S22E0EQ3CR9RLASCDLVMM1BNFPUPTCT46DLKNJ4DACASJ6U0DR5J8S3R2UJL49682JS7MOVRAB8P8A4PJH0"
35
36 void
37 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
38 {
39
40   dest->plugin = GNUNET_strdup (plugin);
41   dest->session = session;
42   if (addrlen > 0)
43   {
44     dest->addr = GNUNET_malloc (addrlen);
45     memcpy (dest->addr, addr, addrlen);
46   }
47   else
48       dest->addr = NULL;
49   dest->addr_len = addrlen;
50 }
51
52 void
53 free_test_address (struct Test_Address *dest)
54 {
55   GNUNET_free (dest->plugin);
56   if (NULL != dest->addr)
57     GNUNET_free (dest->addr);
58 }
59
60 int
61 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
62                    const struct GNUNET_HELLO_Address *address2, void *session2)
63 {
64   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
65   {
66       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
67       return GNUNET_SYSERR;
68   }
69   if (0 != strcmp (address1->transport_name, address2->transport_name))
70   {
71       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
72       return GNUNET_SYSERR;
73   }
74   if (address1->address_length != address2->address_length)
75   {
76       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
77       return GNUNET_SYSERR;
78
79   }
80   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
81   {
82       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
83       return GNUNET_SYSERR;
84   }
85   if (session1 != session2)
86   {
87       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
88                   session1, session2);
89       return GNUNET_SYSERR;
90
91   }
92   return GNUNET_OK;
93 }
94
95
96 int
97 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
98              const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
99 {
100   unsigned int c_o;
101   unsigned int c_i;
102   char *prop[] = GNUNET_ATS_PropertyStrings;
103   uint32_t type1;
104   uint32_t type2;
105   uint32_t val1;
106   uint32_t val2;
107   int res = GNUNET_OK;
108
109   for (c_o = 0; c_o < ats_count_is; c_o++)
110   {
111     for (c_i = 0; c_i < ats_count_should; c_i++)
112     {
113         type1 = ntohl(ats_is[c_o].type);
114         type2 = ntohl(ats_should[c_i].type);
115         if (type1 == type2)
116         {
117             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
118                         prop[type1]);
119             val1 = ntohl(ats_is[c_o].value);
120             val2 = ntohl(ats_should[c_i].value);
121             if (val1 != val2)
122             {
123                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
124                             prop[type1],
125                             val1, val2);
126                 res = GNUNET_SYSERR;
127             }
128             else
129             {
130               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
131                           prop[type1],
132                           val1, val2);
133             }
134         }
135     }
136   }
137   return res;
138 }
139
140
141 /**
142  * Load quotas for networks from configuration
143  *
144  * @param cfg configuration handle
145  * @param out_dest where to write outbound quotas
146  * @param in_dest where to write inbound quotas
147  * @param dest_length length of inbound and outbound arrays
148  * @return number of networks loaded
149  */
150 unsigned int
151 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
152                                                  unsigned long long *out_dest,
153                                                  unsigned long long *in_dest,
154                                                  int dest_length)
155 {
156   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
157   char * entry_in = NULL;
158   char * entry_out = NULL;
159   char * quota_out_str;
160   char * quota_in_str;
161   int c;
162   int res;
163
164   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
165   {
166     in_dest[c] = 0;
167     out_dest[c] = 0;
168     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
169     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
170
171     /* quota out */
172     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
173     {
174       res = GNUNET_NO;
175       if (0 == strcmp(quota_out_str, BIG_M_STRING))
176       {
177         out_dest[c] = GNUNET_ATS_MaxBandwidth;
178         res = GNUNET_YES;
179       }
180       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
181         res = GNUNET_YES;
182       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
183          res = GNUNET_YES;
184
185       if (GNUNET_NO == res)
186       {
187           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
188               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
189           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
190       }
191       else
192       {
193           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
194               network_str[c], out_dest[c]);
195       }
196       GNUNET_free (quota_out_str);
197     }
198     else
199     {
200       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
201           network_str[c], GNUNET_ATS_DefaultBandwidth);
202       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
203     }
204
205     /* quota in */
206     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
207     {
208       res = GNUNET_NO;
209       if (0 == strcmp(quota_in_str, BIG_M_STRING))
210       {
211         in_dest[c] = GNUNET_ATS_MaxBandwidth;
212         res = GNUNET_YES;
213       }
214       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
215         res = GNUNET_YES;
216       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
217          res = GNUNET_YES;
218
219       if (GNUNET_NO == res)
220       {
221           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
222               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
223           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
224       }
225       else
226       {
227           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
228               network_str[c], in_dest[c]);
229       }
230       GNUNET_free (quota_in_str);
231     }
232     else
233     {
234       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
235           network_str[c], GNUNET_ATS_DefaultBandwidth);
236       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
237     }
238     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
239     GNUNET_free (entry_out);
240     GNUNET_free (entry_in);
241   }
242   return GNUNET_ATS_NetworkTypeCount;
243 }
244
245 /**
246  * Create a ATS_address with the given information
247  * @param peer peer
248  * @param plugin_name plugin
249  * @param plugin_addr address
250  * @param plugin_addr_len address length
251  * @param session_id session
252  * @return the ATS_Address
253  */
254 struct ATS_Address *
255 create_address (const struct GNUNET_PeerIdentity *peer,
256                 const char *plugin_name,
257                 const void *plugin_addr, size_t plugin_addr_len,
258                 uint32_t session_id)
259 {
260   struct ATS_Address *aa = NULL;
261
262   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
263   aa->peer = *peer;
264   aa->addr_len = plugin_addr_len;
265   aa->addr = &aa[1];
266   aa->plugin = (char *) &aa[1] + plugin_addr_len;
267   memcpy (&aa[1], plugin_addr, plugin_addr_len);
268   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
269   aa->session_id = session_id;
270   aa->active = GNUNET_NO;
271   aa->used = GNUNET_NO;
272   aa->solver_information = NULL;
273   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
274   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
275   return aa;
276 }
277
278 /* end of file test_ats_api_common.c */