From 02b3b8060a456cdedf81cc068b4b0ece37149d88 Mon Sep 17 00:00:00 2001 From: Viktor Krykun Date: Thu, 21 Jun 2012 11:36:33 +0300 Subject: [PATCH] Refactor test cases Modified-by: Travis Cross Signed-off-by: Travis Cross --- libs/libzrtp/test/engine_helpers.c | 69 ++++++++++++++++++ libs/libzrtp/test/go_secure_test.c | 61 +--------------- libs/libzrtp/test/zrtphash_test.c | 113 +++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 60 deletions(-) create mode 100644 libs/libzrtp/test/engine_helpers.c create mode 100644 libs/libzrtp/test/zrtphash_test.c diff --git a/libs/libzrtp/test/engine_helpers.c b/libs/libzrtp/test/engine_helpers.c new file mode 100644 index 0000000000..3a3c2cb80a --- /dev/null +++ b/libs/libzrtp/test/engine_helpers.c @@ -0,0 +1,69 @@ +/* + * libZRTP SDK library, implements the ZRTP secure VoIP protocol. + * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved. + * Contact: http://philzimmermann.com + * For licensing and other legal details, see the file zrtp_legal.c. + * + * Viktor Krykun + */ + +#include /*chmockery dependency*/ +#include /*chmockery dependency*/ +#include /*for usleep*/ + +#include "cmockery/cmockery.h" +#include "test_engine.h" + +static zrtp_test_id_t g_alice, g_bob; +static zrtp_test_id_t g_alice_sid, g_bob_sid; +static zrtp_test_id_t g_secure_audio_channel; + + +static void prepare_alice_bob() { + zrtp_status_t s; + + zrtp_test_session_cfg_t session_config; + zrtp_test_session_config_defaults(&session_config); + + /* + * Create two test sessions, one for Alice and one for Bob and link them + * into test secure channel + */ + s = zrtp_test_session_create(g_alice, &session_config, &g_alice_sid); + assert_int_equal(zrtp_status_ok, s); + assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_alice_sid); + + s = zrtp_test_session_create(g_bob, &session_config, &g_bob_sid); + assert_int_equal(zrtp_status_ok, s); + assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_bob_sid); + + s = zrtp_test_channel_create2(g_alice_sid, g_bob_sid, 0, &g_secure_audio_channel); + assert_int_equal(zrtp_status_ok, s); + assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_secure_audio_channel); +} + +static void release_alice_bob() { + zrtp_test_session_destroy(g_alice_sid); + zrtp_test_session_destroy(g_bob_sid); + + zrtp_test_channel_destroy(g_secure_audio_channel); +} + +static void start_alice_bob_and_wait4secure() { + zrtp_status_t s; + zrtp_test_channel_info_t channel_info; + + /* Everything is ready. Let's start the stream and give it few seconds to switch secure. */ + s = zrtp_test_channel_start(g_secure_audio_channel); + assert_int_equal(zrtp_status_ok, s); + + unsigned i = 30; + for (; i>0; i--) { + usleep(100*1000); + } + + s = zrtp_test_channel_get(g_secure_audio_channel, &channel_info); + assert_int_equal(zrtp_status_ok, s); + + assert_true(channel_info.is_secure); +} diff --git a/libs/libzrtp/test/go_secure_test.c b/libs/libzrtp/test/go_secure_test.c index be0f28583d..d75af8d91e 100644 --- a/libs/libzrtp/test/go_secure_test.c +++ b/libs/libzrtp/test/go_secure_test.c @@ -7,16 +7,7 @@ * Viktor Krykun */ -#include /*chmockery dependency*/ -#include /*chmockery dependency*/ -#include /*for usleep*/ - -#include "cmockery/cmockery.h" -#include "test_engine.h" - -static zrtp_test_id_t g_alice, g_bob; -static zrtp_test_id_t g_alice_sid, g_bob_sid; -static zrtp_test_id_t g_secure_audio_channel; +#include "engine_helpers.c" static void setup() { zrtp_status_t s; @@ -38,56 +29,6 @@ static void teardown() { zrtp_test_endpoint_destroy(g_bob); } -static void prepare_alice_bob() { - zrtp_status_t s; - - //zrtp_test_channel_info_t channel_info; - zrtp_test_session_cfg_t session_config; - zrtp_test_session_config_defaults(&session_config); - - /* - * Create two test sessions, one for Alice and one for Bob and link them - * into test secure channel - */ - s = zrtp_test_session_create(g_alice, &session_config, &g_alice_sid); - assert_int_equal(zrtp_status_ok, s); - assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_alice_sid); - - s = zrtp_test_session_create(g_bob, &session_config, &g_bob_sid); - assert_int_equal(zrtp_status_ok, s); - assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_bob_sid); - - s = zrtp_test_channel_create2(g_alice_sid, g_bob_sid, 0, &g_secure_audio_channel); - assert_int_equal(zrtp_status_ok, s); - assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_secure_audio_channel); -} - -static void release_alice_bob() { - zrtp_test_session_destroy(g_alice_sid); - zrtp_test_session_destroy(g_bob_sid); - - zrtp_test_channel_destroy(g_secure_audio_channel); -} - -static void start_alice_bob_and_wait4secure() { - zrtp_status_t s; - zrtp_test_channel_info_t channel_info; - - /* Everything is ready. Let's start the stream and give it few seconds to switch secure. */ - s = zrtp_test_channel_start(g_secure_audio_channel); - assert_int_equal(zrtp_status_ok, s); - - unsigned i = 30; - for (; i>0; i--) { - usleep(100*1000); - } - - s = zrtp_test_channel_get(g_secure_audio_channel, &channel_info); - assert_int_equal(zrtp_status_ok, s); - - assert_true(channel_info.is_secure); -} - static void go_secure_test() { /* diff --git a/libs/libzrtp/test/zrtphash_test.c b/libs/libzrtp/test/zrtphash_test.c new file mode 100644 index 0000000000..0566148914 --- /dev/null +++ b/libs/libzrtp/test/zrtphash_test.c @@ -0,0 +1,113 @@ +/* + * libZRTP SDK library, implements the ZRTP secure VoIP protocol. + * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved. + * Contact: http://philzimmermann.com + * For licensing and other legal details, see the file zrtp_legal.c. + * + * Viktor Krykun + */ + +#include "engine_helpers.c" + +static void setup() { + zrtp_status_t s; + + zrtp_test_endpoint_cfg_t endpoint_cfg; + zrtp_test_endpoint_config_defaults(&endpoint_cfg); + + s = zrtp_test_endpoint_create(&endpoint_cfg, "Alice", &g_alice); + assert_int_equal(zrtp_status_ok, s); + assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_alice); + + s = zrtp_test_endpoint_create(&endpoint_cfg, "Bob", &g_bob); + assert_int_equal(zrtp_status_ok, s); + assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_bob); +} + +static void teardown() { + zrtp_test_endpoint_destroy(g_alice); + zrtp_test_endpoint_destroy(g_bob); +} + + +static void zrtp_hash_export_import_sunny_test() { + zrtp_status_t s; + char alice_zrtp_hash[ZRTP_SIGN_ZRTP_HASH_LENGTH]; + char bob_zrtp_hash[ZRTP_SIGN_ZRTP_HASH_LENGTH]; + zrtp_stream_t *alice_zrtp_stream, *bob_zrtp_stream; + + /* Create two test sessions, one for Alice and one for Bob and link them into test secure channel */ + prepare_alice_bob(); + + alice_zrtp_stream = zrtp_stream_for_test_stream(zrtp_test_session_get_stream_by_idx(g_alice_sid, 0)); + bob_zrtp_stream = zrtp_stream_for_test_stream(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0)); + assert_non_null(alice_zrtp_stream); assert_non_null(bob_zrtp_stream); + + /* Take Alice's hash and give it to Bob */ + s = zrtp_signaling_hash_get(alice_zrtp_stream, alice_zrtp_hash, sizeof(alice_zrtp_hash)); + assert_int_equal(zrtp_status_ok, s); + + s = zrtp_signaling_hash_set(bob_zrtp_stream, alice_zrtp_hash, ZRTP_SIGN_ZRTP_HASH_LENGTH); + assert_int_equal(zrtp_status_ok, s); + + /* Take Bob's hash and give it to Alice */ + s = zrtp_signaling_hash_get(bob_zrtp_stream, bob_zrtp_hash, sizeof(bob_zrtp_hash)); + assert_int_equal(zrtp_status_ok, s); + + s = zrtp_signaling_hash_set(alice_zrtp_stream, bob_zrtp_hash, ZRTP_SIGN_ZRTP_HASH_LENGTH); + assert_int_equal(zrtp_status_ok, s); + + /* Start and wait for Secure */ + start_alice_bob_and_wait4secure(); + + /* Check if ZRTP_EVENT_WRONG_SIGNALING_HASH was not triggered for any of test endpoints */ + assert_false(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_alice_sid, 0), + ZRTP_EVENT_WRONG_SIGNALING_HASH)); + + assert_false(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0), + ZRTP_EVENT_WRONG_SIGNALING_HASH)); + + /* Release test setup */ + release_alice_bob(); +} + +static void zrtp_hash_import_wrong_test() { + zrtp_status_t s; + char wrong_alice_zrtp_hash[ZRTP_SIGN_ZRTP_HASH_LENGTH]; + zrtp_stream_t *bob_zrtp_stream; + + /* Create two test sessions, one for Alice and one for Bob and link them into test secure channel */ + prepare_alice_bob(); + + bob_zrtp_stream = zrtp_stream_for_test_stream(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0)); + assert_non_null(bob_zrtp_stream); + + /* Let's provide wrong hash to bob */ + zrtp_memset(wrong_alice_zrtp_hash, 6, ZRTP_SIGN_ZRTP_HASH_LENGTH); + + s = zrtp_signaling_hash_set(bob_zrtp_stream, wrong_alice_zrtp_hash, ZRTP_SIGN_ZRTP_HASH_LENGTH); + assert_int_equal(zrtp_status_ok, s); + + /* Start and wait for Secure */ + start_alice_bob_and_wait4secure(); + + /* Check if Alice don't receive ZRTP_EVENT_WRONG_SIGNALING_HASH, but Bob should get one */ + assert_false(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_alice_sid, 0), + ZRTP_EVENT_WRONG_SIGNALING_HASH)); + + assert_true(zrtp_stream_did_event_receive(zrtp_test_session_get_stream_by_idx(g_bob_sid, 0), + ZRTP_EVENT_WRONG_SIGNALING_HASH)); + + /* Release test setup */ + release_alice_bob(); +} + + +int main(void) { + const UnitTest tests[] = { + unit_test_setup_teardown(zrtp_hash_export_import_sunny_test, setup, teardown), + unit_test_setup_teardown(zrtp_hash_import_wrong_test, setup, teardown), + }; + + return run_tests(tests); +}