s3: VFS: solarisacl: Add solarisacl_sys_acl_delete_def_fd().
[Samba.git] / lib / util / tests / test_memcache.c
blob8a3997817c11281ce3f840121ed6bb5fd51ecec5
1 /*
2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2021 Andreas Schneider <asn@samba.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <setjmp.h>
24 #include <cmocka.h>
26 #include "lib/replace/replace.h"
27 #include "lib/util/talloc_stack.h"
28 #include "lib/util/memcache.h"
30 static int setup_talloc_context(void **state)
32 TALLOC_CTX *frame = talloc_stackframe();
34 *state = frame;
35 return 0;
38 static int teardown_talloc_context(void **state)
40 TALLOC_CTX *frame = *state;
41 TALLOC_FREE(frame);
42 return 0;
45 static void torture_memcache_init(void **state)
47 TALLOC_CTX *mem_ctx = *state;
48 struct memcache *cache = NULL;
50 cache = memcache_init(mem_ctx, 0);
51 assert_non_null(cache);
53 TALLOC_FREE(cache);
55 cache = memcache_init(mem_ctx, 10);
56 assert_non_null(cache);
58 TALLOC_FREE(cache);
61 static void torture_memcache_add_lookup_delete(void **state)
63 TALLOC_CTX *mem_ctx = *state;
64 struct memcache *cache = NULL;
65 DATA_BLOB key1, key2;
66 char *path1 = NULL, *path2 = NULL;
68 cache = memcache_init(mem_ctx, 0);
69 assert_non_null(cache);
71 key1 = data_blob_const("key1", 4);
72 path1 = talloc_strdup(mem_ctx, "/tmp/one");
73 assert_non_null(path1);
75 key2 = data_blob_const("key2", 4);
76 path2 = talloc_strdup(mem_ctx, "/tmp/two");
77 assert_non_null(path1);
79 memcache_add_talloc(cache, GETWD_CACHE, key1, &path1);
80 assert_null(path1);
82 memcache_add_talloc(cache, GETWD_CACHE, key2, &path2);
83 assert_null(path2);
85 path1 = memcache_lookup_talloc(cache, GETWD_CACHE, key1);
86 assert_non_null(path1);
87 assert_string_equal(path1, "/tmp/one");
89 path2 = memcache_lookup_talloc(cache, GETWD_CACHE, key2);
90 assert_non_null(path2);
91 assert_string_equal(path2, "/tmp/two");
93 memcache_delete(cache, GETWD_CACHE, key1);
94 path1 = memcache_lookup_talloc(cache, GETWD_CACHE, key1);
95 assert_null(path1);
97 memcache_flush(cache, GETWD_CACHE);
98 path2 = memcache_lookup_talloc(cache, GETWD_CACHE, key2);
99 assert_null(path2);
101 TALLOC_FREE(path1);
102 TALLOC_FREE(path2);
103 TALLOC_FREE(cache);
106 static void torture_memcache_add_oversize(void **state)
108 TALLOC_CTX *mem_ctx = *state;
109 struct memcache *cache = NULL;
110 DATA_BLOB key1, key2;
111 char *path1 = NULL, *path2 = NULL;
113 cache = memcache_init(mem_ctx, 10);
114 assert_non_null(cache);
116 key1 = data_blob_const("key1", 4);
117 path1 = talloc_strdup(mem_ctx, "/tmp/one");
118 assert_non_null(path1);
120 key2 = data_blob_const("key2", 4);
121 path2 = talloc_strdup(mem_ctx, "/tmp/two");
122 assert_non_null(path1);
124 memcache_add_talloc(cache, GETWD_CACHE, key1, &path1);
125 assert_null(path1);
127 memcache_add_talloc(cache, GETWD_CACHE, key2, &path2);
128 assert_null(path2);
130 path1 = memcache_lookup_talloc(cache, GETWD_CACHE, key1);
131 assert_null(path1);
133 path2 = memcache_lookup_talloc(cache, GETWD_CACHE, key2);
134 assert_non_null(path2);
135 assert_string_equal(path2, "/tmp/two");
137 TALLOC_FREE(path1);
138 TALLOC_FREE(path2);
139 TALLOC_FREE(cache);
142 int main(int argc, char *argv[])
144 int rc;
145 const struct CMUnitTest tests[] = {
146 cmocka_unit_test(torture_memcache_init),
147 cmocka_unit_test(torture_memcache_add_lookup_delete),
148 cmocka_unit_test(torture_memcache_add_oversize),
151 if (argc == 2) {
152 cmocka_set_test_filter(argv[1]);
154 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
156 rc = cmocka_run_group_tests(tests,
157 setup_talloc_context,
158 teardown_talloc_context);
160 return rc;