libamxp  1.4.0
Patterns C Implementation
test_connection.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/time.h>
55 #include <sys/resource.h>
56 
57 #include <string.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <stdarg.h>
61 #include <stddef.h>
62 #include <setjmp.h>
63 #include <inttypes.h>
64 #include <limits.h>
65 #include <unistd.h>
66 #include <fcntl.h>
67 #include <cmocka.h>
68 
69 #include <amxc/amxc_macros.h>
70 #include <amxc/amxc.h>
71 #include <amxp/amxp_signal.h>
72 #include <amxp/amxp_slot.h>
73 #include <amxp/amxp_connection.h>
74 
75 #include "test_connection.h"
76 
77 static int fds[2];
78 static char* el_data = "Some Data";
79 
80 static void reader(UNUSED int fd, UNUSED void* priv) {
81 
82 }
83 
84 static void writer(UNUSED int fd, UNUSED void* priv) {
85 
86 }
87 
88 static void connection_check_signal(const char* const sig_name,
89  const amxc_var_t* const data,
90  UNUSED void* const priv) {
91  uint32_t data_type = amxc_var_type_of(data);
92  uint32_t data_value = GET_UINT32(data, NULL);
93 
94  check_expected(sig_name);
95  check_expected(data_type);
96  check_expected(data_value);
97 }
98 
99 int test_connection_setup(UNUSED void** state) {
100  assert_int_equal(pipe(fds), 0);
101 
102  while(amxp_signal_read() == 0) {
103  }
104 
105  amxp_slot_connect(NULL, "connection-added", NULL, connection_check_signal, NULL);
106  amxp_slot_connect(NULL, "connection-deleted", NULL, connection_check_signal, NULL);
107  amxp_slot_connect(NULL, "listen-added", NULL, connection_check_signal, NULL);
108  amxp_slot_connect(NULL, "listen-deleted", NULL, connection_check_signal, NULL);
109  amxp_slot_connect(NULL, "connection-wait-write", NULL, connection_check_signal, NULL);
110 
111  return 0;
112 }
113 
114 int test_connection_teardown(UNUSED void** state) {
116  close(fds[0]);
117  close(fds[1]);
118  return 0;
119 }
120 
121 void test_can_add_connection(UNUSED void** state) {
122  expect_string(connection_check_signal, sig_name, "connection-added");
123  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
124  expect_value(connection_check_signal, data_value, fds[0]);
125 
126  assert_int_equal(amxp_connection_add(fds[0], reader, "test://localhost:1700", AMXP_CONNECTION_CUSTOM, NULL), 0);
127  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 1);
128  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
129 }
130 
131 void test_can_fetch_connection(UNUSED void** state) {
133 
134  assert_non_null(con);
135  assert_int_equal(con->fd, fds[0]);
136  assert_ptr_equal(con->reader, reader);
137  assert_string_equal(con->uri, "test://localhost:1700");
138  assert_int_equal(con->type, AMXP_CONNECTION_CUSTOM);
139 }
140 
142  expect_string(connection_check_signal, sig_name, "connection-wait-write");
143  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
144  expect_value(connection_check_signal, data_value, fds[0]);
145 
146  assert_int_equal(amxp_connection_wait_write(fds[0], writer), 0);
147  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 1);
148  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
149 }
150 
153  assert_int_equal(amxp_connection_set_el_data(fds[0], el_data), 0);
154  assert_int_not_equal(amxp_connection_set_el_data(fds[1], el_data), 0);
155 
156  assert_ptr_equal(con->el_data, el_data);
157 }
158 
160  amxp_connection_t* con = NULL;
161 
162  expect_string(connection_check_signal, sig_name, "connection-added");
163  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
164  expect_value(connection_check_signal, data_value, fds[1]);
165 
166  assert_int_equal(amxp_connection_add(fds[1], reader, "test://localhost:1700", AMXP_CONNECTION_BUS, NULL), 0);
167  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 2);
168  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
169 
171  assert_non_null(con);
172  assert_int_equal(con->fd, fds[0]);
173  assert_ptr_equal(con->reader, reader);
174  assert_string_equal(con->uri, "test://localhost:1700");
175  assert_int_equal(con->type, AMXP_CONNECTION_CUSTOM);
176  assert_non_null(con);
178  assert_null(con);
179 }
180 
181 void test_can_remove_connection(UNUSED void** state) {
182  expect_string(connection_check_signal, sig_name, "connection-deleted");
183  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
184  expect_value(connection_check_signal, data_value, fds[0]);
185 
186  assert_int_equal(amxp_connection_remove(fds[0]), 0);
187  assert_null(amxp_connection_get(fds[0]));
188 
189  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 1);
190  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
191 
192  expect_string(connection_check_signal, sig_name, "connection-deleted");
193  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
194  expect_value(connection_check_signal, data_value, fds[1]);
195 
196  assert_int_equal(amxp_connection_remove(fds[1]), 0);
197  assert_null(amxp_connection_get(fds[1]));
198 
199  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 0);
200  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
201 }
202 
203 void test_can_add_listener(UNUSED void** state) {
204  expect_string(connection_check_signal, sig_name, "listen-added");
205  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
206  expect_value(connection_check_signal, data_value, fds[0]);
207 
208  assert_int_equal(amxp_connection_add(fds[0], reader, "test://localhost:1700", AMXP_CONNECTION_LISTEN, NULL), 0);
209  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 0);
210  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 1);
211 }
212 
213 void test_can_fetch_listener(UNUSED void** state) {
215 
216  assert_non_null(con);
217  assert_int_equal(con->fd, fds[0]);
218  assert_ptr_equal(con->reader, reader);
219  assert_string_equal(con->uri, "test://localhost:1700");
220  assert_int_equal(con->type, AMXP_CONNECTION_LISTEN);
221 }
222 
223 void test_can_remove_listener(UNUSED void** state) {
224  expect_string(connection_check_signal, sig_name, "listen-deleted");
225  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
226  expect_value(connection_check_signal, data_value, fds[0]);
227 
228  assert_int_equal(amxp_connection_remove(fds[0]), 0);
229  assert_null(amxp_connection_get(fds[0]));
230 
231  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 0);
232  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
233 }
#define AMXP_CONNECTION_CUSTOM
#define AMXP_CONNECTION_BUS
#define AMXP_CONNECTION_LISTEN
Ambiorix signal manager and signal API header file.
Ambiorix slot API header file.
#define UNUSED
Definition: main.c:68
int amxp_connection_add(int fd, amxp_fd_cb_t reader, const char *uri, uint32_t type, void *priv)
Adds a file descriptor (fd) to the list of fds that must be watched.
amxc_llist_t * amxp_connection_get_listeners(void)
Get of the current listen sockets of the application.
amxp_connection_t * amxp_connection_get_next(amxp_connection_t *con, uint32_t type)
Gets the next connection data for a file descriptor.
int amxp_connection_set_el_data(int fd, void *el_data)
Sets event-loop data.
int amxp_connection_wait_write(int fd, amxp_fd_cb_t can_write_cb)
Adds a watcher to check if a fd is ready for write.
amxp_connection_t * amxp_connection_get_first(uint32_t type)
Gets the first connection of the given type.
amxp_connection_t * amxp_connection_get(int fd)
Gets the connection data for a file descriptor.
amxc_llist_t * amxp_connection_get_connections(void)
Get a list of the current connections of the application.
int amxp_connection_remove(int fd)
Removes the fd from the connection list.
int amxp_signal_read(void)
Reads from the amxp signal file descriptor.
Definition: amxp_signal.c:769
void amxp_slot_disconnect_all(amxp_slot_fn_t fn)
Disconnects a slot from all signals it was connected to.
Definition: amxp_slot.c:459
int amxp_slot_connect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, const char *const expression, amxp_slot_fn_t fn, void *const priv)
Connects a slot (function) to a named signal of a signal manager.
Definition: amxp_slot.c:300
amxp_fd_cb_t reader
void test_can_store_event_loop_data(UNUSED void **state)
int test_connection_teardown(UNUSED void **state)
void test_can_fetch_connection(UNUSED void **state)
void test_can_fetch_listener(UNUSED void **state)
void test_can_add_wait_write_connection(UNUSED void **state)
void test_can_add_listener(UNUSED void **state)
static void reader(UNUSED int fd, UNUSED void *priv)
void test_can_add_connection(UNUSED void **state)
void test_can_remove_connection(UNUSED void **state)
static char * el_data
int test_connection_setup(UNUSED void **state)
void test_can_iterate_connections(UNUSED void **state)
static void writer(UNUSED int fd, UNUSED void *priv)
static int fds[2]
void test_can_remove_listener(UNUSED void **state)
static void connection_check_signal(const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)