test_conversation_api_reject.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013, 2014, 2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file conversation/test_conversation_api_reject.c
  18. * @brief testcase for conversation_api.c
  19. *
  20. * This test performs the operations of a call to a phone
  21. * where the phone user immediately hangs up (rejecting the
  22. * call).
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_testing_lib.h"
  27. #include "gnunet_gnsrecord_lib.h"
  28. #include "gnunet_conversation_service.h"
  29. #include "gnunet_identity_service.h"
  30. #include "gnunet_namestore_service.h"
  31. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
  32. static int ok = 1;
  33. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  34. static struct GNUNET_IDENTITY_Handle *id;
  35. static struct GNUNET_IDENTITY_Operation *op;
  36. static struct GNUNET_CONVERSATION_Phone *phone;
  37. static struct GNUNET_NAMESTORE_Handle *ns;
  38. static struct GNUNET_CONVERSATION_Call *call;
  39. static struct GNUNET_NAMESTORE_QueueEntry *qe;
  40. static char *gns_name;
  41. static char *gns_caller_id;
  42. static int
  43. enable_speaker (void *cls)
  44. {
  45. (void) cls;
  46. GNUNET_break (0);
  47. return GNUNET_SYSERR;
  48. }
  49. static void
  50. disable_speaker (void *cls)
  51. {
  52. (void) cls;
  53. GNUNET_break (0);
  54. }
  55. static void
  56. play (void *cls, size_t data_size, const void *data)
  57. {
  58. (void) cls;
  59. (void) data_size;
  60. (void) data;
  61. GNUNET_break (0);
  62. }
  63. static void
  64. destroy_speaker (void *cls)
  65. {
  66. (void) cls;
  67. }
  68. static struct GNUNET_SPEAKER_Handle call_speaker = { &enable_speaker,
  69. &play,
  70. &disable_speaker,
  71. &destroy_speaker,
  72. "caller" };
  73. static int
  74. enable_mic (void *cls,
  75. GNUNET_MICROPHONE_RecordedDataCallback rdc,
  76. void *rdc_cls)
  77. {
  78. (void) cls;
  79. (void) rdc;
  80. (void) rdc_cls;
  81. GNUNET_break (0);
  82. return GNUNET_SYSERR;
  83. }
  84. static void
  85. disable_mic (void *cls)
  86. {
  87. (void) cls;
  88. GNUNET_break (0);
  89. }
  90. static void
  91. destroy_mic (void *cls)
  92. {
  93. (void) cls;
  94. }
  95. static struct GNUNET_MICROPHONE_Handle call_mic = { &enable_mic,
  96. &disable_mic,
  97. &destroy_mic,
  98. "caller" };
  99. /**
  100. * Signature of the main function of a task.
  101. *
  102. * @param cls closure
  103. */
  104. static void
  105. end_test (void *cls)
  106. {
  107. (void) cls;
  108. GNUNET_SCHEDULER_shutdown ();
  109. if (NULL != op)
  110. {
  111. GNUNET_IDENTITY_cancel (op);
  112. op = NULL;
  113. }
  114. if (NULL != call)
  115. {
  116. GNUNET_CONVERSATION_call_stop (call);
  117. call = NULL;
  118. }
  119. if (NULL != phone)
  120. {
  121. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
  122. GNUNET_CONVERSATION_phone_destroy (phone);
  123. phone = NULL;
  124. }
  125. if (NULL != id)
  126. {
  127. GNUNET_IDENTITY_disconnect (id);
  128. id = NULL;
  129. }
  130. if (NULL != qe)
  131. {
  132. GNUNET_NAMESTORE_cancel (qe);
  133. qe = NULL;
  134. }
  135. if (NULL != ns)
  136. {
  137. GNUNET_NAMESTORE_disconnect (ns);
  138. ns = NULL;
  139. }
  140. }
  141. static void
  142. phone_event_handler (void *cls,
  143. enum GNUNET_CONVERSATION_PhoneEventCode code,
  144. struct GNUNET_CONVERSATION_Caller *caller,
  145. const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
  146. {
  147. static enum GNUNET_CONVERSATION_PhoneEventCode expect =
  148. GNUNET_CONVERSATION_EC_PHONE_RING;
  149. (void) cls;
  150. (void) caller_id;
  151. GNUNET_break (code == expect);
  152. switch (code)
  153. {
  154. case GNUNET_CONVERSATION_EC_PHONE_RING:
  155. GNUNET_CONVERSATION_caller_hang_up (caller);
  156. break;
  157. default:
  158. fprintf (stderr, "Unexpected phone code: %d\n", code);
  159. break;
  160. }
  161. }
  162. static void
  163. call_event_handler (void *cls, enum GNUNET_CONVERSATION_CallEventCode code)
  164. {
  165. static enum GNUNET_CONVERSATION_CallEventCode expect =
  166. GNUNET_CONVERSATION_EC_CALL_RINGING;
  167. (void) cls;
  168. GNUNET_break (code == expect);
  169. switch (code)
  170. {
  171. case GNUNET_CONVERSATION_EC_CALL_RINGING:
  172. expect = GNUNET_CONVERSATION_EC_CALL_HUNG_UP;
  173. break;
  174. case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
  175. call = NULL;
  176. ok = 0;
  177. GNUNET_SCHEDULER_shutdown ();
  178. expect = -1;
  179. break;
  180. case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
  181. case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
  182. case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
  183. case GNUNET_CONVERSATION_EC_CALL_RESUMED:
  184. fprintf (stderr, "Unexpected call code: %d\n", code);
  185. break;
  186. case GNUNET_CONVERSATION_EC_CALL_ERROR:
  187. fprintf (stderr, "Unexpected call code: %d\n", code);
  188. call = NULL;
  189. break;
  190. }
  191. }
  192. static void
  193. caller_ego_create_cont (void *cls,
  194. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
  195. const char *emsg)
  196. {
  197. (void) cls;
  198. op = NULL;
  199. GNUNET_assert (NULL == emsg);
  200. }
  201. static void
  202. namestore_put_cont (void *cls, int32_t success, const char *emsg)
  203. {
  204. (void) cls;
  205. qe = NULL;
  206. GNUNET_assert (GNUNET_YES == success);
  207. GNUNET_assert (NULL == emsg);
  208. GNUNET_assert (NULL == op);
  209. op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
  210. }
  211. static void
  212. identity_cb (void *cls,
  213. struct GNUNET_IDENTITY_Ego *ego,
  214. void **ctx,
  215. const char *name)
  216. {
  217. struct GNUNET_GNSRECORD_Data rd;
  218. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  219. (void) cls;
  220. (void) ctx;
  221. if (NULL == name)
  222. return;
  223. if (NULL == ego)
  224. return;
  225. if (0 == strcmp (name, "phone-ego"))
  226. {
  227. GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
  228. GNUNET_asprintf (&gns_name,
  229. "phone.%s",
  230. GNUNET_GNSRECORD_pkey_to_zkey (&pub));
  231. phone =
  232. GNUNET_CONVERSATION_phone_create (cfg, ego, &phone_event_handler, NULL);
  233. GNUNET_assert (NULL != phone);
  234. memset (&rd, 0, sizeof(rd));
  235. GNUNET_CONVERSATION_phone_get_record (phone, &rd);
  236. GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
  237. rd.expiration_time = UINT64_MAX;
  238. qe =
  239. GNUNET_NAMESTORE_records_store (ns,
  240. GNUNET_IDENTITY_ego_get_private_key (ego),
  241. "phone" /* GNS label */,
  242. 1,
  243. &rd,
  244. &namestore_put_cont,
  245. NULL);
  246. return;
  247. }
  248. if (0 == strcmp (name, "caller-ego"))
  249. {
  250. GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
  251. GNUNET_asprintf (&gns_caller_id,
  252. "%s",
  253. GNUNET_GNSRECORD_pkey_to_zkey (&pub));
  254. call = GNUNET_CONVERSATION_call_start (cfg,
  255. ego,
  256. gns_name,
  257. &call_speaker,
  258. &call_mic,
  259. &call_event_handler,
  260. NULL);
  261. return;
  262. }
  263. }
  264. static void
  265. phone_ego_create_cont (void *cls,
  266. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
  267. const char *emsg)
  268. {
  269. (void) cls;
  270. op = NULL;
  271. GNUNET_assert (NULL == emsg);
  272. }
  273. static void
  274. run (void *cls,
  275. const struct GNUNET_CONFIGURATION_Handle *c,
  276. struct GNUNET_TESTING_Peer *peer)
  277. {
  278. (void) cls;
  279. (void) peer;
  280. cfg = c;
  281. GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
  282. id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
  283. op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
  284. ns = GNUNET_NAMESTORE_connect (cfg);
  285. }
  286. int
  287. main (int argc, char *argv[])
  288. {
  289. (void) argc;
  290. (void) argv;
  291. if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
  292. "test_conversation.conf",
  293. &run,
  294. NULL))
  295. return 1;
  296. return ok;
  297. }
  298. /* end of test_conversation_api_reject.c */