2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file psyc/test_psyc_api_join.c
23 * @brief Testbed test for the PSYC API.
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_common.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_testbed_service.h"
32 #include "gnunet_psyc_util_lib.h"
33 #include "gnunet_psyc_service.h"
34 #include "psyc_test_lib.h"
36 static struct pctx PEERS[2];
43 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "master stopped\n");
49 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "slave parted\n");
53 shutdown_task (void *cls)
55 if (NULL != timeout_task_id) {
56 GNUNET_SCHEDULER_cancel (timeout_task_id);
57 timeout_task_id = NULL;
60 for (int i=0;i<2;i++) {
61 GNUNET_free (PEERS[i].channel_pub_key);
63 if (NULL != PEERS[i].psyc)
65 GNUNET_PSYC_master_stop (PEERS[i].psyc, GNUNET_NO, mst_stop_cb, NULL);
67 GNUNET_PSYC_slave_part (PEERS[i].psyc, GNUNET_NO, slv_part_cb, NULL);
70 for (int i=0;i<MAX_TESTBED_OPS;i++)
72 GNUNET_TESTBED_operation_done (op[i]);
74 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shut down!\n");
78 timeout_task (void *cls)
80 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Timeout!\n");
82 timeout_task_id = NULL;
84 result = GNUNET_SYSERR;
85 GNUNET_SCHEDULER_shutdown ();
91 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "mst_connect_cb()\n");
95 slv_connect_cb (void *cls, int result, uint64_t max_message_id)
97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "slv_connect_cb()\n");
101 join_decision_cb (void *cls,
102 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
104 const struct GNUNET_PSYC_Message *join_msg)
107 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
108 "Slave got join decision: %d\n", is_admitted);
109 GNUNET_SCHEDULER_shutdown ();
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "join_request_cb()\n");
121 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "message_cb()\n");
127 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "message_part_cb()\n");
133 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "disconnect form PSYC service\n");
137 psyc_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
139 struct pctx *peer = (struct pctx*) cls;
142 if (0 == peer->idx) {
143 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to PSYC as master ...\n");
145 peer->psyc = (struct GNUNET_PSYC_Master *)
146 GNUNET_PSYC_master_start (cfg,
148 GNUNET_PSYC_CHANNEL_PRIVATE,
157 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to PSYC as slave ...\n");
159 struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
160 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN, "_foo", "bar baz", 7);
161 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN, "_foo_bar", "foo bar baz", 11);
163 struct GNUNET_PSYC_Message *
164 join_msg = GNUNET_PSYC_message_create ("_request_join", env, "some data", 40);
166 // THOUGHT: It's possible, that psyc slave is to fast and can not connect
167 // to master! ask tg and grothoff about message queueing
168 peer->psyc = (struct GNUNET_PSYC_Slave *)
169 GNUNET_PSYC_slave_join (cfg,
170 peer->channel_pub_key,
172 GNUNET_PSYC_SLAVE_JOIN_NONE,
173 peer->peer_id_master,
183 GNUNET_free (join_msg);
184 peer->channel = GNUNET_PSYC_slave_get_channel (peer->psyc);
185 GNUNET_PSYC_env_destroy (env);
191 service_connect (void *cls,
192 struct GNUNET_TESTBED_Operation *op,
196 GNUNET_assert (NULL != ca_result);
198 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to the service\n");
202 connect_to_services (void *cls)
204 for (int i = 0; i < 2; i++)
206 PEERS[i].peer_id_master = PEERS[0].peer_id;
209 GNUNET_TESTBED_service_connect (NULL, PEERS[i].testbed_peer, "psyc",
210 &service_connect, &PEERS[i], &psyc_ca,
211 &psyc_da, &PEERS[i]);
217 struct GNUNET_TESTBED_Operation *operation,
218 const struct GNUNET_TESTBED_PeerInformation *pinfo,
221 struct pctx *peer = (struct pctx*) cls;
223 peer->peer_id = pinfo->result.id;
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting test\n");
230 GNUNET_SCHEDULER_add_now (&connect_to_services, NULL);
234 testbed_master (void *cls,
235 struct GNUNET_TESTBED_RunHandle *h,
236 unsigned int num_peers,
237 struct GNUNET_TESTBED_Peer **p,
238 unsigned int links_succeeded,
239 unsigned int links_failed)
241 struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key = NULL;
243 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to testbed_master\n");
245 // Set up shutdown logic
246 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
248 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
249 &timeout_task, NULL);
250 GNUNET_assert (NULL != timeout_task_id);
252 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
253 GNUNET_assert (NULL != channel_key);
255 // Set up information contexts for peers
256 for (int i=0 ; i < 2 ; i++)
259 PEERS[i].testbed_peer = p[i];
262 PEERS[i].id_key = GNUNET_CRYPTO_ecdsa_key_create ();
264 // Set up channel keys shared by master and slave
265 PEERS[i].channel_key = channel_key;
267 PEERS[i].channel_pub_key =
268 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
270 GNUNET_CRYPTO_eddsa_key_get_public (PEERS[i].channel_key,
271 PEERS[i].channel_pub_key);
274 GNUNET_TESTBED_peer_get_information (p[i],
275 GNUNET_TESTBED_PIT_IDENTITY,
276 pinfo_cb, &PEERS[i]);
281 main (int argc, char *argv[])
285 ret = GNUNET_TESTBED_test_run ("test-psyc-api-join", "test_psyc.conf",
287 &testbed_master, NULL);
289 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
295 /* end of test_psyc_api_join.c */