missing check
[oweals/gnunet.git] / src / util / test_mq_client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 /**
22  * @file util/test_mq_client.c
23  * @brief tests for mq with connection client
24  */
25 #include "platform.h"
26 #include "gnunet_common.h"
27 #include "gnunet_util_lib.h"
28
29 #define PORT 23336
30
31 #define MY_TYPE 128
32
33
34 static struct GNUNET_SERVER_Handle *server;
35
36 static struct GNUNET_CLIENT_Connection *client;
37
38 static struct GNUNET_CONFIGURATION_Handle *cfg;
39
40 static int ok;
41
42 static int notify = GNUNET_NO;
43
44 static int received = 0;
45
46
47 static void
48 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
49          const struct GNUNET_MessageHeader *message)
50 {
51   received++;
52   if ((received == 2) && (GNUNET_YES == notify))
53   {
54     GNUNET_SERVER_receive_done (argclient, GNUNET_NO);
55     return;
56   }
57
58   /* can happen if notify does not work */
59   GNUNET_assert (received < 2);
60
61   GNUNET_SERVER_receive_done (argclient, GNUNET_YES);
62 }
63
64
65 static void
66 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 {
68   GNUNET_SERVER_destroy (server);
69   server = NULL;
70   GNUNET_CONFIGURATION_destroy (cfg);
71   cfg = NULL;
72 }
73
74
75 /**
76  * Functions with this signature are called whenever a client
77  * is disconnected on the network level.
78  *
79  * @param cls closure
80  * @param client identification of the client
81  */
82 static void
83 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
84 {
85   if (client == NULL)
86     return;
87   ok = 0;
88   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
89 }
90
91
92 static struct GNUNET_SERVER_MessageHandler handlers[] = {
93   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
94   {NULL, NULL, 0, 0}
95 };
96
97 void send_cb (void *cls)
98 {
99   /* the notify should only be called once */
100   GNUNET_assert (GNUNET_NO == notify);
101   notify = GNUNET_YES;
102 }
103
104 void test_mq (struct GNUNET_CLIENT_Connection *client)
105 {
106   struct GNUNET_MQ_Handle *mq;
107   struct GNUNET_MQ_Envelope *mqm;
108
109   /* FIXME: test handling responses */
110   mq = GNUNET_MQ_queue_for_connection_client (client, NULL, NULL, NULL);
111
112   mqm = GNUNET_MQ_msg_header (MY_TYPE);
113   GNUNET_MQ_send (mq, mqm);
114
115   mqm = GNUNET_MQ_msg_header (MY_TYPE);
116   GNUNET_MQ_notify_sent (mqm, send_cb, NULL);
117   GNUNET_MQ_send (mq, mqm);
118
119   /* FIXME: add a message that will be canceled */
120 }
121
122
123 static void
124 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 {
126   struct sockaddr_in sa;
127   struct sockaddr *sap[2];
128   socklen_t slens[2];
129
130   sap[0] = (struct sockaddr *) &sa;
131   slens[0] = sizeof (sa);
132   sap[1] = NULL;
133   slens[1] = 0;
134   memset (&sa, 0, sizeof (sa));
135 #if HAVE_SOCKADDR_IN_SIN_LEN
136   sa.sin_len = sizeof (sa);
137 #endif
138   sa.sin_family = AF_INET;
139   sa.sin_port = htons (PORT);
140   server =
141       GNUNET_SERVER_create (NULL, NULL, sap, slens,
142                             GNUNET_TIME_relative_multiply
143                             (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
144   GNUNET_assert (server != NULL);
145   handlers[0].callback_cls = cls;
146   GNUNET_SERVER_add_handlers (server, handlers);
147   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
148   cfg = GNUNET_CONFIGURATION_create ();
149   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
150   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
151   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
152                                          "localhost");
153   client = GNUNET_CLIENT_connect ("test", cfg);
154   GNUNET_assert (client != NULL);
155
156   test_mq (client);
157 }
158
159
160 int
161 main (int argc, char *argv[])
162 {
163   GNUNET_log_setup ("test-mq-client",
164                     "INFO",
165                     NULL);
166   ok = 1;
167   GNUNET_SCHEDULER_run (&task, NULL);
168   return ok;
169 }
170