diff options
Diffstat (limited to 'src/lib/hello/test_hello.c')
-rw-r--r-- | src/lib/hello/test_hello.c | 253 |
1 files changed, 0 insertions, 253 deletions
diff --git a/src/lib/hello/test_hello.c b/src/lib/hello/test_hello.c deleted file mode 100644 index 8631d2af3..000000000 --- a/src/lib/hello/test_hello.c +++ /dev/null | |||
@@ -1,253 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2009, 2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file hello/test_hello.c | ||
22 | * @brief test for hello.c | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_hello_lib.h" | ||
27 | |||
28 | |||
29 | /** | ||
30 | * | ||
31 | * | ||
32 | * @param cls | ||
33 | * @param max | ||
34 | * @param buf | ||
35 | * @return | ||
36 | */ | ||
37 | static ssize_t | ||
38 | my_addr_gen (void *cls, | ||
39 | size_t max, | ||
40 | void *buf) | ||
41 | { | ||
42 | unsigned int *i = cls; | ||
43 | size_t ret; | ||
44 | struct GNUNET_HELLO_Address address; | ||
45 | |||
46 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
47 | "DEBUG: my_addr_gen called with i = %d\n", | ||
48 | *i); | ||
49 | if (0 == *i) | ||
50 | return GNUNET_SYSERR; | ||
51 | memset (&address.peer, 0, sizeof(struct GNUNET_PeerIdentity)); | ||
52 | address.address = "address_information"; | ||
53 | address.transport_name = "test"; | ||
54 | address.address_length = *i; | ||
55 | ret = GNUNET_HELLO_add_address (&address, | ||
56 | GNUNET_TIME_absolute_get (), | ||
57 | buf, | ||
58 | max); | ||
59 | (*i)--; | ||
60 | return ret; | ||
61 | } | ||
62 | |||
63 | |||
64 | /** | ||
65 | * | ||
66 | * | ||
67 | * @param cls | ||
68 | * @param address | ||
69 | * @param expiration | ||
70 | * @return | ||
71 | */ | ||
72 | static int | ||
73 | check_addr (void *cls, | ||
74 | const struct GNUNET_HELLO_Address *address, | ||
75 | struct GNUNET_TIME_Absolute expiration) | ||
76 | { | ||
77 | unsigned int *i = cls; | ||
78 | |||
79 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
80 | "DEBUG: check_addr called with i = %d and addrlen = %u\n", | ||
81 | *i, | ||
82 | (unsigned int) address->address_length); | ||
83 | GNUNET_assert (address->address_length > 0); | ||
84 | GNUNET_assert (*i & (1 << (address->address_length - 1))); | ||
85 | *i -= (1 << (address->address_length - 1)); | ||
86 | GNUNET_assert (0 == | ||
87 | strncmp ("address_information", | ||
88 | address->address, | ||
89 | address->address_length)); | ||
90 | GNUNET_assert (0 == strcmp ("test", | ||
91 | address->transport_name)); | ||
92 | return GNUNET_OK; | ||
93 | } | ||
94 | |||
95 | |||
96 | /** | ||
97 | * | ||
98 | * | ||
99 | * @param cls | ||
100 | * @param address | ||
101 | * @param expiration | ||
102 | * @return | ||
103 | */ | ||
104 | static int | ||
105 | remove_some (void *cls, | ||
106 | const struct GNUNET_HELLO_Address *address, | ||
107 | struct GNUNET_TIME_Absolute expiration) | ||
108 | { | ||
109 | unsigned int *i = cls; | ||
110 | |||
111 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
112 | "DEBUG: remove_some called with i = %d and addrlen = %u\n", | ||
113 | *i, | ||
114 | (unsigned int) address->address_length); | ||
115 | GNUNET_assert (address->address_length > 0); | ||
116 | if (*i & (1 << (address->address_length - 1))) | ||
117 | { | ||
118 | *i -= (1 << (address->address_length - 1)); | ||
119 | return GNUNET_NO; | ||
120 | } | ||
121 | return GNUNET_OK; | ||
122 | } | ||
123 | |||
124 | |||
125 | int | ||
126 | main (int argc, | ||
127 | char *argv[]) | ||
128 | { | ||
129 | struct GNUNET_HELLO_Message *msg1; | ||
130 | struct GNUNET_HELLO_Message *msg2; | ||
131 | struct GNUNET_HELLO_Message *msg3; | ||
132 | struct GNUNET_CRYPTO_EddsaPublicKey publicKey; | ||
133 | struct GNUNET_PeerIdentity pid; | ||
134 | struct GNUNET_TIME_Absolute startup_time; | ||
135 | unsigned int i; | ||
136 | |||
137 | GNUNET_log_setup ("test-hello", | ||
138 | "DEBUG", | ||
139 | NULL); | ||
140 | startup_time = GNUNET_TIME_absolute_get (); | ||
141 | memset (&publicKey, 42, sizeof(publicKey)); | ||
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
143 | "Testing HELLO creation (without addresses)...\n"); | ||
144 | i = 0; | ||
145 | msg1 = GNUNET_HELLO_create (&publicKey, | ||
146 | &my_addr_gen, | ||
147 | &i, | ||
148 | GNUNET_NO); | ||
149 | GNUNET_assert (msg1 != NULL); | ||
150 | GNUNET_assert (0 < GNUNET_HELLO_size (msg1)); | ||
151 | |||
152 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
153 | "Testing address iteration (empty set)...\n"); | ||
154 | GNUNET_assert (NULL == | ||
155 | GNUNET_HELLO_iterate_addresses (msg1, | ||
156 | GNUNET_NO, | ||
157 | &check_addr, | ||
158 | &i)); | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
160 | "Testing HELLO creation (with one address)...\n"); | ||
161 | i = 1; | ||
162 | msg2 = GNUNET_HELLO_create (&publicKey, | ||
163 | &my_addr_gen, | ||
164 | &i, | ||
165 | GNUNET_NO); | ||
166 | GNUNET_assert (msg2 != NULL); | ||
167 | GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2)); | ||
168 | |||
169 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
170 | "Testing address iteration (one address)...\n"); | ||
171 | i = 1; | ||
172 | GNUNET_assert (NULL == | ||
173 | GNUNET_HELLO_iterate_addresses (msg2, | ||
174 | GNUNET_NO, | ||
175 | &check_addr, | ||
176 | &i)); | ||
177 | GNUNET_assert (i == 0); | ||
178 | |||
179 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
180 | "Testing get_key from HELLO...\n"); | ||
181 | GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (msg2, &pid)); | ||
182 | GNUNET_assert (0 == GNUNET_memcmp (&publicKey, | ||
183 | &pid.public_key)); | ||
184 | GNUNET_free (msg1); | ||
185 | |||
186 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
187 | "Testing HELLO creation (with two addresses)...\n"); | ||
188 | i = 2; | ||
189 | msg3 = GNUNET_HELLO_create (&publicKey, | ||
190 | &my_addr_gen, | ||
191 | &i, | ||
192 | GNUNET_NO); | ||
193 | GNUNET_assert (msg3 != NULL); | ||
194 | GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3)); | ||
195 | |||
196 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
197 | "Testing address iteration (two addresses)...\n"); | ||
198 | i = 3; | ||
199 | GNUNET_assert (NULL == | ||
200 | GNUNET_HELLO_iterate_addresses (msg3, | ||
201 | GNUNET_NO, | ||
202 | &check_addr, | ||
203 | &i)); | ||
204 | GNUNET_assert (i == 0); | ||
205 | |||
206 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
207 | "Testing HELLO merge...\n"); | ||
208 | msg1 = GNUNET_HELLO_merge (msg2, msg3); | ||
209 | GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3)); | ||
210 | |||
211 | i = 3; | ||
212 | GNUNET_assert (NULL == | ||
213 | GNUNET_HELLO_iterate_addresses (msg1, | ||
214 | GNUNET_NO, | ||
215 | &check_addr, | ||
216 | &i)); | ||
217 | GNUNET_assert (i == 0); | ||
218 | GNUNET_free (msg1); | ||
219 | |||
220 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
221 | "Testing address iteration to copy HELLO...\n"); | ||
222 | i = 2; | ||
223 | msg1 = GNUNET_HELLO_iterate_addresses (msg3, | ||
224 | GNUNET_YES, | ||
225 | &remove_some, | ||
226 | &i); | ||
227 | GNUNET_assert (msg1 != NULL); | ||
228 | GNUNET_assert (i == 0); | ||
229 | i = 1; | ||
230 | GNUNET_assert (NULL == | ||
231 | GNUNET_HELLO_iterate_addresses (msg1, | ||
232 | GNUNET_NO, | ||
233 | &check_addr, | ||
234 | &i)); | ||
235 | GNUNET_assert (i == 0); | ||
236 | GNUNET_free (msg1); | ||
237 | |||
238 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
239 | "Testing delta address iteration...\n"); | ||
240 | i = 2; | ||
241 | GNUNET_HELLO_iterate_new_addresses (msg3, | ||
242 | msg2, | ||
243 | startup_time, | ||
244 | &check_addr, | ||
245 | &i); | ||
246 | GNUNET_assert (i == 0); | ||
247 | GNUNET_free (msg2); | ||
248 | GNUNET_free (msg3); | ||
249 | return 0; /* testcase passed */ | ||
250 | } | ||
251 | |||
252 | |||
253 | /* end of test_hello.c */ | ||