]> git.proxmox.com Git - pve-cluster.git/blob - data/src/check_memdb.c
63f096e2bf6f12bdcafa7093173469482017c9cb
[pve-cluster.git] / data / src / check_memdb.c
1 /*
2 Copyright (C) 2010 - 2020 Proxmox Server Solutions GmbH
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Affero General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 Author: Dietmar Maurer <dietmar@proxmox.com>
18
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <check.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #include "cfs-utils.h"
31 #include "status.h"
32 #include "memdb.h"
33
34 cfs_t cfs = {
35 .debug = 0,
36 .nodename = "testnode",
37 };
38
39 #define TESTDB "/tmp/test.db"
40
41 static memdb_t *memdb;
42
43 static void
44 setup(void)
45 {
46 unlink(TESTDB);
47 memdb = memdb_open(TESTDB);
48 ck_assert (memdb != NULL);
49
50 struct statvfs stbuf;
51 ck_assert(memdb_statfs(memdb, &stbuf) == 0);
52
53 int count = stbuf.f_files - stbuf.f_ffree;
54 ck_assert(count == 1);
55 }
56
57 void
58 teardown(void)
59 {
60 ck_assert (memdb != NULL);
61
62 memdb_close(memdb);
63 }
64
65 START_TEST(test_indextest1)
66 {
67 char namebuf[100];
68
69 time_t ctime = 1234;
70 int testsize = 1024*32;
71 gchar *testdata = g_malloc0(testsize);
72
73 for (int i = 0; i < 100; i++) {
74 sprintf(namebuf, "testfile%d", i);
75
76 ck_assert(memdb_create(memdb, namebuf, 0, ctime) == 0);
77 ck_assert(memdb_write(memdb, namebuf, 0, ctime, testdata, testsize, 0, 0) == testsize);
78 }
79
80 struct statvfs stbuf;
81 ck_assert(memdb_statfs(memdb, &stbuf) == 0);
82
83 int count = stbuf.f_files - stbuf.f_ffree;
84 ck_assert(count == 101);
85
86 memdb_index_t *idx = memdb_encode_index(memdb->index, memdb->root);
87 ck_assert(idx != NULL);
88
89 ck_assert(idx->version == 201);
90 ck_assert(idx->last_inode == 200);
91 ck_assert(idx->writer == 0);
92 ck_assert(idx->size == 101);
93 ck_assert(idx->bytes == (101*40 + sizeof( memdb_index_t)));
94
95 GChecksum *sha256 = g_checksum_new(G_CHECKSUM_SHA256);
96 ck_assert(sha256 != NULL);
97 g_checksum_update(sha256, (unsigned char *)idx, idx->bytes);
98 const char *csum = g_checksum_get_string(sha256);
99 ck_assert_msg(
100 strcmp(csum, "913fd95015af9d93f10dd51ba2a7bb11351bcfe040be21e95fcba834adc3ec10") == 0,
101 "wrong idx checksum %s",
102 csum
103 );
104 g_free(idx);
105 g_free(testdata);
106
107 }
108 END_TEST
109
110 START_TEST (test_dirtest1)
111 {
112 const char *dn = "/dir1";
113 const char *sdn = "/dir1/sdir1";
114 time_t ctime = 1234;
115
116 ck_assert(memdb_mkdir(memdb, sdn, 0, ctime) == -ENOENT);
117 ck_assert(memdb_delete(memdb, dn, 0, ctime) == -ENOENT);
118
119 ck_assert(memdb_mkdir(memdb, dn, 0, ctime) == 0);
120 ck_assert(memdb_mkdir(memdb, dn, 0, ctime) == -EEXIST);
121 ck_assert(memdb_mkdir(memdb, sdn, 0, ctime) == 0);
122 ck_assert(memdb_mkdir(memdb, sdn, 0, ctime) == -EEXIST);
123 ck_assert(memdb_delete(memdb, dn, 0, ctime) == -ENOTEMPTY);
124 ck_assert(memdb_delete(memdb, sdn, 0, ctime) == 0);
125 ck_assert(memdb_delete(memdb, dn, 0, ctime) == 0);
126 }
127 END_TEST
128
129 START_TEST (test_filetest1)
130 {
131 const char *dn = "/dir1";
132 const char *fn = "/dir1/f1";
133 time_t ctime = 1234;
134 gpointer data;
135
136 char buf[1024];
137 memset(buf, 0, sizeof(buf));
138
139 ck_assert(memdb_read(memdb, fn, &data) == -ENOENT);
140
141 ck_assert(memdb_mkdir(memdb, dn, 0, ctime) == 0);
142
143 ck_assert(memdb_read(memdb, fn, &data) == -ENOENT);
144
145 ck_assert(memdb_write(memdb, fn, 0, ctime, buf, sizeof(buf), 0, 0) == -ENOENT);
146
147 ck_assert(memdb_create(memdb, fn, 0, ctime) == 0);
148
149 ck_assert(memdb_write(memdb, fn, 0, ctime, buf, sizeof(buf), 0, 0) == sizeof(buf));
150
151 ck_assert(memdb_read(memdb, fn, &data) == sizeof(buf));
152
153 ck_assert(memcmp(buf, data, sizeof(buf)) == 0);
154
155 g_free(data);
156
157 ck_assert(memdb_write(memdb, fn, 0, ctime, "0123456789", 10, 0, 1) == 10);
158
159 ck_assert(memdb_read(memdb, fn, &data) == 10);
160 g_free(data);
161
162 ck_assert(memdb_write(memdb, fn, 0, ctime, "X", 1, 3, 0) == 1);
163
164 ck_assert(memdb_write(memdb, fn, 0, ctime, "X", 1, 6, 0) == 1);
165
166 ck_assert(memdb_read(memdb, fn, &data) == 10);
167
168 ck_assert(strncmp(data, "012X45X789", 10) == 0);
169 g_free(data);
170
171 ck_assert(memdb_delete(memdb, fn, 0, ctime) == 0);
172
173 ck_assert(memdb_delete(memdb, fn, 0, ctime) == -ENOENT);
174
175 ck_assert(memdb_delete(memdb, dn, 0, ctime) == 0);
176 }
177 END_TEST
178
179 /* Nornmaly, parent inode number is always less than contained inode,
180 * but this is not allways the case. A simple move can destroy that
181 * ordering. This code test the placeholder algorithm in
182 * bdb_backend_load_index()
183 */
184 START_TEST (test_loaddb1)
185 {
186 time_t ctime = 1234;
187
188 ck_assert(memdb_mkdir(memdb, "dir1", 0, ctime) == 0);
189
190 ck_assert(memdb_create(memdb, "dir1/file1", 0, ctime) == 0);
191
192 ck_assert(memdb_create(memdb, "dir1/file2", 0, ctime) == 0);
193
194 ck_assert(memdb_mkdir(memdb, "dir2", 0, ctime) == 0);
195
196 ck_assert(memdb_rename(memdb, "dir1/file1", "dir2/file1", 0, ctime) == 0);
197
198 ck_assert(memdb_rename(memdb, "dir1/file2", "dir2/file2", 0, ctime) == 0);
199
200 ck_assert(memdb_create(memdb, "dir2/file1", 0, ctime) == -EEXIST);
201
202 ck_assert(memdb_create(memdb, "dir2/file2", 0, ctime) == -EEXIST);
203
204 //memdb_dump(memdb);
205
206 memdb_close(memdb);
207
208 memdb = memdb_open(TESTDB);
209 ck_assert (memdb != NULL);
210
211 ck_assert(memdb_create(memdb, "dir2/file1", 0, ctime) == -EEXIST);
212
213 ck_assert(memdb_create(memdb, "dir2/file2", 0, ctime) == -EEXIST);
214
215 //memdb_dump(memdb);
216
217 }
218 END_TEST
219
220 START_TEST (test_loaddb2)
221 {
222 time_t ctime = 1234;
223
224 ck_assert(memdb_mkdir(memdb, "dir1", 0, ctime) == 0);
225
226 ck_assert(memdb_mkdir(memdb, "dir1/sd1", 0, ctime) == 0);
227
228 ck_assert(memdb_create(memdb, "dir1/file1", 0, ctime) == 0);
229
230 ck_assert(memdb_create(memdb, "dir1/file2", 0, ctime) == 0);
231
232 ck_assert(memdb_mkdir(memdb, "dir2", 0, ctime) == 0);
233
234 ck_assert(memdb_rename(memdb, "dir1/sd1", "dir2/sd1", 0, ctime) == 0);
235
236 ck_assert(memdb_rename(memdb, "dir1/file1", "dir2/sd1/file1", 0, ctime) == 0);
237
238 ck_assert(memdb_rename(memdb, "dir1/file2", "dir2/sd1/file2", 0, ctime) == 0);
239
240 ck_assert(memdb_create(memdb, "dir2/file3", 0, ctime) == 0);
241
242 ck_assert(memdb_mkdir(memdb, "dir2/sd1", 0, ctime) == -EEXIST);
243
244 //memdb_dump(memdb);
245
246 memdb_close(memdb);
247
248 memdb = memdb_open(TESTDB);
249 ck_assert (memdb != NULL);
250
251 ck_assert(memdb_mkdir(memdb, "dir2/sd1", 0, ctime) == -EEXIST);
252
253 //memdb_dump(memdb);
254
255 }
256 END_TEST
257
258 static void
259 add_test(
260 Suite *s,
261 TFun tf,
262 const char *name)
263 {
264 TCase *tc = tcase_create (name);
265 tcase_add_checked_fixture (tc, setup, teardown);
266 tcase_add_test (tc, tf);
267 suite_add_tcase (s, tc);
268 }
269
270 static Suite *
271 memdb_suite(void)
272 {
273 Suite *s = suite_create ("memdb");
274
275 add_test(s, test_dirtest1, "dirtest1");
276
277 add_test(s, test_filetest1, "filetest1");
278
279 add_test(s, test_indextest1, "indextest1");
280
281 add_test(s, test_loaddb1, "loaddb1");
282
283 add_test(s, test_loaddb2, "loaddb2");
284
285 return s;
286 }
287
288 int
289 main(void)
290 {
291 int number_failed;
292
293 cfs_status_init();
294
295 Suite *s = memdb_suite();
296 SRunner *sr = srunner_create(s);
297 srunner_run_all(sr, CK_NORMAL);
298 number_failed = srunner_ntests_failed(sr);
299 srunner_free(sr);
300
301 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
302 }
303