test_conscache.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "config.h"
  5. #include "conscache.h"
  6. #include "test.h"
  7. #ifdef HAVE_UTIME_H
  8. #include <utime.h>
  9. #endif
  10. static void
  11. test_conscache_simple_usage(void *arg)
  12. {
  13. (void)arg;
  14. consensus_cache_entry_t *ent = NULL, *ent2 = NULL;
  15. /* Make a temporary datadir for these tests */
  16. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cache"));
  17. tor_free(get_options_mutable()->DataDirectory);
  18. get_options_mutable()->DataDirectory = tor_strdup(ddir_fname);
  19. check_private_dir(ddir_fname, CPD_CREATE, NULL);
  20. consensus_cache_t *cache = consensus_cache_open("cons", 128);
  21. tt_assert(cache);
  22. /* Create object; make sure it exists. */
  23. config_line_t *labels = NULL;
  24. config_line_append(&labels, "Hello", "world");
  25. config_line_append(&labels, "Adios", "planetas");
  26. ent = consensus_cache_add(cache,
  27. labels, (const uint8_t *)"A\0B\0C", 5);
  28. config_free_lines(labels);
  29. labels = NULL;
  30. tt_assert(ent);
  31. /* Make a second object */
  32. config_line_append(&labels, "Hello", "mundo");
  33. config_line_append(&labels, "Adios", "planets");
  34. ent2 = consensus_cache_add(cache,
  35. labels, (const uint8_t *)"xyzzy", 5);
  36. config_free_lines(labels);
  37. labels = NULL;
  38. tt_assert(ent2);
  39. tt_assert(! consensus_cache_entry_is_mapped(ent2));
  40. consensus_cache_entry_decref(ent2);
  41. ent2 = NULL;
  42. /* Check get_value */
  43. tt_ptr_op(NULL, OP_EQ, consensus_cache_entry_get_value(ent, "hebbo"));
  44. tt_str_op("world", OP_EQ, consensus_cache_entry_get_value(ent, "Hello"));
  45. /* Check find_first */
  46. ent2 = consensus_cache_find_first(cache, "Hello", "world!");
  47. tt_ptr_op(ent2, OP_EQ, NULL);
  48. ent2 = consensus_cache_find_first(cache, "Hello", "world");
  49. tt_ptr_op(ent2, OP_EQ, ent);
  50. ent2 = consensus_cache_find_first(cache, "Hello", "mundo");
  51. tt_ptr_op(ent2, OP_NE, ent);
  52. tt_assert(! consensus_cache_entry_is_mapped(ent));
  53. /* Check get_body */
  54. const uint8_t *bp = NULL;
  55. size_t sz = 0;
  56. int r = consensus_cache_entry_get_body(ent, &bp, &sz);
  57. tt_int_op(r, OP_EQ, 0);
  58. tt_u64_op(sz, OP_EQ, 5);
  59. tt_mem_op(bp, OP_EQ, "A\0B\0C", 5);
  60. tt_assert(consensus_cache_entry_is_mapped(ent));
  61. /* Free and re-create the cache, to rescan the directory. */
  62. consensus_cache_free(cache);
  63. consensus_cache_entry_decref(ent);
  64. cache = consensus_cache_open("cons", 128);
  65. /* Make sure the entry is still there */
  66. ent = consensus_cache_find_first(cache, "Hello", "mundo");
  67. tt_assert(ent);
  68. ent2 = consensus_cache_find_first(cache, "Adios", "planets");
  69. tt_ptr_op(ent, OP_EQ, ent2);
  70. consensus_cache_entry_incref(ent);
  71. tt_assert(! consensus_cache_entry_is_mapped(ent));
  72. r = consensus_cache_entry_get_body(ent, &bp, &sz);
  73. tt_int_op(r, OP_EQ, 0);
  74. tt_u64_op(sz, OP_EQ, 5);
  75. tt_mem_op(bp, OP_EQ, "xyzzy", 5);
  76. tt_assert(consensus_cache_entry_is_mapped(ent));
  77. /* There should be two entries total. */
  78. smartlist_t *entries = smartlist_new();
  79. consensus_cache_find_all(entries, cache, NULL, NULL);
  80. int n = smartlist_len(entries);
  81. smartlist_free(entries);
  82. tt_int_op(n, OP_EQ, 2);
  83. done:
  84. consensus_cache_entry_decref(ent);
  85. tor_free(ddir_fname);
  86. consensus_cache_free(cache);
  87. }
  88. #define ENT(name) \
  89. { #name, test_conscache_ ## name, TT_FORK, NULL, NULL }
  90. struct testcase_t conscache_tests[] = {
  91. ENT(simple_usage),
  92. END_OF_TESTCASES
  93. };