diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-09-19 08:40:49 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-09-19 08:40:49 +0000 |
commit | 08f1726b6552ac29830637e9f7be4d42e2ea4294 (patch) | |
tree | 093a456dd05228fa3c592285085aae25eaba1164 /src/util/test_client.c | |
parent | 9f649cbc9454317228c4a4d3580de3a8f8f008a8 (diff) | |
download | gnunet-08f1726b6552ac29830637e9f7be4d42e2ea4294.tar.gz gnunet-08f1726b6552ac29830637e9f7be4d42e2ea4294.zip |
-modifying tests to use new service MQ API, implementing more of service MQ API
Diffstat (limited to 'src/util/test_client.c')
-rw-r--r-- | src/util/test_client.c | 210 |
1 files changed, 101 insertions, 109 deletions
diff --git a/src/util/test_client.c b/src/util/test_client.c index 558a3cf1e..aa4d84495 100644 --- a/src/util/test_client.c +++ b/src/util/test_client.c | |||
@@ -20,95 +20,48 @@ | |||
20 | /** | 20 | /** |
21 | * @file util/test_client.c | 21 | * @file util/test_client.c |
22 | * @brief tests for client.c | 22 | * @brief tests for client.c |
23 | * @author Christian Grothoff | ||
23 | */ | 24 | */ |
24 | #include "platform.h" | 25 | #include "platform.h" |
25 | #include "gnunet_util_lib.h" | 26 | #include "gnunet_util_lib.h" |
26 | 27 | ||
28 | static int global_ret; | ||
27 | 29 | ||
28 | #define PORT 14325 | 30 | static struct GNUNET_MQ_Handle *client_mq; |
29 | |||
30 | #define MYNAME "test_client" | ||
31 | |||
32 | static struct GNUNET_MQ_Handle *mq; | ||
33 | |||
34 | static struct GNUNET_SERVER_Handle *server; | ||
35 | |||
36 | static struct GNUNET_CONFIGURATION_Handle *cfg; | ||
37 | 31 | ||
38 | #define MY_TYPE 130 | 32 | #define MY_TYPE 130 |
39 | 33 | ||
40 | struct CopyContext | ||
41 | { | ||
42 | struct GNUNET_SERVER_Client *client; | ||
43 | struct GNUNET_MessageHeader *cpy; | ||
44 | }; | ||
45 | |||
46 | |||
47 | static size_t | ||
48 | copy_msg (void *cls, size_t size, void *buf) | ||
49 | { | ||
50 | struct CopyContext *ctx = cls; | ||
51 | struct GNUNET_MessageHeader *cpy = ctx->cpy; | ||
52 | |||
53 | GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size)); | ||
54 | GNUNET_assert (size >= ntohs (cpy->size)); | ||
55 | GNUNET_memcpy (buf, cpy, ntohs (cpy->size)); | ||
56 | GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK); | ||
57 | GNUNET_free (cpy); | ||
58 | GNUNET_free (ctx); | ||
59 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
60 | "Message bounced back to client\n"); | ||
61 | return sizeof (struct GNUNET_MessageHeader); | ||
62 | } | ||
63 | |||
64 | 34 | ||
65 | /** | 35 | /** |
66 | * Callback that just bounces the message back to the sender. | 36 | * Callback that just bounces the message back to the sender. |
67 | */ | 37 | */ |
68 | static void | 38 | static void |
69 | echo_cb (void *cls, | 39 | handle_echo (void *cls, |
70 | struct GNUNET_SERVER_Client *client, | 40 | const struct GNUNET_MessageHeader *message) |
71 | const struct GNUNET_MessageHeader *message) | ||
72 | { | 41 | { |
73 | struct CopyContext *cc; | 42 | struct GNUNET_SERVICE_Client *c = cls; |
74 | struct GNUNET_MessageHeader *cpy; | 43 | struct GNUNET_MQ_Handle *mq = GNUNET_SERVICE_client_get_mq (c); |
44 | struct GNUNET_MQ_Envelope *env; | ||
75 | 45 | ||
76 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 46 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
77 | "Receiving message from client, bouncing back\n"); | 47 | "Receiving message from client, bouncing back\n"); |
78 | GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)); | 48 | env = GNUNET_MQ_msg_copy (message); |
79 | cc = GNUNET_new (struct CopyContext); | 49 | GNUNET_MQ_send (mq, |
80 | cc->client = client; | 50 | env); |
81 | cpy = GNUNET_malloc (ntohs (message->size)); | 51 | GNUNET_SERVICE_client_continue (c); |
82 | GNUNET_memcpy (cpy, message, ntohs (message->size)); | ||
83 | cc->cpy = cpy; | ||
84 | GNUNET_assert (NULL != | ||
85 | GNUNET_SERVER_notify_transmit_ready (client, | ||
86 | ntohs (message->size), | ||
87 | GNUNET_TIME_UNIT_SECONDS, | ||
88 | ©_msg, cc)); | ||
89 | } | 52 | } |
90 | 53 | ||
91 | 54 | ||
92 | static struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
93 | {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)}, | ||
94 | {NULL, NULL, 0, 0} | ||
95 | }; | ||
96 | |||
97 | |||
98 | static void | 55 | static void |
99 | handle_bounce (void *cls, | 56 | handle_bounce (void *cls, |
100 | const struct GNUNET_MessageHeader *got) | 57 | const struct GNUNET_MessageHeader *got) |
101 | { | 58 | { |
102 | int *ok = cls; | ||
103 | |||
104 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 59 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
105 | "Receiving bounce, checking content\n"); | 60 | "Receiving bounce, checking content\n"); |
106 | GNUNET_assert (NULL != got); | 61 | GNUNET_assert (NULL != got); |
107 | GNUNET_MQ_destroy (mq); | 62 | global_ret = 2; |
108 | mq = NULL; | 63 | GNUNET_MQ_destroy (client_mq); |
109 | GNUNET_SERVER_destroy (server); | 64 | client_mq = NULL; |
110 | server = NULL; | ||
111 | *ok = 0; | ||
112 | } | 65 | } |
113 | 66 | ||
114 | 67 | ||
@@ -129,13 +82,10 @@ mq_error_handler (void *cls, | |||
129 | 82 | ||
130 | 83 | ||
131 | static void | 84 | static void |
132 | task (void *cls) | 85 | task (void *cls, |
86 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
87 | struct GNUNET_SERVICE_Handle *sh) | ||
133 | { | 88 | { |
134 | struct sockaddr_in sa; | ||
135 | struct sockaddr *sap[2]; | ||
136 | socklen_t slens[2]; | ||
137 | struct GNUNET_MQ_Envelope *env; | ||
138 | struct GNUNET_MessageHeader *msg; | ||
139 | struct GNUNET_MQ_MessageHandler chandlers[] = { | 89 | struct GNUNET_MQ_MessageHandler chandlers[] = { |
140 | GNUNET_MQ_hd_fixed_size (bounce, | 90 | GNUNET_MQ_hd_fixed_size (bounce, |
141 | MY_TYPE, | 91 | MY_TYPE, |
@@ -143,61 +93,103 @@ task (void *cls) | |||
143 | cls), | 93 | cls), |
144 | GNUNET_MQ_handler_end () | 94 | GNUNET_MQ_handler_end () |
145 | }; | 95 | }; |
96 | struct GNUNET_MQ_Envelope *env; | ||
97 | struct GNUNET_MessageHeader *msg; | ||
146 | 98 | ||
147 | /* test that ill-configured client fails instantly */ | 99 | /* test that ill-configured client fails instantly */ |
148 | GNUNET_assert (NULL == GNUNET_CLIENT_connecT (cfg, | 100 | GNUNET_assert (NULL == |
149 | "invalid-service", | 101 | GNUNET_CLIENT_connecT (cfg, |
150 | NULL, | 102 | "invalid-service", |
151 | &mq_error_handler, | 103 | NULL, |
152 | NULL)); | 104 | &mq_error_handler, |
153 | 105 | NULL)); | |
154 | /* test IPC between client and server */ | 106 | client_mq = GNUNET_CLIENT_connecT (cfg, |
155 | sap[0] = (struct sockaddr *) &sa; | 107 | "test_client", |
156 | slens[0] = sizeof (sa); | 108 | chandlers, |
157 | sap[1] = NULL; | 109 | &mq_error_handler, |
158 | slens[1] = 0; | 110 | NULL); |
159 | memset (&sa, 0, sizeof (sa)); | 111 | GNUNET_assert (NULL != client_mq); |
160 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
161 | sa.sin_len = sizeof (sa); | ||
162 | #endif | ||
163 | sa.sin_family = AF_INET; | ||
164 | sa.sin_port = htons (PORT); | ||
165 | server = | ||
166 | GNUNET_SERVER_create (NULL, NULL, sap, slens, | ||
167 | GNUNET_TIME_relative_multiply | ||
168 | (GNUNET_TIME_UNIT_SECONDS, 10), GNUNET_NO); | ||
169 | GNUNET_assert (server != NULL); | ||
170 | handlers[0].callback_cls = cls; | ||
171 | handlers[1].callback_cls = cls; | ||
172 | GNUNET_SERVER_add_handlers (server, handlers); | ||
173 | mq = GNUNET_CLIENT_connecT (cfg, | ||
174 | MYNAME, | ||
175 | chandlers, | ||
176 | &mq_error_handler, | ||
177 | NULL); | ||
178 | GNUNET_assert (NULL != mq); | ||
179 | env = GNUNET_MQ_msg (msg, | 112 | env = GNUNET_MQ_msg (msg, |
180 | MY_TYPE); | 113 | MY_TYPE); |
181 | GNUNET_MQ_send (mq, | 114 | GNUNET_MQ_send (client_mq, |
182 | env); | 115 | env); |
183 | } | 116 | } |
184 | 117 | ||
185 | 118 | ||
119 | /** | ||
120 | * Function called when the client connects to the service. | ||
121 | * | ||
122 | * @param cls the name of the service | ||
123 | * @param c connecting client | ||
124 | * @param mq message queue to talk to the client | ||
125 | * @return @a c | ||
126 | */ | ||
127 | static void * | ||
128 | connect_cb (void *cls, | ||
129 | struct GNUNET_SERVICE_Client *c, | ||
130 | struct GNUNET_MQ_Handle *mq) | ||
131 | { | ||
132 | return c; | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Function called when the client disconnects. | ||
138 | * | ||
139 | * @param cls our service name | ||
140 | * @param c disconnecting client | ||
141 | * @param internal_cls must match @a c | ||
142 | */ | ||
143 | static void | ||
144 | disconnect_cb (void *cls, | ||
145 | struct GNUNET_SERVICE_Client *c, | ||
146 | void *internal_cls) | ||
147 | { | ||
148 | if (2 == global_ret) | ||
149 | { | ||
150 | GNUNET_SCHEDULER_shutdown (); | ||
151 | global_ret = 0; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | |||
186 | int | 156 | int |
187 | main (int argc, char *argv[]) | 157 | main (int argc, |
158 | char *argv[]) | ||
188 | { | 159 | { |
189 | int ok; | 160 | struct GNUNET_MQ_MessageHandler shandlers[] = { |
161 | GNUNET_MQ_hd_fixed_size (echo, | ||
162 | MY_TYPE, | ||
163 | struct GNUNET_MessageHeader, | ||
164 | NULL), | ||
165 | GNUNET_MQ_handler_end () | ||
166 | }; | ||
167 | char * test_argv[] = { | ||
168 | (char *) "test_client", | ||
169 | "-c", | ||
170 | "test_client_data.conf", | ||
171 | NULL | ||
172 | }; | ||
190 | 173 | ||
191 | GNUNET_log_setup ("test_client", | 174 | GNUNET_log_setup ("test_client", |
192 | "WARNING", | 175 | "WARNING", |
193 | NULL); | 176 | NULL); |
194 | cfg = GNUNET_CONFIGURATION_create (); | 177 | if (0 != strstr (argv[0], |
195 | GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT); | 178 | "unix")) |
196 | GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost"); | 179 | test_argv[2] = "test_client_unix.conf"; |
197 | ok = 1; | 180 | global_ret = 1; |
198 | GNUNET_SCHEDULER_run (&task, &ok); | 181 | if (0 != |
199 | GNUNET_CONFIGURATION_destroy (cfg); | 182 | GNUNET_SERVICE_ruN_ (3, |
200 | return ok; | 183 | test_argv, |
184 | "test_client", | ||
185 | GNUNET_SERVICE_OPTION_NONE, | ||
186 | &task, | ||
187 | &connect_cb, | ||
188 | &disconnect_cb, | ||
189 | NULL, | ||
190 | shandlers)) | ||
191 | global_ret = 3; | ||
192 | return global_ret; | ||
201 | } | 193 | } |
202 | 194 | ||
203 | /* end of test_client.c */ | 195 | /* end of test_client.c */ |