diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-05-24 16:26:10 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-05-24 16:26:10 +0000 |
commit | fef362e060524f45452a6f4f2c31f52474f9cf7e (patch) | |
tree | f1ce418865d4efaff221e5976d7bc73cae0504b2 /src/util | |
parent | 5cdc9eb4091dfbfd1d65c4aaf1e3dfdd4b5b6316 (diff) | |
download | gnunet-fef362e060524f45452a6f4f2c31f52474f9cf7e.tar.gz gnunet-fef362e060524f45452a6f4f2c31f52474f9cf7e.zip |
testcase
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Makefile.am | 11 | ||||
-rw-r--r-- | src/util/test_server_with_client_unix.c | 217 |
2 files changed, 228 insertions, 0 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index b24b3ab7c..3a03516db 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -16,6 +16,10 @@ libgnunetutilwin_la_LIBADD = \ | |||
16 | WINLIB = libgnunetutilwin.la | 16 | WINLIB = libgnunetutilwin.la |
17 | endif | 17 | endif |
18 | 18 | ||
19 | if !MINGW | ||
20 | SERVER_CLIENT_UNIX = test_server_with_client_unix | ||
21 | endif | ||
22 | |||
19 | if USE_COVERAGE | 23 | if USE_COVERAGE |
20 | AM_CFLAGS = --coverage -O0 | 24 | AM_CFLAGS = --coverage -O0 |
21 | XLIB = -lgcov | 25 | XLIB = -lgcov |
@@ -150,6 +154,7 @@ check_PROGRAMS = \ | |||
150 | test_server \ | 154 | test_server \ |
151 | test_server_disconnect \ | 155 | test_server_disconnect \ |
152 | test_server_with_client \ | 156 | test_server_with_client \ |
157 | $(SERVER_CLIENT_UNIX) \ | ||
153 | test_service \ | 158 | test_service \ |
154 | test_strings \ | 159 | test_strings \ |
155 | test_time \ | 160 | test_time \ |
@@ -362,6 +367,12 @@ test_server_with_client_SOURCES = \ | |||
362 | test_server_with_client_LDADD = \ | 367 | test_server_with_client_LDADD = \ |
363 | $(top_builddir)/src/util/libgnunetutil.la | 368 | $(top_builddir)/src/util/libgnunetutil.la |
364 | 369 | ||
370 | test_server_with_client_unix_SOURCES = \ | ||
371 | test_server_with_client_unix.c | ||
372 | test_server_with_client_unix_LDADD = \ | ||
373 | $(top_builddir)/src/util/libgnunetutil.la | ||
374 | |||
375 | |||
365 | test_service_SOURCES = \ | 376 | test_service_SOURCES = \ |
366 | test_service.c | 377 | test_service.c |
367 | test_service_LDADD = \ | 378 | test_service_LDADD = \ |
diff --git a/src/util/test_server_with_client_unix.c b/src/util/test_server_with_client_unix.c new file mode 100644 index 000000000..8c0e4b84c --- /dev/null +++ b/src/util/test_server_with_client_unix.c | |||
@@ -0,0 +1,217 @@ | |||
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 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 util/test_server_with_client_unix.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 MY_TYPE 128 | ||
36 | |||
37 | |||
38 | static struct GNUNET_SERVER_Handle *server; | ||
39 | |||
40 | static struct GNUNET_CLIENT_Connection *client; | ||
41 | |||
42 | static struct GNUNET_CONFIGURATION_Handle *cfg; | ||
43 | |||
44 | static int ok; | ||
45 | |||
46 | static void | ||
47 | send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
48 | { | ||
49 | struct GNUNET_SERVER_Client *argclient = cls; | ||
50 | GNUNET_assert (ok == 3); | ||
51 | ok++; | ||
52 | GNUNET_SERVER_receive_done (argclient, GNUNET_OK); | ||
53 | } | ||
54 | |||
55 | |||
56 | static void | ||
57 | recv_cb (void *cls, | ||
58 | struct GNUNET_SERVER_Client *argclient, | ||
59 | const struct GNUNET_MessageHeader *message) | ||
60 | { | ||
61 | switch (ok) | ||
62 | { | ||
63 | case 2: | ||
64 | ok++; | ||
65 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
66 | (GNUNET_TIME_UNIT_MILLISECONDS, 50), | ||
67 | &send_done, argclient); | ||
68 | break; | ||
69 | case 4: | ||
70 | ok++; | ||
71 | GNUNET_CLIENT_disconnect (client, GNUNET_YES); | ||
72 | GNUNET_SERVER_receive_done (argclient, GNUNET_OK); | ||
73 | break; | ||
74 | default: | ||
75 | GNUNET_assert (0); | ||
76 | } | ||
77 | |||
78 | } | ||
79 | |||
80 | |||
81 | static void | ||
82 | clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
83 | { | ||
84 | GNUNET_SERVER_destroy (server); | ||
85 | server = NULL; | ||
86 | GNUNET_CONFIGURATION_destroy (cfg); | ||
87 | cfg = NULL; | ||
88 | } | ||
89 | |||
90 | |||
91 | /** | ||
92 | * Functions with this signature are called whenever a client | ||
93 | * is disconnected on the network level. | ||
94 | * | ||
95 | * @param cls closure | ||
96 | * @param client identification of the client | ||
97 | */ | ||
98 | static void | ||
99 | notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client) | ||
100 | { | ||
101 | if (client == NULL) | ||
102 | return; | ||
103 | GNUNET_assert (ok == 5); | ||
104 | ok = 0; | ||
105 | GNUNET_SCHEDULER_add_now (&clean_up, NULL); | ||
106 | } | ||
107 | |||
108 | |||
109 | static size_t | ||
110 | notify_ready (void *cls, size_t size, void *buf) | ||
111 | { | ||
112 | struct GNUNET_MessageHeader *msg; | ||
113 | |||
114 | GNUNET_assert (size >= 256); | ||
115 | GNUNET_assert (1 == ok); | ||
116 | ok++; | ||
117 | msg = buf; | ||
118 | msg->type = htons (MY_TYPE); | ||
119 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
120 | msg++; | ||
121 | msg->type = htons (MY_TYPE); | ||
122 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
123 | return 2 * sizeof (struct GNUNET_MessageHeader); | ||
124 | } | ||
125 | |||
126 | |||
127 | static struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
128 | {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)}, | ||
129 | {NULL, NULL, 0, 0} | ||
130 | }; | ||
131 | |||
132 | |||
133 | static void | ||
134 | task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
135 | { | ||
136 | char * unixpath = "/tmp/testsock"; | ||
137 | int slen = strlen (unixpath) + 1; | ||
138 | |||
139 | struct sockaddr_un un; | ||
140 | |||
141 | memset(&un, 0, sizeof(un)); | ||
142 | un.sun_family = AF_UNIX; | ||
143 | memcpy (un.sun_path, unixpath, slen); | ||
144 | un.sun_path[slen] = '\0'; | ||
145 | #if LINUX | ||
146 | un.sun_path[0] = '\0'; | ||
147 | #endif | ||
148 | |||
149 | struct sockaddr * sap[2]; | ||
150 | socklen_t slens[2]; | ||
151 | |||
152 | sap[0] = (struct sockaddr*) &un; | ||
153 | slens[0] = sizeof (un); | ||
154 | |||
155 | sap[1] = NULL; | ||
156 | slens[1] = 0; | ||
157 | |||
158 | server = GNUNET_SERVER_create (NULL, | ||
159 | NULL, | ||
160 | sap, | ||
161 | slens, | ||
162 | GNUNET_TIME_relative_multiply | ||
163 | (GNUNET_TIME_UNIT_MILLISECONDS, 250), | ||
164 | GNUNET_NO); | ||
165 | GNUNET_assert (server != NULL); | ||
166 | handlers[0].callback_cls = cls; | ||
167 | GNUNET_SERVER_add_handlers (server, handlers); | ||
168 | GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls); | ||
169 | cfg = GNUNET_CONFIGURATION_create (); | ||
170 | |||
171 | GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", | ||
172 | unixpath); | ||
173 | GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", | ||
174 | "localhost"); | ||
175 | |||
176 | client = GNUNET_CLIENT_connect ("test", cfg); | ||
177 | GNUNET_assert (client != NULL); | ||
178 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
179 | 256, | ||
180 | GNUNET_TIME_relative_multiply | ||
181 | (GNUNET_TIME_UNIT_MILLISECONDS, 250), | ||
182 | GNUNET_NO, ¬ify_ready, NULL); | ||
183 | } | ||
184 | |||
185 | |||
186 | /** | ||
187 | * Main method, starts scheduler with task1, | ||
188 | * checks that "ok" is correct at the end. | ||
189 | */ | ||
190 | static int | ||
191 | check () | ||
192 | { | ||
193 | |||
194 | ok = 1; | ||
195 | GNUNET_SCHEDULER_run (&task, NULL); | ||
196 | return ok; | ||
197 | } | ||
198 | |||
199 | |||
200 | int | ||
201 | main (int argc, char *argv[]) | ||
202 | { | ||
203 | int ret = 0; | ||
204 | |||
205 | GNUNET_log_setup ("test_server_with_client_unix", | ||
206 | #if VERBOSE | ||
207 | "DEBUG", | ||
208 | #else | ||
209 | "WARNING", | ||
210 | #endif | ||
211 | NULL); | ||
212 | ret += check (); | ||
213 | |||
214 | return ret; | ||
215 | } | ||
216 | |||
217 | /* end of test_server_with_client_unix.c */ | ||