fix testname
[oweals/gnunet.git] / src / ats / test_ats_normalization_update_quality.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_normalization_update_quality.c
22  * @brief test updating an address
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "ats.h"
30 #include "test_ats_api_common.h"
31
32 static GNUNET_SCHEDULER_TaskIdentifier die_task;
33
34 /**
35  * Scheduling handle
36  */
37 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
38
39 /**
40  * Return value
41  */
42 static int ret;
43
44 /**
45  * Test address
46  */
47 static struct Test_Address test_addr;
48
49 /**
50  * Test peer
51  */
52 static struct PeerContext p[2];
53
54 /**
55  * HELLO test address
56  */
57
58 struct GNUNET_HELLO_Address test_hello_address[3];
59
60 /**
61  * Test ats info
62  */
63 struct GNUNET_ATS_Information test_ats_info[3];
64
65 /**
66  * Test ats count
67  */
68 uint32_t test_ats_count;
69
70
71 static void
72 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {
74   die_task = GNUNET_SCHEDULER_NO_TASK;
75
76   if (sched_ats != NULL)
77     GNUNET_ATS_scheduling_done (sched_ats);
78   free_test_address (&test_addr);
79   ret = 0;
80 }
81
82
83 static void
84 end ()
85 {
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
87   if (die_task != GNUNET_SCHEDULER_NO_TASK)
88   {
89     GNUNET_SCHEDULER_cancel (die_task);
90     die_task = GNUNET_SCHEDULER_NO_TASK;
91   }
92   GNUNET_ATS_scheduling_done (sched_ats);
93   sched_ats = NULL;
94   free_test_address (&test_addr);
95 }
96
97
98 static void
99 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
100                     struct Session *session,
101                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
102                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
103                     const struct GNUNET_ATS_Information *atsi,
104                     uint32_t ats_count)
105 {
106   static int stage = 0;
107   if (0 == stage)
108   {
109     GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
110
111     /* Update address */
112     /* Prepare ATS Information */
113
114     test_ats_info[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
115     test_ats_info[0].value = htonl(20);
116     test_ats_count = 1;
117
118     GNUNET_ATS_address_update (sched_ats, &test_hello_address[0], NULL, test_ats_info, test_ats_count);
119
120     test_ats_info[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
121     test_ats_info[0].value = htonl(20);
122     test_ats_count = 1;
123
124     GNUNET_ATS_address_update (sched_ats, &test_hello_address[0], NULL, test_ats_info, test_ats_count);
125
126
127     /* Request address */
128     stage ++;
129   }
130 }
131
132 static void
133 run (void *cls,
134      const struct GNUNET_CONFIGURATION_Handle *cfg,
135      struct GNUNET_TESTING_Peer *peer)
136 {
137   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
138
139   /* Connect to ATS scheduling */
140   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
141   if (sched_ats == NULL)
142   {
143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
144     ret = 1;
145     end ();
146     return;
147   }
148
149   /* Set up peer */
150   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
151   {
152       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
153       ret = GNUNET_SYSERR;
154       end ();
155       return;
156   }
157
158   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
159
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
161               GNUNET_i2s_full(&p[0].id));
162
163   /* Set up peer */
164   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
165   {
166       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
167       ret = GNUNET_SYSERR;
168       end ();
169       return;
170   }
171
172   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
173
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
175               GNUNET_i2s_full(&p[1].id));
176
177
178
179   /* Adding address for peer 0 */
180   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
181   /* Prepare ATS Information */
182   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
183   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
184   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
185   test_ats_info[1].value = htonl(30);
186   test_ats_count = 2;
187
188   test_hello_address[0].peer = p[0].id;
189   test_hello_address[0].transport_name = test_addr.plugin;
190   test_hello_address[0].address = test_addr.addr;
191   test_hello_address[0].address_length = test_addr.addr_len;
192   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], NULL, test_ats_info, test_ats_count);
193
194   /* Adding address for peer 1 */
195   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
196   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
197   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
198   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
199   test_ats_info[1].value = htonl(10);
200   test_ats_count = 2;
201
202   test_hello_address[1].peer = p[1].id;
203   test_hello_address[1].transport_name = test_addr.plugin;
204   test_hello_address[1].address = test_addr.addr;
205   test_hello_address[1].address_length = test_addr.addr_len;
206   GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], NULL, test_ats_info, test_ats_count);
207
208   /* Adding 2nd address for peer 1 */
209   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
210   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
211   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
212   test_ats_info[1].value = htonl(20);
213   test_ats_count = 2;
214
215   test_hello_address[2].peer = p[1].id;
216   test_hello_address[2].transport_name = test_addr.plugin;
217   test_hello_address[2].address = test_addr.addr;
218   test_hello_address[2].address_length = test_addr.addr_len;
219   GNUNET_ATS_address_add (sched_ats, &test_hello_address[3], NULL, test_ats_info, test_ats_count);
220
221   /* Request address */
222   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
223 }
224
225
226 int
227 main (int argc, char *argv[])
228 {
229   if (0 != GNUNET_TESTING_peer_run ("test_ats_normalization_update_quality",
230                                     "test_ats_api.conf",
231                                     &run, NULL))
232     return 1;
233   return ret;
234 }
235
236 /* end of file test_ats_normalization_update_quality.c*/