1 /* check-sources:disable-copyright-check */
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdbool.h>
7 #include <check.h>
8 #include <droplet.h>
9 #include <droplet/ntinydb.h>
10 
11 #include "utest_main.h"
12 
13 typedef struct {
14   unsigned int nelem;
15   const char** keys;
16   bool* found;
17 } arg_list;
18 
check_keys(const char * k,int kl,void * arg)19 static int check_keys(const char* k, int kl, void* arg)
20 {
21   arg_list* a = (arg_list*)arg;
22   unsigned int i;
23   for (i = 0; i < a->nelem; i++) {
24     if (0 == strncmp(k, a->keys[i], kl)) {
25       a->found[i] = true;
26       break;
27     }
28   }
29   return 0;
30 }
31 
new_pseudorandom_bytes(int n)32 static char* new_pseudorandom_bytes(int n)
33 {
34   unsigned char* x;
35   int i;
36   unsigned long bits;
37   int shift = -1;
38 
39   x = malloc(n);
40   dpl_assert_ptr_not_null(x);
41   for (i = 0; i < n; i++) {
42     if (shift < 0) {
43       bits = lrand48();
44       /* lrand48() always returns 32b regardless of sizeof(long) */
45       shift = 24;
46     }
47     x[i] = (bits >> shift) & 0xff;
48     shift -= 8;
49   }
50   return (char*)x;
51 }
52 
53 
START_TEST(ntinydb_test)54 START_TEST(ntinydb_test)
55 {
56   dpl_sbuf_t* b = dpl_sbuf_new(1);
57   const char* keys[]
58       = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
59          "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
60   char* datas[sizeof(keys) / sizeof(keys[0])];
61   unsigned int i;
62   int ret;
63   dpl_status_t s;
64 
65   srand48(0xdeadbeef);
66 
67   for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
68     datas[i] = new_pseudorandom_bytes(512);
69     s = dpl_ntinydb_set(b, keys[i], datas[i], 512);
70     dpl_assert_int_eq(DPL_SUCCESS, s);
71   }
72   for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
73     const char* datas_check[sizeof(keys) / sizeof(keys[0])];
74     int out_len;
75     s = dpl_ntinydb_get(b->buf, b->len, keys[i], datas_check + i, &out_len);
76     dpl_assert_int_eq(DPL_SUCCESS, s);
77     dpl_assert_int_eq(512, out_len);
78     fail_unless(0 == memcmp(datas_check[i], datas[i], 512));
79   }
80   arg_list all_keys;
81   all_keys.nelem = sizeof(keys) / sizeof(keys[0]);
82   all_keys.keys = keys;
83   all_keys.found = malloc(sizeof(bool) * all_keys.nelem);
84   for (i = 0; i < all_keys.nelem; i++) { all_keys.found[i] = false; }
85 
86   s = dpl_ntinydb_list(b->buf, b->len, check_keys, &all_keys);
87   dpl_assert_int_eq(DPL_SUCCESS, s);
88   for (i = 0; i < all_keys.nelem; i++) {
89     dpl_assert_int_eq(true, all_keys.found[i]);
90   }
91   free(all_keys.found);
92 
93   for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) free(datas[i]);
94   dpl_sbuf_free(b);
95 }
96 END_TEST
97 
98 
ntinydb_suite(void)99 Suite* ntinydb_suite(void)
100 {
101   Suite* s = suite_create("ntinydb");
102   TCase* d = tcase_create("base");
103   tcase_add_test(d, ntinydb_test);
104   suite_add_tcase(s, d);
105   return s;
106 }
107