sasrelay_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Viktor Krykun <v.krikun at zfoneproject.com>
  8. */
  9. #include <setjmp.h> /*chmockery dependency*/
  10. #include <stdio.h> /*chmockery dependency*/
  11. #include <unistd.h> /*for usleep*/
  12. #include "cmockery/cmockery.h"
  13. #include "test_engine.h"
  14. #include "enroll_test_helpers.c"
  15. static void enrollment_test() {
  16. zrtp_status_t s;
  17. zrtp_test_channel_info_t a2pbx_channel_info, b2pbx_channel_info;
  18. zrtp_test_session_cfg_t session_config, session_config_enroll;
  19. zrtp_test_session_config_defaults(&session_config);
  20. zrtp_test_session_config_defaults(&session_config_enroll);
  21. session_config_enroll.is_enrollment = 1;
  22. /**************************************************************************
  23. * Enroll both Alice and Bob to PBX
  24. */
  25. prepare_alice_pbx_bob_setup(&session_config, &session_config, &session_config_enroll, &session_config_enroll);
  26. /* Everything is ready. Let's start the stream and give it few seconds to switch secure. */
  27. s = zrtp_test_channel_start(g_alice2pbx_channel);
  28. assert_int_equal(zrtp_status_ok, s);
  29. s = zrtp_test_channel_start(g_bob2pbx_channel);
  30. assert_int_equal(zrtp_status_ok, s);
  31. int i = 30;
  32. for (; i>0; i--) {
  33. usleep(100*1000);
  34. }
  35. s = zrtp_test_channel_get(g_alice2pbx_channel, &a2pbx_channel_info);
  36. assert_int_equal(zrtp_status_ok, s);
  37. s = zrtp_test_channel_get(g_bob2pbx_channel, &b2pbx_channel_info);
  38. assert_int_equal(zrtp_status_ok, s);
  39. /* Both, Alice and Bob should switch secure and ready for enrollment */
  40. assert_true(a2pbx_channel_info.is_secure);
  41. assert_true(b2pbx_channel_info.is_secure);
  42. /* Confirm enrollment for both, Alice and Bob */
  43. zrtp_test_id_t alice2pbx_stream = zrtp_test_session_get_stream_by_idx(g_alice_sid, 0);
  44. zrtp_test_id_t bob2pbx_stream = zrtp_test_session_get_stream_by_idx(g_bob_sid, 0);
  45. s = zrtp_register_with_trusted_mitm(zrtp_stream_for_test_stream(alice2pbx_stream));
  46. assert_int_equal(zrtp_status_ok, s);
  47. s = zrtp_register_with_trusted_mitm(zrtp_stream_for_test_stream(bob2pbx_stream));
  48. assert_int_equal(zrtp_status_ok, s);
  49. /* Clean-up */
  50. cleanup_alice_pbx_bob_setup();
  51. /**************************************************************************
  52. * Now, when we have two enrolled parties, make one more call and initiate
  53. * SAS Relay at the PBX side. Both endpoints should received SASRelay, but
  54. * just one should get ZRTP_EVENT_LOCAL_SAS_UPDATED event.
  55. */
  56. prepare_alice_pbx_bob_setup(&session_config, &session_config, &session_config, &session_config);
  57. /* Everything is ready. Let's start the stream and give it few seconds to switch secure. */
  58. s = zrtp_test_channel_start(g_alice2pbx_channel);
  59. assert_int_equal(zrtp_status_ok, s);
  60. s = zrtp_test_channel_start(g_bob2pbx_channel);
  61. assert_int_equal(zrtp_status_ok, s);
  62. i = 30;
  63. for (; i>0; i--) {
  64. usleep(100*1000);
  65. }
  66. s = zrtp_test_channel_get(g_alice2pbx_channel, &a2pbx_channel_info);
  67. assert_int_equal(zrtp_status_ok, s);
  68. s = zrtp_test_channel_get(g_bob2pbx_channel, &b2pbx_channel_info);
  69. assert_int_equal(zrtp_status_ok, s);
  70. /* Both, Alice and Bob should switch secure */
  71. assert_true(a2pbx_channel_info.is_secure);
  72. assert_true(b2pbx_channel_info.is_secure);
  73. zrtp_test_id_t pbx2alice_stream = zrtp_test_session_get_stream_by_idx(g_pbxa_sid, 0);
  74. zrtp_test_id_t pbx2bob_stream = zrtp_test_session_get_stream_by_idx(g_pbxb_sid, 0);
  75. alice2pbx_stream = zrtp_test_session_get_stream_by_idx(g_alice_sid, 0);
  76. bob2pbx_stream = zrtp_test_session_get_stream_by_idx(g_bob_sid, 0);
  77. /* Resolve MiTM call! */
  78. s = zrtp_resolve_mitm_call(zrtp_stream_for_test_stream(pbx2alice_stream),
  79. zrtp_stream_for_test_stream(pbx2bob_stream));
  80. i = 20;
  81. for (; i>0; i--) {
  82. usleep(100*1000);
  83. }
  84. /* Alice and Bob should receive Enrollment notification */
  85. unsigned sas_update1 = zrtp_stream_did_event_receive(alice2pbx_stream, ZRTP_EVENT_LOCAL_SAS_UPDATED);
  86. unsigned sas_update2 = zrtp_stream_did_event_receive(bob2pbx_stream, ZRTP_EVENT_LOCAL_SAS_UPDATED);
  87. assert_true(sas_update1 ^ sas_update2);
  88. /* Clean-up */
  89. cleanup_alice_pbx_bob_setup();
  90. }
  91. int main(void) {
  92. const UnitTest tests[] = {
  93. unit_test_setup_teardown(enrollment_test, pbx_setup, pbx_teardown),
  94. };
  95. return run_tests(tests);
  96. }