diff options
Diffstat (limited to 'src/hello/test_hello.c')
-rw-r--r-- | src/hello/test_hello.c | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/src/hello/test_hello.c b/src/hello/test_hello.c new file mode 100644 index 000000000..728afd63f --- /dev/null +++ b/src/hello/test_hello.c | |||
@@ -0,0 +1,185 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2009 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 2, 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 hello/test_hello.c | ||
22 | * @brief test for hello.c | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | |||
26 | /** | ||
27 | * Testcase for HELLO code. | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | |||
32 | #define DEBUG 0 | ||
33 | |||
34 | static size_t | ||
35 | my_addr_gen (void *cls, size_t max, void *buf) | ||
36 | { | ||
37 | unsigned int *i = cls; | ||
38 | size_t ret; | ||
39 | |||
40 | #if DEBUG | ||
41 | fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i); | ||
42 | #endif | ||
43 | if (0 == *i) | ||
44 | return 0; | ||
45 | ret = GNUNET_HELLO_add_address ("test", | ||
46 | GNUNET_TIME_absolute_get (), | ||
47 | "address_information", *i, buf, max); | ||
48 | (*i)--; | ||
49 | return ret; | ||
50 | } | ||
51 | |||
52 | |||
53 | static int | ||
54 | check_addr (void *cls, | ||
55 | const char *tname, | ||
56 | struct GNUNET_TIME_Absolute expiration, | ||
57 | const void *addr, size_t addrlen) | ||
58 | { | ||
59 | unsigned int *i = cls; | ||
60 | |||
61 | #if DEBUG | ||
62 | fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n", | ||
63 | *i, addrlen); | ||
64 | #endif | ||
65 | GNUNET_assert (addrlen > 0); | ||
66 | GNUNET_assert (*i & (1 << (addrlen - 1))); | ||
67 | *i -= (1 << (addrlen - 1)); | ||
68 | GNUNET_assert (0 == strncmp ("address_information", addr, addrlen)); | ||
69 | GNUNET_assert (0 == strcmp ("test", tname)); | ||
70 | return GNUNET_OK; | ||
71 | } | ||
72 | |||
73 | |||
74 | static int | ||
75 | remove_some (void *cls, | ||
76 | const char *tname, | ||
77 | struct GNUNET_TIME_Absolute expiration, | ||
78 | const void *addr, size_t addrlen) | ||
79 | { | ||
80 | unsigned int *i = cls; | ||
81 | |||
82 | #if DEBUG | ||
83 | fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n", | ||
84 | *i, addrlen); | ||
85 | #endif | ||
86 | GNUNET_assert (addrlen > 0); | ||
87 | if (*i & (1 << (addrlen - 1))) | ||
88 | { | ||
89 | *i -= (1 << (addrlen - 1)); | ||
90 | return GNUNET_NO; | ||
91 | } | ||
92 | return GNUNET_OK; | ||
93 | } | ||
94 | |||
95 | |||
96 | int | ||
97 | main (int argc, char *argv[]) | ||
98 | { | ||
99 | struct GNUNET_HELLO_Message *msg1; | ||
100 | struct GNUNET_HELLO_Message *msg2; | ||
101 | struct GNUNET_HELLO_Message *msg3; | ||
102 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey; | ||
103 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk; | ||
104 | struct GNUNET_TIME_Absolute startup_time; | ||
105 | int ok; | ||
106 | unsigned int i; | ||
107 | |||
108 | GNUNET_log_setup ("test-hello", "DEBUG", NULL); | ||
109 | startup_time = GNUNET_TIME_absolute_get (); | ||
110 | ok = 0; | ||
111 | memset (&publicKey, 42, sizeof (publicKey)); | ||
112 | fprintf (stderr, "Testing HELLO creation (without addresses)...\n"); | ||
113 | i = 0; | ||
114 | msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i); | ||
115 | GNUNET_assert (msg1 != NULL); | ||
116 | GNUNET_assert (0 < GNUNET_HELLO_size (msg1)); | ||
117 | |||
118 | fprintf (stderr, "Testing address iteration (empty set)...\n"); | ||
119 | GNUNET_assert (NULL == | ||
120 | GNUNET_HELLO_iterate_addresses (msg1, | ||
121 | GNUNET_NO, &check_addr, &i)); | ||
122 | |||
123 | fprintf (stderr, "Testing HELLO creation (with one address)...\n"); | ||
124 | i = 1; | ||
125 | msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i); | ||
126 | GNUNET_assert (msg2 != NULL); | ||
127 | GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2)); | ||
128 | |||
129 | fprintf (stderr, "Testing address iteration (one address)...\n"); | ||
130 | i = 1; | ||
131 | GNUNET_assert (NULL == | ||
132 | GNUNET_HELLO_iterate_addresses (msg2, | ||
133 | GNUNET_NO, &check_addr, &i)); | ||
134 | GNUNET_assert (i == 0); | ||
135 | |||
136 | fprintf (stderr, "Testing get_key from HELLO...\n"); | ||
137 | GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk)); | ||
138 | GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk))); | ||
139 | GNUNET_free (msg1); | ||
140 | |||
141 | fprintf (stderr, "Testing HELLO creation (with two addresses)...\n"); | ||
142 | i = 2; | ||
143 | msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i); | ||
144 | GNUNET_assert (msg3 != NULL); | ||
145 | GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3)); | ||
146 | |||
147 | fprintf (stderr, "Testing address iteration (two addresses)...\n"); | ||
148 | i = 3; | ||
149 | GNUNET_assert (NULL == | ||
150 | GNUNET_HELLO_iterate_addresses (msg3, | ||
151 | GNUNET_NO, &check_addr, &i)); | ||
152 | GNUNET_assert (i == 0); | ||
153 | |||
154 | fprintf (stderr, "Testing HELLO merge...\n"); | ||
155 | msg1 = GNUNET_HELLO_merge (msg2, msg3); | ||
156 | GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3)); | ||
157 | |||
158 | i = 3; | ||
159 | GNUNET_assert (NULL == | ||
160 | GNUNET_HELLO_iterate_addresses (msg1, | ||
161 | GNUNET_NO, &check_addr, &i)); | ||
162 | GNUNET_assert (i == 0); | ||
163 | GNUNET_free (msg1); | ||
164 | |||
165 | fprintf (stderr, "Testing address iteration to copy HELLO...\n"); | ||
166 | i = 2; | ||
167 | msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i); | ||
168 | GNUNET_assert (msg1 != NULL); | ||
169 | GNUNET_assert (i == 0); | ||
170 | i = 1; | ||
171 | GNUNET_assert (NULL == | ||
172 | GNUNET_HELLO_iterate_addresses (msg1, | ||
173 | GNUNET_NO, &check_addr, &i)); | ||
174 | GNUNET_assert (i == 0); | ||
175 | GNUNET_free (msg1); | ||
176 | |||
177 | fprintf (stderr, "Testing delta address iteration...\n"); | ||
178 | i = 2; | ||
179 | GNUNET_HELLO_iterate_new_addresses (msg3, | ||
180 | msg2, startup_time, &check_addr, &i); | ||
181 | GNUNET_assert (i == 0); | ||
182 | GNUNET_free (msg2); | ||
183 | GNUNET_free (msg3); | ||
184 | return 0; /* testcase passed */ | ||
185 | } | ||