1 /**
2  * @file
3  * Common code for store tests
4  *
5  * @authors
6  * Copyright (C) 2020 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #define TEST_NO_MAIN
24 #include "config.h"
25 #include "acutest.h"
26 #include <stdbool.h>
27 #include "mutt/lib.h"
28 #include "store/lib.h"
29 #include "test_common.h"
30 
test_store_setup(char * buf,size_t buflen)31 bool test_store_setup(char *buf, size_t buflen)
32 {
33   if (!buf)
34     return false;
35 
36   test_gen_path(buf, buflen, "%s/tmp/XXXXXX");
37 
38   if (!mkdtemp(buf))
39     return false;
40 
41   return true;
42 }
43 
test_store_degenerate(const struct StoreOps * sops,const char * name)44 bool test_store_degenerate(const struct StoreOps *sops, const char *name)
45 {
46   if (!sops)
47     return false;
48 
49   if (!TEST_CHECK(mutt_str_equal(sops->name, name)))
50     return false;
51 
52   if (!TEST_CHECK(sops->open(NULL) == NULL))
53     return false;
54 
55   if (!TEST_CHECK(sops->fetch(NULL, NULL, 0, NULL) == NULL))
56     return false;
57 
58   void *ptr = NULL;
59   sops->free(NULL, NULL);
60   TEST_CHECK_(1, "sops->free(NULL, NULL)");
61   sops->free(NULL, &ptr);
62   TEST_CHECK_(1, "sops->free(NULL, ptr)");
63 
64   if (!TEST_CHECK(sops->store(NULL, NULL, 0, NULL, 0) != 0))
65     return false;
66 
67   if (!TEST_CHECK(sops->delete_record(NULL, NULL, 0) != 0))
68     return false;
69 
70   sops->close(NULL);
71   TEST_CHECK_(1, "sops->close(NULL)");
72 
73   sops->close(&ptr);
74   TEST_CHECK_(1, "sops->close(&ptr)");
75 
76   if (!TEST_CHECK(sops->version() != NULL))
77     return false;
78 
79   return true;
80 }
81 
test_store_db(const struct StoreOps * sops,void * db)82 bool test_store_db(const struct StoreOps *sops, void *db)
83 {
84   if (!sops || !db)
85     return false;
86 
87   const char *key = "one";
88   size_t klen = strlen(key);
89   char *value = "abcdefghijklmnopqrstuvwxyz";
90   size_t vlen = strlen(value);
91   int rc;
92 
93   rc = sops->store(db, key, klen, value, vlen);
94   if (!TEST_CHECK(rc == 0))
95     return false;
96 
97   void *data = NULL;
98   vlen = 0;
99   data = sops->fetch(db, key, klen, &vlen);
100   if (!TEST_CHECK(data != NULL))
101     return false;
102 
103   sops->free(db, &data);
104   TEST_CHECK_(1, "sops->free(db, &data)");
105 
106   rc = sops->delete_record(db, key, klen);
107   if (!TEST_CHECK(rc == 0))
108     return false;
109 
110   return true;
111 }
112