source: mainline/uspace/lib/sif/test/sif.c@ 8bf9058

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8bf9058 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * Copyright (c) 2018 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
39/** Test sif_create. */
40PCUT_TEST(sif_create)
41{
42 sif_sess_t *sess;
43 errno_t rc;
44 int rv;
45 char *fname;
46 char *p;
47
48 fname = calloc(L_tmpnam, 1);
49 PCUT_ASSERT_NOT_NULL(fname);
50
51 p = tmpnam(fname);
52 PCUT_ASSERT_TRUE(p == fname);
53
54 rc = sif_create(fname, &sess);
55 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
56
57 rc = sif_close(sess);
58 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
59
60 rv = remove(fname);
61 PCUT_ASSERT_INT_EQUALS(0, rv);
62}
63
64/** Test sif_open. */
65PCUT_TEST(sif_open)
66{
67 sif_sess_t *sess;
68 errno_t rc;
69 int rv;
70 char *fname;
71 char *p;
72
73 fname = calloc(L_tmpnam, 1);
74 PCUT_ASSERT_NOT_NULL(fname);
75
76 p = tmpnam(fname);
77 PCUT_ASSERT_TRUE(p == fname);
78
79 rc = sif_create(fname, &sess);
80 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
81
82 rc = sif_close(sess);
83 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
84
85 rc = sif_open(fname, &sess);
86 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
87
88 rc = sif_close(sess);
89 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
90
91 rv = remove(fname);
92 PCUT_ASSERT_INT_EQUALS(0, rv);
93}
94
95/** Test sif_get_root. */
96PCUT_TEST(sif_get_root)
97{
98 sif_sess_t *sess;
99 sif_node_t *root;
100 errno_t rc;
101 int rv;
102 char *fname;
103 char *p;
104
105 fname = calloc(L_tmpnam, 1);
106 PCUT_ASSERT_NOT_NULL(fname);
107
108 p = tmpnam(fname);
109 PCUT_ASSERT_TRUE(p == fname);
110
111 rc = sif_create(fname, &sess);
112 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
113
114 root = sif_get_root(sess);
115 PCUT_ASSERT_NOT_NULL(root);
116 PCUT_ASSERT_INT_EQUALS(0, str_cmp(sif_node_get_type(root), "sif"));
117
118 rc = sif_close(sess);
119 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
120
121 rv = remove(fname);
122 PCUT_ASSERT_INT_EQUALS(0, rv);
123}
124
125/** Test sif_node_prepend_child. */
126PCUT_TEST(sif_node_prepend_child)
127{
128 sif_sess_t *sess;
129 sif_node_t *root;
130 sif_node_t *ca;
131 sif_node_t *cb;
132 sif_node_t *c1;
133 sif_node_t *c2;
134 sif_node_t *c3;
135 sif_trans_t *trans;
136 errno_t rc;
137 int rv;
138 char *fname;
139 char *p;
140
141 fname = calloc(L_tmpnam, 1);
142 PCUT_ASSERT_NOT_NULL(fname);
143
144 p = tmpnam(fname);
145 PCUT_ASSERT_TRUE(p == fname);
146
147 rc = sif_create(fname, &sess);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149
150 root = sif_get_root(sess);
151
152 rc = sif_trans_begin(sess, &trans);
153 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
154
155 rc = sif_node_prepend_child(trans, root, "a", &ca);
156 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
157
158 rc = sif_node_prepend_child(trans, root, "b", &cb);
159 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
160
161 rc = sif_trans_end(trans);
162 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
163
164 c1 = sif_node_first_child(root);
165 PCUT_ASSERT_TRUE(c1 == cb);
166
167 c2 = sif_node_next_child(c1);
168 PCUT_ASSERT_TRUE(c2 == ca);
169
170 c3 = sif_node_next_child(c2);
171 PCUT_ASSERT_TRUE(c3 == NULL);
172
173 rc = sif_close(sess);
174 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
175
176 rv = remove(fname);
177 PCUT_ASSERT_INT_EQUALS(0, rv);
178}
179
180/** Test sif_node_append_child. */
181PCUT_TEST(sif_node_append_child)
182{
183 sif_sess_t *sess;
184 sif_node_t *root;
185 sif_node_t *ca;
186 sif_node_t *cb;
187 sif_node_t *c1;
188 sif_node_t *c2;
189 sif_node_t *c3;
190 sif_trans_t *trans;
191 errno_t rc;
192 int rv;
193 char *fname;
194 char *p;
195
196 fname = calloc(L_tmpnam, 1);
197 PCUT_ASSERT_NOT_NULL(fname);
198
199 p = tmpnam(fname);
200 PCUT_ASSERT_TRUE(p == fname);
201
202 rc = sif_create(fname, &sess);
203 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
204
205 root = sif_get_root(sess);
206
207 rc = sif_trans_begin(sess, &trans);
208 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
209
210 rc = sif_node_append_child(trans, root, "a", &ca);
211 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
212
213 rc = sif_node_append_child(trans, root, "b", &cb);
214 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
215
216 rc = sif_trans_end(trans);
217 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
218
219 c1 = sif_node_first_child(root);
220 PCUT_ASSERT_TRUE(c1 == ca);
221
222 c2 = sif_node_next_child(c1);
223 PCUT_ASSERT_TRUE(c2 == cb);
224
225 c3 = sif_node_next_child(c2);
226 PCUT_ASSERT_TRUE(c3 == NULL);
227
228 rc = sif_close(sess);
229 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
230
231 rv = remove(fname);
232 PCUT_ASSERT_INT_EQUALS(0, rv);
233}
234
235/** Test sif_node_insert_before. */
236PCUT_TEST(sif_node_insert_before)
237{
238 sif_sess_t *sess;
239 sif_node_t *root;
240 sif_node_t *ca;
241 sif_node_t *cb;
242 sif_node_t *cc;
243 sif_node_t *c1;
244 sif_node_t *c2;
245 sif_node_t *c3;
246 sif_node_t *c4;
247 sif_trans_t *trans;
248 errno_t rc;
249 int rv;
250 char *fname;
251 char *p;
252
253 fname = calloc(L_tmpnam, 1);
254 PCUT_ASSERT_NOT_NULL(fname);
255
256 p = tmpnam(fname);
257 PCUT_ASSERT_TRUE(p == fname);
258
259 rc = sif_create(fname, &sess);
260 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
261
262 root = sif_get_root(sess);
263
264 rc = sif_trans_begin(sess, &trans);
265 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
266
267 rc = sif_node_append_child(trans, root, "a", &ca);
268 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
269
270 rc = sif_node_append_child(trans, root, "c", &cc);
271 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
272
273 rc = sif_node_insert_before(trans, cc, "b", &cb);
274 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
275
276 rc = sif_trans_end(trans);
277 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
278
279 c1 = sif_node_first_child(root);
280 PCUT_ASSERT_TRUE(c1 == ca);
281
282 c2 = sif_node_next_child(c1);
283 PCUT_ASSERT_TRUE(c2 == cb);
284
285 c3 = sif_node_next_child(c2);
286 PCUT_ASSERT_TRUE(c3 == cc);
287
288 c4 = sif_node_next_child(c3);
289 PCUT_ASSERT_TRUE(c4 == NULL);
290
291 rc = sif_close(sess);
292 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
293
294 rv = remove(fname);
295 PCUT_ASSERT_INT_EQUALS(0, rv);
296}
297
298/** Test sif_node_insert_after. */
299PCUT_TEST(sif_node_insert_after)
300{
301 sif_sess_t *sess;
302 sif_node_t *root;
303 sif_node_t *ca;
304 sif_node_t *cb;
305 sif_node_t *cc;
306 sif_node_t *c1;
307 sif_node_t *c2;
308 sif_node_t *c3;
309 sif_node_t *c4;
310 sif_trans_t *trans;
311 errno_t rc;
312 int rv;
313 char *fname;
314 char *p;
315
316 fname = calloc(L_tmpnam, 1);
317 PCUT_ASSERT_NOT_NULL(fname);
318
319 p = tmpnam(fname);
320 PCUT_ASSERT_TRUE(p == fname);
321
322 rc = sif_create(fname, &sess);
323 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
324
325 root = sif_get_root(sess);
326
327 rc = sif_trans_begin(sess, &trans);
328 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
329
330 rc = sif_node_append_child(trans, root, "a", &ca);
331 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
332
333 rc = sif_node_append_child(trans, root, "c", &cc);
334 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
335
336 rc = sif_node_insert_after(trans, ca, "b", &cb);
337 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
338
339 rc = sif_trans_end(trans);
340 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
341
342 c1 = sif_node_first_child(root);
343 PCUT_ASSERT_TRUE(c1 == ca);
344
345 c2 = sif_node_next_child(c1);
346 PCUT_ASSERT_TRUE(c2 == cb);
347
348 c3 = sif_node_next_child(c2);
349 PCUT_ASSERT_TRUE(c3 == cc);
350
351 c4 = sif_node_next_child(c3);
352 PCUT_ASSERT_TRUE(c4 == NULL);
353
354 rc = sif_close(sess);
355 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
356
357 rv = remove(fname);
358 PCUT_ASSERT_INT_EQUALS(0, rv);
359}
360
361/** Test sif_node_destroy. */
362PCUT_TEST(sif_node_destroy)
363{
364 sif_sess_t *sess;
365 sif_node_t *root;
366 sif_node_t *ca;
367 sif_node_t *cb;
368 sif_node_t *cc;
369 sif_node_t *c1;
370 sif_node_t *c2;
371 sif_node_t *c3;
372 sif_trans_t *trans;
373 errno_t rc;
374 int rv;
375 char *fname;
376 char *p;
377
378 fname = calloc(L_tmpnam, 1);
379 PCUT_ASSERT_NOT_NULL(fname);
380
381 p = tmpnam(fname);
382 PCUT_ASSERT_TRUE(p == fname);
383
384 rc = sif_create(fname, &sess);
385 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
386
387 root = sif_get_root(sess);
388
389 rc = sif_trans_begin(sess, &trans);
390 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
391
392 rc = sif_node_append_child(trans, root, "a", &ca);
393 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
394
395 rc = sif_node_append_child(trans, root, "b", &cb);
396 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
397
398 rc = sif_node_append_child(trans, root, "c", &cc);
399 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
400
401 rc = sif_trans_end(trans);
402 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
403
404 rc = sif_trans_begin(sess, &trans);
405 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
406
407 sif_node_destroy(trans, cb);
408
409 rc = sif_trans_end(trans);
410 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
411
412 c1 = sif_node_first_child(root);
413 PCUT_ASSERT_TRUE(c1 == ca);
414
415 c2 = sif_node_next_child(c1);
416 PCUT_ASSERT_TRUE(c2 == cc);
417
418 c3 = sif_node_next_child(c2);
419 PCUT_ASSERT_TRUE(c3 == NULL);
420
421 rc = sif_close(sess);
422 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
423
424 rv = remove(fname);
425 PCUT_ASSERT_INT_EQUALS(0, rv);
426}
427
428/** Test sif_node_set_attr. */
429PCUT_TEST(sif_node_set_attr)
430{
431 sif_sess_t *sess;
432 sif_node_t *root;
433 sif_node_t *node;
434 sif_trans_t *trans;
435 errno_t rc;
436 int rv;
437 char *fname;
438 char *p;
439 const char *aval;
440
441 fname = calloc(L_tmpnam, 1);
442 PCUT_ASSERT_NOT_NULL(fname);
443
444 p = tmpnam(fname);
445 PCUT_ASSERT_TRUE(p == fname);
446
447 rc = sif_create(fname, &sess);
448 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
449
450 root = sif_get_root(sess);
451
452 rc = sif_trans_begin(sess, &trans);
453 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
454
455 rc = sif_node_append_child(trans, root, "node", &node);
456 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
457
458 rc = sif_node_set_attr(trans, node, "a", "?");
459 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
460
461 rc = sif_node_set_attr(trans, node, "a", "X");
462 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
463
464 rc = sif_node_set_attr(trans, node, "b", "Y");
465 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
466
467 rc = sif_trans_end(trans);
468 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
469
470 aval = sif_node_get_attr(node, "a");
471 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "X"));
472
473 aval = sif_node_get_attr(node, "b");
474 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "Y"));
475
476 aval = sif_node_get_attr(node, "c");
477 PCUT_ASSERT_NULL((void *) aval); // XXX PCUT_ASSERT_NULL does not accept const pointer
478
479 rc = sif_close(sess);
480 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
481
482 rv = remove(fname);
483 PCUT_ASSERT_INT_EQUALS(0, rv);
484}
485
486/** Test sif_node_unset_attr. */
487PCUT_TEST(sif_node_unset_attr)
488{
489 sif_sess_t *sess;
490 sif_node_t *root;
491 sif_node_t *node;
492 sif_trans_t *trans;
493 errno_t rc;
494 int rv;
495 char *fname;
496 char *p;
497 const char *aval;
498
499 fname = calloc(L_tmpnam, 1);
500 PCUT_ASSERT_NOT_NULL(fname);
501
502 p = tmpnam(fname);
503 PCUT_ASSERT_TRUE(p == fname);
504
505 rc = sif_create(fname, &sess);
506 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
507
508 root = sif_get_root(sess);
509
510 rc = sif_trans_begin(sess, &trans);
511 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
512
513 rc = sif_node_append_child(trans, root, "node", &node);
514 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
515
516 rc = sif_node_set_attr(trans, node, "a", "X");
517 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
518
519 rc = sif_trans_end(trans);
520 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
521
522 aval = sif_node_get_attr(node, "a");
523 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "X"));
524
525 rc = sif_trans_begin(sess, &trans);
526 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
527
528 sif_node_unset_attr(trans, node, "a");
529 sif_node_unset_attr(trans, node, "b");
530
531 rc = sif_trans_end(trans);
532 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
533
534 aval = sif_node_get_attr(node, "a");
535 PCUT_ASSERT_NULL((void *) aval); // XXX PCUT_ASSERT_NULL does not accept const pointer
536
537 rc = sif_close(sess);
538 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
539
540 rv = remove(fname);
541 PCUT_ASSERT_INT_EQUALS(0, rv);
542}
543
544/** Test persistence of nodes and attributes. */
545PCUT_TEST(sif_persist)
546{
547 sif_sess_t *sess;
548 sif_node_t *root;
549 sif_node_t *node;
550 sif_trans_t *trans;
551 errno_t rc;
552 int rv;
553 char *fname;
554 char *p;
555 const char *aval;
556
557 fname = calloc(L_tmpnam, 1);
558 PCUT_ASSERT_NOT_NULL(fname);
559
560 p = tmpnam(fname);
561 PCUT_ASSERT_TRUE(p == fname);
562
563 rc = sif_create(fname, &sess);
564 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
565
566 root = sif_get_root(sess);
567
568 rc = sif_trans_begin(sess, &trans);
569 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
570
571 rc = sif_node_append_child(trans, root, "node", &node);
572 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
573
574 rc = sif_node_set_attr(trans, node, "a", "X");
575 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
576
577 rc = sif_trans_end(trans);
578 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
579
580 rc = sif_close(sess);
581 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
582
583 /* Now reopen the repository */
584
585 rc = sif_open(fname, &sess);
586 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
587
588 root = sif_get_root(sess);
589
590 node = sif_node_first_child(root);
591 PCUT_ASSERT_NOT_NULL(node);
592 PCUT_ASSERT_INT_EQUALS(0, str_cmp(sif_node_get_type(node), "node"));
593
594 aval = sif_node_get_attr(node, "a");
595 PCUT_ASSERT_INT_EQUALS(0, str_cmp(aval, "X"));
596
597 rc = sif_close(sess);
598 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
599
600 rv = remove(fname);
601 PCUT_ASSERT_INT_EQUALS(0, rv);
602}
603
604PCUT_EXPORT(sif);
Note: See TracBrowser for help on using the repository browser.