glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / test_client.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file util/test_client.c
17  * @brief tests for client.c
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22
23 static int global_ret;
24
25 static struct GNUNET_MQ_Handle *client_mq;
26
27 #define MY_TYPE 130
28
29
30 /**
31  * Callback that just bounces the message back to the sender.
32  */
33 static void
34 handle_echo (void *cls,
35              const struct GNUNET_MessageHeader *message)
36 {
37   struct GNUNET_SERVICE_Client *c = cls;
38   struct GNUNET_MQ_Handle *mq = GNUNET_SERVICE_client_get_mq (c);
39   struct GNUNET_MQ_Envelope *env;
40
41   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
42               "Receiving message from client, bouncing back\n");
43   env = GNUNET_MQ_msg_copy (message);
44   GNUNET_MQ_send (mq,
45                   env);
46   GNUNET_SERVICE_client_continue (c);
47 }
48
49
50 static void
51 handle_bounce (void *cls,
52                const struct GNUNET_MessageHeader *got)
53 {
54   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
55               "Receiving bounce, checking content\n");
56   GNUNET_assert (NULL != got);
57   global_ret = 2;
58   GNUNET_MQ_destroy (client_mq);
59   client_mq = NULL;
60 }
61
62
63 /**
64  * Generic error handler, called with the appropriate error code and
65  * the same closure specified at the creation of the message queue.
66  * Not every message queue implementation supports an error handler.
67  *
68  * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
69  * @param error error code
70  */
71 static void
72 mq_error_handler (void *cls,
73                   enum GNUNET_MQ_Error error)
74 {
75   GNUNET_assert (0); /* should never happen */
76 }
77
78
79 static void
80 task (void *cls,
81       const struct GNUNET_CONFIGURATION_Handle *cfg,
82       struct GNUNET_SERVICE_Handle *sh)
83 {
84   struct GNUNET_MQ_MessageHandler chandlers[] = {
85     GNUNET_MQ_hd_fixed_size (bounce,
86                              MY_TYPE,
87                              struct GNUNET_MessageHeader,
88                              cls),
89     GNUNET_MQ_handler_end ()
90   };
91   struct GNUNET_MQ_Envelope *env;
92   struct GNUNET_MessageHeader *msg;
93
94   /* test that ill-configured client fails instantly */
95   GNUNET_assert (NULL ==
96                  GNUNET_CLIENT_connect (cfg,
97                                         "invalid-service",
98                                         NULL,
99                                         &mq_error_handler,
100                                         NULL));
101   client_mq = GNUNET_CLIENT_connect (cfg,
102                                      "test_client",
103                                      chandlers,
104                                      &mq_error_handler,
105                                      NULL);
106   GNUNET_assert (NULL != client_mq);
107   env = GNUNET_MQ_msg (msg,
108                        MY_TYPE);
109   GNUNET_MQ_send (client_mq,
110                   env);
111 }
112
113
114 /**
115  * Function called when the client connects to the service.
116  *
117  * @param cls the name of the service
118  * @param c connecting client
119  * @param mq message queue to talk to the client
120  * @return @a c
121  */
122 static void *
123 connect_cb (void *cls,
124             struct GNUNET_SERVICE_Client *c,
125             struct GNUNET_MQ_Handle *mq)
126 {
127   return c;
128 }
129
130
131 /**
132  * Function called when the client disconnects.
133  *
134  * @param cls our service name
135  * @param c disconnecting client
136  * @param internal_cls must match @a c
137  */ 
138 static void
139 disconnect_cb (void *cls,
140                struct GNUNET_SERVICE_Client *c,
141                void *internal_cls)
142 {
143   if (2 == global_ret)
144   {
145     GNUNET_SCHEDULER_shutdown ();
146     global_ret = 0;
147   }
148 }
149
150
151 int
152 main (int argc,
153       char *argv[])
154 {
155   struct GNUNET_MQ_MessageHandler shandlers[] = {
156     GNUNET_MQ_hd_fixed_size (echo,
157                              MY_TYPE,
158                              struct GNUNET_MessageHeader,
159                              NULL),
160     GNUNET_MQ_handler_end ()
161   };
162   char * test_argv[] = {
163     (char *) "test_client",
164     "-c",
165     "test_client_data.conf",
166     NULL
167   };
168
169   GNUNET_log_setup ("test_client",
170                     "WARNING",
171                     NULL);
172   if (0 != strstr (argv[0],
173                    "unix"))
174     test_argv[2] = "test_client_unix.conf";
175   global_ret = 1;
176   if (0 !=
177       GNUNET_SERVICE_run_ (3,
178                            test_argv,
179                            "test_client",
180                            GNUNET_SERVICE_OPTION_NONE,
181                            &task,
182                            &connect_cb,
183                            &disconnect_cb,
184                            NULL,
185                            shandlers))
186     global_ret = 3;
187   return global_ret;
188 }
189
190 /* end of test_client.c */