MIDAS
Loading...
Searching...
No Matches
test_odb.cxx
Go to the documentation of this file.
1//
2// test_mvodb.cxx --- test ODB access functions
3//
4// K.Olchanski
5//
6
7#include <stdio.h>
8#include <sys/time.h>
9#include <iostream>
10#include <assert.h>
11#include <signal.h>
12#include <string.h>
13#include <stdlib.h> // exit()
14
15#include "tmfe.h"
16#include "midas.h"
17
18std::string toString(int i)
19{
20 char buf[256];
21 sprintf(buf, "%d", i);
22 return buf;
23}
24
25void print_ia(const std::vector<int> &ia)
26{
27 int size = ia.size();
28 printf("int[%d] has [", size);
29 for (int i=0; i<size; i++) {
30 if (i>0)
31 printf(", ");
32 printf("%d", ia[i]);
33 }
34 printf("]");
35}
36
37void print_da(const std::vector<double> &da)
38{
39 int size = da.size();
40 printf("int[%d] has [", size);
41 for (int i=0; i<size; i++) {
42 if (i>0)
43 printf(", ");
44 printf("%f", da[i]);
45 }
46 printf("]");
47}
48
49static int gCountFail = 0;
50
51bool report_fail(const char* text)
52{
53 printf("FAIL: %s\n", text);
54 gCountFail++;
55 return false;
56}
57
58bool report_fail(const std::string& text)
59{
60 printf("FAIL: %s\n", text.c_str());
61 gCountFail++;
62 return false;
63}
64
65bool rmdir(HNDLE hDB, HNDLE hROOT, const char* path)
66{
67 HNDLE hKey;
68
69 printf("rmdir(%s)\n", path);
70
71 int status = db_find_link(hDB, hROOT, path, &hKey);
72
73 if (status == DB_NO_KEY)
74 return true;
75
76 if (status != DB_SUCCESS)
77 return report_fail(msprintf("test_mkdir: Cannot delete subdirectory \"%s\", db_find_link() status %d\n", path, status));
78
80
81 if (status == DB_SUCCESS)
82 return true;
83
84 return report_fail(msprintf("test_mkdir: Delete subdirectory \"%s\" failed, status %d\n", path, status));
85}
86
87bool test_mkdir(HNDLE hDB, HNDLE hROOT, const char* path)
88{
89 printf("mkdir(%s)\n", path);
90
91 int status = db_create_key(hDB, hROOT, path, TID_KEY);
92
93 if (status == DB_SUCCESS)
94 return true;
95
96 return report_fail(msprintf("test_mkdir: Create subdirectory \"%s\" failed, status %d\n", path, status));
97}
98
99bool test_mkdir_fail(HNDLE hDB, HNDLE hROOT, const char* path, int fail_status)
100{
101 printf("mkdir_fail(%s)\n", path);
102
103 int status = db_create_key(hDB, hROOT, path, TID_KEY);
104
105 if (status == fail_status)
106 return true;
107
108 return report_fail(msprintf("test_mkdir_fail: Create subdirectory \"%s\" failed, unexpcted status %d instead of %d\n", path, status, fail_status));
109}
110
111bool test_chdir(HNDLE hDB, HNDLE hROOT, const char* path, HNDLE* phKey)
112{
113 printf("chdir(%s)\n", path);
114
115 int status = db_find_link(hDB, hROOT, path, phKey);
116
117 if (status == DB_SUCCESS)
118 return true;
119
120 return report_fail(msprintf("test_mkdir: Cannot find subdirectory \"%s\", db_find_link() status %d\n", path, status));
121}
122
123bool test_delete_key(HNDLE hDB, HNDLE hROOT, const char* path)
124{
125 HNDLE hKey;
126
127 printf("delete_key(%s)\n", path);
128
129 int status = db_find_link(hDB, hROOT, path, &hKey);
130
131 if (status == DB_NO_KEY)
132 return true;
133
134 if (status != DB_SUCCESS)
135 return report_fail(msprintf("test_delete_key: Cannot delete \"%s\", db_find_link() status %d\n", path, status));
136
138
139 if (status == DB_SUCCESS)
140 return true;
141
142 return report_fail(msprintf("test_delete_key: Delete \"%s\" failed, status %d\n", path, status));
143}
144
145bool test_delete(HNDLE hDB, HNDLE hROOT, const char* path)
146{
147 printf("delete(%s)\n", path);
148
149 int status = db_delete(hDB, hROOT, path);
150
151 if (status == DB_SUCCESS)
152 return true;
153
154 return report_fail(msprintf("test_delete: Delete \"%s\" failed, status %d\n", path, status));
155}
156
157bool test_find_fail(HNDLE hDB, HNDLE hROOT, const char* path, int fail_status)
158{
159 HNDLE hKey;
160
161 printf("find_fail(%s)\n", path);
162
163 int status = db_find_link(hDB, hROOT, path, &hKey);
164
165 if (status == fail_status)
166 return true;
167
168 return report_fail(msprintf("test_find_fail: Unexpected db_find_link(%s) status %d instead of %d\n", path, status, fail_status));
169}
170
171bool test_find_key_fail(HNDLE hDB, HNDLE hROOT, const char* path, int fail_status)
172{
173 HNDLE hKey;
174
175 printf("find_key_fail(%s)\n", path);
176
177 int status = db_find_key(hDB, hROOT, path, &hKey);
178
179 if (status == fail_status)
180 return true;
181
182 return report_fail(msprintf("test_find_key_fail: Unexpected db_find_key(%s) status %d instead of %d\n", path, status, fail_status));
183}
184
185bool test_symlink(HNDLE hDB, HNDLE hROOT, const char* path, const char* link_target)
186{
187 printf("symlink(%s) to \"%s\"\n", path, link_target);
188
189 int status = db_create_link(hDB, hROOT, path, link_target);
190
191 if (status == DB_SUCCESS)
192 return true;
193
194 return report_fail(msprintf("test_symlink: db_create_link(%s) status %d\n", path, status));
195}
196
197bool test_symlink_fail(HNDLE hDB, HNDLE hROOT, const char* path, const char* link_target, int fail_status)
198{
199 printf("symlink_fail(%s) to \"%s\"\n", path, link_target);
200
201 int status = db_create_link(hDB, hROOT, path, link_target);
202
203 if (status == fail_status)
204 return true;
205
206 return report_fail(msprintf("test_symlink_fail: db_create_link(%s) status %d instead of %d\n", path, status, fail_status));
207}
208
209bool test_enum(const std::vector<HNDLE>& hlist, const std::vector<KEY>& klist)
210{
211 printf("test_enum\n");
212
213 if (hlist.size() != klist.size()) {
214 return report_fail(msprintf("test_enum: hlist and klist size mismatch\n"));
215 }
216
217 bool ok = true;
218
219 size_t expected_size = 6;
220
221 if (klist.size() != expected_size)
222 ok &= report_fail(msprintf("test_enum: klist size %zu different from expected %zu\n", klist.size(), expected_size));
223
224 for (size_t i=0; i<klist.size(); i++) {
225 printf("key %zu: \"%s\" tid %d, num_values %d\n", i, klist[i].name, klist[i].type, klist[i].num_values);
226 }
227
228 return ok;
229}
230
231bool test_enum_links(const std::vector<HNDLE>& hlist, const std::vector<KEY>& klist)
232{
233 printf("test_enum_links\n");
234
235 if (hlist.size() != klist.size()) {
236 return report_fail(msprintf("test_enum: hlist and klist size mismatch\n"));
237 }
238
239 bool ok = true;
240
241 size_t expected_size = 6;
242
243 if (klist.size() != expected_size)
244 ok &= report_fail(msprintf("test_enum: klist size %zu different from expected %zu\n", klist.size(), expected_size));
245
246 for (size_t i=0; i<klist.size(); i++) {
247 printf("key %zu: \"%s\" tid %d, num_values %d\n", i, klist[i].name, klist[i].type, klist[i].num_values);
248 }
249
250 return ok;
251}
252
254{
255 bool ok = true;
256
257 ok &= rmdir(hDB, 0, "test_odb");
258
259 if (!ok)
260 return false;
261
262 ok &= test_mkdir(hDB, 0, "test_odb");
263
264 if (!ok)
265 return false;
266
267 HNDLE hROOT = 0;
268
269 ok &= test_chdir(hDB, 0, "test_odb", &hROOT);
270
271 if (!ok)
272 return false;
273
274 ok &= test_find_fail(hDB, hROOT, "does-not-exist", DB_NO_KEY);
275 ok &= test_find_fail(hDB, hROOT, "/does-not-exist", DB_NO_KEY);
276
277 ok &= test_find_fail(hDB, 0, "/System", DB_SUCCESS); // exists at top level
278 ok &= test_find_fail(hDB, hROOT, "/System", DB_NO_KEY); // but not in our subdirectory
279
280 ok &= test_mkdir(hDB, hROOT, "test_mkdir");
281 ok &= test_mkdir_fail(hDB, hROOT, "/test_mkdir", DB_KEY_EXIST); // unexpected, leading slash is ignored?
282
283 // test db_create_key()
284
285 ok &= test_mkdir_fail(hDB, hROOT, "invalid_name[", DB_INVALID_NAME);
286 ok &= test_mkdir_fail(hDB, hROOT, "invalid_name]", DB_INVALID_NAME);
287 ok &= test_mkdir_fail(hDB, hROOT, "", DB_INVALID_NAME);
288 ok &= test_mkdir_fail(hDB, hROOT, "/", DB_INVALID_NAME);
289 ok &= test_mkdir_fail(hDB, hROOT, "//", DB_INVALID_NAME);
290 ok &= test_mkdir_fail(hDB, hROOT, "invalid//invalid", DB_INVALID_NAME);
291 //ok &= test_mkdir_fail(hDB, hROOT, NULL, DB_INVALID_NAME); -- core dump if rpc_is_remote()
292
293 // test db_delete_key()
294
295 ok &= test_mkdir(hDB, hROOT, "test_delete_key");
296 ok &= test_delete_key(hDB, hROOT, "test_delete_key");
297 ok &= test_find_fail(hDB, hROOT, "test_delete_key", DB_NO_KEY); // deleted
298
299 ok &= test_mkdir(hDB, hROOT, "test_delete_key_dir/test/test/test_mkdir"); // multi-level create and delete
300 ok &= test_delete_key(hDB, hROOT, "test_delete_key_dir");
301 ok &= test_find_fail(hDB, hROOT, "test_delete_key_dir", DB_NO_KEY); // deleted
302
303 // test db_delete()
304
305 ok &= test_mkdir(hDB, hROOT, "test_delete");
306 ok &= test_delete(hDB, hROOT, "test_delete");
307 ok &= test_find_fail(hDB, hROOT, "test_delete", DB_NO_KEY); // deleted
308
309 ok &= test_mkdir(hDB, hROOT, "test_delete_dir/test/test/test_mkdir"); // multi-level create and delete
310 ok &= test_delete(hDB, hROOT, "test_delete_dir");
311 ok &= test_find_fail(hDB, hROOT, "test_delete_dir", DB_NO_KEY); // deleted
312
313 // test symlinks
314
315 ok &= test_symlink_fail(hDB, hROOT, "test_symlink_fail", "/does-not-exist", DB_NO_KEY);
316 ok &= test_symlink_fail(hDB, hROOT, "test_symlink_fail", "no-leading-slash", DB_INVALID_NAME);
317 ok &= test_symlink_fail(hDB, hROOT, "test_symlink", "/test_odb/test_mkdir", DB_NO_KEY); // unexpected
318 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink", "/test_odb/test_mkdir");
319
320 // test symlink to symlink
321
322 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink_to_symlink", "/test_odb/test_symlink");
323
324 //
325 // test db_delete_key of symlink
326 //
327
328 ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink1_target");
329 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink1", "/test_odb/test_symlink1_target");
330 ok &= test_delete_key(hDB, hROOT, "test_symlink1"); // delete symlink
331 ok &= test_find_fail(hDB, hROOT, "test_symlink1", DB_NO_KEY); // symlinks deleted
332 ok &= test_find_fail(hDB, hROOT, "test_symlink1_target", DB_SUCCESS); // symlink target remains
333
334 //
335 // test db_delete_key of symlink
336 //
337
338 ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink2_target");
339 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink2", "/test_odb/test_symlink2_target");
340 ok &= test_delete(hDB, hROOT, "test_symlink2"); // delete symlink
341 ok &= test_find_fail(hDB, hROOT, "test_symlink2", DB_NO_KEY); // symlinks deleted
342 ok &= test_find_fail(hDB, hROOT, "test_symlink2_target", DB_SUCCESS); // symlink target remains
343
344#if 0
345 //
346 // test db_delete_key with follow_links set to true
347 //
348
349 // delete(false) deletes the symlink,
350 // delete(true) deletes the link target, and the link
351
352 ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink_target");
353 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink1", "/test_odb/test_symlink_target");
354 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink2", "/test_odb/test_symlink_target");
355 ok &= test_delete(hDB, hROOT, "test_symlink1"); // delete symlink
356 ok &= test_delete(hDB, hROOT, "test_symlink2", true); // delete symlink target
357 ok &= test_find_fail(hDB, hROOT, "test_symlink_target", DB_NO_KEY); // deleted
358 ok &= test_find_fail(hDB, hROOT, "test_symlink1", DB_NO_KEY); // deleted
359 ok &= test_find_fail(hDB, hROOT, "test_symlink2", DB_NO_KEY); // dangling link
360
361 // chained links, target of last link is deleted, intermediate link remain (dangling)
362
363 ok &= test_mkdir(hDB, 0, "/test_odb/test_link_to_link");
364 ok &= test_symlink(hDB, 0, "/test_odb/test_link_to_link1", "/test_odb/test_link_to_link");
365 ok &= test_symlink(hDB, 0, "/test_odb/test_link_to_link2", "/test_odb/test_link_to_link1");
366 ok &= test_delete(hDB, hROOT, "test_link_to_link2", true); // delete symlink target
367 ok &= test_find_fail(hDB, hROOT, "test_link_to_link", DB_NO_KEY); // deleted
368 ok &= test_find_fail(hDB, hROOT, "test_link_to_link1", DB_SUCCESS); // unexpectedly not deleted
369 ok &= test_find_fail(hDB, hROOT, "test_link_to_link2", DB_NO_KEY); // deleted
370 ok &= test_find_key_fail(hDB, hROOT, "test_link_to_link1", DB_INVALID_LINK); // dangling link
371
372 // subdirectory has a link to something else, it is also deleted
373
374 ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink_dir1");
375 ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink_dir2");
376 ok &= test_symlink(hDB, 0, "/test_odb/test_symlink_dir1/test_symlink", "/test_odb/test_symlink_dir2");
377 ok &= test_delete(hDB, hROOT, "test_symlink_dir1", true); // delete symlink target
378 ok &= test_find_fail(hDB, hROOT, "test_symlink_dir1", DB_NO_KEY); // deleted
379 ok &= test_find_fail(hDB, hROOT, "test_symlink_dir2", DB_NO_KEY); // deleted
380
381 // test delete of circular links
382
383 ok &= test_mkdir(hDB, 0, "/test_odb/test_circular_link");
384 ok &= test_symlink(hDB, 0, "/test_odb/test_circular_link/test_link", "/test_odb/test_circular_link");
385 ok &= test_symlink(hDB, 0, "/test_odb/test_circular_link1", "/test_odb/test_circular_link");
386 ok &= test_delete(hDB, hROOT, "test_circular_link1", true); // delete symlink target
387 ok &= test_find_fail(hDB, hROOT, "test_circular_link", DB_NO_KEY); // deleted
388 ok &= test_find_fail(hDB, hROOT, "test_circular_link1", DB_NO_KEY); // deleted
389#endif
390
391 // test db_enum
392
393 {
394 int status;
395 HNDLE hKeyRoot;
396 std::vector<HNDLE> hlist;
397 std::vector<KEY> klist;
398
399 status = db_enum(hDB, 0, "/test_odb", &hlist, &klist);
400
401 if (status != DB_SUCCESS) {
402 ok &= report_fail(msprintf("test_enum: db_enum(path) status %d\n", status));
403 }
404
405 ok &= test_enum(hlist, klist);
406
407 status = db_find_key(hDB, 0, "/test_odb", &hKeyRoot);
408
409 status = db_enum(hDB, hKeyRoot, &hlist, &klist);
410
411 if (status != DB_SUCCESS) {
412 ok &= report_fail(msprintf("test_enum: db_enum(hkey) status %d\n", status));
413 }
414
415 ok &= test_enum(hlist, klist);
416 }
417
418 {
419 int status;
420 HNDLE hKeyRoot;
421 std::vector<HNDLE> hlist;
422 std::vector<KEY> klist;
423
424 status = db_enum(hDB, 0, "/test_odb", &hlist, &klist, false);
425
426 if (status != DB_SUCCESS) {
427 ok &= report_fail(msprintf("test_enum: db_enum(path) status %d\n", status));
428 }
429
430 ok &= test_enum_links(hlist, klist);
431
432 status = db_find_link(hDB, 0, "/test_odb", &hKeyRoot);
433
434 status = db_enum(hDB, hKeyRoot, &hlist, &klist, false);
435
436 if (status != DB_SUCCESS) {
437 ok &= report_fail(msprintf("test_enum: db_enum(hkey) status %d\n", status));
438 }
439
440 ok &= test_enum_links(hlist, klist);
441 }
442
443 return ok;
444}
445
446// Main function call
447
448int main(int argc, char *argv[])
449{
450 setbuf(stdout,NULL);
451 setbuf(stderr,NULL);
452
453 signal(SIGILL, SIG_DFL);
454 signal(SIGBUS, SIG_DFL);
455 signal(SIGSEGV, SIG_DFL);
456 signal(SIGPIPE, SIG_DFL);
457
458 const char* hostname = NULL;
459 const char* exptname = NULL;
460
461 if (argc > 1)
462 hostname = argv[1];
463
464 TMFE* mfe = TMFE::Instance();
465
466 TMFeResult r;
467
468 r = mfe->Connect("test_odb", hostname, exptname);
469
470 if (r.error_flag) {
471 fprintf(stderr,"Cannot connect to MIDAS, error %s\n", r.error_message.c_str());
472 return -1;
473 }
474
475 HNDLE hDB = mfe->fDB;
476
477 bool ok = true;
478
479 printf("\n");
480 printf("Starting tests:\n");
481 printf("\n");
482
483 ok &= test_all(hDB);
484
485 if (mfe)
486 mfe->Disconnect();
487
488 if (ok) {
489 printf("\n");
490 printf("Tests passed OK\n");
491 printf("\n");
492 } else {
493 printf("\n");
494 printf("Number of FAILED tests: %d\n", gCountFail);
495 printf("\n");
496 }
497
498 return 0;
499}
500
501//end
502
503/* emacs
504 * Local Variables:
505 * tab-width: 8
506 * c-basic-offset: 3
507 * indent-tabs-mode: nil
508 * End:
509 */
Definition tmfe.h:381
int fDB
ODB database handle.
Definition tmfe.h:394
TMFeResult Connect(const char *progname=NULL, const char *hostname=NULL, const char *exptname=NULL)
Definition tmfe.cxx:65
static TMFE * Instance()
Definition tmfe.cxx:57
TMFeResult Disconnect()
Definition tmfe.cxx:154
bool error_flag
Definition tmfe.h:89
std::string error_message
Definition tmfe.h:91
#define DB_KEY_EXIST
Definition midas.h:642
#define DB_SUCCESS
Definition midas.h:632
#define DB_INVALID_NAME
Definition midas.h:635
#define DB_NO_KEY
Definition midas.h:643
#define DB_INVALID_LINK
Definition midas.h:653
#define TID_KEY
Definition midas.h:349
INT db_delete_key(HNDLE hDB, HNDLE hKey, BOOL follow_links)
Definition odb.cxx:3933
INT db_find_link(HNDLE hDB, HNDLE hKey, const char *key_name, HNDLE *subhKey)
Definition odb.cxx:4293
INT EXPRT db_enum(HNDLE hDB, HNDLE hKeyRoot, std::vector< HNDLE > *sub_handles, std::vector< KEY > *sub_keys, bool follow_links)
Definition odb.cxx:5768
INT db_create_key(HNDLE hDB, HNDLE hKey, const char *key_name, DWORD type)
Definition odb.cxx:3392
INT db_delete(HNDLE hDB, HNDLE hKeyRoot, const char *odb_path)
Definition odb.cxx:3999
INT db_find_key(HNDLE hDB, HNDLE hKey, const char *key_name, HNDLE *subhKey)
Definition odb.cxx:4256
INT db_create_link(HNDLE hDB, HNDLE hKey, const char *link_name, const char *destination)
Definition odb.cxx:3688
int main()
Definition hwtest.cxx:23
HNDLE hKey
INT type
Definition mana.cxx:269
HNDLE hDB
main ODB handle
Definition mana.cxx:207
INT i
Definition mdump.cxx:32
std::string msprintf(const char *format,...)
Definition midas.cxx:419
INT HNDLE
Definition midas.h:132
#define name(x)
Definition midas_macro.h:24
DWORD status
Definition odbhist.cxx:39
void print_da(const std::vector< double > &da)
Definition test_odb.cxx:37
bool test_find_fail(HNDLE hDB, HNDLE hROOT, const char *path, int fail_status)
Definition test_odb.cxx:157
static int gCountFail
Definition test_odb.cxx:49
bool test_mkdir(HNDLE hDB, HNDLE hROOT, const char *path)
Definition test_odb.cxx:87
bool test_symlink(HNDLE hDB, HNDLE hROOT, const char *path, const char *link_target)
Definition test_odb.cxx:185
bool test_mkdir_fail(HNDLE hDB, HNDLE hROOT, const char *path, int fail_status)
Definition test_odb.cxx:99
bool test_all(HNDLE hDB)
Definition test_odb.cxx:253
bool test_find_key_fail(HNDLE hDB, HNDLE hROOT, const char *path, int fail_status)
Definition test_odb.cxx:171
bool report_fail(const char *text)
Definition test_odb.cxx:51
bool test_enum_links(const std::vector< HNDLE > &hlist, const std::vector< KEY > &klist)
Definition test_odb.cxx:231
void print_ia(const std::vector< int > &ia)
Definition test_odb.cxx:25
bool test_symlink_fail(HNDLE hDB, HNDLE hROOT, const char *path, const char *link_target, int fail_status)
Definition test_odb.cxx:197
bool test_enum(const std::vector< HNDLE > &hlist, const std::vector< KEY > &klist)
Definition test_odb.cxx:209
bool test_delete_key(HNDLE hDB, HNDLE hROOT, const char *path)
Definition test_odb.cxx:123
bool test_chdir(HNDLE hDB, HNDLE hROOT, const char *path, HNDLE *phKey)
Definition test_odb.cxx:111
bool rmdir(HNDLE hDB, HNDLE hROOT, const char *path)
Definition test_odb.cxx:65
std::string toString(int i)
Definition test_odb.cxx:18
bool test_delete(HNDLE hDB, HNDLE hROOT, const char *path)
Definition test_odb.cxx:145