-LRN: remove unused variables
[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
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
34
35 static struct GNUNET_ATS_SchedulingHandle *ats;
36
37 static int ret;
38
39 struct Address
40 {
41   char *plugin;
42   size_t plugin_len;
43
44   void *addr;
45   size_t addr_len;
46
47   struct GNUNET_ATS_Information *ats;
48   int ats_count;
49
50   void *session;
51 };
52
53 struct PeerContext
54 {
55   struct GNUNET_PeerIdentity id;
56
57   struct Address *addr;
58 };
59
60
61 static struct Address test_addr;
62
63 static struct PeerContext p;
64
65
66 static void
67 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69   die_task = GNUNET_SCHEDULER_NO_TASK;
70   if (ats != NULL)
71     GNUNET_ATS_scheduling_done (ats);
72   ret = GNUNET_SYSERR;
73 }
74
75
76 static void
77 end ()
78 {
79   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
80   if (die_task != GNUNET_SCHEDULER_NO_TASK)
81   {
82     GNUNET_SCHEDULER_cancel (die_task);
83     die_task = GNUNET_SCHEDULER_NO_TASK;
84   }
85   GNUNET_ATS_scheduling_done (ats);
86   ret = 0;
87 }
88
89
90 static void
91 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
92                     struct Session *session,
93                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
94                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
95                     const struct GNUNET_ATS_Information *ats,
96                     uint32_t ats_count)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
99               GNUNET_i2s (&address->peer));
100
101   GNUNET_assert (0 ==
102                  memcmp (&address->peer, &p.id,
103                          sizeof (struct GNUNET_PeerIdentity)));
104   GNUNET_assert (0 == strcmp (address->transport_name, test_addr.plugin));
105   GNUNET_assert (address->address_length == test_addr.addr_len);
106   GNUNET_assert (0 ==
107                  memcmp (address->address, test_addr.plugin,
108                          address->address_length));
109   GNUNET_assert (test_addr.session == session);
110
111   ret = 0;
112
113   GNUNET_SCHEDULER_add_now (&end, NULL);
114 }
115
116
117 static void
118 run (void *cls, 
119      const struct GNUNET_CONFIGURATION_Handle *cfg,
120      struct GNUNET_TESTING_Peer *peer)
121 {
122   struct GNUNET_HELLO_Address address0;
123
124   ret = GNUNET_SYSERR;
125   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
126   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
127
128   if (ats == NULL)
129   {
130     ret = GNUNET_SYSERR;
131     end ();
132     return;
133   }
134
135   /* set up peer */
136   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
137                                     &p.id.hashPubKey);
138   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
139               GNUNET_i2s (&p.id));
140
141   test_addr.plugin = "test";
142   test_addr.session = NULL;
143   test_addr.addr = GNUNET_strdup ("test");
144   test_addr.addr_len = 4;
145
146   /* Adding address without session */
147   address0.peer = p.id;
148   address0.transport_name = test_addr.plugin;
149   address0.address = test_addr.addr;
150   address0.address_length = test_addr.addr_len;
151   GNUNET_ATS_address_add (ats, &address0, test_addr.session, NULL, 0);
152
153   test_addr.session = &test_addr;
154   /* Update address with session */
155   GNUNET_ATS_address_add (ats, &address0, test_addr.session, NULL, 0);
156
157   /* Update address with session */
158   test_addr.session = &address0;
159   GNUNET_assert (GNUNET_OK == GNUNET_ATS_address_add (ats, &address0, test_addr.session, NULL, 0));
160   GNUNET_log_skip (2, GNUNET_NO);
161   GNUNET_assert (GNUNET_SYSERR == GNUNET_ATS_address_add (ats, &address0, test_addr.session, NULL, 0));
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
164               GNUNET_i2s (&p.id));
165   GNUNET_ATS_suggest_address (ats, &p.id);
166 }
167
168
169 int
170 main (int argc, char *argv[])
171 {
172   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
173                                     "test_ats_api.conf",
174                                     &run, NULL))
175     return 1;
176   return ret;
177 }
178
179 /* end of file test_ats_api_scheduling_add_address.c */