2 * This file is part of GNUnet
3 * (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file psyc/test_psyc.c
23 * @brief Test for the PSYC service.
24 * @author Gabor X Toth
25 * @author Christian Grothoff
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"
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38 #define DEBUG_SERVICE 1
42 * Return value from 'main'.
46 static const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Handle for task for timeout termination.
51 static GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
53 static struct GNUNET_PSYC_Master *mst;
54 static struct GNUNET_PSYC_Slave *slv;
55 static struct GNUNET_PSYC_Channel *ch;
57 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
58 static struct GNUNET_CRYPTO_EddsaPrivateKey *slave_key;
60 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
61 static struct GNUNET_CRYPTO_EddsaPublicKey slave_pub_key;
63 struct GNUNET_PSYC_MasterTransmitHandle *mth;
66 * Clean up all resources used.
73 GNUNET_PSYC_master_stop (mst);
76 GNUNET_SCHEDULER_shutdown ();
81 * Terminate the testcase (failure).
84 * @param tc scheduler context
87 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 * Terminate the testcase (success).
98 * @param tc scheduler context
101 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 * Finish the testcase (successfully).
114 if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
116 GNUNET_SCHEDULER_cancel (end_badly_task);
117 end_badly_task = GNUNET_SCHEDULER_NO_TASK;
119 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
120 &end_normally, NULL);
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)
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);
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)
147 struct TransmitClosure
149 struct GNUNET_PSYC_MasterTransmitHandle *handle;
151 uint8_t fragment_count;
153 uint16_t fragment_sizes[16];
158 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Transmit resume\n");
161 struct TransmitClosure *tmit = cls;
162 GNUNET_PSYC_master_transmit_resume (tmit->handle);
167 transmit_notify (void *cls, size_t *data_size, void *data)
169 struct TransmitClosure *tmit = cls;
170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
171 "Transmit notify: %lu bytes\n", *data_size);
173 if (tmit->fragment_count <= tmit->n)
176 GNUNET_assert (tmit->fragment_sizes[tmit->n] <= *data_size);
178 *data_size = tmit->fragment_sizes[tmit->n];
179 memcpy (data, tmit->fragments[tmit->n], *data_size);
182 if (tmit->n == tmit->fragment_count - 1)
184 /* Send last fragment later. */
185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &transmit_resume,
190 return tmit->n <= tmit->fragment_count ? GNUNET_NO : GNUNET_YES;
194 master_started (void *cls, uint64_t max_message_id)
196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Master started: %lu\n", max_message_id);
198 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
199 GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
200 "_foo", "bar baz", 7);
201 GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
202 "_foo_bar", "foo bar baz", 11);
204 struct TransmitClosure *tmit = GNUNET_new (struct TransmitClosure);
205 tmit->fragment_count = 2;
206 tmit->fragments[0] = "foo bar";
207 tmit->fragment_sizes[0] = 7;
208 tmit->fragments[1] = "baz!";
209 tmit->fragment_sizes[1] = 4;
211 = GNUNET_PSYC_master_transmit (mst, "_test", env, transmit_notify, tmit,
212 GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN);
217 slave_joined (void *cls, uint64_t max_message_id)
219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave joined: %lu\n", max_message_id);
224 * Main function of the test, run from scheduler.
227 * @param cfg configuration we use (also to connect to PSYC service)
228 * @param peer handle to access more of the peer (not used)
232 run (void *cls, char *const *args, const char *cfgfile,
233 const struct GNUNET_CONFIGURATION_Handle *c)
236 const struct GNUNET_CONFIGURATION_Handle *c,
237 struct GNUNET_TESTING_Peer *peer)
241 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
243 channel_key = GNUNET_CRYPTO_ecc_key_create ();
244 slave_key = GNUNET_CRYPTO_ecc_key_create ();
246 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
247 GNUNET_CRYPTO_eddsa_key_get_public (slave_key, &slave_pub_key);
249 mst = GNUNET_PSYC_master_start (cfg, channel_key,
250 GNUNET_PSYC_CHANNEL_PRIVATE,
251 &method, &join, &master_started, NULL);
253 struct GNUNET_PeerIdentity origin;
254 struct GNUNET_PeerIdentity relays[16];
255 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
256 GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
257 "_foo", "bar baz", 7);
258 GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN,
259 "_foo_bar", "foo bar baz", 11);
260 slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin,
261 16, relays, &method, &join, &slave_joined,
262 NULL, "_request_join", env, "some data", 9);
263 GNUNET_ENV_environment_destroy (env);
268 main (int argc, char *argv[])
272 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
273 GNUNET_GETOPT_OPTION_END
275 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
276 "test-psyc [options]",
280 if (0 != GNUNET_TESTING_service_run ("test-psyc", "psyc",
281 "test_psyc.conf", &run, NULL))
287 /* end of test_psyc.c */