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.
 
 
 
 

283 lines
7.9 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_psyc_api_join.c
  21. * @brief Testbed test for the PSYC API.
  22. * @author xrs
  23. */
  24. /**
  25. * Lessons Learned:
  26. * - define topology in config
  27. * - psyc slave join needs part to end (same with master)
  28. * - GNUNET_SCHEDULER_add_delayed return value will outdate at call time
  29. * - main can not contain GNUNET_log()
  30. */
  31. #include "platform.h"
  32. #include "gnunet_crypto_lib.h"
  33. #include "gnunet_common.h"
  34. #include "gnunet_util_lib.h"
  35. #include "gnunet_testbed_service.h"
  36. #include "gnunet_psyc_util_lib.h"
  37. #include "gnunet_psyc_service.h"
  38. #include "psyc_test_lib.h"
  39. static struct pctx PEERS[2];
  40. static int pids;
  41. static void
  42. shutdown_task (void *cls)
  43. {
  44. if (NULL != timeout_task_id) {
  45. GNUNET_SCHEDULER_cancel (timeout_task_id);
  46. timeout_task_id = NULL;
  47. }
  48. for (int i=0;i<2;i++) {
  49. GNUNET_free (PEERS[i].channel_pub_key);
  50. if (NULL != PEERS[i].psyc)
  51. {
  52. if (0 == i)
  53. GNUNET_PSYC_master_stop (PEERS[i].psyc, GNUNET_NO, NULL, NULL);
  54. else
  55. GNUNET_PSYC_slave_part (PEERS[i].psyc, GNUNET_NO, NULL, NULL);
  56. }
  57. }
  58. for (int i=0;i<MAX_TESTBED_OPS;i++)
  59. if (NULL != op[i])
  60. GNUNET_TESTBED_operation_done (op[i]);
  61. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shut down!\n");
  62. }
  63. static void
  64. timeout_task (void *cls)
  65. {
  66. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Timeout!\n");
  67. timeout_task_id = NULL;
  68. result = GNUNET_SYSERR;
  69. GNUNET_SCHEDULER_shutdown ();
  70. }
  71. static void
  72. join_decision_cb (void *cls,
  73. const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
  74. int is_admitted,
  75. const struct GNUNET_PSYC_Message *join_msg)
  76. {
  77. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  78. "slave: got join decision: %s\n",
  79. (GNUNET_YES == is_admitted) ? "admitted":"rejected");
  80. result = (GNUNET_YES == is_admitted) ? GNUNET_OK : GNUNET_SYSERR;
  81. GNUNET_SCHEDULER_shutdown ();
  82. }
  83. static void
  84. join_request_cb (void *cls,
  85. const struct GNUNET_PSYC_JoinRequestMessage *req,
  86. const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
  87. const struct GNUNET_PSYC_Message *join_msg,
  88. struct GNUNET_PSYC_JoinHandle *jh)
  89. {
  90. struct GNUNET_HashCode slave_key_hash;
  91. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "master: got join request.\n");
  92. GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
  93. GNUNET_PSYC_join_decision (jh, GNUNET_YES, 0, NULL, NULL);
  94. }
  95. static void
  96. psyc_da ()
  97. {
  98. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "disconnect form PSYC service\n");
  99. }
  100. static void *
  101. psyc_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
  102. {
  103. struct pctx *peer = (struct pctx*) cls;
  104. // Case: master role
  105. if (0 == peer->idx) {
  106. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to PSYC as master ...\n");
  107. peer->psyc = (struct GNUNET_PSYC_Master *)
  108. GNUNET_PSYC_master_start (cfg,
  109. peer->channel_key,
  110. GNUNET_PSYC_CHANNEL_PRIVATE,
  111. NULL,
  112. join_request_cb,
  113. NULL,
  114. NULL,
  115. cls);
  116. return peer->psyc;
  117. }
  118. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to PSYC as slave ...\n");
  119. struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
  120. GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN, "_foo", "bar baz", 7);
  121. GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN, "_foo_bar", "foo bar baz", 11);
  122. struct GNUNET_PSYC_Message *
  123. join_msg = GNUNET_PSYC_message_create ("_request_join", env, "some data", 40);
  124. peer->psyc = (struct GNUNET_PSYC_Slave *)
  125. GNUNET_PSYC_slave_join (cfg,
  126. peer->channel_pub_key,
  127. peer->id_key,
  128. GNUNET_PSYC_SLAVE_JOIN_NONE,
  129. peer->peer_id_master,
  130. 0,
  131. NULL,
  132. NULL,
  133. NULL,
  134. NULL,
  135. join_decision_cb,
  136. cls,
  137. join_msg);
  138. GNUNET_free (join_msg);
  139. peer->channel = GNUNET_PSYC_slave_get_channel (peer->psyc);
  140. GNUNET_PSYC_env_destroy (env);
  141. return peer->psyc;
  142. }
  143. static void
  144. service_connect (void *cls,
  145. struct GNUNET_TESTBED_Operation *op,
  146. void *ca_result,
  147. const char *emsg)
  148. {
  149. GNUNET_assert (NULL != ca_result);
  150. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to the service\n");
  151. }
  152. static void
  153. connect_to_services (void *cls)
  154. {
  155. for (int i = 0; i < 2; i++)
  156. {
  157. PEERS[i].peer_id_master = PEERS[0].peer_id;
  158. op[op_cnt++] =
  159. GNUNET_TESTBED_service_connect (NULL, PEERS[i].testbed_peer, "psyc",
  160. &service_connect, &PEERS[i], &psyc_ca,
  161. &psyc_da, &PEERS[i]);
  162. }
  163. }
  164. static void
  165. pinfo_cb (void *cls,
  166. struct GNUNET_TESTBED_Operation *operation,
  167. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  168. const char *emsg)
  169. {
  170. struct pctx *peer = (struct pctx*) cls;
  171. peer->peer_id = pinfo->result.id;
  172. pids++;
  173. if (pids < 2)
  174. return;
  175. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting test\n");
  176. GNUNET_SCHEDULER_add_now (&connect_to_services, NULL);
  177. }
  178. static void
  179. testbed_master (void *cls,
  180. struct GNUNET_TESTBED_RunHandle *h,
  181. unsigned int num_peers,
  182. struct GNUNET_TESTBED_Peer **p,
  183. unsigned int links_succeeded,
  184. unsigned int links_failed)
  185. {
  186. struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key = NULL;
  187. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to testbed_master\n");
  188. // Set up shutdown logic
  189. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  190. timeout_task_id =
  191. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15),
  192. &timeout_task, NULL);
  193. GNUNET_assert (NULL != timeout_task_id);
  194. // Set up channel key
  195. channel_key = GNUNET_CRYPTO_eddsa_key_create ();
  196. GNUNET_assert (NULL != channel_key);
  197. // Set up information contexts for peers
  198. for (int i=0 ; i < 2 ; i++)
  199. {
  200. PEERS[i].idx = i;
  201. PEERS[i].testbed_peer = p[i];
  202. // Create "egos"
  203. PEERS[i].id_key = GNUNET_CRYPTO_ecdsa_key_create ();
  204. // Set up channel keys shared by master and slave
  205. PEERS[i].channel_key = channel_key;
  206. PEERS[i].channel_pub_key =
  207. GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
  208. // Get public key
  209. GNUNET_CRYPTO_eddsa_key_get_public (PEERS[i].channel_key,
  210. PEERS[i].channel_pub_key);
  211. // Get peerinfo
  212. op[op_cnt++] =
  213. GNUNET_TESTBED_peer_get_information (p[i],
  214. GNUNET_TESTBED_PIT_IDENTITY,
  215. pinfo_cb, &PEERS[i]);
  216. }
  217. }
  218. int
  219. main (int argc, char *argv[])
  220. {
  221. int ret;
  222. ret = GNUNET_TESTBED_test_run ("test_psyc_api_join", "test_psyc.conf",
  223. 2, 0LL, NULL, NULL,
  224. &testbed_master, NULL);
  225. if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
  226. return 1;
  227. return 0;
  228. }
  229. /* end of test_psyc_api_join.c */