Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / psyc / test_psyc2.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file psyc/test_psyc2.c
23  * @brief Testbed test for the PSYC API.
24  * @author xrs
25  */
26
27 #include "platform.h"
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
35 #define PEERS_REQUESTED 2
36
37 static int result;
38
39 static struct GNUNET_SCHEDULER_Task *timeout_tid;
40 static struct pctx **pctx;
41
42 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
43 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
44
45 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
46 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
47
48 /**
49  * Task To perform tests
50  */
51 static struct GNUNET_SCHEDULER_Task *test_task;
52
53 /**
54  * Peer id couter
55  */
56 static unsigned int pids;
57
58 struct pctx
59 {
60   int idx;
61   struct GNUNET_TESTBED_Peer *peer;
62   const struct GNUNET_PeerIdentity *id;
63
64   struct GNUNET_TESTBED_Operation *op; 
65
66   /**
67    * psyc service handle
68    */
69   void *psyc;
70   struct GNUNET_PSYC_Master *mst;
71   struct GNUNET_PSYC_Slave *slv;
72
73   /**
74    * result for test on peer
75    */
76   int test_ok;
77 };
78
79 static void
80 shutdown_task (void *cls)
81 {
82   if (NULL != pctx)
83   {
84     if (NULL != pctx[0]->mst)
85       GNUNET_PSYC_master_stop (pctx[0]->mst, GNUNET_NO, NULL, NULL);  
86
87     for (int i=0; i < PEERS_REQUESTED; i++)
88     {
89       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Operation done.\n");
90       GNUNET_TESTBED_operation_done (pctx[i]->op);
91       GNUNET_free_non_null (pctx[i]);
92     }
93     GNUNET_free (pctx);
94   }
95
96   if (NULL != timeout_tid)
97     GNUNET_SCHEDULER_cancel (timeout_tid);
98 }
99
100 static void
101 timeout_task (void *cls)
102 {
103   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout!\n");
104   result = GNUNET_SYSERR;
105   GNUNET_SCHEDULER_shutdown ();
106 }
107
108 static void 
109 start_test (void *cls)
110 {
111 }
112
113 static void
114 pinfo_cb (void *cls,
115           struct GNUNET_TESTBED_Operation *operation,
116           const struct GNUNET_TESTBED_PeerInformation *pinfo,
117           const char *emsg)
118 {
119   struct pctx *pc = (struct pctx*) cls;
120
121   pc->id = pinfo->result.id;
122
123   pids++;
124   if (pids < (PEERS_REQUESTED - 1))
125     return;
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
127   test_task = GNUNET_SCHEDULER_add_now (&start_test, NULL);
128 }
129
130 static void
131 mst_start_cb () 
132 {
133 }
134
135 static void 
136 join_request_cb ()
137 {
138 }
139
140 static void
141 mst_message_cb ()
142 {
143 }
144
145 static void
146 mst_message_part_cb ()
147 {
148 }
149
150 static void 
151 slv_message_cb ()
152 {
153 }
154
155 static void 
156 slv_message_part_cb ()
157 {
158 }
159
160 static void
161 slv_connect_cb () 
162 {
163 }
164
165 static void
166 join_decision_cb ()
167 {
168 }
169
170 static void *
171 psyc_ca (void *cls,
172          const struct GNUNET_CONFIGURATION_Handle *cfg)
173 {
174   struct GNUNET_PSYC_Message *join_msg = NULL;
175   struct pctx *pc = (struct pctx *) cls;
176
177   if (0 == pc->idx)
178   {
179     pc->mst = GNUNET_PSYC_master_start (cfg, channel_key, 
180                                         GNUNET_PSYC_CHANNEL_PRIVATE,
181                                         &mst_start_cb, &join_request_cb,
182                                         &mst_message_cb, &mst_message_part_cb,
183                                         NULL);
184     return pc->mst;
185   }
186
187   pc->slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key,
188                                     GNUNET_PSYC_SLAVE_JOIN_NONE,
189                                     &pid, 0, NULL, &slv_message_cb, 
190                                     &slv_message_part_cb,
191                                     &slv_connect_cb, &join_decision_cb, 
192                                     NULL, join_msg);
193   return pc->slv;
194
195
196 static void
197 psyc_da (void *cls,
198          void *op_result)
199 {
200   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnected from service.\n");
201
202
203 static void
204 service_connect (void *cls,
205                  struct GNUNET_TESTBED_Operation *op,
206                  void *ca_result,
207                  const char *emsg)
208 {
209   struct pctx *pc = (struct pctx *) cls;
210
211   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
212               "Connected to service\n");
213
214   GNUNET_assert (NULL != ca_result);
215
216   // FIXME: we need a simple service handle to connect to the service, then 
217   // get peer information and AFTER that make PSYC ops. Compare to CADET. 
218   pc->psyc = ca_result;
219
220   GNUNET_TESTBED_peer_get_information (pc->peer, 
221                                        GNUNET_TESTBED_PIT_IDENTITY, 
222                                        pinfo_cb, pc);
223 }
224
225 static void
226 testbed_master (void *cls,
227      struct GNUNET_TESTBED_RunHandle *h,
228      unsigned int num_peers,
229      struct GNUNET_TESTBED_Peer **p,
230      unsigned int links_succeeded,
231      unsigned int links_failed)
232 {
233   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to testbed_master()\n");
234
235   // Create ctx for peers
236   pctx = GNUNET_new_array (PEERS_REQUESTED, struct pctx*);
237   for (int i = 0; i<PEERS_REQUESTED; i++) 
238   {
239     pctx[i] = GNUNET_new (struct pctx);
240     pctx[i]->idx = i;
241     pctx[i]->peer = p[i];
242     pctx[i]->id = NULL;
243     pctx[i]->mst = NULL;
244     pctx[i]->op = NULL;
245     pctx[i]->test_ok = GNUNET_NO;
246   }
247
248   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
249   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
250
251   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
252   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
253
254   pctx[0]->op = 
255     GNUNET_TESTBED_service_connect (NULL, p[0], "psyc", service_connect, 
256                                     pctx[0], psyc_ca, psyc_da, pctx[0]);
257
258   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 
259
260   timeout_tid = 
261     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
262                                   &timeout_task, NULL);
263 }
264
265 int
266 main (int argc, char *argv[])
267 {
268   int ret;
269
270   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test\n");
271
272   result = GNUNET_SYSERR;
273
274   ret = GNUNET_TESTBED_test_run ("test-psyc2", "test_psyc.conf",
275                                  PEERS_REQUESTED, 0LL, NULL, NULL, 
276                                  testbed_master, NULL);
277
278   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
279     return 1;
280
281   return 0;
282 }
283
284 /* end of test-psyc2.c */