1 /**
2  * @file metadata.c
3  * @author Radek Krejci <rkrejci@cesnet.cz>
4  * @brief libyang extension test plugin - special plugin for internal cmocka tests of extensions implementation.
5  * The extension definition can be found in libyang/tests/schema/yang/files/ext-def.yang
6  *
7  * Copyright (c) 2016-2017 CESNET, z.s.p.o.
8  *
9  * This source code is licensed under BSD 3-Clause License (the "License").
10  * You may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     https://opensource.org/licenses/BSD-3-Clause
14  */
15 
16 #ifdef __GNUC__
17 #  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
18 #else
19 #  define UNUSED(x) UNUSED_ ## x
20 #endif
21 
22 #include "../extensions.h"
23 
24 /**
25  * @brief Storage for ID used to check plugin API version compatibility.
26  */
27 LYEXT_VERSION_CHECK
28 
29 /**
30  * @brief Callback to check that the annotation can be instantiated inside the provided node
31  *
32  * @param[in] parent The parent of the instantiated extension.
33  * @param[in] parent_type The type of the structure provided as \p parent.
34  * @param[in] substmt_type libyang does not store all the extension instances in the structures where they are
35  *                         instantiated in the module. In some cases (see #LYEXT_SUBSTMT) they are stored in parent
36  *                         structure and marked with flag to know in which substatement of the parent the extension
37  *                         was originally instantiated.
38  * @return 0 - ok
39  *         1 - error
40  */
libyang_ext_test_position(const void * UNUSED (parent),LYEXT_PAR UNUSED (parent_type),LYEXT_SUBSTMT UNUSED (substmt_type))41 int libyang_ext_test_position(const void * UNUSED(parent), LYEXT_PAR UNUSED(parent_type),
42                               LYEXT_SUBSTMT UNUSED(substmt_type))
43 {
44     /* allow extension instance anywhere */
45     return 0;
46 }
47 
libyang_ext_skipped_position(const void * UNUSED (parent),LYEXT_PAR UNUSED (parent_type),LYEXT_SUBSTMT UNUSED (substmt_type))48 int libyang_ext_skipped_position(const void * UNUSED(parent), LYEXT_PAR UNUSED(parent_type),
49                               LYEXT_SUBSTMT UNUSED(substmt_type))
50 {
51     /* skip extension instance */
52     return 2;
53 }
54 
55 struct lyext_substmt libyang_ext_test_substmt[] = {
56     {LY_STMT_ARGUMENT,      0,                       LY_STMT_CARD_OPT}, /* const char* + uint8_t */
57     {LY_STMT_BASE,          1 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
58     {LY_STMT_BELONGSTO,     2 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char*[2] */
59     {LY_STMT_CONTACT,       4 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
60     {LY_STMT_DEFAULT,       5 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
61     {LY_STMT_DESCRIPTION,   6 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
62     {LY_STMT_ERRTAG,        7 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
63     {LY_STMT_ERRMSG,        8 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
64     {LY_STMT_KEY,           9 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
65     {LY_STMT_NAMESPACE,    10 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
66     {LY_STMT_ORGANIZATION, 11 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
67     {LY_STMT_PATH,         12 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
68     {LY_STMT_PREFIX,       13 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
69     {LY_STMT_PRESENCE,     14 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
70     {LY_STMT_REFERENCE,    15 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
71     {LY_STMT_REVISIONDATE, 16 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
72     {LY_STMT_UNITS,        17 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* const char* */
73     {LY_STMT_MODIFIER,     18 * sizeof(const char*) + 1 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* uint8_t */
74     {LY_STMT_REQINSTANCE,  18 * sizeof(const char*) + 2 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* uint8_t */
75     {LY_STMT_CONFIG,       18 * sizeof(const char*) + 3 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared uint16_t */
76     {LY_STMT_MANDATORY,    18 * sizeof(const char*) + 3 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared uint16_t */
77     {LY_STMT_ORDEREDBY,    18 * sizeof(const char*) + 3 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared uint16_t */
78     {LY_STMT_STATUS,       18 * sizeof(const char*) + 3 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared uint16_t */
79     {LY_STMT_DIGITS,       18 * sizeof(const char*) + 3 * sizeof(uint8_t) + sizeof(uint16_t), LY_STMT_CARD_OPT}, /* uint8_t */
80     {LY_STMT_MAX,          18 * sizeof(const char*) + 4 * sizeof(uint8_t) + sizeof(uint16_t), LY_STMT_CARD_OPT}, /* uint32_t* */
81     {LY_STMT_MIN,          18 * sizeof(const char*) + 4 * sizeof(uint8_t) +
82                            sizeof(uint16_t) + 1 * sizeof(uint32_t*), LY_STMT_CARD_OPT}, /* uint32_t* */
83     {LY_STMT_POSITION,     18 * sizeof(const char*) + 4 * sizeof(uint8_t) +
84                            sizeof(uint16_t) + 2 * sizeof(uint32_t*), LY_STMT_CARD_OPT}, /* uint32_t* */
85     {LY_STMT_VALUE,        18 * sizeof(const char*) + 4 * sizeof(uint8_t) +
86                            sizeof(uint16_t) + 2 * sizeof(uint32_t*) + 1 * sizeof(int32_t*), LY_STMT_CARD_OPT}, /* uint32_t* */
87 /* compress the offset calculation */
88     {LY_STMT_UNIQUE,       22 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_unique* */
89     {LY_STMT_MODULE,       23 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_module* */
90     {LY_STMT_ACTION,       24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
91     {LY_STMT_ANYDATA,      24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
92     {LY_STMT_ANYXML,       24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
93     {LY_STMT_CASE,         24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
94     {LY_STMT_CHOICE,       24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
95     {LY_STMT_CONTAINER,    24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
96     {LY_STMT_GROUPING,     24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
97     {LY_STMT_INPUT,        24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
98     {LY_STMT_LEAF,         24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
99     {LY_STMT_LEAFLIST,     24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
100     {LY_STMT_LIST,         24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
101     {LY_STMT_NOTIFICATION, 24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
102     {LY_STMT_OUTPUT,       24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
103     {LY_STMT_USES,         24 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* shared struct lys_node* */
104     {LY_STMT_TYPEDEF,      25 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_tpdf* */
105     {LY_STMT_TYPE,         26 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_type* */
106     {LY_STMT_IFFEATURE,    27 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_iffeature* */
107     {LY_STMT_LENGTH,       28 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_restr* */
108     {LY_STMT_MUST,         29 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_restr* */
109     {LY_STMT_PATTERN,      30 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_restr* */
110     {LY_STMT_RANGE,        31 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_restr* */
111     {LY_STMT_WHEN,         32 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_when* */
112     {LY_STMT_REVISION,     33 * sizeof(void*) + 6 * sizeof(uint8_t), LY_STMT_CARD_OPT}, /* struct lys_revision* */
113     {0, 0, 0} /* terminating item */
114 };
115 
116 struct lyext_substmt libyang_ext_test_substmt_arrays[] = {
117     {LY_STMT_ARGUMENT,      0,                       LY_STMT_CARD_ANY}, /* const char** + uint8_t* */
118     {LY_STMT_BASE,          1 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
119     {LY_STMT_BELONGSTO,     2 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char**[2] */
120     {LY_STMT_CONTACT,       4 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
121     {LY_STMT_DEFAULT,       5 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
122     {LY_STMT_DESCRIPTION,   6 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
123     {LY_STMT_ERRTAG,        7 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
124     {LY_STMT_ERRMSG,        8 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
125     {LY_STMT_KEY,           9 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
126     {LY_STMT_NAMESPACE,    10 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
127     {LY_STMT_ORGANIZATION, 11 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
128     {LY_STMT_PATH,         12 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
129     {LY_STMT_PREFIX,       13 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
130     {LY_STMT_PRESENCE,     14 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
131     {LY_STMT_REFERENCE,    15 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
132     {LY_STMT_REVISIONDATE, 16 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
133     {LY_STMT_UNITS,        17 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* const char** */
134     {LY_STMT_DIGITS,       18 * sizeof(const char*) + 1 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* uint8_t* */
135     {LY_STMT_MAX,          18 * sizeof(const char*) + 2 * sizeof(uint8_t*), LY_STMT_CARD_ANY}, /* uint32_t* */
136     {LY_STMT_MIN,          18 * sizeof(const char*) + 2 * sizeof(uint8_t*) +
137                             1 * sizeof(uint32_t*), LY_STMT_CARD_ANY}, /* uint32_t* */
138     {LY_STMT_POSITION,     18 * sizeof(const char*) + 2 * sizeof(uint8_t*) +
139                             2 * sizeof(uint32_t*), LY_STMT_CARD_ANY}, /* uint32_t* */
140     {LY_STMT_VALUE,        18 * sizeof(const char*) + 2 * sizeof(uint8_t*) +
141                             2 * sizeof(uint32_t*) + 1 * sizeof(int32_t*), LY_STMT_CARD_ANY}, /* int32_t* */
142 /* compress the offset calculation */
143     {LY_STMT_UNIQUE,       24 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_unique** */
144     {LY_STMT_MODULE,       25 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_module** */
145     {LY_STMT_ACTION,       26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
146     {LY_STMT_ANYDATA,      26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
147     {LY_STMT_ANYXML,       26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
148     {LY_STMT_CASE,         26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
149     {LY_STMT_CHOICE,       26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
150     {LY_STMT_CONTAINER,    26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
151     {LY_STMT_GROUPING,     26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
152     {LY_STMT_INPUT,        26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
153     {LY_STMT_LEAF,         26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
154     {LY_STMT_LEAFLIST,     26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
155     {LY_STMT_LIST,         26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
156     {LY_STMT_NOTIFICATION, 26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
157     {LY_STMT_OUTPUT,       26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
158     {LY_STMT_USES,         26 * sizeof(void*), LY_STMT_CARD_ANY}, /* shared struct lys_node* */
159     {LY_STMT_TYPEDEF,      27 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_tpdf** */
160     {LY_STMT_TYPE,         28 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_type** */
161     {LY_STMT_IFFEATURE,    29 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_iffeature** */
162     {LY_STMT_LENGTH,       30 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_restr** */
163     {LY_STMT_MUST,         31 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_restr** */
164     {LY_STMT_PATTERN,      32 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_restr** */
165     {LY_STMT_RANGE,        33 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_restr** */
166     {LY_STMT_WHEN,         34 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_when** */
167     {LY_STMT_REVISION,     35 * sizeof(void*), LY_STMT_CARD_ANY}, /* struct lys_revision** */
168     {0, 0, 0} /* terminating item */
169 };
170 
171 struct lyext_substmt libyang_ext_test_substmt_mand[] = {
172     {LY_STMT_DESCRIPTION,   0,                 LY_STMT_CARD_MAND}, /* const char* */
173     {LY_STMT_DEFAULT,       1 * sizeof(void*), LY_STMT_CARD_SOME}, /* const char* */
174     {LY_STMT_CONFIG,        2 * sizeof(void*), LY_STMT_CARD_MAND}, /* shared uint16_t */
175     {LY_STMT_MANDATORY,     2 * sizeof(void*), LY_STMT_CARD_MAND}, /* shared uint16_t */
176     {LY_STMT_STATUS,        2 * sizeof(void*), LY_STMT_CARD_MAND}, /* shared uint16_t */
177     {LY_STMT_DIGITS,        2 * sizeof(void*) + sizeof(uint16_t), LY_STMT_CARD_MAND}, /* uint8_t */
178     {LY_STMT_MIN,           2 * sizeof(void*) + sizeof(uint8_t) + sizeof(uint16_t), LY_STMT_CARD_SOME}, /* uint32_t* */
179     {LY_STMT_LEAF,          3 * sizeof(void*) + sizeof(uint8_t) + sizeof(uint16_t), LY_STMT_CARD_SOME}, /* shared struct lys_node* */
180     {LY_STMT_MUST,          4 * sizeof(void*) + sizeof(uint8_t) + sizeof(uint16_t), LY_STMT_CARD_SOME}, /* struct lys_restr* */
181     {0, 0, 0} /* terminating item */
182 };
183 
184 /**
185  * @brief Plugin structure with cardinalities up to 1
186  */
187 struct lyext_plugin_complex libyang_ext_test_p = {
188     .type = LYEXT_COMPLEX,
189     .flags = 0,
190     .check_position = &libyang_ext_test_position,
191     .check_result = NULL,
192     .check_inherit = NULL,
193 
194     /* specification of allowed substatements of the extension instance */
195     .substmt = libyang_ext_test_substmt,
196 
197     /* final size of the extension instance structure with the space for storing the substatements */
198     .instance_size = (sizeof(struct lys_ext_instance_complex) - 1) + 34 * sizeof(void*) + 6 * sizeof(uint8_t)
199 };
200 
201 /**
202  * @brief Plugin structure with cardinalities higher than 1
203  */
204 struct lyext_plugin_complex libyang_ext_test_arrays_p = {
205     .type = LYEXT_COMPLEX,
206     .flags = 0,
207     .check_position = &libyang_ext_test_position,
208     .check_result = NULL,
209     .check_inherit = NULL,
210 
211     /* specification of allowed substatements of the extension instance */
212     .substmt = libyang_ext_test_substmt_arrays,
213 
214     /* final size of the extension instance structure with the space for storing the substatements */
215     .instance_size = (sizeof(struct lys_ext_instance_complex) - 1) + 36 * sizeof(void*)
216 };
217 
218 /**
219  * @brief Plugin structure with mandatory tests
220  */
221 struct lyext_plugin_complex libyang_ext_test_mand_p = {
222     .type = LYEXT_COMPLEX,
223     .flags = 0,
224     .check_position = &libyang_ext_test_position,
225     .check_result = NULL,
226     .check_inherit = NULL,
227 
228     /* specification of allowed substatements of the extension instance */
229     .substmt = libyang_ext_test_substmt_mand,
230 
231     /* final size of the extension instance structure with the space for storing the substatements */
232     .instance_size = (sizeof(struct lys_ext_instance_complex) - 1) + 5 * sizeof(void*) + sizeof(uint8_t) + sizeof(uint16_t)
233 };
234 
235 struct lyext_plugin libyang_ext_test_skipped = {
236     .type = LYEXT_FLAG,
237     .flags = 0,
238     .check_position = &libyang_ext_skipped_position,
239     .check_result = NULL,
240     .check_inherit = NULL,
241     .valid_data = NULL
242 };
243 
244 /**
245  * @brief list of all extension plugins implemented here
246  *
247  * MANDATORY object for all libyang extension plugins, the name must match the <name>.so
248  */
249 struct lyext_plugin_list libyang_ext_test[] = {
250     {"ext-def", "2017-01-18", "complex", (struct lyext_plugin*)&libyang_ext_test_p},
251     {"ext-def", "2017-01-18", "complex-arrays", (struct lyext_plugin*)&libyang_ext_test_arrays_p},
252     {"ext-def", "2017-01-18", "complex-mand", (struct lyext_plugin*)&libyang_ext_test_mand_p},
253     {"ext-def", "2017-01-18", "skipped", (struct lyext_plugin*)&libyang_ext_test_skipped},
254     {NULL, NULL, NULL, NULL} /* terminating item */
255 };
256