libamxb  4.8.2
Bus Agnostic C API
test_amxb_ba_connect.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 
75 #include "test_amxb_ba_connect.h"
76 
77 #include <amxc/amxc_macros.h>
78 static char* dummy_ctx = "xbus";
79 static char verify_host[64];
80 static char verify_port[64];
81 static char verify_path[64];
82 static int return_val;
83 
84 int __wrap_dlclose(void* handle);
85 
86 static void* dummy_connect(const char* host,
87  const char* port,
88  const char* path,
89  UNUSED amxp_signal_mngr_t* sigmngr) {
90  if(host != NULL) {
91  strcpy(verify_host, host);
92  }
93  if(port != NULL) {
94  strcpy(verify_port, port);
95  }
96  strcpy(verify_path, path);
97 
98  return dummy_ctx;
99 }
100 
101 static int dummy_disconnect(void* ctx) {
102  assert_ptr_equal(ctx, dummy_ctx);
103  return return_val;
104 }
105 
106 static void* dummy_accept(UNUSED void* ctx, UNUSED amxp_signal_mngr_t* sigmngr) {
107  return dummy_ctx;
108 }
109 
110 static int dummy_get_fd(void* ctx) {
111  assert_ptr_equal(ctx, dummy_ctx);
112  return return_val;
113 }
114 
115 static void dummy_free(void* ctx) {
116  assert_ptr_equal(ctx, dummy_ctx);
117 }
118 
119 static int dummy_read(void* const ctx) {
120  assert_ptr_equal(ctx, dummy_ctx);
121  return return_val;
122 }
123 
124 static int dummy_read_raw(void* const ctx, UNUSED void* buf, UNUSED size_t size) {
125  assert_ptr_equal(ctx, dummy_ctx);
126  return return_val;
127 }
128 
131  .disconnect = dummy_disconnect,
132  .listen = dummy_connect,
133  .accept = dummy_accept,
134  .get_fd = dummy_get_fd,
135  .read = dummy_read,
136  .read_raw = dummy_read_raw,
137  .free = dummy_free,
138  .name = "xbus",
139  .size = sizeof(amxb_be_funcs_t),
140 };
141 
143  .size = sizeof(amxb_be_funcs_t),
144  .name = "ybus",
145 };
146 
148  .size = sizeof(amxb_be_funcs_t),
150  .name = "zbus",
151 };
152 
153 int __wrap_dlclose(UNUSED void* handle) {
154  return 0;
155 }
156 
157 void test_amxb_connect(UNUSED void** state) {
158  amxb_bus_ctx_t* ctx = NULL;
159  assert_int_equal(amxb_be_register(&dummy_be1), 0);
160  assert_int_equal(amxb_be_register(&dummy_be2), 0);
161 
162  assert_int_equal(amxb_connect(&ctx, "xbus://ipc:[/var/run/test.sock]"), AMXB_ERROR_INVALID_URI);
163  assert_int_equal(amxb_connect(&ctx, "rommmel/test"), AMXB_ERROR_INVALID_URI);
164  assert_int_equal(amxb_connect(NULL, "xbus://test:80/var/run/xbus.sock"), -1);
165  assert_int_equal(amxb_connect(&ctx, "fake://test:80/var/run/fake.sock"), AMXB_ERROR_NOT_SUPPORTED_SCHEME);
166  assert_ptr_equal(ctx, NULL);
167 
168  assert_int_equal(amxb_connect(&ctx, "ybus://test:80/var/run/ybus.sock"), AMXB_ERROR_NOT_SUPPORTED_OP);
169  assert_ptr_equal(ctx, NULL);
170 
171  dummy_ctx = NULL;
172  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), AMXB_ERROR_BACKEND_FAILED);
173  dummy_ctx = "xbus";
174  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
175  assert_ptr_not_equal(ctx, NULL);
176  assert_string_equal(verify_host, "test");
177  assert_string_equal(verify_port, "80");
178  assert_string_equal(verify_path, "/var/run/xbus.sock");
179  assert_int_not_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
180  assert_int_equal(amxb_disconnect(ctx), 0);
181  amxb_free(&ctx);
182 
183  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
184  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
185  ctx = NULL;
186  assert_int_not_equal(amxb_disconnect(ctx), 0);
187  amxb_free(&ctx);
188  assert_ptr_equal(ctx, NULL);
189  amxb_free(&ctx);
190  amxb_free(NULL);
191 
192  assert_int_equal(amxb_be_unregister(&dummy_be2), 0);
193 }
194 
195 void test_amxb_listen(UNUSED void** state) {
196  amxb_bus_ctx_t* ctx = NULL;
197  assert_int_equal(amxb_be_register(&dummy_be1), 0);
198 
199  dummy_ctx = "xbus";
200  assert_int_equal(amxb_listen(&ctx, "xbus:/tmp/test.sock"), 0);
201  assert_ptr_not_equal(ctx, NULL);
202  assert_string_equal(verify_path, "/tmp/test.sock");
203  assert_int_equal(amxb_disconnect(ctx), 0);
204  amxb_free(&ctx);
205 
206  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
207 }
208 
209 void test_amxb_accept(UNUSED void** state) {
210  amxb_bus_ctx_t* lctx = NULL;
211  amxb_bus_ctx_t* actx = NULL;
212  assert_int_equal(amxb_be_register(&dummy_be1), 0);
213 
214  dummy_ctx = "xbus";
215  assert_int_equal(amxb_listen(&lctx, "xbus:/tmp/test.sock"), 0);
216  assert_int_equal(amxb_accept(lctx, &actx), 0);
217  assert_ptr_not_equal(actx, NULL);
218  assert_int_equal(amxb_disconnect(actx), 0);
219  assert_int_equal(amxb_disconnect(lctx), 0);
220  amxb_free(&lctx);
221  amxb_free(&actx);
222 
223  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
224 }
225 
226 void test_amxb_disconnect(UNUSED void** state) {
227  amxb_bus_ctx_t* ctx = NULL;
228  assert_int_equal(amxb_be_register(&dummy_be1), 0);
229  assert_int_equal(amxb_be_register(&dummy_be3), 0);
230 
231  assert_int_not_equal(amxb_disconnect(NULL), 0);
232 
233  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
234  assert_ptr_not_equal(ctx, NULL);
235  assert_int_equal(amxb_disconnect(ctx), 0);
236  amxb_free(&ctx);
237 
238  assert_ptr_equal(ctx, NULL);
239  assert_int_not_equal(amxb_disconnect(ctx), 0);
240  assert_ptr_equal(ctx, NULL);
241 
242  assert_int_equal(amxb_connect(&ctx, "zbus://test:80/var/run/zbus.sock"), 0);
243  assert_ptr_not_equal(ctx, NULL);
244  assert_int_not_equal(amxb_disconnect(ctx), 0);
245  assert_ptr_not_equal(ctx, NULL);
246  amxb_free(&ctx);
247  assert_ptr_equal(ctx, NULL);
248 
249  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
250  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
251 }
252 
253 void test_amxb_get_fd(UNUSED void** state) {
254  amxb_bus_ctx_t* ctx = NULL;
255  assert_int_equal(amxb_be_register(&dummy_be1), 0);
256  assert_int_equal(amxb_be_register(&dummy_be3), 0);
257 
258  assert_int_not_equal(amxb_get_fd(NULL), 0);
259 
260  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
261  return_val = STDIN_FILENO;
262  assert_int_equal(amxb_get_fd(ctx), return_val);
263  return_val = -1;
264  assert_int_equal(amxb_get_fd(ctx), return_val);
265  return_val = 0;
266  assert_int_equal(amxb_disconnect(ctx), 0);
267  amxb_free(&ctx);
268 
269  return_val = 0;
270  assert_int_equal(amxb_connect(&ctx, "zbus://test:80/var/run/zbus.sock"), 0);
271  return_val = STDIN_FILENO;
272  assert_int_equal(amxb_get_fd(ctx), -1);
273  return_val = 0;
274  assert_int_not_equal(amxb_disconnect(ctx), 0);
275  amxb_free(&ctx);
276 
277  return_val = 0;
278  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
279  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
280 }
281 
282 void test_amxb_read(UNUSED void** state) {
283  amxb_bus_ctx_t* ctx = NULL;
284  assert_int_equal(amxb_be_register(&dummy_be1), 0);
285  assert_int_equal(amxb_be_register(&dummy_be3), 0);
286 
287  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
288  return_val = 0;
289  assert_int_equal(amxb_read(ctx), return_val);
290  return_val = -1;
291  assert_int_equal(amxb_read(ctx), return_val);
292  return_val = 0;
293  assert_int_equal(amxb_disconnect(ctx), 0);
294  amxb_free(&ctx);
295  assert_ptr_equal(ctx, NULL);
296 
297  assert_int_equal(amxb_connect(&ctx, "zbus://test:80/var/run/zbus.sock"), 0);
298  return_val = 0;
299  assert_int_equal(amxb_read(ctx), -1);
300  return_val = -1;
301  assert_int_equal(amxb_read(ctx), -1);
302  return_val = 0;
303  amxb_free(&ctx);
304 
305  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
306  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
307 
308  assert_int_not_equal(amxb_read(NULL), 0);
309 }
310 
311 void test_amxb_find_uris(UNUSED void** state) {
312  amxb_bus_ctx_t* ctx1 = NULL;
313  amxb_bus_ctx_t* ctx2 = NULL;
314  assert_int_equal(amxb_be_register(&dummy_be1), 0);
315  assert_int_equal(amxb_be_register(&dummy_be3), 0);
316 
317  assert_int_equal(amxb_connect(&ctx1, "xbus://test:80/var/run/xbus.sock"), 0);
318  assert_int_equal(amxb_connect(&ctx2, "zbus://test:80/var/run/zbus.sock"), 0);
319 
320  assert_ptr_equal(amxb_find_uri("xbus://test:80/var/run/xbus.sock"), ctx1);
321  assert_ptr_equal(amxb_find_uri("zbus://test:80/var/run/zbus.sock"), ctx2);
322  assert_ptr_equal(amxb_find_uri("ybus://test:80/var/run/ybus.sock"), NULL);
323 
324  amxb_free(&ctx1);
325  amxb_free(&ctx2);
326 
327  assert_ptr_equal(amxb_find_uri("xbus://test:80/var/run/xbus.sock"), NULL);
328  assert_ptr_equal(amxb_find_uri("zbus://test:80/var/run/zbus.sock"), NULL);
329  assert_ptr_equal(amxb_find_uri("ybus://test:80/var/run/ybus.sock"), NULL);
330 
331 
332  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
333  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
334 }
335 
336 void test_amxb_list_uris(UNUSED void** state) {
337  amxb_bus_ctx_t* ctx1 = NULL;
338  amxb_bus_ctx_t* ctx2 = NULL;
339  amxc_array_t* array = NULL;
340  assert_int_equal(amxb_be_register(&dummy_be1), 0);
341  assert_int_equal(amxb_be_register(&dummy_be3), 0);
342 
343  assert_int_equal(amxb_connect(&ctx1, "xbus://test:80/var/run/xbus.sock"), 0);
344  assert_int_equal(amxb_connect(&ctx2, "zbus://test:80/var/run/zbus.sock"), 0);
345 
346  array = amxb_list_uris();
347  assert_int_equal(amxc_array_size(array), 2);
348  amxc_array_delete(&array, NULL);
349 
350  amxb_free(&ctx1);
351  amxb_free(&ctx2);
352 
353  array = amxb_list_uris();
354  assert_int_equal(amxc_array_size(array), 0);
355  amxc_array_delete(&array, NULL);
356 
357  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
358  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
359 }
360 
361 void test_amxb_read_raw(UNUSED void** state) {
362  amxb_bus_ctx_t* ctx = NULL;
363  char* buffer[100];
364  assert_int_equal(amxb_be_register(&dummy_be1), 0);
365  assert_int_equal(amxb_be_register(&dummy_be3), 0);
366 
367  assert_int_equal(amxb_connect(&ctx, "xbus://test:80/var/run/xbus.sock"), 0);
368  return_val = 100;
369  assert_int_equal(amxb_read_raw(ctx, buffer, 100), return_val);
370  assert_int_equal(amxb_read_raw(ctx, NULL, 100), -1);
371  assert_int_equal(amxb_read_raw(ctx, buffer, 0), -1);
372  return_val = -1;
373  assert_int_equal(amxb_read_raw(ctx, buffer, 100), return_val);
374  return_val = 0;
375  assert_int_equal(amxb_disconnect(ctx), 0);
376  amxb_free(&ctx);
377  assert_ptr_equal(ctx, NULL);
378 
379  assert_int_equal(amxb_connect(&ctx, "zbus://test:80/var/run/zbus.sock"), 0);
380  return_val = 0;
381  assert_int_equal(amxb_read_raw(ctx, buffer, 100), -1);
382  return_val = -1;
383  assert_int_equal(amxb_read_raw(ctx, buffer, 100), -1);
384  return_val = 0;
385  amxb_free(&ctx);
386 
387  assert_int_equal(amxb_be_unregister(&dummy_be1), 0);
388  assert_int_equal(amxb_be_unregister(&dummy_be3), 0);
389 
390  assert_int_equal(amxb_read_raw(NULL, buffer, 100), -1);
391 }
Ambiorix bus agnostic API header file.
Ambiorix bus agnostic API header file.
auto connect
Definition: test.odl:65
struct _amxb_be_funcs amxb_be_funcs_t
Definition: amxb_types.h:79
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_accept(amxb_bus_ctx_t *listen_ctx, amxb_bus_ctx_t **accepted_ctx)
Accepts an incomming connection request.
int amxb_listen(amxb_bus_ctx_t **ctx, const char *uri)
Create a listen socket.
amxc_array_t * amxb_list_uris(void)
List all open connections by their uri.
int amxb_connect(amxb_bus_ctx_t **ctx, const char *uri)
Create a bus connection.
amxb_bus_ctx_t * amxb_find_uri(const char *uri)
Find the bus context for a given uri.
int amxb_read_raw(const amxb_bus_ctx_t *const ctx, void *buf, size_t count)
Attempts to read up to count bytes from the file descriptor into the buffer starting at buf.
void amxb_free(amxb_bus_ctx_t **ctx)
Frees allocated memory.
int amxb_get_fd(const amxb_bus_ctx_t *const ctx)
Get the connection file descriptor.
int amxb_disconnect(amxb_bus_ctx_t *ctx)
Disconnects a bus connection.
int amxb_read(const amxb_bus_ctx_t *const ctx)
Reads data from the file descriptor.
#define AMXB_ERROR_BACKEND_FAILED
Back-end failed.
Definition: amxb_error.h:128
#define AMXB_ERROR_NOT_SUPPORTED_OP
Function/operation not supported.
Definition: amxb_error.h:110
#define AMXB_ERROR_NOT_SUPPORTED_SCHEME
URI scheme not supported.
Definition: amxb_error.h:104
#define AMXB_ERROR_INVALID_URI
Invalid URI.
Definition: amxb_error.h:98
The back-end interface structure.
amxb_be_connect_fn_t connect
static int dummy_read_raw(void *const ctx, UNUSED void *buf, UNUSED size_t size)
void test_amxb_listen(UNUSED void **state)
void test_amxb_read(UNUSED void **state)
void test_amxb_list_uris(UNUSED void **state)
static amxb_be_funcs_t dummy_be1
static int return_val
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)
static char verify_host[64]
int __wrap_dlclose(void *handle)
static void * dummy_accept(UNUSED void *ctx, UNUSED amxp_signal_mngr_t *sigmngr)
static int dummy_disconnect(void *ctx)
void test_amxb_disconnect(UNUSED void **state)
static void dummy_free(void *ctx)
void test_amxb_read_raw(UNUSED void **state)
void test_amxb_connect(UNUSED void **state)
static amxb_be_funcs_t dummy_be3
void test_amxb_accept(UNUSED void **state)
static char verify_path[64]
void test_amxb_get_fd(UNUSED void **state)
static int dummy_read(void *const ctx)
static char * dummy_ctx
void test_amxb_find_uris(UNUSED void **state)
static amxb_be_funcs_t dummy_be2