Amxb_Ubus  3.3.1
Ambiorix Ubus API
test_amxb_ubus_subscribe.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/types.h>
55 #include <sys/stat.h>
56 #include <fcntl.h>
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <dlfcn.h>
61 #include <stdarg.h>
62 #include <stddef.h>
63 #include <setjmp.h>
64 #include <cmocka.h>
65 #include <signal.h>
66 
67 #include <amxc/amxc.h>
68 #include <amxp/amxp.h>
69 
70 #include <amxd/amxd_dm.h>
71 
72 #include <amxb/amxb.h>
73 
75 
76 static amxb_bus_ctx_t* bus_ctx = NULL;
77 
78 static const char* test_amxb_ubus_get_socket(void) {
79  struct stat sb;
80  if(stat("/var/run/ubus.sock", &sb) == 0) {
81  return "ubus:/var/run/ubus.sock";
82  }
83  if(stat("/var/run/ubus/ubus.sock", &sb) == 0) {
84  return "ubus:/var/run/ubus/ubus.sock";
85  }
86  return NULL;
87 }
88 
89 static void handle_events(void) {
90  printf("Handling events \n");
91  while(amxb_read(bus_ctx) > 0) {
92  printf("R");
93  }
94  while(amxp_signal_read() == 0) {
95  printf(".");
96  }
97  sleep(1);
98  while(amxb_read(bus_ctx) > 0) {
99  printf("R");
100  }
101  while(amxp_signal_read() == 0) {
102  printf(".");
103  }
104  printf("\n");
105  fflush(stdout);
106 }
107 
108 int test_amxb_ubus_subscribe_setup(UNUSED void** state) {
109  amxc_string_t txt;
110  sigset_t mask;
111  const char* ubus_sock = test_amxb_ubus_get_socket();
112  printf("Ubus socket = %s\n", ubus_sock);
113 
114  sigemptyset(&mask);
115  sigaddset(&mask, SIGALRM);
116 
117  sigprocmask(SIG_BLOCK, &mask, NULL);
118 
119 
120  amxc_string_init(&txt, 0);
121 
122  amxp_sigmngr_add_signal(NULL, "config:changed");
123 
124  amxc_string_reset(&txt);
125  amxc_string_setf(&txt, "amxrt -u ubus: -B ../mod-amxb-test-ubus.so -A ../test_data/test_nemo.odl &");
126  system(amxc_string_get(&txt, 0));
127 
128  amxc_string_clean(&txt);
129 
130  assert_int_equal(amxb_be_load("../mod-amxb-test-ubus.so"), 0);
131  assert_int_equal(amxb_connect(&bus_ctx, ubus_sock), 0);
132 
133  sleep(1);
134 
135  return 0;
136 }
137 
138 int test_amxb_ubus_subscribe_teardown(UNUSED void** state) {
139  amxb_disconnect(bus_ctx);
140  amxb_free(&bus_ctx);
141 
142  system("killall amxrt");
143 
144  amxb_be_remove_all();
145 
146  return 0;
147 }
148 
149 static void test_event_cb(UNUSED const char* const sig_name,
150  UNUSED const amxc_var_t* const data,
151  UNUSED void* const priv) {
152  printf("Event recieved %s\n", sig_name);
153 }
154 
155 void test_ubus_can_subscribe(UNUSED void** state) {
156  amxc_var_t ret;
157 
158  amxc_var_init(&ret);
159 
160  handle_events();
161 
162  assert_int_equal(amxb_subscribe(bus_ctx, "NeMo.Intf.", NULL, test_event_cb, NULL), AMXB_STATUS_OK);
163  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 5, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
164 
165  handle_events();
166 
167  amxc_var_clean(&ret);
168 }
169 
170 void test_ubus_can_unsubscribe(UNUSED void** state) {
171  amxc_var_t ret;
172 
173  amxc_var_init(&ret);
174 
175  handle_events();
176 
177 
178  assert_int_equal(amxb_unsubscribe(bus_ctx, "NeMo.Intf.", test_event_cb, NULL), AMXB_STATUS_OK);
179  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 6, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
180  handle_events();
181 
182  amxc_var_clean(&ret);
183 }
184 
185 void test_ubus_can_wait_for_object(UNUSED void** state) {
186  system("killall amxrt");
187 
188  amxp_sigmngr_add_signal(NULL, "wait:done");
189  amxp_slot_connect(NULL, "wait:done", NULL, test_event_cb, NULL);
190  assert_int_equal(amxb_wait_for_object("NeMo."), AMXB_STATUS_OK);
191  system("amxrt -u ubus: -B ../mod-amxb-test-ubus.so -A ../test_data/test_nemo.odl &");
192  sleep(1);
193 
194  handle_events();
195 
196  amxp_slot_disconnect(NULL, "wait:done", test_event_cb);
197 }
198 
199 void test_ubus_can_reactivate_subscriptions(UNUSED void** state) {
200  amxc_var_t config;
201  amxc_var_t* ubus_config = NULL;
202  amxc_var_t ret;
203 
204  amxc_var_init(&ret);
205 
206  amxc_var_init(&config);
207  amxc_var_set_type(&config, AMXC_VAR_ID_HTABLE);
208  ubus_config = amxc_var_add_key(amxc_htable_t, &config, "ubus", NULL);
209  amxc_var_add_key(bool, ubus_config, "watch-ubus-events", true);
210  amxb_set_config(&config);
211  amxp_sigmngr_trigger_signal(NULL, "config:changed", NULL);
212 
213  handle_events();
214  assert_int_equal(amxb_subscribe(bus_ctx, "NeMo.Intf.", NULL, test_event_cb, NULL), AMXB_STATUS_OK);
215 
216  system("killall amxrt");
217  handle_events();
218  amxp_timers_calculate();
219  amxp_timers_check();
220  sleep(1);
221  system("amxrt -u ubus: -B ../mod-amxb-test-ubus.so -A ../test_data/test_nemo.odl &");
222  sleep(1);
223  handle_events();
224  amxp_timers_calculate();
225  amxp_timers_check();
226  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 7, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
227 
228  handle_events();
229  amxp_timers_calculate();
230  amxp_timers_check();
231 
232  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 8, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
233  handle_events();
234 
235  amxb_set_config(NULL);
236  amxc_var_clean(&config);
237  amxc_var_clean(&ret);
238 }
void test_ubus_can_wait_for_object(UNUSED void **state)
void test_ubus_can_reactivate_subscriptions(UNUSED void **state)
static const char * test_amxb_ubus_get_socket(void)
void test_ubus_can_unsubscribe(UNUSED void **state)
int test_amxb_ubus_subscribe_setup(UNUSED void **state)
int test_amxb_ubus_subscribe_teardown(UNUSED void **state)
static amxb_bus_ctx_t * bus_ctx
void test_ubus_can_subscribe(UNUSED void **state)
static void test_event_cb(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
static void handle_events(void)