libamxb  4.8.2
Bus Agnostic C API
test_amxb_ba_register.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 <stdarg.h>
57 #include <stddef.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <setjmp.h>
61 #include <cmocka.h>
62 
63 #include <amxc/amxc_variant.h>
64 #include <amxc/amxc_htable.h>
65 #include <amxc/amxc_lqueue.h>
66 
67 #include <amxp/amxp_signal.h>
68 #include <amxp/amxp_slot.h>
69 
70 #include <amxd/amxd_dm.h>
71 
72 #include <amxb/amxb_be.h>
73 #include <amxb/amxb.h>
74 #include <amxb/amxb_register.h>
75 
76 #include "test_amxb_ba_register.h"
77 
78 #include <amxc/amxc_macros.h>
79 static char* dummy_ctx = "xbus";
80 static char verify_host[64];
81 static char verify_port[64];
82 static char verify_path[64];
83 static int return_val;
84 
85 static amxd_dm_t mydm;
86 
87 int __wrap_dlclose(void* handle);
88 
89 static void* dummy_connect(const char* host,
90  const char* port,
91  const char* path,
92  UNUSED amxp_signal_mngr_t* sigmngr) {
93  strcpy(verify_host, host);
94  strcpy(verify_port, port);
95  strcpy(verify_path, path);
96 
97  return dummy_ctx;
98 }
99 
100 static int dummy_disconnect(void* ctx) {
101  assert_ptr_equal(ctx, dummy_ctx);
102  return return_val;
103 }
104 
105 static int dummy_get_fd(void* ctx) {
106  assert_ptr_equal(ctx, dummy_ctx);
107  return return_val;
108 }
109 
110 static void dummy_free(void* ctx) {
111  assert_ptr_equal(ctx, dummy_ctx);
112 }
113 
114 static int dummy_read(void* const ctx) {
115  assert_ptr_equal(ctx, dummy_ctx);
116  return return_val;
117 }
118 
119 static int dummy_register(void* const ctx,
120  amxd_dm_t* const dm) {
121  assert_ptr_equal(ctx, dummy_ctx);
122  assert_ptr_equal(dm, &mydm);
123  return return_val;
124 }
125 
126 static int dummy_describe(UNUSED void* const bus_ctx,
127  UNUSED const char* object,
128  UNUSED const char* search_path,
129  UNUSED uint32_t flags,
130  UNUSED uint32_t access,
131  amxc_var_t* retval,
132  UNUSED int timeout) {
133  amxc_var_set(cstring_t, retval, "test");
134  return 0;
135 }
136 
139  .disconnect = dummy_disconnect,
140  .get_fd = dummy_get_fd,
141  .read = dummy_read,
142  .register_dm = dummy_register,
143  .free = dummy_free,
144  .describe = dummy_describe,
145  .name = "xbus",
146  .size = sizeof(amxb_be_funcs_t),
147 };
148 
151  .name = "zbus",
152  .size = sizeof(amxb_be_funcs_t),
153 };
154 
155 int __wrap_dlclose(UNUSED void* handle) {
156  return 0;
157 }
158 
159 void test_amxb_register(UNUSED void** state) {
160  amxb_bus_ctx_t* ctx = NULL;
161 
162  assert_int_equal(amxd_dm_init(&mydm), 0);
163  assert_int_equal(amxb_be_register(&dummy_be1), 0);
164  assert_int_equal(amxb_be_register(&dummy_be3), 0);
165 
166  return_val = 0;
167  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
168  assert_int_not_equal(amxb_register(ctx, NULL), 0);
169  assert_int_equal(amxb_register(ctx, &mydm), return_val);
170  amxb_free(&ctx);
171 
172  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
173  return_val = 1;
174  assert_int_equal(amxb_register(ctx, &mydm), return_val);
175  amxb_free(&ctx);
176 
177  return_val = 0;
178  assert_int_equal(amxb_connect(&ctx, "zbus://test:80/var/run/zbus.sock"), 0);
179  assert_int_equal(amxb_register(ctx, &mydm), AMXB_ERROR_NOT_SUPPORTED_OP);
180  amxb_free(&ctx);
181 
182 
183  return_val = 0;
184  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
185  amxb_disconnect(ctx);
186  assert_int_not_equal(amxb_register(ctx, &mydm), 0);
187  amxb_free(&ctx);
188 
189  assert_int_not_equal(amxb_register(ctx, &mydm), 0);
190 
191  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
192  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
193 
194  amxd_dm_clean(&mydm);
195 }
196 
197 void test_amxb_who_has(UNUSED void** state) {
198  amxb_bus_ctx_t* ctx = NULL;
199 
200  assert_int_equal(amxd_dm_init(&mydm), 0);
201  assert_int_equal(amxb_be_register(&dummy_be1), 0);
202 
203  return_val = 0;
204  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
205  assert_int_not_equal(amxb_register(ctx, NULL), 0);
206  assert_int_equal(amxb_register(ctx, &mydm), return_val);
207 
208  assert_ptr_equal(amxb_be_who_has("Main"), ctx);
209 
210  amxb_free(&ctx);
211  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
212  amxd_dm_clean(&mydm);
213 }
Ambiorix bus agnostic API header file.
Ambiorix bus agnostic API header file.
Ambiorix Bus Agnostic Data Model registration.
struct _amxb_be_funcs amxb_be_funcs_t
Definition: amxb_types.h:79
amxb_bus_ctx_t * amxb_be_who_has(const char *object_path)
Searches a bus context that can provide a certain object.
int amxb_be_unregister(amxb_be_funcs_t *const funcs)
Unregisters a backend interface.
int amxb_be_register(amxb_be_funcs_t *const funcs)
Registers backend interface.
int amxb_connect(amxb_bus_ctx_t **ctx, const char *uri)
Create a bus connection.
void amxb_free(amxb_bus_ctx_t **ctx)
Frees allocated memory.
int amxb_disconnect(amxb_bus_ctx_t *ctx)
Disconnects a bus connection.
#define AMXB_ERROR_NOT_SUPPORTED_OP
Function/operation not supported.
Definition: amxb_error.h:110
int amxb_register(amxb_bus_ctx_t *const ctx, amxd_dm_t *const dm)
Registers a data model to a certain bus context (connection).
The back-end interface structure.
amxb_be_connect_fn_t connect
static int dummy_register(void *const ctx, amxd_dm_t *const dm)
static amxb_be_funcs_t dummy_be1
static int return_val
void test_amxb_who_has(UNUSED void **state)
static void * dummy_connect(const char *host, const char *port, const char *path, UNUSED amxp_signal_mngr_t *sigmngr)
static char verify_port[64]
static int dummy_get_fd(void *ctx)
void test_amxb_register(UNUSED void **state)
static char verify_host[64]
int __wrap_dlclose(void *handle)
static int dummy_disconnect(void *ctx)
static int dummy_describe(UNUSED void *const bus_ctx, UNUSED const char *object, UNUSED const char *search_path, UNUSED uint32_t flags, UNUSED uint32_t access, amxc_var_t *retval, UNUSED int timeout)
static void dummy_free(void *ctx)
static amxd_dm_t mydm
static amxb_be_funcs_t dummy_be3
static char verify_path[64]
static int dummy_read(void *const ctx)
static char * dummy_ctx
static amxd_dm_t dm
Definition: test_amxb_e2e.c:85
static amxb_bus_ctx_t * bus_ctx
Definition: test_amxb_e2e.c:84