test_conversation_api_reject.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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", NULL, &caller_ego_create_cont,
  210. NULL);
  211. }
  212. static void
  213. identity_cb (void *cls,
  214. struct GNUNET_IDENTITY_Ego *ego,
  215. void **ctx,
  216. const char *name)
  217. {
  218. struct GNUNET_GNSRECORD_Data rd;
  219. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  220. (void) cls;
  221. (void) ctx;
  222. if (NULL == name)
  223. return;
  224. if (NULL == ego)
  225. return;
  226. if (0 == strcmp (name, "phone-ego"))
  227. {
  228. GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
  229. GNUNET_asprintf (&gns_name,
  230. "phone.%s",
  231. GNUNET_GNSRECORD_pkey_to_zkey (&pub));
  232. phone =
  233. GNUNET_CONVERSATION_phone_create (cfg, ego, &phone_event_handler, NULL);
  234. GNUNET_assert (NULL != phone);
  235. memset (&rd, 0, sizeof(rd));
  236. GNUNET_CONVERSATION_phone_get_record (phone, &rd);
  237. GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
  238. rd.expiration_time = UINT64_MAX;
  239. qe =
  240. GNUNET_NAMESTORE_records_store (ns,
  241. GNUNET_IDENTITY_ego_get_private_key (ego),
  242. "phone" /* GNS label */,
  243. 1,
  244. &rd,
  245. &namestore_put_cont,
  246. NULL);
  247. return;
  248. }
  249. if (0 == strcmp (name, "caller-ego"))
  250. {
  251. GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
  252. GNUNET_asprintf (&gns_caller_id,
  253. "%s",
  254. GNUNET_GNSRECORD_pkey_to_zkey (&pub));
  255. call = GNUNET_CONVERSATION_call_start (cfg,
  256. ego,
  257. gns_name,
  258. &call_speaker,
  259. &call_mic,
  260. &call_event_handler,
  261. NULL);
  262. return;
  263. }
  264. }
  265. static void
  266. phone_ego_create_cont (void *cls,
  267. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
  268. const char *emsg)
  269. {
  270. (void) cls;
  271. op = NULL;
  272. GNUNET_assert (NULL == emsg);
  273. }
  274. static void
  275. run (void *cls,
  276. const struct GNUNET_CONFIGURATION_Handle *c,
  277. struct GNUNET_TESTING_Peer *peer)
  278. {
  279. (void) cls;
  280. (void) peer;
  281. cfg = c;
  282. GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
  283. id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
  284. op = GNUNET_IDENTITY_create (id, "phone-ego", NULL, &phone_ego_create_cont,
  285. NULL);
  286. ns = GNUNET_NAMESTORE_connect (cfg);
  287. }
  288. int
  289. main (int argc, char *argv[])
  290. {
  291. (void) argc;
  292. (void) argv;
  293. if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
  294. "test_conversation.conf",
  295. &run,
  296. NULL))
  297. return 1;
  298. return ok;
  299. }
  300. /* end of test_conversation_api_reject.c */