libamxp  1.4.0
Patterns C Implementation
test_var_search.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <sys/signalfd.h>
55 #include <signal.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 
61 #include <stdarg.h>
62 #include <stddef.h>
63 #include <setjmp.h>
64 #include <fcntl.h>
65 #include <cmocka.h>
66 #include <errno.h>
67 
68 #include <yajl/yajl_gen.h>
69 
70 #include <amxc/amxc.h>
71 #include <amxp/amxp_expression.h>
72 
73 #include <amxj/amxj_variant.h>
74 
75 #include "test_var_search.h"
76 
77 static amxc_var_t* test_set1 = NULL;
78 static amxc_var_t* test_set2 = NULL;
79 
80 static int read_data(const char* file, amxc_var_t** data) {
81  int fd = -1;
82  variant_json_t* reader = NULL;
83  // create a json reader
84  if(amxj_reader_new(&reader) != 0) {
85  printf("Failed to create json file reader");
86  return 1;
87  }
88 
89  // open the json file
90  fd = open(file, O_RDONLY);
91  if(fd == -1) {
92  amxj_reader_delete(&reader);
93  printf("File open file %s - error 0x%8.8X", file, errno);
94  return 2;
95  }
96 
97  // read the json file and parse the json text
98  amxj_read(reader, fd);
99 
100  // get the variant
101  *data = amxj_reader_result(reader);
102 
103  // delete the reader and close the file
104  amxj_reader_delete(&reader);
105  close(fd);
106 
107  return 0;
108 }
109 
110 int test_var_search_setup(UNUSED void** state) {
111  assert_int_equal(read_data("./test_set1.data", &test_set1), 0);
112  assert_int_equal(read_data("./test_set2.data", &test_set2), 0);
113  return 0;
114 }
115 
116 int test_var_search_teardown(UNUSED void** state) {
117  amxc_var_delete(&test_set1);
118  amxc_var_delete(&test_set2);
119  return 0;
120 }
121 
122 void test_can_filter_values(UNUSED void** state) {
123  const char* sp1 = "94:83:c4:06:da:66.[MACAddress == '00:01:02:03:44:f0'].Extender";
124  const char* sp2 = "['Foundation' in Science-fiction].Author";
125  const char* sp3 = "*.Awards.*.[year == 1980]";
126  const char* sp4 = "0.[Age > 50]";
127  const char* sp5 = "0.[Age > 60]";
128  const char* sp6 = "0.[Age > 10]";
129 
130  amxc_htable_it_t* it = NULL;
131  amxc_var_t* data = NULL;
132  amxc_htable_t results;
133  amxc_htable_init(&results, 5);
134 
135  assert_int_equal(amxp_expr_find_var_values(test_set1, &results, sp1), 0);
136  assert_int_equal(amxc_htable_size(&results), 1);
137  it = amxc_htable_get(&results, "'94:83:c4:06:da:66'.0.'Extender'");
138  assert_non_null(it);
139  data = amxc_var_from_htable_it(it);
140  assert_non_null(data);
141  assert_int_equal(amxc_var_type_of(data), AMXC_VAR_ID_BOOL);
142  amxc_htable_clean(&results, variant_htable_it_free);
143 
144  amxc_htable_init(&results, 5);
145  assert_int_equal(amxp_expr_find_var_values(test_set2, &results, sp2), 0);
146  assert_int_equal(amxc_htable_size(&results), 1);
147  it = amxc_htable_get(&results, "1.'Author'");
148  assert_non_null(it);
149  data = amxc_var_from_htable_it(it);
150  assert_non_null(data);
151  assert_int_equal(amxc_var_type_of(data), AMXC_VAR_ID_CSTRING);
152  amxc_htable_clean(&results, variant_htable_it_free);
153 
154  amxc_htable_init(&results, 5);
155  assert_int_equal(amxp_expr_find_var_values(test_set2, &results, sp3), 0);
156  assert_int_equal(amxc_htable_size(&results), 1);
157  it = amxc_htable_get(&results, "2.'Awards'.'Hugo'.1");
158  assert_non_null(it);
159  data = amxc_var_from_htable_it(it);
160  assert_non_null(data);
161  assert_int_equal(amxc_var_type_of(data), AMXC_VAR_ID_HTABLE);
162  amxc_htable_clean(&results, variant_htable_it_free);
163 
164  amxc_htable_init(&results, 5);
165  assert_int_equal(amxp_expr_find_var_values(test_set2, &results, sp4), 0);
166  assert_int_equal(amxc_htable_size(&results), 1);
167  it = amxc_htable_get(&results, "0.'John Doe'");
168  assert_non_null(it);
169  data = amxc_var_from_htable_it(it);
170  assert_non_null(data);
171  assert_int_equal(amxc_var_type_of(data), AMXC_VAR_ID_HTABLE);
172  amxc_htable_clean(&results, variant_htable_it_free);
173 
174  amxc_htable_init(&results, 5);
175  assert_int_equal(amxp_expr_find_var_values(test_set2, &results, sp5), 0);
176  assert_int_equal(amxc_htable_size(&results), 0);
177  amxc_htable_clean(&results, variant_htable_it_free);
178 
179  amxc_htable_init(&results, 5);
180  assert_int_equal(amxp_expr_find_var_values(test_set2, &results, sp6), 0);
181  assert_int_equal(amxc_htable_size(&results), 2);
182  amxc_htable_clean(&results, variant_htable_it_free);
183 }
184 
185 void test_can_use_paths(UNUSED void** state) {
186  const char* sp1 = "2.'Awards'.'Hugo'.1";
187  amxc_llist_it_t* it = NULL;
188  amxc_string_t* path = NULL;
189  amxc_llist_t results;
190  amxc_llist_init(&results);
191 
192  assert_int_equal(amxp_expr_find_var_paths(test_set2, &results, sp1), 0);
193  assert_int_equal(amxc_llist_size(&results), 1);
194  it = amxc_llist_get_first(&results);
195  assert_non_null(it);
196  path = amxc_string_from_llist_it(it);
197  assert_non_null(path);
198  assert_string_equal(amxc_string_get(path, 0), sp1);
199  amxc_llist_clean(&results, amxc_string_list_it_free);
200 }
201 
203  const char* sp1 = "2.'Awards'.'Hugo'.1";
204  const char* sp2 = "*.Awards.*.[year == 1980]";
205  amxc_var_t* data = NULL;
206 
207  data = amxp_expr_find_var(test_set2, sp1);
208  assert_non_null(data);
209 
210  data = amxp_expr_find_var(test_set2, sp2);
211  assert_non_null(data);
212 }
213 
215  const char* sp1 = "0.[Age > 10]";
216  amxc_var_t* data = NULL;
217 
218  data = amxp_expr_find_var(test_set2, sp1);
219  assert_null(data);
220 }
221 
223  const char* sp1 = "0.Age.[Age > 10]";
224  amxc_var_t* data = NULL;
225 
226  data = amxp_expr_find_var(test_set2, sp1);
227  assert_null(data);
228 }
Ambiorix expression parser and evaluate API header file.
#define UNUSED
Definition: main.c:68
int amxp_expr_find_var_values(const amxc_var_t *const var, amxc_htable_t *values, const char *path)
Search matching variant paths in a composite variant.
int amxp_expr_find_var_paths(const amxc_var_t *const var, amxc_llist_t *paths, const char *path)
Search matching variant paths in a composite variant.
amxc_var_t * amxp_expr_find_var(const amxc_var_t *const var, const char *path)
Search a matching variant and returns a pointer to that variant.
static void reader(UNUSED int fd, UNUSED void *priv)
static amxc_string_t path
static amxc_var_t * test_set1
void test_filter_fails_if_on_simple_type(UNUSED void **state)
void test_get_filtered_value_fails_when_multiple_matches_found(UNUSED void **state)
void test_can_filter_values(UNUSED void **state)
int test_var_search_teardown(UNUSED void **state)
void test_can_use_paths(UNUSED void **state)
int test_var_search_setup(UNUSED void **state)
static amxc_var_t * test_set2
void test_can_get_filtered_value(UNUSED void **state)
static int read_data(const char *file, amxc_var_t **data)