static void test_rend_cache_clean(void *data) { rend_cache_entry_t *one, *two; rend_service_descriptor_t *desc_one, *desc_two; strmap_iter_t *iter = NULL; const char *key; void *val; (void)data; rend_cache_init(); // Test with empty rendcache rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT); tt_int_op(strmap_size(rend_cache), OP_EQ, 0); // Test with two old entries one = tor_malloc_zero(sizeof(rend_cache_entry_t)); two = tor_malloc_zero(sizeof(rend_cache_entry_t)); desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t)); desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t)); one->parsed = desc_one; two->parsed = desc_two; desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST; desc_two->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10; desc_one->pk = pk_generate(0); desc_two->pk = pk_generate(1); strmap_set_lc(rend_cache, "foo1", one); strmap_set_lc(rend_cache, "foo2", two); rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT); tt_int_op(strmap_size(rend_cache), OP_EQ, 0); // Test with one old entry and one newer entry one = tor_malloc_zero(sizeof(rend_cache_entry_t)); two = tor_malloc_zero(sizeof(rend_cache_entry_t)); desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t)); desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t)); one->parsed = desc_one; two->parsed = desc_two; desc_one->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10; desc_two->timestamp = time(NULL) - 100; desc_one->pk = pk_generate(0); desc_two->pk = pk_generate(1); strmap_set_lc(rend_cache, "foo1", one); strmap_set_lc(rend_cache, "foo2", two); rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT); tt_int_op(strmap_size(rend_cache), OP_EQ, 1); iter = strmap_iter_init(rend_cache); strmap_iter_get(iter, &key, &val); tt_str_op(key, OP_EQ, "foo2"); done: rend_cache_free_all(); }
/** Run unit tests for string-to-void* map functions */ static void test_container_strmap(void) { strmap_t *map; strmap_iter_t *iter; const char *k; void *v; char *visited = NULL; smartlist_t *found_keys = NULL; map = strmap_new(); test_assert(map); test_eq(strmap_size(map), 0); test_assert(strmap_isempty(map)); v = strmap_set(map, "K1", (void*)99); test_eq_ptr(v, NULL); test_assert(!strmap_isempty(map)); v = strmap_set(map, "K2", (void*)101); test_eq_ptr(v, NULL); v = strmap_set(map, "K1", (void*)100); test_eq_ptr(v, (void*)99); test_eq_ptr(strmap_get(map,"K1"), (void*)100); test_eq_ptr(strmap_get(map,"K2"), (void*)101); test_eq_ptr(strmap_get(map,"K-not-there"), NULL); strmap_assert_ok(map); v = strmap_remove(map,"K2"); strmap_assert_ok(map); test_eq_ptr(v, (void*)101); test_eq_ptr(strmap_get(map,"K2"), NULL); test_eq_ptr(strmap_remove(map,"K2"), NULL); strmap_set(map, "K2", (void*)101); strmap_set(map, "K3", (void*)102); strmap_set(map, "K4", (void*)103); test_eq(strmap_size(map), 4); strmap_assert_ok(map); strmap_set(map, "K5", (void*)104); strmap_set(map, "K6", (void*)105); strmap_assert_ok(map); /* Test iterator. */ iter = strmap_iter_init(map); found_keys = smartlist_new(); while (!strmap_iter_done(iter)) { strmap_iter_get(iter,&k,&v); smartlist_add(found_keys, tor_strdup(k)); test_eq_ptr(v, strmap_get(map, k)); if (!strcmp(k, "K2")) { iter = strmap_iter_next_rmv(map,iter); } else { iter = strmap_iter_next(map,iter); } } /* Make sure we removed K2, but not the others. */ test_eq_ptr(strmap_get(map, "K2"), NULL); test_eq_ptr(strmap_get(map, "K5"), (void*)104); /* Make sure we visited everyone once */ smartlist_sort_strings(found_keys); visited = smartlist_join_strings(found_keys, ":", 0, NULL); test_streq(visited, "K1:K2:K3:K4:K5:K6"); strmap_assert_ok(map); /* Clean up after ourselves. */ strmap_free(map, NULL); map = NULL; /* Now try some lc functions. */ map = strmap_new(); strmap_set_lc(map,"Ab.C", (void*)1); test_eq_ptr(strmap_get(map,"ab.c"), (void*)1); strmap_assert_ok(map); test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1); test_eq_ptr(strmap_get(map,"AB.C"), NULL); test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1); strmap_assert_ok(map); test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL); done: if (map) strmap_free(map,NULL); if (found_keys) { SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp)); smartlist_free(found_keys); } tor_free(visited); }