test_conversation_api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2013, 2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file conversation/test_conversation_api.c
  19. * @brief testcase for conversation_api.c
  20. *
  21. * This test performs the operations of a call to a phone
  22. * where the phone user picks up and then the call is
  23. * terminated by the party that initiated the call.
  24. */
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_testing_lib.h"
  28. #include "gnunet_gnsrecord_lib.h"
  29. #include "gnunet_conversation_service.h"
  30. #include "gnunet_identity_service.h"
  31. #include "gnunet_namestore_service.h"
  32. #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
  33. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
  34. static int ok = 1;
  35. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  36. static struct GNUNET_IDENTITY_Handle *id;
  37. static struct GNUNET_IDENTITY_Operation *op;
  38. static struct GNUNET_CONVERSATION_Phone *phone;
  39. static struct GNUNET_NAMESTORE_Handle *ns;
  40. static struct GNUNET_CONVERSATION_Call *call;
  41. static struct GNUNET_NAMESTORE_QueueEntry *qe;
  42. static struct GNUNET_CONVERSATION_Caller *active_caller;
  43. static char *gns_name;
  44. static char *gns_caller_id;
  45. static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
  46. static void *phone_rdc_cls;
  47. static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
  48. static void *call_rdc_cls;
  49. static GNUNET_SCHEDULER_TaskIdentifier phone_task;
  50. static GNUNET_SCHEDULER_TaskIdentifier call_task;
  51. static void
  52. phone_send (void *cls,
  53. const struct GNUNET_SCHEDULER_TaskContext *tc)
  54. {
  55. static unsigned int i;
  56. char buf[32];
  57. GNUNET_assert (NULL != phone_rdc);
  58. GNUNET_snprintf (buf, sizeof (buf), "phone-%u", i++);
  59. phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
  60. phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
  61. &phone_send, NULL);
  62. }
  63. static void
  64. call_send (void *cls,
  65. const struct GNUNET_SCHEDULER_TaskContext *tc)
  66. {
  67. static unsigned int i;
  68. char buf[32];
  69. GNUNET_assert (NULL != call_rdc);
  70. GNUNET_snprintf (buf, sizeof (buf), "call-%u", i++);
  71. call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
  72. call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
  73. &call_send, NULL);
  74. }
  75. static int
  76. enable_speaker (void *cls)
  77. {
  78. const char *origin = cls;
  79. fprintf (stderr,
  80. "Speaker %s enabled\n",
  81. origin);
  82. return GNUNET_OK;
  83. }
  84. static void
  85. disable_speaker (void *cls)
  86. {
  87. const char *origin = cls;
  88. fprintf (stderr,
  89. "Speaker %s disabled\n",
  90. origin);
  91. }
  92. static void
  93. play (void *cls,
  94. size_t data_size,
  95. const void *data)
  96. {
  97. const char *origin = cls;
  98. static unsigned int phone_i = 1;
  99. static unsigned int call_i;
  100. char buf[32];
  101. if (0 == strcmp (origin, "phone"))
  102. GNUNET_snprintf (buf, sizeof (buf), "call-%u", call_i++);
  103. else
  104. GNUNET_snprintf (buf, sizeof (buf), "phone-%u", phone_i++);
  105. if ( (data_size != strlen (buf) + 1) ||
  106. (0 != strncmp (buf, data, data_size)) )
  107. {
  108. fprintf (stderr,
  109. "Expected %s, received %.*s\n",
  110. buf,
  111. (int) data_size,
  112. (const char *) data);
  113. }
  114. else
  115. {
  116. fprintf (stderr, ".");
  117. }
  118. if ( (20 < call_i) &&
  119. (20 < phone_i) &&
  120. (NULL != call) )
  121. {
  122. /* time to hang up ... */
  123. GNUNET_CONVERSATION_call_stop (call);
  124. call = NULL;
  125. }
  126. }
  127. static void
  128. destroy_speaker (void *cls)
  129. {
  130. const char *origin = cls;
  131. fprintf (stderr, "Speaker %s destroyed\n", origin);
  132. }
  133. static struct GNUNET_SPEAKER_Handle call_speaker = {
  134. &enable_speaker,
  135. &play,
  136. &disable_speaker,
  137. &destroy_speaker,
  138. "caller"
  139. };
  140. static struct GNUNET_SPEAKER_Handle phone_speaker = {
  141. &enable_speaker,
  142. &play,
  143. &disable_speaker,
  144. &destroy_speaker,
  145. "phone"
  146. };
  147. static int
  148. enable_mic (void *cls,
  149. GNUNET_MICROPHONE_RecordedDataCallback rdc,
  150. void *rdc_cls)
  151. {
  152. const char *origin = cls;
  153. fprintf (stderr,
  154. "Mic %s enabled\n",
  155. origin);
  156. if (0 == strcmp (origin, "phone"))
  157. {
  158. phone_rdc = rdc;
  159. phone_rdc_cls = rdc_cls;
  160. phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
  161. }
  162. else
  163. {
  164. call_rdc = rdc;
  165. call_rdc_cls = rdc_cls;
  166. call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
  167. }
  168. return GNUNET_OK;
  169. }
  170. static void
  171. disable_mic (void *cls)
  172. {
  173. const char *origin = cls;
  174. fprintf (stderr,
  175. "Mic %s disabled\n",
  176. origin);
  177. if (0 == strcmp (origin, "phone"))
  178. {
  179. phone_rdc = NULL;
  180. phone_rdc_cls = NULL;
  181. GNUNET_SCHEDULER_cancel (phone_task);
  182. phone_task = GNUNET_SCHEDULER_NO_TASK;
  183. }
  184. else
  185. {
  186. call_rdc = NULL;
  187. call_rdc_cls = NULL;
  188. GNUNET_SCHEDULER_cancel (call_task);
  189. call_task = GNUNET_SCHEDULER_NO_TASK;
  190. }
  191. }
  192. static void
  193. destroy_mic (void *cls)
  194. {
  195. const char *origin = cls;
  196. fprintf (stderr,
  197. "Mic %s destroyed\n",
  198. origin);
  199. }
  200. static struct GNUNET_MICROPHONE_Handle call_mic = {
  201. &enable_mic,
  202. &disable_mic,
  203. &destroy_mic,
  204. "caller"
  205. };
  206. static struct GNUNET_MICROPHONE_Handle phone_mic = {
  207. &enable_mic,
  208. &disable_mic,
  209. &destroy_mic,
  210. "phone"
  211. };
  212. /**
  213. * Signature of the main function of a task.
  214. *
  215. * @param cls closure
  216. * @param tc context information (why was this task triggered now)
  217. */
  218. static void
  219. end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  220. {
  221. GNUNET_SCHEDULER_shutdown ();
  222. if (NULL != op)
  223. {
  224. GNUNET_IDENTITY_cancel (op);
  225. op = NULL;
  226. }
  227. if (NULL != call)
  228. {
  229. GNUNET_CONVERSATION_call_stop (call);
  230. call = NULL;
  231. }
  232. if (NULL != phone)
  233. {
  234. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
  235. GNUNET_CONVERSATION_phone_destroy (phone);
  236. phone = NULL;
  237. }
  238. if (NULL != id)
  239. {
  240. GNUNET_IDENTITY_disconnect (id);
  241. id = NULL;
  242. }
  243. if (NULL != qe)
  244. {
  245. GNUNET_NAMESTORE_cancel (qe);
  246. qe = NULL;
  247. }
  248. if (NULL != ns)
  249. {
  250. GNUNET_NAMESTORE_disconnect (ns);
  251. ns = NULL;
  252. }
  253. }
  254. static void
  255. caller_event_handler (void *cls,
  256. enum GNUNET_CONVERSATION_CallerEventCode code)
  257. {
  258. switch (code)
  259. {
  260. case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
  261. case GNUNET_CONVERSATION_EC_CALLER_RESUME:
  262. fprintf (stderr, "Unexpected caller code: %d\n", code);
  263. break;
  264. }
  265. }
  266. static void
  267. phone_event_handler (void *cls,
  268. enum GNUNET_CONVERSATION_PhoneEventCode code,
  269. struct GNUNET_CONVERSATION_Caller *caller,
  270. const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
  271. {
  272. static enum GNUNET_CONVERSATION_PhoneEventCode expect
  273. = GNUNET_CONVERSATION_EC_PHONE_RING;
  274. GNUNET_break (code == expect);
  275. switch (code)
  276. {
  277. case GNUNET_CONVERSATION_EC_PHONE_RING:
  278. active_caller = caller;
  279. GNUNET_CONVERSATION_caller_pick_up (caller,
  280. &caller_event_handler,
  281. NULL,
  282. &phone_speaker,
  283. &phone_mic);
  284. expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
  285. break;
  286. case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
  287. GNUNET_break (caller == active_caller);
  288. active_caller = NULL;
  289. ok = 0;
  290. GNUNET_SCHEDULER_shutdown ();
  291. break;
  292. default:
  293. fprintf (stderr, "Unexpected phone code: %d\n", code);
  294. break;
  295. }
  296. }
  297. static void
  298. call_event_handler (void *cls,
  299. enum GNUNET_CONVERSATION_CallEventCode code)
  300. {
  301. static enum GNUNET_CONVERSATION_CallEventCode expect
  302. = GNUNET_CONVERSATION_EC_CALL_RINGING;
  303. GNUNET_break (code == expect);
  304. switch (code)
  305. {
  306. case GNUNET_CONVERSATION_EC_CALL_RINGING:
  307. expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
  308. break;
  309. case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
  310. expect = -1;
  311. break;
  312. case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
  313. case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
  314. call = NULL;
  315. fprintf (stderr, "Unexpected call code: %d\n", code);
  316. break;
  317. case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
  318. case GNUNET_CONVERSATION_EC_CALL_RESUMED:
  319. case GNUNET_CONVERSATION_EC_CALL_ERROR:
  320. fprintf (stderr, "Unexpected call code: %d\n", code);
  321. break;
  322. }
  323. }
  324. static void
  325. caller_ego_create_cont (void *cls,
  326. const char *emsg)
  327. {
  328. op = NULL;
  329. GNUNET_assert (NULL == emsg);
  330. }
  331. static void
  332. namestore_put_cont (void *cls,
  333. int32_t success,
  334. const char *emsg)
  335. {
  336. qe = NULL;
  337. GNUNET_assert (GNUNET_YES == success);
  338. GNUNET_assert (NULL == emsg);
  339. GNUNET_assert (NULL == op);
  340. op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
  341. }
  342. static void
  343. identity_cb (void *cls,
  344. struct GNUNET_IDENTITY_Ego *ego,
  345. void **ctx,
  346. const char *name)
  347. {
  348. struct GNUNET_GNSRECORD_Data rd;
  349. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  350. if (NULL == name)
  351. return;
  352. if (NULL == ego)
  353. return;
  354. if (0 == strcmp (name, "phone-ego"))
  355. {
  356. GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
  357. GNUNET_asprintf (&gns_name,
  358. "phone.%s",
  359. GNUNET_GNSRECORD_pkey_to_zkey (&pub));
  360. phone = GNUNET_CONVERSATION_phone_create (cfg,
  361. ego,
  362. &phone_event_handler,
  363. NULL);
  364. GNUNET_assert (NULL != phone);
  365. memset (&rd, 0, sizeof (rd));
  366. GNUNET_CONVERSATION_phone_get_record (phone,
  367. &rd);
  368. GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
  369. rd.expiration_time = UINT64_MAX;
  370. qe = GNUNET_NAMESTORE_records_store (ns,
  371. GNUNET_IDENTITY_ego_get_private_key (ego),
  372. "phone" /* GNS label */,
  373. 1,
  374. &rd,
  375. &namestore_put_cont,
  376. NULL);
  377. return;
  378. }
  379. if (0 == strcmp (name, "caller-ego"))
  380. {
  381. GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
  382. GNUNET_asprintf (&gns_caller_id,
  383. "%s",
  384. GNUNET_GNSRECORD_pkey_to_zkey (&pub));
  385. call = GNUNET_CONVERSATION_call_start (cfg,
  386. ego,
  387. ego,
  388. gns_name,
  389. &call_speaker,
  390. &call_mic,
  391. &call_event_handler,
  392. NULL);
  393. return;
  394. }
  395. }
  396. static void
  397. phone_ego_create_cont (void *cls,
  398. const char *emsg)
  399. {
  400. op = NULL;
  401. GNUNET_assert (NULL == emsg);
  402. }
  403. static void
  404. run (void *cls,
  405. const struct GNUNET_CONFIGURATION_Handle *c,
  406. struct GNUNET_TESTING_Peer *peer)
  407. {
  408. cfg = c;
  409. GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
  410. NULL);
  411. id = GNUNET_IDENTITY_connect (cfg,
  412. &identity_cb,
  413. NULL);
  414. op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
  415. ns = GNUNET_NAMESTORE_connect (cfg);
  416. }
  417. int
  418. main (int argc, char *argv[])
  419. {
  420. if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
  421. "test_conversation.conf",
  422. &run, NULL))
  423. return 1;
  424. return ok;
  425. }
  426. /* end of test_conversation_api.c */