2
0

cache_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 <stdarg.h>
  10. #include <stddef.h>
  11. #include <setjmp.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include "zrtp.h"
  15. #include "cmockery/cmockery.h"
  16. #define TEST_CACHE_PATH "./zrtp_cache_test.dat"
  17. static zrtp_global_t g_zrtp_cfg;
  18. static zrtp_string16_t zid_my = ZSTR_INIT_WITH_CONST_CSTRING("000000000_00");
  19. static zrtp_string16_t zid_a = ZSTR_INIT_WITH_CONST_CSTRING("000000000_02");
  20. static zrtp_string16_t zid_b = ZSTR_INIT_WITH_CONST_CSTRING("000000000_03");
  21. static zrtp_string16_t zid_c = ZSTR_INIT_WITH_CONST_CSTRING("000000000_04");
  22. static zrtp_string16_t zid_mitm1 = ZSTR_INIT_WITH_CONST_CSTRING("000000000_m1");
  23. static zrtp_string16_t zid_mitm2 = ZSTR_INIT_WITH_CONST_CSTRING("000000000_m2");
  24. static zrtp_shared_secret_t rs_my4a, rs_my4b, rs_my4c, rs_my4mitm1, rs_my4mitm2;
  25. static zrtp_shared_secret_t rs_my4a_r, rs_my4b_r, rs_my4c_r, rs_my4mitm1_r, rs_my4mitm2_r;
  26. static zrtp_cache_id_t secerets_to_delete[24];
  27. static unsigned secerets_to_delete_count = 0;
  28. static void init_rs_secret_(zrtp_shared_secret_t *sec, unsigned char val_fill);
  29. extern void zrtp_cache_create_id(const zrtp_stringn_t* first_ZID,
  30. const zrtp_stringn_t* second_ZID,
  31. zrtp_cache_id_t id);
  32. void cache_setup() {
  33. zrtp_status_t status;
  34. /* Delete cache file from previous test if it exists. */
  35. remove(TEST_CACHE_PATH);
  36. secerets_to_delete_count = 0;
  37. ZSTR_SET_EMPTY(g_zrtp_cfg.def_cache_path);
  38. /* Configure and Initialize ZRTP cache */
  39. zrtp_zstrcpyc(ZSTR_GV(g_zrtp_cfg.def_cache_path), TEST_CACHE_PATH);
  40. init_rs_secret_(&rs_my4a, 'a'); init_rs_secret_(&rs_my4b, 'b'); init_rs_secret_(&rs_my4c, 'c');
  41. init_rs_secret_(&rs_my4mitm1, '1'); init_rs_secret_(&rs_my4mitm2, '2');
  42. init_rs_secret_(&rs_my4a_r, 0); init_rs_secret_(&rs_my4b_r, 0); init_rs_secret_(&rs_my4c_r, 0);
  43. init_rs_secret_(&rs_my4mitm1_r, 0); init_rs_secret_(&rs_my4mitm2_r, 0);
  44. /* It should NOT crash and return OK. */
  45. status = zrtp_def_cache_init(&g_zrtp_cfg);
  46. assert_int_equal(status, zrtp_status_ok);
  47. /* Add few values into it */
  48. printf("==> Add few test entries.\n");
  49. status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a);
  50. assert_int_equal(status, zrtp_status_ok);
  51. status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b);
  52. assert_int_equal(status, zrtp_status_ok);
  53. status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c);
  54. assert_int_equal(status, zrtp_status_ok);
  55. status = zrtp_def_cache_put_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1);
  56. assert_int_equal(status, zrtp_status_ok);
  57. status = zrtp_def_cache_put_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2);
  58. assert_int_equal(status, zrtp_status_ok);
  59. status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c);
  60. assert_int_equal(status, zrtp_status_ok);
  61. /* Close the cache, it should be flushed to the file. */
  62. printf("==> Close the cache.\n");
  63. zrtp_def_cache_down();
  64. printf("==> Open just prepared cache file.\n");
  65. status = zrtp_def_cache_init(&g_zrtp_cfg);
  66. assert_int_equal(status, zrtp_status_ok);
  67. printf("==> Ready for the test!.\n");
  68. }
  69. void cache_teardown() {
  70. zrtp_def_cache_down();
  71. }
  72. /*
  73. * Simply init ZRTP cache with empty or non-existing filer and close it.
  74. * The app should not crash and trigger no errors.
  75. */
  76. void cache_init_store_empty_test() {
  77. zrtp_def_cache_down();
  78. }
  79. /*
  80. * Add few entries to the empty cache, flush it and then load again. Check if
  81. * all the entries were restored successfully.
  82. */
  83. void cache_add2empty_test() {
  84. zrtp_status_t status;
  85. int intres;
  86. /* Now, let's open the cache again and check if all the previously added values were restored successfully */
  87. printf("==> And open it again, it should contain all the stored values.\n");
  88. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  89. assert_int_equal(status, zrtp_status_ok);
  90. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4a_r.value), ZSTR_GV(rs_my4a.value)));
  91. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  92. assert_int_equal(status, zrtp_status_ok);
  93. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4b_r.value), ZSTR_GV(rs_my4b.value)));
  94. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  95. assert_int_equal(status, zrtp_status_ok);
  96. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm1_r.value), ZSTR_GV(rs_my4mitm1.value)));
  97. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  98. assert_int_equal(status, zrtp_status_ok);
  99. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  100. }
  101. /*
  102. * Test if cache properly handles Open-Close-Open with now no changes to the cache values.
  103. */
  104. void cache_save_unchanged_test() {
  105. zrtp_status_t status;
  106. /* Now, let's open the cache again and check if all the previously added values were restored successfully */
  107. printf("==> Now let's Open the cache and Close it right after, make no changes.\n");
  108. zrtp_def_cache_down();
  109. /*
  110. * TEST: now let's store the cache making no changes to it.
  111. * After opening it should include all the secrets untouched.
  112. */
  113. printf("==> And the cache again, it should contain all the stored values.\n");
  114. status = zrtp_def_cache_init(&g_zrtp_cfg);
  115. assert_int_equal(status, zrtp_status_ok);
  116. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  117. assert_int_equal(status, zrtp_status_ok);
  118. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4a_r.value), ZSTR_GV(rs_my4a.value)));
  119. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  120. assert_int_equal(status, zrtp_status_ok);
  121. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4b_r.value), ZSTR_GV(rs_my4b.value)));
  122. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  123. assert_int_equal(status, zrtp_status_ok);
  124. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm1_r.value), ZSTR_GV(rs_my4mitm1.value)));
  125. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  126. assert_int_equal(status, zrtp_status_ok);
  127. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  128. }
  129. /*
  130. * Check how the cache handles flushing of several dirty (modified) values. The cache should
  131. * flush to the disk modified values only and leave rest of the items untouched.
  132. */
  133. void cache_modify_and_save_test() {
  134. zrtp_status_t status;
  135. int intres;
  136. printf("==> And open it again, it should contain all the stored values.\n");
  137. /*
  138. * Now, let's modify just few entries and check of the fill will be stored.
  139. *
  140. * We will change RS secrets rs_my4b, rs_my4c and rs_my4mitm1 while leaving
  141. * rs_my4a and rs_my4mitm2 untouched.
  142. */
  143. init_rs_secret_(&rs_my4b, 'x'); init_rs_secret_(&rs_my4c, 'y');
  144. init_rs_secret_(&rs_my4mitm1, 'z');
  145. printf("==> Now we gonna to update few cache entries and flush the cache mack to the file.\n");
  146. status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b);
  147. assert_int_equal(status, zrtp_status_ok);
  148. status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c);
  149. assert_int_equal(status, zrtp_status_ok);
  150. status = zrtp_def_cache_put_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1);
  151. assert_int_equal(status, zrtp_status_ok);
  152. /* Flush the cache and open it again. */
  153. zrtp_def_cache_down();
  154. printf("==> Open the cache and make sure all our prev. modifications saved properly.\n");
  155. status = zrtp_def_cache_init(&g_zrtp_cfg);
  156. assert_int_equal(status, zrtp_status_ok);
  157. /* Let's check if all our modifications are in place. */
  158. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  159. assert_int_equal(status, zrtp_status_ok);
  160. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4a_r.value), ZSTR_GV(rs_my4a.value)));
  161. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  162. assert_int_equal(status, zrtp_status_ok);
  163. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4b_r.value), ZSTR_GV(rs_my4b.value)));
  164. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c_r, 0);
  165. assert_int_equal(status, zrtp_status_ok);
  166. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4c_r.value), ZSTR_GV(rs_my4c.value)));
  167. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  168. assert_int_equal(status, zrtp_status_ok);
  169. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm1_r.value), ZSTR_GV(rs_my4mitm1.value)));
  170. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  171. assert_int_equal(status, zrtp_status_ok);
  172. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  173. }
  174. /*
  175. * The basic idea of all cache_delete_* tests is to delete few cache entries
  176. * from preconfigured setup, flush caches, open the cache again and check if
  177. * non-deleted values are Ok.
  178. */
  179. static int cache_foreach_del_func(zrtp_cache_elem_t* elem, int is_mitm, void* data, int* del) {
  180. unsigned c;
  181. //printf("AAAA cache_foreach_del_func(): elem index=%u\n", elem->_index);
  182. for (c=0; c<secerets_to_delete_count; c++) {
  183. if (!zrtp_memcmp(elem->id, secerets_to_delete[c], sizeof(zrtp_cache_id_t))) {
  184. printf("\t==> Delete cache element index=%u.\n", elem->_index);
  185. *del = 1;
  186. break;
  187. }
  188. }
  189. return 1;
  190. }
  191. void cache_delete_few_rs_test() {
  192. zrtp_status_t status;
  193. printf("==> Delete few RS secrets and flush the cache.\n");
  194. secerets_to_delete_count = 0;
  195. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_b), secerets_to_delete[secerets_to_delete_count++]);
  196. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_a), secerets_to_delete[secerets_to_delete_count++]);
  197. zrtp_def_cache_foreach(&g_zrtp_cfg, 0, &cache_foreach_del_func, NULL);
  198. /* Flush the cache and open it again. */
  199. zrtp_def_cache_down();
  200. printf("==> Open the cache and make sure all our prev. Modifications saved properly.\n");
  201. status = zrtp_def_cache_init(&g_zrtp_cfg);
  202. assert_int_equal(status, zrtp_status_ok);
  203. /* Let's check if all our modifications are in place. */
  204. /* my4a should be deleted. */
  205. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  206. assert_int_not_equal(status, zrtp_status_ok);
  207. /* my4b should be deleted. */
  208. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  209. assert_int_not_equal(status, zrtp_status_ok);
  210. /* The rest of the secrets should be in place. */
  211. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c_r, 0);
  212. assert_int_equal(status, zrtp_status_ok);
  213. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4c_r.value), ZSTR_GV(rs_my4c.value)));
  214. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  215. assert_int_equal(status, zrtp_status_ok);
  216. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm1_r.value), ZSTR_GV(rs_my4mitm1.value)));
  217. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  218. assert_int_equal(status, zrtp_status_ok);
  219. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  220. }
  221. void cache_delete_few_mitm_test() {
  222. zrtp_status_t status;
  223. printf("==> Delete few MiTM secrets and flush the cache.\n");
  224. secerets_to_delete_count = 0;
  225. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), secerets_to_delete[secerets_to_delete_count++]);
  226. zrtp_def_cache_foreach(&g_zrtp_cfg, 1, &cache_foreach_del_func, NULL);
  227. /* Flush the cache and open it again. */
  228. zrtp_def_cache_down();
  229. printf("==> Open the cache and make sure all our prev. Modifications saved properly.\n");
  230. status = zrtp_def_cache_init(&g_zrtp_cfg);
  231. assert_int_equal(status, zrtp_status_ok);
  232. /* Let's check if all our modifications are in place. */
  233. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  234. assert_int_equal(status, zrtp_status_ok);
  235. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4a_r.value), ZSTR_GV(rs_my4a.value)));
  236. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  237. assert_int_equal(status, zrtp_status_ok);
  238. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4b_r.value), ZSTR_GV(rs_my4b.value)));
  239. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c_r, 0);
  240. assert_int_equal(status, zrtp_status_ok);
  241. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4c_r.value), ZSTR_GV(rs_my4c.value)));
  242. /* Should be deleted */
  243. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  244. assert_int_not_equal(status, zrtp_status_ok);
  245. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  246. assert_int_equal(status, zrtp_status_ok);
  247. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  248. }
  249. void cache_delete_few_rs_and_mitm_test() {
  250. zrtp_status_t status;
  251. printf("==> Delete few RS secrets and flush the cache.\n");
  252. secerets_to_delete_count = 0;
  253. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_b), secerets_to_delete[secerets_to_delete_count++]);
  254. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_a), secerets_to_delete[secerets_to_delete_count++]);
  255. zrtp_def_cache_foreach(&g_zrtp_cfg, 0, &cache_foreach_del_func, NULL);
  256. secerets_to_delete_count = 0;
  257. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), secerets_to_delete[secerets_to_delete_count++]);
  258. zrtp_def_cache_foreach(&g_zrtp_cfg, 1, &cache_foreach_del_func, NULL);
  259. /* Flush the cache and open it again. */
  260. zrtp_def_cache_down();
  261. printf("==> Open the cache and make sure all our prev. Modifications saved properly.\n");
  262. status = zrtp_def_cache_init(&g_zrtp_cfg);
  263. assert_int_equal(status, zrtp_status_ok);
  264. /* Let's check if all our modifications are in place. */
  265. /* Should be deleted. */
  266. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  267. assert_int_not_equal(status, zrtp_status_ok);
  268. /* Should be deleted. */
  269. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  270. assert_int_not_equal(status, zrtp_status_ok);
  271. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c_r, 0);
  272. assert_int_equal(status, zrtp_status_ok);
  273. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4c_r.value), ZSTR_GV(rs_my4c.value)));
  274. /* Should be deleted. */
  275. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  276. assert_int_not_equal(status, zrtp_status_ok);
  277. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  278. assert_int_equal(status, zrtp_status_ok);
  279. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  280. }
  281. void cache_delete_all_rs_test() {
  282. zrtp_status_t status;
  283. printf("==> Delete few RS secrets and flush the cache.\n");
  284. secerets_to_delete_count = 0;
  285. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_b), secerets_to_delete[secerets_to_delete_count++]);
  286. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_a), secerets_to_delete[secerets_to_delete_count++]);
  287. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_c), secerets_to_delete[secerets_to_delete_count++]);
  288. zrtp_def_cache_foreach(&g_zrtp_cfg, 0, &cache_foreach_del_func, NULL);
  289. /* Flush the cache and open it again. */
  290. zrtp_def_cache_down();
  291. printf("==> Open the cache and make sure all our prev. Modifications saved properly.\n");
  292. status = zrtp_def_cache_init(&g_zrtp_cfg);
  293. assert_int_equal(status, zrtp_status_ok);
  294. /* Let's check if all our modifications are in place. */
  295. /* All RS values should be deleted. */
  296. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  297. assert_int_not_equal(status, zrtp_status_ok);
  298. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  299. assert_int_not_equal(status, zrtp_status_ok);
  300. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c_r, 0);
  301. assert_int_not_equal(status, zrtp_status_ok);
  302. /* MiTM secrets should be in place. */
  303. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  304. assert_int_equal(status, zrtp_status_ok);
  305. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm1_r.value), ZSTR_GV(rs_my4mitm1.value)));
  306. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r);
  307. assert_int_equal(status, zrtp_status_ok);
  308. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4mitm2_r.value), ZSTR_GV(rs_my4mitm2.value)));
  309. }
  310. void cache_delete_all_mitm_test() {
  311. zrtp_status_t status;
  312. printf("==> Delete few MiTM secrets and flush the cache.\n");
  313. secerets_to_delete_count = 0;
  314. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), secerets_to_delete[secerets_to_delete_count++]);
  315. zrtp_cache_create_id(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), secerets_to_delete[secerets_to_delete_count++]);
  316. zrtp_def_cache_foreach(&g_zrtp_cfg, 1, &cache_foreach_del_func, NULL);
  317. /* Flush the cache and open it again. */
  318. zrtp_def_cache_down();
  319. printf("==> Open the cache and make sure all our prev. Modifications saved properly.\n");
  320. status = zrtp_def_cache_init(&g_zrtp_cfg);
  321. assert_int_equal(status, zrtp_status_ok);
  322. /* Let's check if all our modifications are in place. */
  323. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
  324. assert_int_equal(status, zrtp_status_ok);
  325. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4a_r.value), ZSTR_GV(rs_my4a.value)));
  326. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b_r, 0);
  327. assert_int_equal(status, zrtp_status_ok);
  328. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4b_r.value), ZSTR_GV(rs_my4b.value)));
  329. status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c_r, 0);
  330. assert_int_equal(status, zrtp_status_ok);
  331. assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4c_r.value), ZSTR_GV(rs_my4c.value)));
  332. /* All MiTM secrets should be deleted. */
  333. status = zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1_r);
  334. assert_int_not_equal(status, zrtp_status_ok);
  335. assert_int_not_equal(zrtp_def_cache_get_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm2), &rs_my4mitm2_r), zrtp_status_ok);
  336. }
  337. int main(void) {
  338. const UnitTest tests[] = {
  339. unit_test_setup_teardown(cache_init_store_empty_test, cache_setup, cache_teardown),
  340. unit_test_setup_teardown(cache_add2empty_test, cache_setup, cache_teardown),
  341. unit_test_setup_teardown(cache_save_unchanged_test, cache_setup, cache_teardown),
  342. unit_test_setup_teardown(cache_modify_and_save_test, cache_setup, cache_teardown),
  343. unit_test_setup_teardown(cache_delete_few_rs_test, cache_setup, cache_teardown),
  344. unit_test_setup_teardown(cache_delete_few_mitm_test, cache_setup, cache_teardown),
  345. unit_test_setup_teardown(cache_delete_few_rs_and_mitm_test, cache_setup, cache_teardown),
  346. unit_test_setup_teardown(cache_delete_all_mitm_test, cache_setup, cache_teardown),
  347. };
  348. return run_tests(tests);
  349. }
  350. /******************************************************************************
  351. * Helpers
  352. *****************************************************************************/
  353. static void init_rs_secret_(zrtp_shared_secret_t *sec, unsigned char val_fill) {
  354. char val_buff[ZRTP_HASH_SIZE];
  355. zrtp_memset(val_buff, val_fill, sizeof(val_buff));
  356. ZSTR_SET_EMPTY(sec->value);
  357. zrtp_zstrcpyc(ZSTR_GV(sec->value), val_buff);
  358. sec->_cachedflag = 0;
  359. sec->ttl = 0;
  360. sec->lastused_at = 0;
  361. }