]> git.ipfire.org Git - thirdparty/freeswitch.git/blob - libs/libzrtp/test/enrollment_test.c
[mod_http_cache] Fix leaking curl handle in http_get()
[thirdparty/freeswitch.git] / libs / libzrtp / test / enrollment_test.c
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
10 #include <setjmp.h> /*chmockery dependency*/
11 #include <stdio.h> /*chmockery dependency*/
12 #include <unistd.h> /*for usleep*/
13
14 #include "cmockery/cmockery.h"
15 #include "test_engine.h"
16
17 #include "enroll_test_helpers.c"
18
19 static void enrollment_test() {
20 zrtp_status_t s;
21
22 zrtp_test_channel_info_t a2pbx_channel_info;
23 zrtp_test_session_cfg_t session_config, session_config_enroll;
24 zrtp_test_session_config_defaults(&session_config);
25 zrtp_test_session_config_defaults(&session_config_enroll);
26
27 session_config_enroll.is_enrollment = 1;
28
29 /**************************************************************************
30 * Enroll Alice to PBX and check triggered events.
31 */
32 prepare_alice_pbx_bob_setup(&session_config, NULL, &session_config_enroll, NULL);
33
34 /* Everything is ready. Let's start the stream and give it few seconds to switch secure. */
35 s = zrtp_test_channel_start(g_alice2pbx_channel);
36 assert_int_equal(zrtp_status_ok, s);
37
38 int i = 30;
39 for (; i>0; i--) {
40 usleep(100*1000);
41 }
42
43 s = zrtp_test_channel_get(g_alice2pbx_channel, &a2pbx_channel_info);
44 assert_int_equal(zrtp_status_ok, s);
45
46 /* Both, Alice and PBX should switch secure */
47 assert_true(a2pbx_channel_info.is_secure);
48
49 /* Alice should receive Enrollment notification */
50 zrtp_test_id_t alice2pbx_stream = zrtp_test_session_get_stream_by_idx(g_alice_sid, 0);
51 assert_true(zrtp_stream_did_event_receive(alice2pbx_stream, ZRTP_EVENT_IS_CLIENT_ENROLLMENT));
52
53 /* PBX streams should receive incoming enrollment notification */
54 zrtp_test_id_t pbx2alice_stream = zrtp_test_session_get_stream_by_idx(g_pbxa_sid, 0);
55 assert_true(zrtp_stream_did_event_receive(pbx2alice_stream, ZRTP_EVENT_NEW_USER_ENROLLED));
56
57 /* Confirm enrollment at the PBX side */
58 s = zrtp_register_with_trusted_mitm(zrtp_stream_for_test_stream(alice2pbx_stream));
59 assert_int_equal(zrtp_status_ok, s);
60
61 /* Clean-up */
62 cleanup_alice_pbx_bob_setup();
63
64 /**************************************************************************
65 * Try to make one more enrollment call. This time it should say "Already enrolled"
66 */
67 prepare_alice_pbx_bob_setup(&session_config, NULL, &session_config_enroll, NULL);
68
69 /* Everything is ready. Let's start the stream and give it few seconds to switch secure. */
70 s = zrtp_test_channel_start(g_alice2pbx_channel);
71 assert_int_equal(zrtp_status_ok, s);
72
73 i = 30;
74 for (; i>0; i--) {
75 usleep(100*1000);
76 }
77
78 s = zrtp_test_channel_get(g_alice2pbx_channel, &a2pbx_channel_info);
79 assert_int_equal(zrtp_status_ok, s);
80
81 assert_true(a2pbx_channel_info.is_secure);
82
83 /* Alice should receive Enrollment notification */
84 alice2pbx_stream = zrtp_test_session_get_stream_by_idx(g_alice_sid, 0);
85 assert_true(zrtp_stream_did_event_receive(alice2pbx_stream, ZRTP_EVENT_IS_CLIENT_ENROLLMENT));
86
87 /* PBX streams should receive incoming enrollment notification */
88 pbx2alice_stream = zrtp_test_session_get_stream_by_idx(g_pbxa_sid, 0);
89 assert_true(zrtp_stream_did_event_receive(pbx2alice_stream, ZRTP_EVENT_USER_ALREADY_ENROLLED));
90
91 // TODO: check if we have PBX secret cached
92 // TODO: test zrtp_is_user_enrolled()
93 }
94
95 int main(void) {
96 const UnitTest tests[] = {
97 unit_test_setup_teardown(enrollment_test, pbx_setup, pbx_teardown),
98 };
99
100 return run_tests(tests);
101 }