diff options
Diffstat (limited to 'src/util/test_server_with_client.c')
-rw-r--r-- | src/util/test_server_with_client.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/src/util/test_server_with_client.c b/src/util/test_server_with_client.c new file mode 100644 index 000000000..4ca2b5bc3 --- /dev/null +++ b/src/util/test_server_with_client.c | |||
@@ -0,0 +1,215 @@ | |||
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 util/test_server_with_client.c | ||
22 | * @brief tests for server.c and client.c, | ||
23 | * specifically disconnect_notify, | ||
24 | * client_get_address and receive_done (resume processing) | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_common.h" | ||
28 | #include "gnunet_scheduler_lib.h" | ||
29 | #include "gnunet_client_lib.h" | ||
30 | #include "gnunet_server_lib.h" | ||
31 | #include "gnunet_time_lib.h" | ||
32 | |||
33 | #define VERBOSE GNUNET_NO | ||
34 | |||
35 | #define PORT 22335 | ||
36 | |||
37 | #define MY_TYPE 128 | ||
38 | |||
39 | |||
40 | static struct GNUNET_SERVER_Handle *server; | ||
41 | |||
42 | static struct GNUNET_CLIENT_Connection *client; | ||
43 | |||
44 | static struct GNUNET_SCHEDULER_Handle *sched; | ||
45 | |||
46 | static struct GNUNET_CONFIGURATION_Handle *cfg; | ||
47 | |||
48 | static int ok; | ||
49 | |||
50 | static void | ||
51 | send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
52 | { | ||
53 | struct GNUNET_SERVER_Client *argclient = cls; | ||
54 | GNUNET_assert (ok == 3); | ||
55 | ok++; | ||
56 | GNUNET_SERVER_receive_done (argclient, GNUNET_OK); | ||
57 | } | ||
58 | |||
59 | |||
60 | static void | ||
61 | recv_cb (void *cls, | ||
62 | struct GNUNET_SERVER_Handle *server, | ||
63 | struct GNUNET_SERVER_Client *argclient, | ||
64 | const struct GNUNET_MessageHeader *message) | ||
65 | { | ||
66 | void *addr; | ||
67 | size_t addrlen; | ||
68 | struct sockaddr_in sa; | ||
69 | struct sockaddr_in *have; | ||
70 | |||
71 | GNUNET_assert (GNUNET_OK == | ||
72 | GNUNET_SERVER_client_get_address (argclient, | ||
73 | &addr, &addrlen)); | ||
74 | |||
75 | GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); | ||
76 | have = addr; | ||
77 | memset (&sa, 0, sizeof (sa)); | ||
78 | sa.sin_family = AF_INET; | ||
79 | sa.sin_port = have->sin_port; | ||
80 | sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | ||
81 | GNUNET_assert (0 == memcmp (&sa, addr, addrlen)); | ||
82 | GNUNET_free (addr); | ||
83 | switch (ok) | ||
84 | { | ||
85 | case 2: | ||
86 | ok++; | ||
87 | GNUNET_SCHEDULER_add_delayed (sched, | ||
88 | GNUNET_YES, | ||
89 | GNUNET_SCHEDULER_PRIORITY_KEEP, | ||
90 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
91 | GNUNET_TIME_relative_multiply | ||
92 | (GNUNET_TIME_UNIT_MILLISECONDS, 50), | ||
93 | &send_done, argclient); | ||
94 | break; | ||
95 | case 4: | ||
96 | ok++; | ||
97 | GNUNET_CLIENT_disconnect (client); | ||
98 | GNUNET_SERVER_receive_done (argclient, GNUNET_OK); | ||
99 | break; | ||
100 | default: | ||
101 | GNUNET_assert (0); | ||
102 | } | ||
103 | |||
104 | } | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Functions with this signature are called whenever a client | ||
109 | * is disconnected on the network level. | ||
110 | * | ||
111 | * @param cls closure | ||
112 | * @param client identification of the client | ||
113 | */ | ||
114 | static void | ||
115 | notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client) | ||
116 | { | ||
117 | GNUNET_assert (ok == 5); | ||
118 | ok = 0; | ||
119 | GNUNET_SCHEDULER_shutdown (sched); | ||
120 | GNUNET_CONFIGURATION_destroy (cfg); | ||
121 | } | ||
122 | |||
123 | |||
124 | static size_t | ||
125 | notify_ready (void *cls, size_t size, void *buf) | ||
126 | { | ||
127 | struct GNUNET_MessageHeader *msg; | ||
128 | |||
129 | GNUNET_assert (size >= 256); | ||
130 | GNUNET_assert (1 == ok); | ||
131 | ok++; | ||
132 | msg = buf; | ||
133 | msg->type = htons (MY_TYPE); | ||
134 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
135 | msg++; | ||
136 | msg->type = htons (MY_TYPE); | ||
137 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
138 | return 2 * sizeof (struct GNUNET_MessageHeader); | ||
139 | } | ||
140 | |||
141 | |||
142 | static struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
143 | {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)}, | ||
144 | {NULL, NULL, 0, 0} | ||
145 | }; | ||
146 | |||
147 | |||
148 | static void | ||
149 | task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
150 | { | ||
151 | struct sockaddr_in sa; | ||
152 | |||
153 | sched = tc->sched; | ||
154 | memset (&sa, 0, sizeof (sa)); | ||
155 | sa.sin_family = AF_INET; | ||
156 | sa.sin_port = htons (PORT); | ||
157 | server = GNUNET_SERVER_create (tc->sched, | ||
158 | NULL, | ||
159 | NULL, | ||
160 | (const struct sockaddr *) &sa, | ||
161 | sizeof (sa), | ||
162 | 1024, | ||
163 | GNUNET_TIME_relative_multiply | ||
164 | (GNUNET_TIME_UNIT_MILLISECONDS, 250), | ||
165 | GNUNET_NO); | ||
166 | GNUNET_assert (server != NULL); | ||
167 | handlers[0].callback_cls = cls; | ||
168 | GNUNET_SERVER_add_handlers (server, handlers); | ||
169 | GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls); | ||
170 | cfg = GNUNET_CONFIGURATION_create (); | ||
171 | GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT); | ||
172 | GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", | ||
173 | "localhost"); | ||
174 | client = GNUNET_CLIENT_connect (tc->sched, "test", cfg); | ||
175 | GNUNET_assert (client != NULL); | ||
176 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
177 | 256, | ||
178 | GNUNET_TIME_relative_multiply | ||
179 | (GNUNET_TIME_UNIT_MILLISECONDS, 250), | ||
180 | ¬ify_ready, NULL); | ||
181 | } | ||
182 | |||
183 | |||
184 | /** | ||
185 | * Main method, starts scheduler with task1, | ||
186 | * checks that "ok" is correct at the end. | ||
187 | */ | ||
188 | static int | ||
189 | check () | ||
190 | { | ||
191 | |||
192 | ok = 1; | ||
193 | GNUNET_SCHEDULER_run (&task, NULL); | ||
194 | return ok; | ||
195 | } | ||
196 | |||
197 | |||
198 | int | ||
199 | main (int argc, char *argv[]) | ||
200 | { | ||
201 | int ret = 0; | ||
202 | |||
203 | GNUNET_log_setup ("test_server_with_client", | ||
204 | #if VERBOSE | ||
205 | "DEBUG", | ||
206 | #else | ||
207 | "WARNING", | ||
208 | #endif | ||
209 | NULL); | ||
210 | ret += check (); | ||
211 | |||
212 | return ret; | ||
213 | } | ||
214 | |||
215 | /* end of test_server_with_client.c */ | ||