added new test: has timing issue, still fails
[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[3];
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[0]);
79   free_test_address (&test_addr[1]);
80   free_test_address (&test_addr[2]);
81   ret = 0;
82 }
83
84
85 static void
86 end ()
87 {
88   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
89   if (die_task != GNUNET_SCHEDULER_NO_TASK)
90   {
91     GNUNET_SCHEDULER_cancel (die_task);
92     die_task = GNUNET_SCHEDULER_NO_TASK;
93   }
94   GNUNET_ATS_scheduling_done (sched_ats);
95   sched_ats = NULL;
96   free_test_address (&test_addr[0]);
97   free_test_address (&test_addr[1]);
98   free_test_address (&test_addr[2]);
99 }
100
101
102 static void
103 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
104                     struct Session *session,
105                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
106                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
107                     const struct GNUNET_ATS_Information *atsi,
108                     uint32_t ats_count)
109 {
110   static int stage = 0;
111   if (0 == stage)
112   {
113     GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
114
115     /* Update address */
116     /* Prepare ATS Information */
117
118     test_ats_info[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
119     test_ats_info[0].value = htonl(20);
120     test_ats_count = 1;
121
122     GNUNET_ATS_address_update (sched_ats, &test_hello_address[0], NULL, test_ats_info, test_ats_count);
123
124     test_ats_info[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
125     test_ats_info[0].value = htonl(20);
126     test_ats_count = 1;
127
128     GNUNET_ATS_address_update (sched_ats, &test_hello_address[0], NULL, test_ats_info, test_ats_count);
129
130
131     /* Request address */
132     stage ++;
133   }
134 }
135
136 static void
137 run (void *cls,
138      const struct GNUNET_CONFIGURATION_Handle *cfg,
139      struct GNUNET_TESTING_Peer *peer)
140 {
141   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
142
143   /* Connect to ATS scheduling */
144   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
145   if (sched_ats == NULL)
146   {
147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
148     ret = 1;
149     end ();
150     return;
151   }
152
153   /* Set up peer */
154   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
155   {
156       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
157       ret = GNUNET_SYSERR;
158       end ();
159       return;
160   }
161
162   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
163
164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
165               GNUNET_i2s_full(&p[0].id));
166
167   /* Set up peer */
168   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
169   {
170       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
171       ret = GNUNET_SYSERR;
172       end ();
173       return;
174   }
175
176   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
177
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
179               GNUNET_i2s_full(&p[1].id));
180
181
182
183   /* Adding address for peer 0 */
184   create_test_address (&test_addr[0], "test", &test_addr, "test_p0_a0", strlen ("test_p0_a0") + 1);
185   /* Prepare ATS Information */
186   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
187   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
188   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
189   test_ats_info[1].value = htonl(30);
190   test_ats_count = 2;
191
192   test_hello_address[0].peer = p[0].id;
193   test_hello_address[0].transport_name = test_addr[0].plugin;
194   test_hello_address[0].address = test_addr[0].addr;
195   test_hello_address[0].address_length = test_addr[0].addr_len;
196   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], NULL, test_ats_info, test_ats_count);
197
198   /* Adding address for peer 1 */
199   create_test_address (&test_addr[1], "test", &test_addr, "test_p1_a0", strlen ("test_p1_a0") + 1);
200   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
201   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
202   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
203   test_ats_info[1].value = htonl(10);
204   test_ats_count = 2;
205
206   test_hello_address[1].peer = p[1].id;
207   test_hello_address[1].transport_name = test_addr[1].plugin;
208   test_hello_address[1].address = test_addr[1].addr;
209   test_hello_address[1].address_length = test_addr[1].addr_len;
210   GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], NULL, test_ats_info, test_ats_count);
211
212   /* Adding 2nd address for peer 1 */
213   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
214   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
215   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
216   test_ats_info[1].value = htonl(20);
217   test_ats_count = 2;
218
219   create_test_address (&test_addr[2], "test", &test_addr, "test_p1_a1", strlen ("test_p1_a1") + 1);
220   test_hello_address[2].peer = p[1].id;
221   test_hello_address[2].transport_name = test_addr[2].plugin;
222   test_hello_address[2].address = test_addr[2].addr;
223   test_hello_address[2].address_length = test_addr[2].addr_len;
224   GNUNET_ATS_address_add (sched_ats, &test_hello_address[2], NULL, test_ats_info, test_ats_count);
225
226   /* Request address */
227   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
228 }
229
230
231 int
232 main (int argc, char *argv[])
233 {
234   if (0 != GNUNET_TESTING_peer_run ("test_ats_normalization_update_quality",
235                                     "test_ats_api.conf",
236                                     &run, NULL))
237     return 1;
238   return ret;
239 }
240
241 /* end of file test_ats_normalization_update_quality.c*/