| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include "common/list.h" |
| 30 |
#include "common/util.h" |
| 31 |
#include "common/_error.h" |
| 32 |
#include "oscap_assert.h" |
| 33 |
|
| 34 |
#define SEEN_LEN 9 |
| 35 |
|
| 36 |
static bool _simple_string_filter(void *first, void *second) |
| 37 |
{ |
| 38 |
return !oscap_strcmp((char *)first, (char *)second); |
| 39 |
} |
| 40 |
|
| 41 |
static void _test_first_item_is_not_skipped(void) |
| 42 |
{ |
| 43 |
|
| 44 |
struct oscap_stringlist *names = oscap_stringlist_new(); |
| 45 |
oscap_assert(oscap_stringlist_add_string(names, "Peter")); |
| 46 |
oscap_assert(oscap_stringlist_add_string(names, "Tomas")); |
| 47 |
oscap_assert(oscap_stringlist_add_string(names, "Peter")); |
| 48 |
struct oscap_string_iterator *it = |
| 49 |
(struct oscap_string_iterator*) oscap_iterator_new_filter( |
| 50 |
(struct oscap_list *) names, |
| 51 |
(oscap_filter_func) _simple_string_filter, |
| 52 |
"Peter"); |
| 53 |
oscap_assert(oscap_string_iterator_has_more(it)); |
| 54 |
oscap_assert(strcmp(oscap_string_iterator_next(it), "Peter") == 0); |
| 55 |
oscap_assert(oscap_string_iterator_has_more(it)); |
| 56 |
oscap_assert(strcmp(oscap_string_iterator_next(it), "Peter") == 0); |
| 57 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 58 |
|
| 59 |
oscap_string_iterator_reset(it); |
| 60 |
oscap_assert(oscap_string_iterator_has_more(it)); |
| 61 |
oscap_assert(strcmp(oscap_string_iterator_next(it), "Peter") == 0); |
| 62 |
oscap_assert(oscap_string_iterator_has_more(it)); |
| 63 |
oscap_assert(strcmp(oscap_string_iterator_next(it), "Peter") == 0); |
| 64 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 65 |
oscap_string_iterator_free(it); |
| 66 |
oscap_stringlist_free(names); |
| 67 |
} |
| 68 |
|
| 69 |
static void _test_not_matching_last_item_is_not_returned(void) |
| 70 |
{ |
| 71 |
|
| 72 |
struct oscap_stringlist *names = oscap_stringlist_new(); |
| 73 |
oscap_assert(oscap_stringlist_add_string(names, "Peter")); |
| 74 |
struct oscap_string_iterator *it = |
| 75 |
(struct oscap_string_iterator*) oscap_iterator_new_filter( |
| 76 |
(struct oscap_list *) names, |
| 77 |
(oscap_filter_func) _simple_string_filter, |
| 78 |
"Tomas"); |
| 79 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 80 |
oscap_string_iterator_reset(it); |
| 81 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 82 |
oscap_string_iterator_free(it); |
| 83 |
oscap_stringlist_free(names); |
| 84 |
} |
| 85 |
|
| 86 |
static void _test_empty_list_has_more(void) |
| 87 |
{ |
| 88 |
struct oscap_stringlist *names = oscap_stringlist_new(); |
| 89 |
struct oscap_string_iterator *it = oscap_stringlist_get_strings(names); |
| 90 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 91 |
oscap_string_iterator_reset(it); |
| 92 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 93 |
oscap_string_iterator_free(it); |
| 94 |
oscap_stringlist_free(names); |
| 95 |
} |
| 96 |
|
| 97 |
static void _test_empty_list_filter_has_more(void) |
| 98 |
{ |
| 99 |
struct oscap_stringlist *names = oscap_stringlist_new(); |
| 100 |
struct oscap_string_iterator *it = |
| 101 |
(struct oscap_string_iterator*) oscap_iterator_new_filter( |
| 102 |
(struct oscap_list *) names, |
| 103 |
(oscap_filter_func) _simple_string_filter, |
| 104 |
NULL); |
| 105 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 106 |
oscap_string_iterator_reset(it); |
| 107 |
oscap_assert(oscap_string_iterator_has_more(it) == false); |
| 108 |
oscap_string_iterator_free(it); |
| 109 |
oscap_stringlist_free(names); |
| 110 |
} |
| 111 |
|
| 112 |
static void _test_hit_empty(void) |
| 113 |
{ |
| 114 |
struct oscap_htable *h = oscap_htable_new(); |
| 115 |
struct oscap_htable_iterator *hit = oscap_htable_iterator_new(h); |
| 116 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 117 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 118 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 119 |
oscap_htable_iterator_free(hit); |
| 120 |
oscap_htable_free0(h); |
| 121 |
} |
| 122 |
|
| 123 |
static void _test_hit_single_item(void) |
| 124 |
{ |
| 125 |
static const char *key = "id-12345"; |
| 126 |
static const char *value = "openscap!"; |
| 127 |
struct oscap_htable *h = oscap_htable_new(); |
| 128 |
oscap_assert(oscap_htable_add(h, key, (char *) value)); |
| 129 |
struct oscap_htable_iterator *hit = oscap_htable_iterator_new(h); |
| 130 |
oscap_assert(oscap_htable_iterator_has_more(hit)); |
| 131 |
const struct oscap_htable_item *item = oscap_htable_iterator_next(hit); |
| 132 |
oscap_assert(item != NULL); |
| 133 |
oscap_assert(strcmp(item->key, key) == 0); |
| 134 |
oscap_assert(strcmp(item->value, value) == 0); |
| 135 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 136 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 137 |
oscap_htable_iterator_free(hit); |
| 138 |
oscap_htable_free0(h); |
| 139 |
} |
| 140 |
|
| 141 |
static void _test_hit_multiple_items(void) |
| 142 |
{ |
| 143 |
bool seen[SEEN_LEN]; |
| 144 |
struct oscap_htable *h = oscap_htable_new(); |
| 145 |
for (int i = 0; i < SEEN_LEN; i++) { |
| 146 |
char key[12]; |
| 147 |
snprintf(key, 12, "%d", i); |
| 148 |
oscap_assert(oscap_htable_add(h, key, NULL)); |
| 149 |
seen[i] = false; |
| 150 |
} |
| 151 |
|
| 152 |
struct oscap_htable_iterator *hit = oscap_htable_iterator_new(h); |
| 153 |
oscap_assert(oscap_htable_iterator_has_more(hit)); |
| 154 |
int i = 0; |
| 155 |
while (oscap_htable_iterator_has_more(hit)) { |
| 156 |
const char *key = oscap_htable_iterator_next_key(hit); |
| 157 |
oscap_assert(key != NULL); |
| 158 |
oscap_assert(strlen(key) == 1); |
| 159 |
seen[key[0] - '0'] = true; |
| 160 |
i++; |
| 161 |
} |
| 162 |
oscap_assert(i == SEEN_LEN); |
| 163 |
oscap_htable_iterator_free(hit); |
| 164 |
for (i = 0; i < SEEN_LEN; i++) { |
| 165 |
oscap_assert(seen[i]); |
| 166 |
} |
| 167 |
oscap_htable_free0(h); |
| 168 |
} |
| 169 |
|
| 170 |
static int _htable_cmp(const char *s1, const char *s2) |
| 171 |
{ |
| 172 |
if (s1 == NULL) |
| 173 |
return -1; |
| 174 |
if (s2 == NULL) |
| 175 |
return 1; |
| 176 |
return strcmp(s1, s2); |
| 177 |
} |
| 178 |
|
| 179 |
static void _test_hit_empty1(void) |
| 180 |
{ |
| 181 |
struct oscap_htable *h = oscap_htable_new1(_htable_cmp, 1); |
| 182 |
struct oscap_htable_iterator *hit = oscap_htable_iterator_new(h); |
| 183 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 184 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 185 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 186 |
oscap_htable_iterator_free(hit); |
| 187 |
oscap_htable_free0(h); |
| 188 |
} |
| 189 |
|
| 190 |
static void _test_hit_single_item1(void) |
| 191 |
{ |
| 192 |
static const char *key = "id-12345"; |
| 193 |
static const char *value = "openscap!"; |
| 194 |
struct oscap_htable *h = oscap_htable_new1(_htable_cmp, 1); |
| 195 |
oscap_assert(oscap_htable_add(h, key, (char *) value)); |
| 196 |
struct oscap_htable_iterator *hit = oscap_htable_iterator_new(h); |
| 197 |
oscap_assert(oscap_htable_iterator_has_more(hit)); |
| 198 |
const struct oscap_htable_item *item = oscap_htable_iterator_next(hit); |
| 199 |
oscap_assert(item != NULL); |
| 200 |
oscap_assert(strcmp(item->key, key) == 0); |
| 201 |
oscap_assert(strcmp(item->value, value) == 0); |
| 202 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 203 |
oscap_assert(!oscap_htable_iterator_has_more(hit)); |
| 204 |
oscap_htable_iterator_free(hit); |
| 205 |
oscap_htable_free0(h); |
| 206 |
} |
| 207 |
|
| 208 |
static void _test_hit_multiple_items1(void) |
| 209 |
{ |
| 210 |
bool seen[SEEN_LEN]; |
| 211 |
struct oscap_htable *h = oscap_htable_new1(_htable_cmp, 1); |
| 212 |
for (int i = 0; i < SEEN_LEN; i++) { |
| 213 |
char key[12]; |
| 214 |
snprintf(key, 12, "%d", i); |
| 215 |
oscap_assert(oscap_htable_add(h, key, NULL)); |
| 216 |
seen[i] = false; |
| 217 |
} |
| 218 |
|
| 219 |
struct oscap_htable_iterator *hit = oscap_htable_iterator_new(h); |
| 220 |
oscap_assert(oscap_htable_iterator_has_more(hit)); |
| 221 |
int i = 0; |
| 222 |
while (oscap_htable_iterator_has_more(hit)) { |
| 223 |
const char *key = oscap_htable_iterator_next_key(hit); |
| 224 |
oscap_assert(key != NULL); |
| 225 |
oscap_assert(strlen(key) == 1); |
| 226 |
seen[key[0] - '0'] = true; |
| 227 |
i++; |
| 228 |
} |
| 229 |
oscap_assert(i == SEEN_LEN); |
| 230 |
oscap_htable_iterator_free(hit); |
| 231 |
for (i = 0; i < SEEN_LEN; i++) { |
| 232 |
oscap_assert(seen[i]); |
| 233 |
} |
| 234 |
oscap_htable_free0(h); |
| 235 |
} |
| 236 |
|
| 237 |
static bool _test_list_remove_ptreq(void *a, void *b) |
| 238 |
{ |
| 239 |
return a == b; |
| 240 |
} |
| 241 |
|
| 242 |
static void _test_list_check_sanity(struct oscap_list *list) |
| 243 |
{ |
| 244 |
struct oscap_list_item *cur = list->first; |
| 245 |
|
| 246 |
int count = list->first == NULL ? 0 : 1; |
| 247 |
while (cur != list->last) { |
| 248 |
|
| 249 |
cur = cur->next; |
| 250 |
++count; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
oscap_assert(oscap_list_get_itemcount(list) == count); |
| 256 |
oscap_assert(list->last->next == NULL); |
| 257 |
oscap_assert(count > 0 || list->first == NULL); |
| 258 |
} |
| 259 |
|
| 260 |
static void _test_list_remove(void) |
| 261 |
{ |
| 262 |
void *a = (void*)1; |
| 263 |
void *b = (void*)2; |
| 264 |
void *c = (void*)3; |
| 265 |
void *d = (void*)4; |
| 266 |
|
| 267 |
struct oscap_list *list = oscap_list_new(); |
| 268 |
|
| 269 |
|
| 270 |
oscap_assert(!oscap_list_remove(list, a, (oscap_cmp_func)_test_list_remove_ptreq, NULL)); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
oscap_assert(oscap_list_add(list, a)); |
| 279 |
oscap_assert(oscap_list_add(list, b)); |
| 280 |
_test_list_check_sanity(list); |
| 281 |
oscap_list_remove(list, b, (oscap_cmp_func)_test_list_remove_ptreq, NULL); |
| 282 |
_test_list_check_sanity(list); |
| 283 |
oscap_assert(oscap_list_get_itemcount(list) == 1); |
| 284 |
oscap_assert(oscap_list_contains(list, a, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 285 |
oscap_assert(!oscap_list_contains(list, b, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 286 |
|
| 287 |
oscap_assert(oscap_list_remove(list, a, (oscap_cmp_func)_test_list_remove_ptreq, NULL)); |
| 288 |
oscap_assert(oscap_list_get_itemcount(list) == 0); |
| 289 |
oscap_assert(!oscap_list_contains(list, a, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 290 |
|
| 291 |
|
| 292 |
oscap_assert(oscap_list_add(list, a)); |
| 293 |
oscap_assert(oscap_list_add(list, b)); |
| 294 |
oscap_assert(oscap_list_add(list, c)); |
| 295 |
_test_list_check_sanity(list); |
| 296 |
oscap_assert(oscap_list_remove(list, c, (oscap_cmp_func)_test_list_remove_ptreq, NULL)); |
| 297 |
_test_list_check_sanity(list); |
| 298 |
oscap_assert(oscap_list_contains(list, a, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 299 |
oscap_assert(oscap_list_contains(list, b, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 300 |
oscap_assert(!oscap_list_contains(list, c, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 301 |
|
| 302 |
|
| 303 |
oscap_assert(oscap_list_add(list, c)); |
| 304 |
oscap_assert(oscap_list_add(list, d)); |
| 305 |
_test_list_check_sanity(list); |
| 306 |
oscap_assert(oscap_list_get_itemcount(list) == 4); |
| 307 |
oscap_assert(oscap_list_remove(list, b, (oscap_cmp_func)_test_list_remove_ptreq, NULL)); |
| 308 |
_test_list_check_sanity(list); |
| 309 |
oscap_assert(oscap_list_contains(list, a, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 310 |
oscap_assert(!oscap_list_contains(list, b, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 311 |
oscap_assert(oscap_list_contains(list, c, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 312 |
oscap_assert(oscap_list_contains(list, d, (oscap_cmp_func)_test_list_remove_ptreq)); |
| 313 |
oscap_assert(oscap_list_get_itemcount(list) == 3); |
| 314 |
|
| 315 |
oscap_list_free(list, NULL); |
| 316 |
} |
| 317 |
|
| 318 |
int main(int argc, char *argv[]) |
| 319 |
{ |
| 320 |
_test_first_item_is_not_skipped(); |
| 321 |
_test_not_matching_last_item_is_not_returned(); |
| 322 |
_test_empty_list_has_more(); |
| 323 |
_test_empty_list_filter_has_more(); |
| 324 |
|
| 325 |
_test_hit_empty(); |
| 326 |
_test_hit_single_item(); |
| 327 |
_test_hit_multiple_items(); |
| 328 |
_test_hit_empty1(); |
| 329 |
_test_hit_single_item1(); |
| 330 |
_test_hit_multiple_items1(); |
| 331 |
|
| 332 |
_test_list_remove(); |
| 333 |
|
| 334 |
return 0; |
| 335 |
} |