source: mainline/uspace/lib/sif/test/sif.c

Last change on this file was bff8619, checked in by Jiri Svoboda <jiri@…>, 10 months ago

Simplify SIF interface, remove contacts

Remove transactions, move to a load/save model. Remove contacts
application as it was never finished and not useful at all.

  • Property mode set to 100644
File size: 12.4 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <pcut/pcut.h>
31#include <stdio.h>
32#include <str.h>
33#include "../include/sif.h"
34
35PCUT_INIT;
36
37PCUT_TEST_SUITE(sif);
38#if 0
39/** Test sif_new and sif_delete. */
40PCUT_TEST(sif_create)
41{
42 sif_doc_t *doc;
43 errno_t rc;
44
45 rc = sif_new(&doc);
46 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
47
48 sif_delete(doc);
49}
50
51/** Test sif_load. */
52PCUT_TEST(sif_load)
53{
54 sif_sess_t *sess;
55 errno_t rc;
56 int rv;
57 char *fname;
58 char *p;
59
60 fname = calloc(L_tmpnam, 1);
61 PCUT_ASSERT_NOT_NULL(fname);
62
63 p = tmpnam(fname);
64 PCUT_ASSERT_TRUE(p == fname);
65
66 rc = sif_create(fname, &sess);
67 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
68
69 rc = sif_close(sess);
70 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
71
72 rc = sif_open(fname, &sess);
73 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
74
75 rc = sif_close(sess);
76 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
77
78 rv = remove(fname);
79 PCUT_ASSERT_INT_EQUALS(0, rv);
80}
81
82/** Test sif_get_root. */
83PCUT_TEST(sif_get_root)
84{
85 sif_sess_t *sess;
86 sif_node_t *root;
87 errno_t rc;
88 int rv;
89 char *fname;
90 char *p;
91
92 fname = calloc(L_tmpnam, 1);
93 PCUT_ASSERT_NOT_NULL(fname);
94
95 p = tmpnam(fname);
96 PCUT_ASSERT_TRUE(p == fname);
97
98 rc = sif_create(fname, &sess);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
100
101 root = sif_get_root(sess);
102 PCUT_ASSERT_NOT_NULL(root);
103 PCUT_ASSERT_INT_EQUALS(0, str_cmp(sif_node_get_type(root), "sif"));
104
105 rc = sif_close(sess);
106 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
107
108 rv = remove(fname);
109 PCUT_ASSERT_INT_EQUALS(0, rv);
110}
111
112/** Test sif_node_prepend_child. */
113PCUT_TEST(sif_node_prepend_child)
114{
115 sif_sess_t *sess;
116 sif_node_t *root;
117 sif_node_t *ca;
118 sif_node_t *cb;
119 sif_node_t *c1;
120 sif_node_t *c2;
121 sif_node_t *c3;
122 sif_trans_t *trans;
123 errno_t rc;
124 int rv;
125 char *fname;
126 char *p;
127
128 fname = calloc(L_tmpnam, 1);
129 PCUT_ASSERT_NOT_NULL(fname);
130
131 p = tmpnam(fname);
132 PCUT_ASSERT_TRUE(p == fname);
133
134 rc = sif_create(fname, &sess);
135 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136
137 root = sif_get_root(sess);
138
139 rc = sif_trans_begin(sess, &trans);
140 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
141
142 rc = sif_node_prepend_child(trans, root, "a", &ca);
143 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
144
145 rc = sif_node_prepend_child(trans, root, "b", &cb);
146 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
147
148 rc = sif_trans_end(trans);
149 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
150
151 c1 = sif_node_first_child(root);
152 PCUT_ASSERT_TRUE(c1 == cb);
153
154 c2 = sif_node_next_child(c1);
155 PCUT_ASSERT_TRUE(c2 == ca);
156
157 c3 = sif_node_next_child(c2);
158 PCUT_ASSERT_TRUE(c3 == NULL);
159
160 rc = sif_close(sess);
161 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162
163 rv = remove(fname);
164 PCUT_ASSERT_INT_EQUALS(0, rv);
165}
166
167/** Test sif_node_append_child. */
168PCUT_TEST(sif_node_append_child)
169{
170 sif_sess_t *sess;
171 sif_node_t *root;
172 sif_node_t *ca;
173 sif_node_t *cb;
174 sif_node_t *c1;
175 sif_node_t *c2;
176 sif_node_t *c3;
177 sif_trans_t *trans;
178 errno_t rc;
179 int rv;
180 char *fname;
181 char *p;
182
183 fname = calloc(L_tmpnam, 1);
184 PCUT_ASSERT_NOT_NULL(fname);
185
186 p = tmpnam(fname);
187 PCUT_ASSERT_TRUE(p == fname);
188
189 rc = sif_create(fname, &sess);
190 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
191
192 root = sif_get_root(sess);
193
194 rc = sif_trans_begin(sess, &trans);
195 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
196
197 rc = sif_node_append_child(trans, root, "a", &ca);
198 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
199
200 rc = sif_node_append_child(trans, root, "b", &cb);
201 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
202
203 rc = sif_trans_end(trans);
204 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
205
206 c1 = sif_node_first_child(root);
207 PCUT_ASSERT_TRUE(c1 == ca);
208
209 c2 = sif_node_next_child(c1);
210 PCUT_ASSERT_TRUE(c2 == cb);
211
212 c3 = sif_node_next_child(c2);
213 PCUT_ASSERT_TRUE(c3 == NULL);
214
215 rc = sif_close(sess);
216 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
217
218 rv = remove(fname);
219 PCUT_ASSERT_INT_EQUALS(0, rv);
220}
221
222/** Test sif_node_insert_before. */
223PCUT_TEST(sif_node_insert_before)
224{
225 sif_sess_t *sess;
226 sif_node_t *root;
227 sif_node_t *ca;
228 sif_node_t *cb;
229 sif_node_t *cc;
230 sif_node_t *c1;
231 sif_node_t *c2;
232 sif_node_t *c3;
233 sif_node_t *c4;
234 sif_trans_t *trans;
235 errno_t rc;
236 int rv;
237 char *fname;
238 char *p;
239
240 fname = calloc(L_tmpnam, 1);
241 PCUT_ASSERT_NOT_NULL(fname);
242
243 p = tmpnam(fname);
244 PCUT_ASSERT_TRUE(p == fname);
245
246 rc = sif_create(fname, &sess);
247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
248
249 root = sif_get_root(sess);
250
251 rc = sif_trans_begin(sess, &trans);
252 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
253
254 rc = sif_node_append_child(trans, root, "a", &ca);
255 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
256
257 rc = sif_node_append_child(trans, root, "c", &cc);
258 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
259
260 rc = sif_node_insert_before(trans, cc, "b", &cb);
261 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
262
263 rc = sif_trans_end(trans);
264 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
265
266 c1 = sif_node_first_child(root);
267 PCUT_ASSERT_TRUE(c1 == ca);
268
269 c2 = sif_node_next_child(c1);
270 PCUT_ASSERT_TRUE(c2 == cb);
271
272 c3 = sif_node_next_child(c2);
273 PCUT_ASSERT_TRUE(c3 == cc);
274
275 c4 = sif_node_next_child(c3);
276 PCUT_ASSERT_TRUE(c4 == NULL);
277
278 rc = sif_close(sess);
279 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
280
281 rv = remove(fname);
282 PCUT_ASSERT_INT_EQUALS(0, rv);
283}
284
285/** Test sif_node_insert_after. */
286PCUT_TEST(sif_node_insert_after)
287{
288 sif_sess_t *sess;
289 sif_node_t *root;
290 sif_node_t *ca;
291 sif_node_t *cb;
292 sif_node_t *cc;
293 sif_node_t *c1;
294 sif_node_t *c2;
295 sif_node_t *c3;
296 sif_node_t *c4;
297 sif_trans_t *trans;
298 errno_t rc;
299 int rv;
300 char *fname;
301 char *p;
302
303 fname = calloc(L_tmpnam, 1);
304 PCUT_ASSERT_NOT_NULL(fname);
305
306 p = tmpnam(fname);
307 PCUT_ASSERT_TRUE(p == fname);
308
309 rc = sif_create(fname, &sess);
310 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
311
312 root = sif_get_root(sess);
313
314 rc = sif_trans_begin(sess, &trans);
315 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
316
317 rc = sif_node_append_child(trans, root, "a", &ca);
318 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
319
320 rc = sif_node_append_child(trans, root, "c", &cc);
321 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
322
323 rc = sif_node_insert_after(trans, ca, "b", &cb);
324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
325
326 rc = sif_trans_end(trans);
327 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
328
329 c1 = sif_node_first_child(root);
330 PCUT_ASSERT_TRUE(c1 == ca);
331
332 c2 = sif_node_next_child(c1);
333 PCUT_ASSERT_TRUE(c2 == cb);
334
335 c3 = sif_node_next_child(c2);
336 PCUT_ASSERT_TRUE(c3 == cc);
337
338 c4 = sif_node_next_child(c3);
339 PCUT_ASSERT_TRUE(c4 == NULL);
340
341 rc = sif_close(sess);
342 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
343
344 rv = remove(fname);
345 PCUT_ASSERT_INT_EQUALS(0, rv);
346}
347
348/** Test sif_node_destroy. */
349PCUT_TEST(sif_node_destroy)
350{
351 sif_sess_t *sess;
352 sif_node_t *root;
353 sif_node_t *ca;
354 sif_node_t *cb;
355 sif_node_t *cc;
356 sif_node_t *c1;
357 sif_node_t *c2;
358 sif_node_t *c3;
359 sif_trans_t *trans;
360 errno_t rc;
361 int rv;
362 char *fname;
363 char *p;
364
365 fname = calloc(L_tmpnam, 1);
366 PCUT_ASSERT_NOT_NULL(fname);
367
368 p = tmpnam(fname);
369 PCUT_ASSERT_TRUE(p == fname);
370
371 rc = sif_create(fname, &sess);
372 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
373
374 root = sif_get_root(sess);
375
376 rc = sif_trans_begin(sess, &trans);
377 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
378
379 rc = sif_node_append_child(trans, root, "a", &ca);
380 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
381
382 rc = sif_node_append_child(trans, root, "b", &cb);
383 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
384
385 rc = sif_node_append_child(trans, root, "c", &cc);
386 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
387
388 rc = sif_trans_end(trans);
389 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
390
391 rc = sif_trans_begin(sess, &trans);
392 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
393
394 sif_node_destroy(trans, cb);
395
396 rc = sif_trans_end(trans);
397 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
398
399 c1 = sif_node_first_child(root);
400 PCUT_ASSERT_TRUE(c1 == ca);
401
402 c2 = sif_node_next_child(c1);
403 PCUT_ASSERT_TRUE(c2 == cc);
404
405 c3 = sif_node_next_child(c2);
406 PCUT_ASSERT_TRUE(c3 == NULL);
407
408 rc = sif_close(sess);
409 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
410
411 rv = remove(fname);
412 PCUT_ASSERT_INT_EQUALS(0, rv);
413}
414
415/** Test sif_node_set_attr. */
416PCUT_TEST(sif_node_set_attr)
417{
418 sif_sess_t *sess;
419 sif_node_t *root;
420 sif_node_t *node;
421 sif_trans_t *trans;
422 errno_t rc;
423 int rv;
424 char *fname;
425 char *p;
426 const char *aval;
427
428 fname = calloc(L_tmpnam, 1);
429 PCUT_ASSERT_NOT_NULL(fname);
430
431 p = tmpnam(fname);
432 PCUT_ASSERT_TRUE(p == fname);
433
434 rc = sif_create(fname, &sess);
435 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
436
437 root = sif_get_root(sess);
438
439 rc = sif_trans_begin(sess, &trans);
440 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
441
442 rc = sif_node_append_child(trans, root, "node", &node);
443 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
444
445 rc = sif_node_set_attr(trans, node, "a", "?");
446 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
447
448 rc = sif_node_set_attr(trans, node, "a", "X");
449 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
450
451 rc = sif_node_set_attr(trans, node, "b", "Y");
452 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
453
454 rc = sif_trans_end(trans);
455 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
456
457 aval = sif_node_get_attr(node, "a");
458 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "X"));
459
460 aval = sif_node_get_attr(node, "b");
461 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "Y"));
462
463 aval = sif_node_get_attr(node, "c");
464 PCUT_ASSERT_NULL((void *) aval); // XXX PCUT_ASSERT_NULL does not accept const pointer
465
466 rc = sif_close(sess);
467 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
468
469 rv = remove(fname);
470 PCUT_ASSERT_INT_EQUALS(0, rv);
471}
472
473/** Test sif_node_unset_attr. */
474PCUT_TEST(sif_node_unset_attr)
475{
476 sif_sess_t *sess;
477 sif_node_t *root;
478 sif_node_t *node;
479 sif_trans_t *trans;
480 errno_t rc;
481 int rv;
482 char *fname;
483 char *p;
484 const char *aval;
485
486 fname = calloc(L_tmpnam, 1);
487 PCUT_ASSERT_NOT_NULL(fname);
488
489 p = tmpnam(fname);
490 PCUT_ASSERT_TRUE(p == fname);
491
492 rc = sif_create(fname, &sess);
493 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
494
495 root = sif_get_root(sess);
496
497 rc = sif_trans_begin(sess, &trans);
498 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
499
500 rc = sif_node_append_child(trans, root, "node", &node);
501 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
502
503 rc = sif_node_set_attr(trans, node, "a", "X");
504 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
505
506 rc = sif_trans_end(trans);
507 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
508
509 aval = sif_node_get_attr(node, "a");
510 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "X"));
511
512 rc = sif_trans_begin(sess, &trans);
513 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
514
515 sif_node_unset_attr(trans, node, "a");
516 sif_node_unset_attr(trans, node, "b");
517
518 rc = sif_trans_end(trans);
519 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
520
521 aval = sif_node_get_attr(node, "a");
522 PCUT_ASSERT_NULL((void *) aval); // XXX PCUT_ASSERT_NULL does not accept const pointer
523
524 rc = sif_close(sess);
525 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
526
527 rv = remove(fname);
528 PCUT_ASSERT_INT_EQUALS(0, rv);
529}
530
531/** Test persistence of nodes and attributes. */
532PCUT_TEST(sif_persist)
533{
534 sif_sess_t *sess;
535 sif_node_t *root;
536 sif_node_t *node;
537 sif_trans_t *trans;
538 errno_t rc;
539 int rv;
540 char *fname;
541 char *p;
542 const char *aval;
543
544 fname = calloc(L_tmpnam, 1);
545 PCUT_ASSERT_NOT_NULL(fname);
546
547 p = tmpnam(fname);
548 PCUT_ASSERT_TRUE(p == fname);
549
550 rc = sif_create(fname, &sess);
551 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
552
553 root = sif_get_root(sess);
554
555 rc = sif_trans_begin(sess, &trans);
556 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
557
558 rc = sif_node_append_child(trans, root, "node", &node);
559 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
560
561 rc = sif_node_set_attr(trans, node, "a", "X");
562 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
563
564 rc = sif_trans_end(trans);
565 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
566
567 rc = sif_close(sess);
568 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
569
570 /* Now reopen the repository */
571
572 rc = sif_open(fname, &sess);
573 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
574
575 root = sif_get_root(sess);
576
577 node = sif_node_first_child(root);
578 PCUT_ASSERT_NOT_NULL(node);
579 PCUT_ASSERT_INT_EQUALS(0, str_cmp(sif_node_get_type(node), "node"));
580
581 aval = sif_node_get_attr(node, "a");
582 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "X"));
583
584 rc = sif_close(sess);
585 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
586
587 rv = remove(fname);
588 PCUT_ASSERT_INT_EQUALS(0, rv);
589}
590#endif
591PCUT_EXPORT(sif);
Note: See TracBrowser for help on using the repository browser.