77f0c3a7ba4e97aaddafa3464cc706e7d54d518e
[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   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n", GNUNET_i2s_full(&address->peer));
108
109   if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
110   {
111       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
112       ret = 1;
113   }
114   else if (0 != strcmp (address->transport_name, test_addr.plugin))
115   {
116       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
117       ret = 1;
118   }
119   else if (address->address_length != test_addr.addr_len)
120   {
121       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
122       ret = 1;
123   }
124   else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
125   {
126       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
127       ret = 1;
128   }
129   else
130   {
131     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
132                 GNUNET_i2s (&address->peer));
133     ret = 0;
134   }
135   GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
136   GNUNET_SCHEDULER_add_now (&end, NULL);
137 }
138
139 static void
140 run (void *cls, 
141      const struct GNUNET_CONFIGURATION_Handle *cfg,
142      struct GNUNET_TESTING_Peer *peer)
143 {
144   struct GNUNET_HELLO_Address hello_address;
145   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
146
147   /* Connect to ATS scheduling */
148   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
149   if (sched_ats == NULL)
150   {
151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
152     ret = 1;
153     end ();
154     return;
155   }
156
157   /* Set up peer */
158   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
159   {
160       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
161       ret = GNUNET_SYSERR;
162       end ();
163       return;
164   }
165
166   GNUNET_assert (0 == strcmp (PEERID, GNUNET_i2s_full (&p.id)));
167
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
169               GNUNET_i2s_full(&p.id));
170
171   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
172   /* Adding address without session */
173   hello_address.peer = p.id;
174   hello_address.transport_name = test_addr.plugin;
175   hello_address.address = test_addr.addr;
176   hello_address.address_length = test_addr.addr_len;
177   GNUNET_ATS_address_add (sched_ats, &hello_address, test_addr.session, NULL, 0);
178
179   GNUNET_ATS_suggest_address (sched_ats, &p.id);
180 }
181
182
183 int
184 main (int argc, char *argv[])
185 {
186   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
187                                     "test_ats_api.conf",
188                                     &run, NULL))
189     return 1;
190   return ret;
191 }
192
193 /* end of file test_ats_api_scheduling_add_address.c */