2
0

zrtphash_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "engine_helpers.c"
  10. static void setup() {
  11. zrtp_status_t s;
  12. zrtp_test_endpoint_cfg_t endpoint_cfg;
  13. zrtp_test_endpoint_config_defaults(&endpoint_cfg);
  14. s = zrtp_test_endpoint_create(&endpoint_cfg, "Alice", &g_alice);
  15. assert_int_equal(zrtp_status_ok, s);
  16. assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_alice);
  17. s = zrtp_test_endpoint_create(&endpoint_cfg, "Bob", &g_bob);
  18. assert_int_equal(zrtp_status_ok, s);
  19. assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_bob);
  20. }
  21. static void teardown() {
  22. zrtp_test_endpoint_destroy(g_alice);
  23. zrtp_test_endpoint_destroy(g_bob);
  24. }
  25. static void zrtp_hash_export_import_sunny_test() {
  26. zrtp_status_t s;
  27. char alice_zrtp_hash[ZRTP_SIGN_ZRTP_HASH_LENGTH];
  28. char bob_zrtp_hash[ZRTP_SIGN_ZRTP_HASH_LENGTH];
  29. zrtp_stream_t *alice_zrtp_stream, *bob_zrtp_stream;
  30. /* Create two test sessions, one for Alice and one for Bob and link them into test secure channel */
  31. prepare_alice_bob();
  32. alice_zrtp_stream = zrtp_stream_for_test_stream(zrtp_test_session_get_stream_by_idx(g_alice_sid, 0));
  33. bob_zrtp_stream = zrtp_stream_for_test_stream(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0));
  34. assert_non_null(alice_zrtp_stream); assert_non_null(bob_zrtp_stream);
  35. /* Take Alice's hash and give it to Bob */
  36. s = zrtp_signaling_hash_get(alice_zrtp_stream, alice_zrtp_hash, sizeof(alice_zrtp_hash));
  37. assert_int_equal(zrtp_status_ok, s);
  38. s = zrtp_signaling_hash_set(bob_zrtp_stream, alice_zrtp_hash, ZRTP_SIGN_ZRTP_HASH_LENGTH);
  39. assert_int_equal(zrtp_status_ok, s);
  40. /* Take Bob's hash and give it to Alice */
  41. s = zrtp_signaling_hash_get(bob_zrtp_stream, bob_zrtp_hash, sizeof(bob_zrtp_hash));
  42. assert_int_equal(zrtp_status_ok, s);
  43. s = zrtp_signaling_hash_set(alice_zrtp_stream, bob_zrtp_hash, ZRTP_SIGN_ZRTP_HASH_LENGTH);
  44. assert_int_equal(zrtp_status_ok, s);
  45. /* Start and wait for Secure */
  46. start_alice_bob_and_wait4secure();
  47. /* Check if ZRTP_EVENT_WRONG_SIGNALING_HASH was not triggered for any of test endpoints */
  48. assert_false(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_alice_sid, 0),
  49. ZRTP_EVENT_WRONG_SIGNALING_HASH));
  50. assert_false(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0),
  51. ZRTP_EVENT_WRONG_SIGNALING_HASH));
  52. /* Release test setup */
  53. release_alice_bob();
  54. }
  55. static void zrtp_hash_import_wrong_test() {
  56. zrtp_status_t s;
  57. char wrong_alice_zrtp_hash[ZRTP_SIGN_ZRTP_HASH_LENGTH];
  58. zrtp_stream_t *bob_zrtp_stream;
  59. /* Create two test sessions, one for Alice and one for Bob and link them into test secure channel */
  60. prepare_alice_bob();
  61. bob_zrtp_stream = zrtp_stream_for_test_stream(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0));
  62. assert_non_null(bob_zrtp_stream);
  63. /* Let's provide wrong hash to bob */
  64. zrtp_memset(wrong_alice_zrtp_hash, 6, ZRTP_SIGN_ZRTP_HASH_LENGTH);
  65. s = zrtp_signaling_hash_set(bob_zrtp_stream, wrong_alice_zrtp_hash, ZRTP_SIGN_ZRTP_HASH_LENGTH);
  66. assert_int_equal(zrtp_status_ok, s);
  67. /* Start and wait for Secure */
  68. start_alice_bob_and_wait4secure();
  69. /* Check if Alice don't receive ZRTP_EVENT_WRONG_SIGNALING_HASH, but Bob should get one */
  70. assert_false(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_alice_sid, 0),
  71. ZRTP_EVENT_WRONG_SIGNALING_HASH));
  72. assert_true(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0),
  73. ZRTP_EVENT_WRONG_SIGNALING_HASH));
  74. /* Release test setup */
  75. release_alice_bob();
  76. }
  77. int main(void) {
  78. const UnitTest tests[] = {
  79. unit_test_setup_teardown(zrtp_hash_export_import_sunny_test, setup, teardown),
  80. unit_test_setup_teardown(zrtp_hash_import_wrong_test, setup, teardown),
  81. };
  82. return run_tests(tests);
  83. }