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