changes
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_add_address.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_scheduling_add_address.c
22  * @brief test adding addresses in automatic transport selection scheduling API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib-new.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;
53
54
55 static void
56 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
57 {
58   dest->plugin = GNUNET_strdup (plugin);
59   dest->session = session;
60   dest->addr = GNUNET_malloc (addrlen);
61   memcpy (dest->addr, addr, addrlen);
62   dest->addr_len = addrlen;
63 }
64
65 static void
66 free_test_address (struct Test_Address *dest)
67 {
68   GNUNET_free (dest->plugin);
69   GNUNET_free (dest->addr);
70 }
71
72
73 static void
74 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   die_task = GNUNET_SCHEDULER_NO_TASK;
77
78   if (sched_ats != NULL)
79     GNUNET_ATS_scheduling_done (sched_ats);
80   free_test_address (&test_addr);
81   ret = GNUNET_SYSERR;
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);
97 }
98
99 static void
100 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
101                     struct Session *session,
102                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
103                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
104                     const struct GNUNET_ATS_Information *atsi,
105                     uint32_t ats_count)
106 {
107   if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
108   {
109       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
110       ret = 1;
111   }
112   else if (0 != strcmp (address->transport_name, test_addr.plugin))
113   {
114       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
115       ret = 1;
116   }
117   else if (address->address_length != test_addr.addr_len)
118   {
119       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
120       ret = 1;
121   }
122   else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
123   {
124       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
125       ret = 1;
126   }
127   else
128   {
129     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
130                 GNUNET_i2s (&address->peer));
131     ret = 0;
132   }
133   GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
134   GNUNET_SCHEDULER_add_now (&end, NULL);
135 }
136
137 static void
138 run (void *cls, 
139      const struct GNUNET_CONFIGURATION_Handle *cfg,
140      struct GNUNET_TESTING_Peer *peer)
141 {
142   struct GNUNET_HELLO_Address hello_address;
143   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
144
145   /* Connect to ATS scheduling */
146   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
147   if (sched_ats == NULL)
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
150     ret = 1;
151     end ();
152     return;
153   }
154
155   /* Set up peer */
156   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
157   {
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
159       ret = GNUNET_SYSERR;
160       end ();
161       return;
162   }
163
164   GNUNET_assert (0 == strcmp (PEERID, GNUNET_i2s_full (&p.id)));
165
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
167               GNUNET_i2s_full(&p.id));
168
169   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
170   /* Adding address without session */
171   hello_address.peer = p.id;
172   hello_address.transport_name = test_addr.plugin;
173   hello_address.address = test_addr.addr;
174   hello_address.address_length = test_addr.addr_len;
175   GNUNET_ATS_address_add (sched_ats, &hello_address, test_addr.session, NULL, 0);
176
177   GNUNET_ATS_suggest_address (sched_ats, &p.id);
178 }
179
180
181 int
182 main (int argc, char *argv[])
183 {
184   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
185                                     "test_ats_api.conf",
186                                     &run, NULL))
187     return 1;
188   return ret;
189 }
190
191 /* end of file test_ats_api_scheduling_add_address.c */