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/>.
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();
38 static int teardown_talloc_context(void **state
)
40 TALLOC_CTX
*frame
= *state
;
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
);
55 cache
= memcache_init(mem_ctx
, 10);
56 assert_non_null(cache
);
61 static void torture_memcache_add_lookup_delete(void **state
)
63 TALLOC_CTX
*mem_ctx
= *state
;
64 struct memcache
*cache
= NULL
;
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
);
82 memcache_add_talloc(cache
, GETWD_CACHE
, key2
, &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
);
97 memcache_flush(cache
, GETWD_CACHE
);
98 path2
= memcache_lookup_talloc(cache
, GETWD_CACHE
, key2
);
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
);
127 memcache_add_talloc(cache
, GETWD_CACHE
, key2
, &path2
);
130 path1
= memcache_lookup_talloc(cache
, GETWD_CACHE
, key1
);
133 path2
= memcache_lookup_talloc(cache
, GETWD_CACHE
, key2
);
134 assert_non_null(path2
);
135 assert_string_equal(path2
, "/tmp/two");
142 int main(int argc
, char *argv
[])
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
),
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
);