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