704819c507df937b380820a28b919e79fc6c1461
[oweals/gnunet.git] / src / psyc / test_psyc.c
1 /*
2  * This file is part of GNUnet
3  * (C) 2013 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 psyc/test_psyc.c
23  * @brief Test for the PSYC service.
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_common.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_env_lib.h"
34 #include "gnunet_psyc_service.h"
35
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37
38 #define DEBUG_SERVICE 1
39
40
41 /**
42  * Return value from 'main'.
43  */
44 static int res;
45
46 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 /**
49  * Handle for task for timeout termination.
50  */
51 static GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
52
53 static struct GNUNET_PSYC_Master *mst;
54 static struct GNUNET_PSYC_Slave *slv;
55 static struct GNUNET_PSYC_Channel *ch;
56
57 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
58 static struct GNUNET_CRYPTO_EddsaPrivateKey *slave_key;
59
60 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
61 static struct GNUNET_CRYPTO_EddsaPublicKey slave_pub_key;
62
63 struct GNUNET_PSYC_MasterTransmitHandle *mth;
64
65 /**
66  * Clean up all resources used.
67  */
68 static void
69 cleanup ()
70 {
71   if (mst != NULL)
72   {
73     GNUNET_PSYC_master_stop (mst);
74     mst = NULL;
75   }
76   GNUNET_SCHEDULER_shutdown ();
77 }
78
79
80 /**
81  * Terminate the testcase (failure).
82  *
83  * @param cls NULL
84  * @param tc scheduler context
85  */
86 static void
87 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   res = 1;
90   cleanup ();
91 }
92
93
94 /**
95  * Terminate the testcase (success).
96  *
97  * @param cls NULL
98  * @param tc scheduler context
99  */
100 static void
101 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   res = 0;
104   cleanup ();
105 }
106
107
108 /**
109  * Finish the testcase (successfully).
110  */
111 static void
112 end ()
113 {
114   if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
115   {
116     GNUNET_SCHEDULER_cancel (end_badly_task);
117     end_badly_task = GNUNET_SCHEDULER_NO_TASK;
118   }
119   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
120                                 &end_normally, NULL);
121 }
122
123
124 static int
125 method (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
126         uint64_t message_id, const char *name,
127         size_t modifier_count, const struct GNUNET_ENV_Modifier *modifiers,
128         uint64_t data_offset, const void *data, size_t data_size,
129         enum GNUNET_PSYC_MessageFlags flags)
130 {
131   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
132               "Method: %s, modifiers: %lu, flags: %u\n%.*s\n",
133               name, modifier_count, flags, data_size, data);
134   return GNUNET_OK;
135 }
136
137
138 static int
139 join (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
140       const char *method_name,
141       size_t variable_count, const struct GNUNET_ENV_Modifier *variables,
142       const void *data, size_t data_size, struct GNUNET_PSYC_JoinHandle *jh)
143 {
144   return GNUNET_OK;
145 }
146
147
148 struct TransmitClosure
149 {
150   struct GNUNET_PSYC_MasterTransmitHandle *handle;
151
152   char *mod_names[16];
153   char *mod_values[16];
154   char *data[16];
155
156   uint8_t mod_count;
157   uint8_t data_count;
158
159   uint8_t paused;
160   uint8_t n;
161 };
162
163
164 static void
165 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
168   struct TransmitClosure *tmit = cls;
169   tmit->paused = GNUNET_NO;
170   GNUNET_PSYC_master_transmit_resume (tmit->handle);
171 }
172
173
174 static int
175 tmit_notify_mod (void *cls, size_t *data_size, void *data)
176 {
177   struct TransmitClosure *tmit = cls;
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179               "Transmit notify modifier: %lu bytes available, "
180               "processing modifier %u/%u.\n",
181               *data_size, tmit->n + 1, tmit->fragment_count);
182   /* FIXME: continuation */
183   uint16_t name_size = strlen (tmit->mod_names[tmit->n]);
184   uint16_t value_size = strlen (tmit->mod_values[tmit->n]);
185   if (name_size + 1 + value_size <= *data_size)
186     return GNUNET_NO;
187
188   *data_size = name_size + 1 + value_size;
189   memcpy (data, tmit->fragments[tmit->n], *data_size);
190
191   if (++tmit->n < tmit->mod_count)
192   {
193     return GNUNET_NO;
194   }
195   else
196   {
197     tmit->n = 0;
198     return GNUNET_YES;
199   }
200 }
201
202
203 static int
204 tmit_notify_data (void *cls, size_t *data_size, void *data)
205 {
206   struct TransmitClosure *tmit = cls;
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Transmit notify data: %lu bytes available, "
209               "processing fragment %u/%u.\n",
210               *data_size, tmit->n + 1, tmit->fragment_count);
211   uint16_t size = strlen (tmit->data[tmit->n]);
212   if (size <= *data_size)
213     return GNUNET_NO;
214
215   if (GNUNET_YES == tmit->paused && tmit->n == tmit->data_count - 1)
216   {
217     /* Send last fragment later. */
218     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
219     tmit->paused = GNUNET_YES;
220     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
221                                   (GNUNET_TIME_UNIT_SECONDS, 3),
222                                   &transmit_resume, tmit);
223     *data_size = 0;
224     return GNUNET_NO;
225   }
226
227   *data_size = size;
228   memcpy (data, tmit->data[tmit->n], size);
229
230   return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
231 }
232
233
234 void
235 master_started (void *cls, uint64_t max_message_id)
236 {
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "Master started: %lu\n", max_message_id);
239
240   struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
241   GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
242                                   "_foo", "bar baz", 7);
243   GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
244                                   "_foo_bar", "foo bar baz", 11);
245
246   struct TransmitClosure *tmit = GNUNET_new (struct TransmitClosure);
247   tmit->data[0] = "foo";
248   tmit->data[1] = "foo bar";
249   tmit->data[2] = "foo bar baz";
250   tmit->data_count = 3;
251   tmit->handle
252     = GNUNET_PSYC_master_transmit (mst, "_test", tmit_notify_mod,
253                                    tmit_notify_data, tmit,
254                                    GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN);
255 }
256
257
258 void
259 slave_joined (void *cls, uint64_t max_message_id)
260 {
261   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave joined: %lu\n", max_message_id);
262 }
263
264
265 /**
266  * Main function of the test, run from scheduler.
267  *
268  * @param cls NULL
269  * @param cfg configuration we use (also to connect to PSYC service)
270  * @param peer handle to access more of the peer (not used)
271  */
272 static void
273 #if DEBUG_SERVICE
274 run (void *cls, char *const *args, const char *cfgfile,
275      const struct GNUNET_CONFIGURATION_Handle *c)
276 #else
277 run (void *cls,
278      const struct GNUNET_CONFIGURATION_Handle *c,
279      struct GNUNET_TESTING_Peer *peer)
280 #endif
281 {
282   cfg = c;
283   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
284
285   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
286   slave_key = GNUNET_CRYPTO_eddsa_key_create ();
287
288   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
289   GNUNET_CRYPTO_eddsa_key_get_public (slave_key, &slave_pub_key);
290
291   mst = GNUNET_PSYC_master_start (cfg, channel_key,
292                                   GNUNET_PSYC_CHANNEL_PRIVATE,
293                                   &method, &join, &master_started, NULL);
294   return;
295   struct GNUNET_PeerIdentity origin;
296   struct GNUNET_PeerIdentity relays[16];
297   struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
298   GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
299                                   "_foo", "bar baz", 7);
300   GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
301                                   "_foo_bar", "foo bar baz", 11);
302   slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin,
303                                 16, relays, &method, &join, &slave_joined,
304                                 NULL, "_request_join", env, "some data", 9);
305   GNUNET_ENV_environment_destroy (env);
306 }
307
308
309 int
310 main (int argc, char *argv[])
311 {
312   res = 1;
313 #if DEBUG_SERVICE
314   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
315     GNUNET_GETOPT_OPTION_END
316   };
317   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
318                                        "test-psyc [options]",
319                                        opts, &run, NULL))
320     return 1;
321 #else
322   if (0 != GNUNET_TESTING_service_run ("test-psyc", "psyc",
323                                        "test_psyc.conf", &run, NULL))
324     return 1;
325 #endif
326   return res;
327 }
328
329 /* end of test_psyc.c */