libamxp  1.4.0
Patterns C Implementation
test_signal_slots_filtered.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 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <stdarg.h>
58 #include <stddef.h>
59 #include <setjmp.h>
60 #include <fcntl.h>
61 #include <unistd.h>
62 #include <signal.h>
63 #include <cmocka.h>
64 
65 #include <amxc/amxc_variant.h>
66 #include <amxc/amxc_lqueue.h>
67 
68 #include <amxp/amxp_signal.h>
69 #include <amxp/amxp_syssig.h>
70 #include <amxp/amxp_slot.h>
71 #include <amxp_signal_priv.h>
72 
74 
75 #include <amxc/amxc_macros.h>
76 static int slot_trigger_count = 0;
77 static const char* expected_sig_name = NULL;
78 
79 static void test_slot1(const char* const sig_name,
80  UNUSED const amxc_var_t* const data,
81  UNUSED void* const priv) {
82  assert_string_equal(expected_sig_name, sig_name);
84 }
85 
86 static void test_slot2(const char* const sig_name,
87  UNUSED const amxc_var_t* const data,
88  UNUSED void* const priv) {
89  assert_string_equal(expected_sig_name, sig_name);
91 }
92 
93 void test_slot_connect_regexp(UNUSED void** state) {
94  amxp_signal_t* sig = NULL;
95 
96  assert_int_equal(amxp_sigmngr_add_signal(NULL, "test:regexp-slot-1"), 0);
97  assert_int_equal(amxp_sigmngr_add_signal(NULL, "test:regexp-slot-2"), 0);
98  assert_int_equal(amxp_sigmngr_add_signal(NULL, "test:regexp-slot-3"), 0);
99 
100  assert_int_equal(amxp_slot_connect_filtered(NULL, "test:.*", NULL, test_slot1, NULL), 0);
101  assert_int_equal(amxp_slot_connect_filtered(NULL, "test:.*-2", NULL, test_slot2, NULL), 0);
102 
103  slot_trigger_count = 0;
104  expected_sig_name = "test:regexp-slot-1";
106  assert_int_equal(slot_trigger_count, 1);
107 
108  slot_trigger_count = 0;
109  expected_sig_name = "test:regexp-slot-2";
111  assert_int_equal(slot_trigger_count, 2);
112 
113  sig = amxp_sigmngr_find_signal(NULL, "test:regexp-slot-1");
114  amxp_sigmngr_remove_signal(NULL, "test:regexp-slot-1");
115  amxp_signal_delete(&sig);
116 
117  sig = amxp_sigmngr_find_signal(NULL, "test:regexp-slot-2");
118  amxp_sigmngr_remove_signal(NULL, "test:regexp-slot-2");
119  amxp_signal_delete(&sig);
120 
121  sig = amxp_sigmngr_find_signal(NULL, "test:regexp-slot-3");
122  amxp_sigmngr_remove_signal(NULL, "test:regexp-slot-3");
123  amxp_signal_delete(&sig);
124 }
125 
129 
130  assert_int_equal(amxp_sigmngr_add_signal(&sigmngr, "test:regexp-slot-1"), 0);
131  assert_int_equal(amxp_sigmngr_add_signal(&sigmngr, "test:regexp-slot-2"), 0);
132  assert_int_equal(amxp_sigmngr_add_signal(&sigmngr, "test:regexp-slot-3"), 0);
133 
134  assert_int_equal(amxp_slot_connect_filtered(&sigmngr, "test:.*", NULL, test_slot1, NULL), 0);
135  assert_int_equal(amxp_slot_connect_filtered(&sigmngr, "test:.*-2", NULL, test_slot2, NULL), 0);
136 
137  slot_trigger_count = 0;
138  expected_sig_name = "test:regexp-slot-1";
140  assert_int_equal(slot_trigger_count, 1);
141 
142  slot_trigger_count = 0;
143  expected_sig_name = "test:regexp-slot-2";
145  assert_int_equal(slot_trigger_count, 2);
146 
148 }
149 
151  assert_int_not_equal(amxp_slot_connect_filtered(NULL, "", NULL, test_slot1, NULL), 0);
152  assert_int_not_equal(amxp_slot_connect_filtered(NULL, NULL, NULL, test_slot1, NULL), 0);
153  assert_int_not_equal(amxp_slot_connect_filtered(NULL, "test", NULL, NULL, NULL), 0);
154 
155  assert_int_not_equal(amxp_slot_connect_filtered(NULL, "test([]]", NULL, test_slot1, NULL), 0);
156 }
157 
160  amxc_var_t filter_data;
161  amxc_var_t* field1 = NULL;
163  amxc_var_init(&filter_data);
164  amxc_var_set_type(&filter_data, AMXC_VAR_ID_HTABLE);
165 
166  assert_int_equal(amxp_sigmngr_add_signal(&sigmngr, "test:filter-slot-1"), 0);
167 
168  assert_int_equal(amxp_slot_connect_filtered(&sigmngr, "test:filter-slot-1", "field1 matches \"test-.*-data\"", test_slot1, NULL), 0);
169 
170  field1 = amxc_var_add_key(cstring_t, &filter_data, "field1", "test-extra-test-here-data");
171  slot_trigger_count = 0;
172  expected_sig_name = "test:filter-slot-1";
174  assert_int_equal(slot_trigger_count, 1);
175 
176  amxc_var_set(cstring_t, field1, "not-matching-data");
177  slot_trigger_count = 0;
178  expected_sig_name = "test:filter-slot-1";
180  assert_int_equal(slot_trigger_count, 0);
181 
182  assert_int_equal(amxp_slot_disconnect(&sigmngr, "test:filter-slot-1", test_slot1), 0);
183 
184  assert_int_equal(amxp_slot_connect_filtered(&sigmngr, "test:filter-slot-1", "field1 matches \"test-.*-data\"", test_slot1, NULL), 0);
185 
186  amxc_var_set_type(&filter_data, AMXC_VAR_ID_HTABLE);
187  amxc_var_add_key(cstring_t, &filter_data, "field2", "test-extra-test-here-data");
188  slot_trigger_count = 0;
189  expected_sig_name = "test:filter-slot-1";
191  assert_int_equal(slot_trigger_count, 0);
192 
193  assert_int_equal(amxp_slot_disconnect(&sigmngr, "*", test_slot1), 0);
194 
196  amxc_var_clean(&filter_data);
197 }
198 
201  amxc_var_t filter_data;
203  amxc_var_init(&filter_data);
204  amxc_var_set_type(&filter_data, AMXC_VAR_ID_HTABLE);
205 
206  assert_int_equal(amxp_sigmngr_add_signal(&sigmngr, "test:filter-slot-1"), 0);
207 
208  assert_int_not_equal(amxp_slot_connect_filtered(&sigmngr, "test-([-data", "field1 matches \".*\"", test_slot1, NULL), 0);
209 
210  amxc_var_add_key(cstring_t, &filter_data, "field1", "test-extra-test-here-data");
211  slot_trigger_count = 0;
212  expected_sig_name = "test:filter-slot-1";
214  assert_int_equal(slot_trigger_count, 0);
215 
217  amxc_var_clean(&filter_data);
218 }
219 
222  amxc_var_t filter_data;
223 
225  amxc_var_init(&filter_data);
226  amxc_var_set_type(&filter_data, AMXC_VAR_ID_HTABLE);
227 
228  assert_int_not_equal(amxp_slot_connect_filtered(&sigmngr, "test:filter-slot-1", "A <> B && !\"Test\"", test_slot1, NULL), 0);
229 
230  amxc_var_add_key(cstring_t, &filter_data, "field1", "test-extra-test-here-data");
231  slot_trigger_count = 0;
232  expected_sig_name = "test:filter-slot-1";
234  assert_int_equal(slot_trigger_count, 0);
235 
237  amxc_var_clean(&filter_data);
238 }
239 
243 
244  assert_int_equal(amxp_slot_connect_filtered(&sigmngr, "my_.*", NULL, test_slot1, NULL), 0);
245 
246  slot_trigger_count = 0;
247  expected_sig_name = "my_hallo";
249  assert_int_equal(slot_trigger_count, 1);
250 
252 }
Ambiorix signal manager and signal API header file.
Ambiorix slot API header file.
Ambiorix Linux Signal Handling.
#define UNUSED
Definition: main.c:68
int amxp_sigmngr_add_signal(amxp_signal_mngr_t *const sig_mngr, const char *name)
Adds a signal to a signal manager.
Definition: amxp_signal.c:433
int amxp_sigmngr_init(amxp_signal_mngr_t *sig_mngr)
Initializing function, initializes members of the amxp_signal_mngr_t structure.
Definition: amxp_signal.c:377
int amxp_sigmngr_clean(amxp_signal_mngr_t *sig_mngr)
Clean-up functions, cleans-up all members of a amxp_signal_mngr_t structure.
Definition: amxp_signal.c:405
void amxp_sigmngr_trigger_signal(amxp_signal_mngr_t *const sig_mngr, const char *name, const amxc_var_t *const data)
Triggers a signal.
Definition: amxp_signal.c:492
int amxp_sigmngr_remove_signal(amxp_signal_mngr_t *const sig_mngr, const char *name)
Removes a signal from a signal manager.
Definition: amxp_signal.c:448
amxp_signal_t * amxp_sigmngr_find_signal(const amxp_signal_mngr_t *const sig_mngr, const char *name)
Get the pointer to the signal.
Definition: amxp_signal.c:475
int amxp_signal_delete(amxp_signal_t **signal)
Destructor function, deletes a signal.
Definition: amxp_signal.c:669
int amxp_slot_connect_filtered(amxp_signal_mngr_t *const sig_mngr, const char *const sig_reg_exp, const char *const expression, amxp_slot_fn_t fn, void *const priv)
Connects a slot (function) to signals using a regular expression.
Definition: amxp_slot.c:330
int amxp_slot_disconnect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, amxp_slot_fn_t fn)
Disconnects a slot from (a) signal(s).
Definition: amxp_slot.c:380
Structure containing the signal manager information.
Definition: amxp_signal.h:103
Structure containing the signal information.
Definition: amxp_signal.h:119
static int slot_trigger_count
void test_slot_connect_regexp_invalid_args(UNUSED void **state)
void test_trigger_not_registered_signal(UNUSED void **state)
static const char * expected_sig_name
static void test_slot2(const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
void test_slot_connect_filtered_signals_invalid_expression(UNUSED void **state)
static void test_slot1(const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
void test_slot_connect_regexp_sigmngr(UNUSED void **state)
void test_slot_connect_filtered_signals(UNUSED void **state)
void test_slot_connect_filtered_signals_invalid_regexp(UNUSED void **state)
void test_slot_connect_regexp(UNUSED void **state)
static amxp_signal_mngr_t * sigmngr