LCOV - code coverage report
Current view: top level - lib/util/tests - test_memcache.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 84 85 98.8 %
Date: 2024-02-28 12:06:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  *
       4             :  * Copyright (C) 2021      Andreas Schneider <asn@samba.org>
       5             :  *
       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.
      10             :  *
      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.
      15             :  *
      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/>.
      18             :  */
      19             : 
      20             : #include <stdarg.h>
      21             : #include <stddef.h>
      22             : #include <stdint.h>
      23             : #include <setjmp.h>
      24             : #include <cmocka.h>
      25             : 
      26             : #include "lib/replace/replace.h"
      27             : #include "lib/util/talloc_stack.h"
      28             : #include "lib/util/memcache.h"
      29             : 
      30           1 : static int setup_talloc_context(void **state)
      31             : {
      32           1 :         TALLOC_CTX *frame = talloc_stackframe();
      33             : 
      34           1 :         *state = frame;
      35           1 :         return 0;
      36             : }
      37             : 
      38           1 : static int teardown_talloc_context(void **state)
      39             : {
      40           1 :         TALLOC_CTX *frame = *state;
      41           1 :         TALLOC_FREE(frame);
      42           1 :         return 0;
      43             : }
      44             : 
      45           1 : static void torture_memcache_init(void **state)
      46             : {
      47           1 :         TALLOC_CTX *mem_ctx = *state;
      48           1 :         struct memcache *cache = NULL;
      49             : 
      50           1 :         cache = memcache_init(mem_ctx, 0);
      51           1 :         assert_non_null(cache);
      52             : 
      53           1 :         TALLOC_FREE(cache);
      54             : 
      55           1 :         cache = memcache_init(mem_ctx, 10);
      56           1 :         assert_non_null(cache);
      57             : 
      58           1 :         TALLOC_FREE(cache);
      59           1 : }
      60             : 
      61           1 : static void torture_memcache_add_lookup_delete(void **state)
      62             : {
      63           1 :         TALLOC_CTX *mem_ctx = *state;
      64           1 :         struct memcache *cache = NULL;
      65           1 :         DATA_BLOB key1, key2;
      66           1 :         char *path1 = NULL, *path2 = NULL;
      67             : 
      68           1 :         cache = memcache_init(mem_ctx, 0);
      69           1 :         assert_non_null(cache);
      70             : 
      71           1 :         key1 = data_blob_const("key1", 4);
      72           1 :         path1 = talloc_strdup(mem_ctx, "/tmp/one");
      73           1 :         assert_non_null(path1);
      74             : 
      75           1 :         key2 = data_blob_const("key2", 4);
      76           1 :         path2 = talloc_strdup(mem_ctx, "/tmp/two");
      77           1 :         assert_non_null(path1);
      78             : 
      79           1 :         memcache_add_talloc(cache, GETWD_CACHE, key1, &path1);
      80           1 :         assert_null(path1);
      81             : 
      82           1 :         memcache_add_talloc(cache, GETWD_CACHE, key2, &path2);
      83           1 :         assert_null(path2);
      84             : 
      85           1 :         path1 = memcache_lookup_talloc(cache, GETWD_CACHE, key1);
      86           1 :         assert_non_null(path1);
      87           1 :         assert_string_equal(path1, "/tmp/one");
      88             : 
      89           1 :         path2 = memcache_lookup_talloc(cache, GETWD_CACHE, key2);
      90           1 :         assert_non_null(path2);
      91           1 :         assert_string_equal(path2, "/tmp/two");
      92             : 
      93           1 :         memcache_delete(cache, GETWD_CACHE, key1);
      94           1 :         path1 = memcache_lookup_talloc(cache, GETWD_CACHE, key1);
      95           1 :         assert_null(path1);
      96             : 
      97           1 :         memcache_flush(cache, GETWD_CACHE);
      98           1 :         path2 = memcache_lookup_talloc(cache, GETWD_CACHE, key2);
      99           1 :         assert_null(path2);
     100             : 
     101           1 :         TALLOC_FREE(path1);
     102           1 :         TALLOC_FREE(path2);
     103           1 :         TALLOC_FREE(cache);
     104           1 : }
     105             : 
     106           1 : static void torture_memcache_add_oversize(void **state)
     107             : {
     108           1 :         TALLOC_CTX *mem_ctx = *state;
     109           1 :         struct memcache *cache = NULL;
     110           1 :         DATA_BLOB key1, key2;
     111           1 :         char *path1 = NULL, *path2 = NULL;
     112             : 
     113           1 :         cache = memcache_init(mem_ctx, 10);
     114           1 :         assert_non_null(cache);
     115             : 
     116           1 :         key1 = data_blob_const("key1", 4);
     117           1 :         path1 = talloc_strdup(mem_ctx, "/tmp/one");
     118           1 :         assert_non_null(path1);
     119             : 
     120           1 :         key2 = data_blob_const("key2", 4);
     121           1 :         path2 = talloc_strdup(mem_ctx, "/tmp/two");
     122           1 :         assert_non_null(path1);
     123             : 
     124           1 :         memcache_add_talloc(cache, GETWD_CACHE, key1, &path1);
     125           1 :         assert_null(path1);
     126             : 
     127           1 :         memcache_add_talloc(cache, GETWD_CACHE, key2, &path2);
     128           1 :         assert_null(path2);
     129             : 
     130           1 :         path1 = memcache_lookup_talloc(cache, GETWD_CACHE, key1);
     131           1 :         assert_null(path1);
     132             : 
     133           1 :         path2 = memcache_lookup_talloc(cache, GETWD_CACHE, key2);
     134           1 :         assert_non_null(path2);
     135           1 :         assert_string_equal(path2, "/tmp/two");
     136             : 
     137           1 :         TALLOC_FREE(path1);
     138           1 :         TALLOC_FREE(path2);
     139           1 :         TALLOC_FREE(cache);
     140           1 : }
     141             : 
     142           1 : int main(int argc, char *argv[])
     143             : {
     144           1 :         int rc;
     145           1 :         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),
     149             :         };
     150             : 
     151           1 :         if (argc == 2) {
     152           0 :                 cmocka_set_test_filter(argv[1]);
     153             :         }
     154           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     155             : 
     156           1 :         rc = cmocka_run_group_tests(tests,
     157             :                                     setup_talloc_context,
     158             :                                     teardown_talloc_context);
     159             : 
     160           1 :         return rc;
     161             : }

Generated by: LCOV version 1.14