You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

285 lines
6.6 KiB

  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 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. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @file psyc/test_psyc2.c
  21. * @brief Testbed test for the PSYC API.
  22. * @author xrs
  23. */
  24. #include "platform.h"
  25. #include "gnunet_crypto_lib.h"
  26. #include "gnunet_common.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_testbed_service.h"
  29. #include "gnunet_psyc_util_lib.h"
  30. #include "gnunet_psyc_service.h"
  31. #define PEERS_REQUESTED 2
  32. static int result;
  33. static struct GNUNET_SCHEDULER_Task *timeout_tid;
  34. static struct pctx **pctx;
  35. static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
  36. static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
  37. static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
  38. static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
  39. /**
  40. * Task To perform tests
  41. */
  42. static struct GNUNET_SCHEDULER_Task *test_task;
  43. /**
  44. * Peer id couter
  45. */
  46. static unsigned int pids;
  47. struct pctx
  48. {
  49. int idx;
  50. struct GNUNET_TESTBED_Peer *peer;
  51. const struct GNUNET_PeerIdentity *id;
  52. struct GNUNET_TESTBED_Operation *op;
  53. /**
  54. * psyc service handle
  55. */
  56. void *psyc;
  57. struct GNUNET_PSYC_Master *mst;
  58. struct GNUNET_PSYC_Slave *slv;
  59. /**
  60. * result for test on peer
  61. */
  62. int test_ok;
  63. };
  64. static void
  65. shutdown_task (void *cls)
  66. {
  67. if (NULL != pctx)
  68. {
  69. if (NULL != pctx[0]->mst)
  70. GNUNET_PSYC_master_stop (pctx[0]->mst, GNUNET_NO, NULL, NULL);
  71. for (int i=0; i < PEERS_REQUESTED; i++)
  72. {
  73. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Operation done.\n");
  74. GNUNET_TESTBED_operation_done (pctx[i]->op);
  75. GNUNET_free_non_null (pctx[i]);
  76. }
  77. GNUNET_free (pctx);
  78. }
  79. if (NULL != timeout_tid)
  80. GNUNET_SCHEDULER_cancel (timeout_tid);
  81. }
  82. static void
  83. timeout_task (void *cls)
  84. {
  85. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout!\n");
  86. result = GNUNET_SYSERR;
  87. GNUNET_SCHEDULER_shutdown ();
  88. }
  89. static void
  90. start_test (void *cls)
  91. {
  92. }
  93. static void
  94. pinfo_cb (void *cls,
  95. struct GNUNET_TESTBED_Operation *operation,
  96. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  97. const char *emsg)
  98. {
  99. struct pctx *pc = (struct pctx*) cls;
  100. pc->id = pinfo->result.id;
  101. pids++;
  102. if (pids < (PEERS_REQUESTED - 1))
  103. return;
  104. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
  105. test_task = GNUNET_SCHEDULER_add_now (&start_test, NULL);
  106. }
  107. static void
  108. mst_start_cb ()
  109. {
  110. }
  111. static void
  112. join_request_cb ()
  113. {
  114. }
  115. static void
  116. mst_message_cb ()
  117. {
  118. }
  119. static void
  120. mst_message_part_cb ()
  121. {
  122. }
  123. static void
  124. slv_message_cb ()
  125. {
  126. }
  127. static void
  128. slv_message_part_cb ()
  129. {
  130. }
  131. static void
  132. slv_connect_cb ()
  133. {
  134. }
  135. static void
  136. join_decision_cb ()
  137. {
  138. }
  139. static void *
  140. psyc_ca (void *cls,
  141. const struct GNUNET_CONFIGURATION_Handle *cfg)
  142. {
  143. struct GNUNET_PSYC_Message *join_msg = NULL;
  144. struct pctx *pc = (struct pctx *) cls;
  145. if (0 == pc->idx)
  146. {
  147. pc->mst = GNUNET_PSYC_master_start (cfg, channel_key,
  148. GNUNET_PSYC_CHANNEL_PRIVATE,
  149. &mst_start_cb, &join_request_cb,
  150. &mst_message_cb, &mst_message_part_cb,
  151. NULL);
  152. return pc->mst;
  153. }
  154. pc->slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key,
  155. GNUNET_PSYC_SLAVE_JOIN_NONE,
  156. &pid, 0, NULL, &slv_message_cb,
  157. &slv_message_part_cb,
  158. &slv_connect_cb, &join_decision_cb,
  159. NULL, join_msg);
  160. return pc->slv;
  161. }
  162. static void
  163. psyc_da (void *cls,
  164. void *op_result)
  165. {
  166. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnected from service.\n");
  167. }
  168. static void
  169. service_connect (void *cls,
  170. struct GNUNET_TESTBED_Operation *op,
  171. void *ca_result,
  172. const char *emsg)
  173. {
  174. struct pctx *pc = (struct pctx *) cls;
  175. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  176. "Connected to service\n");
  177. GNUNET_assert (NULL != ca_result);
  178. // FIXME: we need a simple service handle to connect to the service, then
  179. // get peer information and AFTER that make PSYC ops. Compare to CADET.
  180. pc->psyc = ca_result;
  181. GNUNET_TESTBED_peer_get_information (pc->peer,
  182. GNUNET_TESTBED_PIT_IDENTITY,
  183. pinfo_cb, pc);
  184. }
  185. static void
  186. testbed_master (void *cls,
  187. struct GNUNET_TESTBED_RunHandle *h,
  188. unsigned int num_peers,
  189. struct GNUNET_TESTBED_Peer **p,
  190. unsigned int links_succeeded,
  191. unsigned int links_failed)
  192. {
  193. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to testbed_master()\n");
  194. // Create ctx for peers
  195. pctx = GNUNET_new_array (PEERS_REQUESTED, struct pctx*);
  196. for (int i = 0; i<PEERS_REQUESTED; i++)
  197. {
  198. pctx[i] = GNUNET_new (struct pctx);
  199. pctx[i]->idx = i;
  200. pctx[i]->peer = p[i];
  201. pctx[i]->id = NULL;
  202. pctx[i]->mst = NULL;
  203. pctx[i]->op = NULL;
  204. pctx[i]->test_ok = GNUNET_NO;
  205. }
  206. channel_key = GNUNET_CRYPTO_eddsa_key_create ();
  207. slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
  208. GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
  209. GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
  210. pctx[0]->op =
  211. GNUNET_TESTBED_service_connect (NULL, p[0], "psyc", service_connect,
  212. pctx[0], psyc_ca, psyc_da, pctx[0]);
  213. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  214. timeout_tid =
  215. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
  216. &timeout_task, NULL);
  217. }
  218. int
  219. main (int argc, char *argv[])
  220. {
  221. int ret;
  222. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test\n");
  223. result = GNUNET_SYSERR;
  224. ret = GNUNET_TESTBED_test_run ("test-psyc2", "test_psyc.conf",
  225. PEERS_REQUESTED, 0LL, NULL, NULL,
  226. testbed_master, NULL);
  227. if ((GNUNET_OK != ret) || (GNUNET_OK != result))
  228. return 1;
  229. return 0;
  230. }
  231. /* end of test-psyc2.c */