2
0

srtp_replay_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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>
  10. #include <stdio.h>
  11. #include "zrtp.h"
  12. #include "cmockery/cmockery.h"
  13. #define _ZTU_ "srtp replay test"
  14. zrtp_global_t *zrtp;
  15. #define TEST_MAP_WIDTH 64
  16. #if TEST_MAP_WIDTH%8
  17. # define TEST_MAP_WIDTH_BYTES TEST_MAP_WIDTH/8+1
  18. #else
  19. # define TEST_MAP_WIDTH_BYTES TEST_MAP_WIDTH/8
  20. #endif
  21. #define FIRST_TEST_MAP_INIT_WIDTH 24
  22. extern zrtp_rp_node_t *get_rp_node_non_lock(zrtp_rp_ctx_t *ctx, uint8_t direction, uint32_t ssrc);
  23. extern zrtp_rp_node_t *add_rp_node(zrtp_srtp_ctx_t *srtp_ctx, zrtp_rp_ctx_t *ctx, uint8_t direction, uint32_t ssrc);
  24. extern zrtp_status_t zrtp_srtp_rp_check(zrtp_srtp_rp_t *srtp_rp, zrtp_rtp_info_t *packet);
  25. extern zrtp_status_t zrtp_srtp_rp_add(zrtp_srtp_rp_t *srtp_rp, zrtp_rtp_info_t *packet);
  26. void setup() {
  27. zrtp_status_t s;
  28. zrtp_config_t zrtp_config;
  29. zrtp_config_defaults(&zrtp_config);
  30. s = zrtp_init(&zrtp_config, &zrtp);
  31. assert_int_equal(s, zrtp_status_ok);
  32. }
  33. void teardown() {
  34. zrtp_down(zrtp);
  35. }
  36. static void print_map(uint8_t *map, int width_bytes)
  37. {
  38. int i;
  39. for(i=width_bytes-1; i >= 0; i--) {
  40. ZRTP_LOGC(3, ("%i%i%i%i%i%i%i%i",
  41. zrtp_bitmap_get_bit(map, 8*i+7),
  42. zrtp_bitmap_get_bit(map, 8*i+6),
  43. zrtp_bitmap_get_bit(map, 8*i+5),
  44. zrtp_bitmap_get_bit(map, 8*i+4),
  45. zrtp_bitmap_get_bit(map, 8*i+3),
  46. zrtp_bitmap_get_bit(map, 8*i+2),
  47. zrtp_bitmap_get_bit(map, 8*i+1),
  48. zrtp_bitmap_get_bit(map, 8*i+0)));
  49. }
  50. ZRTP_LOG(3, (_ZTU_, "\n"));
  51. }
  52. static void init_random_map(uint8_t *map, int width, zrtp_global_t *zrtp) {
  53. int i;
  54. for(i=0; i<width; i++) {
  55. uint32_t rnd = 0;
  56. zrtp_randstr(zrtp, (uint8_t*)&rnd, sizeof(rnd));
  57. if(rnd%10 < 5) {
  58. zrtp_bitmap_set_bit(map, i);
  59. } else {
  60. zrtp_bitmap_clear_bit(map, i);
  61. }
  62. }
  63. }
  64. void inject_from_map( zrtp_srtp_global_t *srtp_global,
  65. uint32_t ssrc,
  66. uint8_t *src_map, uint8_t *dst_map, int width) {
  67. zrtp_rp_node_t *rp_node;
  68. int i;
  69. zrtp_rtp_info_t pkt;
  70. rp_node = get_rp_node_non_lock(srtp_global->rp_ctx, RP_INCOMING_DIRECTION, ssrc);
  71. if (NULL == rp_node) {
  72. return;
  73. }
  74. for (i=0; i< width; i++) {
  75. if (1 == zrtp_bitmap_get_bit(src_map, i)) {
  76. pkt.seq = i;
  77. if (zrtp_status_ok == zrtp_srtp_rp_check(&rp_node->rtp_rp, &pkt)) {
  78. zrtp_bitmap_set_bit(dst_map, i);
  79. zrtp_srtp_rp_add(&rp_node->rtp_rp, &pkt);
  80. }
  81. }
  82. }
  83. }
  84. // TODO: split test into several, more atomic tests
  85. static void srtp_replay_test() {
  86. int res = 0;
  87. uint32_t ssrc = 1;
  88. int i = 0;
  89. uint8_t test_map[TEST_MAP_WIDTH_BYTES];
  90. uint8_t result_map[TEST_MAP_WIDTH_BYTES];
  91. uint8_t tmp_window[ZRTP_SRTP_WINDOW_WIDTH_BYTES];
  92. uint32_t tmp_seq;
  93. int delta, shift;
  94. zrtp_rp_node_t *rp_node;
  95. zrtp_srtp_global_t *srtp = zrtp->srtp_global;
  96. rp_node = add_rp_node(NULL, srtp->rp_ctx, RP_INCOMING_DIRECTION, ssrc);
  97. assert_non_null(rp_node);
  98. for (i=0; i< TEST_MAP_WIDTH_BYTES; i++) {
  99. test_map[i] = 0;
  100. result_map[i] = 0;
  101. }
  102. /*
  103. * 1st test
  104. * ----------------------------------------------------------------------
  105. */
  106. init_random_map(test_map, FIRST_TEST_MAP_INIT_WIDTH, zrtp);
  107. inject_from_map(srtp, ssrc, test_map, result_map, TEST_MAP_WIDTH);
  108. ZRTP_LOG(3, (_ZTU_,"1st test. Wnd[%i]...\n", ZRTP_SRTP_WINDOW_WIDTH));
  109. tmp_seq = rp_node->rtp_rp.seq;
  110. for (i=0; i<ZRTP_SRTP_WINDOW_WIDTH_BYTES; i++) {
  111. tmp_window[i] = rp_node->rtp_rp.window[i];
  112. }
  113. delta = tmp_seq-ZRTP_SRTP_WINDOW_WIDTH + 1;
  114. if (delta > 0) {
  115. ZRTP_LOG(3, (_ZTU_,"after wnd: (%i;0]\n", delta));
  116. ZRTP_LOG(3, (_ZTU_,"inside wnd: [%i;%i]\n", tmp_seq, delta));
  117. } else {
  118. ZRTP_LOG(3, (_ZTU_,"after wnd: (0;0)\n"));
  119. ZRTP_LOG(3, (_ZTU_,"inside wnd: [%i;0]\n", tmp_seq));
  120. }
  121. ZRTP_LOG(3, (_ZTU_,"before wnd: [%i;%i)\n", TEST_MAP_WIDTH-1, tmp_seq));
  122. ZRTP_LOG(3, (_ZTU_,"Test map: "));
  123. print_map(test_map, TEST_MAP_WIDTH_BYTES);
  124. ZRTP_LOG(3, (_ZTU_,"Res map: "));
  125. print_map(result_map, TEST_MAP_WIDTH_BYTES);
  126. shift = TEST_MAP_WIDTH;
  127. shift -= rp_node->rtp_rp.seq + 1;
  128. ZRTP_LOG(3, (_ZTU_,"Window : "));
  129. for(i=shift; i > 0; i--){
  130. ZRTP_LOGC(3, (" "));
  131. }
  132. print_map(rp_node->rtp_rp.window, ZRTP_SRTP_WINDOW_WIDTH_BYTES);
  133. /*
  134. * 2nd test
  135. * ----------------------------------------------------------------------
  136. */
  137. for(i=0; i< TEST_MAP_WIDTH_BYTES; i++){
  138. test_map[i] = 0;
  139. result_map[i] = 0;
  140. }
  141. init_random_map(test_map, TEST_MAP_WIDTH, zrtp);
  142. inject_from_map(srtp, ssrc, test_map, result_map, TEST_MAP_WIDTH);
  143. ZRTP_LOG(3, (_ZTU_,"2nd test. Wnd[%i]...\n", ZRTP_SRTP_WINDOW_WIDTH));
  144. ZRTP_LOG(3, (_ZTU_,"Test map: "));
  145. print_map(test_map, TEST_MAP_WIDTH_BYTES);
  146. ZRTP_LOG(3, (_ZTU_,"Res map: "));
  147. print_map(result_map, TEST_MAP_WIDTH_BYTES);
  148. shift = TEST_MAP_WIDTH;
  149. shift -= rp_node->rtp_rp.seq + 1;
  150. ZRTP_LOG(3, (_ZTU_,"Window : "));
  151. for (i=shift; i > 0; i--) {
  152. //zrtp_print_log(ZRTP_LOG_DEBUG, " ");
  153. }
  154. print_map(rp_node->rtp_rp.window, ZRTP_SRTP_WINDOW_WIDTH_BYTES);
  155. /*
  156. in result map:
  157. - after window we should to have all zeroes
  158. - into the window we should have ones only if window have zero at appropriate position
  159. - before window we should have equal values of test map and result map bits
  160. */
  161. for (i=0; i < TEST_MAP_WIDTH; i++) {
  162. if (delta > 0 && i < delta) {
  163. /* After window */
  164. if (0 != zrtp_bitmap_get_bit(result_map, i)) {
  165. ZRTP_LOG(3, (_ZTU_,"After window. %i bit should be 0\n", i));
  166. res = -1;
  167. }
  168. } else if (i <= (int)tmp_seq && i >= delta) {
  169. /* inside window */
  170. /* check window filtering */
  171. if(1 == zrtp_bitmap_get_bit(result_map, i)) {
  172. if (1 == zrtp_bitmap_get_bit(tmp_window, i - (tmp_seq-ZRTP_SRTP_WINDOW_WIDTH) - 1)) {
  173. ZRTP_LOG(3, (_ZTU_,"Inside window. Window filtering fail. %i bit should be 0\n", i));
  174. res = -1;
  175. }
  176. }
  177. /* check test vs result maps */
  178. if ( zrtp_bitmap_get_bit(result_map, i) != zrtp_bitmap_get_bit(test_map, i) &&
  179. !zrtp_bitmap_get_bit(tmp_window, i - (tmp_seq-ZRTP_SRTP_WINDOW_WIDTH) - 1)) {
  180. ZRTP_LOG(3, (_ZTU_, "Inside window. Test map isn't equal to result at bit %i\n", i));
  181. res = -1;
  182. }
  183. } else {
  184. /* after window */
  185. if (zrtp_bitmap_get_bit(result_map, i) != zrtp_bitmap_get_bit(test_map, i)) {
  186. ZRTP_LOG(3, (_ZTU_,"Before window. Test map isn't equal to result at bit %i\n", i));
  187. res = -1;
  188. }
  189. }
  190. }
  191. assert_int_equal(res, 0);
  192. }
  193. int main(void) {
  194. const UnitTest tests[] = {
  195. unit_test_setup_teardown(srtp_replay_test, setup, teardown),
  196. };
  197. return run_tests(tests);
  198. }