1 /**
2 * @file test_sec6_1_3.c
3 * @author Pavol Vican
4 * @brief Cmocka test for RFC 6020 section 6.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 <stdarg.h>
21 #include <cmocka.h>
22 #include <string.h>
23
24 #include "tests/config.h"
25 #include "libyang.h"
26
27 #define TEST_DIR "sec6_1_3"
28 #define TEST_NAME test_sec6_1_3
29 #define TEST_SCHEMA_FORMAT LYS_YANG
30 #define TEST_SCHEMA_COUNT 2
31
32 struct state {
33 struct ly_ctx *ctx;
34 struct lyd_node *node;
35 };
36
37 static int
setup_f(void ** state)38 setup_f(void **state)
39 {
40 struct state *st;
41
42 (*state) = st = calloc(1, sizeof *st);
43 if (!st) {
44 fprintf(stderr, "Memory allocation error");
45 return -1;
46 }
47
48 /* libyang context */
49 st->ctx = ly_ctx_new(NULL, 0);
50 if (!st->ctx) {
51 fprintf(stderr, "Failed to create context.\n");
52 return -1;
53 }
54
55 return 0;
56 }
57
58 static int
teardown_f(void ** state)59 teardown_f(void **state)
60 {
61 struct state *st = (*state);
62
63 lyd_free(st->node);
64 ly_ctx_destroy(st->ctx, NULL);
65 free(st);
66 (*state) = NULL;
67
68 return 0;
69 }
70
71 static void
TEST_QUOTING(void ** state)72 TEST_QUOTING(void **state)
73 {
74 struct state *st = (*state);
75 char buf[1024];
76 const struct lys_module *mod;
77 char *dsc="Test for special characters: { } ; space /* multiple\nline comment */ // comment";
78 char *ref="Test for special characters: { } ; space /* multiple line comment */ // comment";
79 char *contact="\"\" \\\\ \\ \n\t \\n\\t ";
80
81 sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod1.yang");
82 mod = lys_parse_path(st->ctx, buf, TEST_SCHEMA_FORMAT);
83 assert_ptr_not_equal(mod, NULL);
84 assert_string_equal(mod->dsc, dsc);
85 assert_string_equal(mod->ref, ref);
86 assert_string_equal(mod->org, dsc);
87 assert_string_equal(mod->contact, contact);
88 }
89
90 static void
TEST_ESCAPE_CHARACTER_IN_DOUBLE_QUOTING(void ** state)91 TEST_ESCAPE_CHARACTER_IN_DOUBLE_QUOTING(void **state)
92 {
93 struct state *st = (*state);
94 char buf[1024];
95 const struct lys_module *mod;
96 char *dsc="a\n b";
97 char *ref="a\t\n\tb";
98 char *org="a\t \t\n \t\tb";
99 char *contact="a \t\t\n\tb";
100
101 sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod5.yang");
102 mod = lys_parse_path(st->ctx, buf, TEST_SCHEMA_FORMAT);
103 assert_ptr_not_equal(mod, NULL);
104 assert_string_equal(mod->dsc, dsc);
105 assert_string_equal(mod->ref, ref);
106 assert_string_equal(mod->org, org);
107 assert_string_equal(mod->contact, contact);
108 }
109
110 static void
TEST_DOUBLE_QUOTING(void ** state)111 TEST_DOUBLE_QUOTING(void **state)
112 {
113 struct state *st = (*state);
114 char buf[1024];
115 const struct lys_module *mod;
116 char *pattern="This is example text,\nwhich is formatted.";
117
118 sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod2.yang");
119 mod = lys_parse_path(st->ctx, buf, TEST_SCHEMA_FORMAT);
120 assert_ptr_not_equal(mod, NULL);
121 assert_string_equal(mod->dsc, pattern);
122 assert_string_equal(mod->ref, pattern);
123 assert_string_equal(mod->org, pattern);
124 assert_string_equal(mod->contact, pattern);
125 }
126
127 static void
TEST_ILLEGAL_STRING(void ** state)128 TEST_ILLEGAL_STRING(void **state)
129 {
130 struct state *st = (*state);
131 char buf[1024];
132 const struct lys_module *mod;
133 int i;
134
135 for(i=0; i<TEST_SCHEMA_COUNT; i++) {
136 sprintf(buf, TESTS_DIR "/conformance/" TEST_DIR "/mod%d.yang",i+3);
137 mod = lys_parse_path(st->ctx, buf, TEST_SCHEMA_FORMAT);
138 assert_ptr_equal(mod, NULL);
139 }
140 }
141
142 int
main(void)143 main(void)
144 {
145 const struct CMUnitTest tests[] = {
146 cmocka_unit_test_setup_teardown(TEST_QUOTING, setup_f, teardown_f),
147 cmocka_unit_test_setup_teardown(TEST_ESCAPE_CHARACTER_IN_DOUBLE_QUOTING, setup_f, teardown_f),
148 cmocka_unit_test_setup_teardown(TEST_DOUBLE_QUOTING, setup_f, teardown_f),
149 cmocka_unit_test_setup_teardown(TEST_ILLEGAL_STRING, setup_f, teardown_f),
150 };
151
152 return cmocka_run_group_tests(tests, NULL, NULL);
153 }
154
155