blob: f7e422d233f093e665fe55dfd55f9a4f45aa9d40 [file] [log] [blame]
Pavol Vicanc269b722016-07-15 16:07:41 +02001/**
2 * @file test_sec7_1.c
3 * @author Pavol Vican
4 * @brief Cmocka test for RFC 6020 section 7.1 (without 7.1.3) conformance.
5 *
6 * Copyright (c) 2016 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <setjmp.h>
18#include <errno.h>
19#include <unistd.h>
20#include <cmocka.h>
21#include <string.h>
Rastislav Szabo403f9d02016-10-04 15:32:13 +020022#include <sys/wait.h>
Pavol Vicanc269b722016-07-15 16:07:41 +020023
24#include "../config.h"
25#include "../../src/libyang.h"
26
27#define TEST_DIR "sec7_1"
28#define TEST_NAME test_sec7_1
29#define TEST_SCHEMA_COUNT 6
30#define TEST_SCHEMA_LOAD_FAIL 1,1,1,1,1,0
31#define TEST_DATA_FILE_COUNT 0
32#define TEST_DATA_FILE_LOAD_FAIL 0
33
34struct state {
35 struct ly_ctx *ctx;
36 struct lyd_node *node;
37};
38
39static int
40setup_f(void **state)
41{
42 struct state *st;
43
44 (*state) = st = calloc(1, sizeof *st);
45 if (!st) {
46 fprintf(stderr, "Memory allocation error");
47 return -1;
48 }
49
50 /* libyang context */
Radek Krejcidd3263a2017-07-15 11:50:09 +020051 st->ctx = ly_ctx_new(TESTS_DIR "/conformance/" TEST_DIR, 0);
Pavol Vicanc269b722016-07-15 16:07:41 +020052 if (!st->ctx) {
53 fprintf(stderr, "Failed to create context.\n");
54 return -1;
55 }
56
57 return 0;
58}
59
60static int
61teardown_f(void **state)
62{
63 struct state *st = (*state);
64
65 lyd_free(st->node);
66 ly_ctx_destroy(st->ctx, NULL);
67 free(st);
68 (*state) = NULL;
69
70 return 0;
71}
72
73static void
74TEST_MODULE(void **state)
75{
76 struct state *st = (*state);
77 const int schemas_fail[] = {TEST_SCHEMA_LOAD_FAIL};
78 const int data_files_fail[] = {TEST_DATA_FILE_LOAD_FAIL};
79 char buf[1024];
80 LYS_INFORMAT schema_format = LYS_IN_YANG;
81 const struct lys_module *mod;
82 int i, j, ret;
83
84 for (i = 0; i < 2; ++i) {
85 for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
86 sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.%s", j + 1, (schema_format == LYS_IN_YANG ? "yang" : "yin"));
87 mod = lys_parse_path(st->ctx, buf, schema_format);
88 if (schemas_fail[j]) {
89 assert_ptr_equal(mod, NULL);
90 } else {
91 assert_ptr_not_equal(mod, NULL);
92 }
93 }
94
95 for (j = 0; j < TEST_DATA_FILE_COUNT; ++j) {
96 sprintf(buf, TESTS_DIR "/conformance/data%d.xml", j + 1);
97 st->node = lyd_parse_path(st->ctx, buf, LYD_XML, LYD_OPT_CONFIG);
98 if (data_files_fail[j]) {
99 assert_ptr_not_equal(st->node, NULL);
100 } else {
101 assert_ptr_equal(st->node, NULL);
102 }
103 }
104
105 if (schema_format == LYS_IN_YANG) {
106 /* convert the modules */
107 for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
108 sprintf(buf, BUILD_DIR "/yang2yin "
109 TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yang "
110 TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1, j + 1);
111 ret = system(buf);
112 if (ret == -1) {
113 fprintf(stderr, "system() failed (%s).\n", strerror(errno));
114 fail();
115 } else if (WEXITSTATUS(ret) != 0) {
116 fprintf(stderr, "Executing command \"%s\" finished with %d.\n", buf, WEXITSTATUS(ret));
117 fail();
118 }
119 }
120
121 schema_format = LYS_IN_YIN;
122 } else {
123 /* remove the modules */
124 for (j = 0; j < TEST_SCHEMA_COUNT; ++j) {
125 sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yin", j + 1);
126 if (unlink(buf)) {
127 fprintf(stderr, "unlink() on \"%s\" failed (%s).\n", buf, strerror(errno));
128 }
129 }
130 }
131 }
132}
133
134int
135main(void)
136{
137 const struct CMUnitTest tests[] = {
138 cmocka_unit_test_setup_teardown(TEST_MODULE, setup_f, teardown_f),
139 };
140
141 return cmocka_run_group_tests(tests, NULL, NULL);
142}