Asterisk - The Open Source Telephony Project GIT-master-7e7a603
test_aeap_transport.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Sangoma Technologies Corporation
5 *
6 * Kevin Harwell <kharwell@sangoma.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*** MODULEINFO
20 <depend>TEST_FRAMEWORK</depend>
21 <depend>res_aeap</depend>
22 <support_level>core</support_level>
23 ***/
24
25#include "asterisk.h"
26
27#include "asterisk/http.h"
28#include "asterisk/test.h"
29#include "asterisk/module.h"
30
31#include "../res/res_aeap/transport.h"
32
33#define CATEGORY "/res/aeap/transport/"
34
35#define ADDR "127.0.0.1:8088"
36#define TRANSPORT_URL "ws://" ADDR "/ws"
37#define TRANSPORT_URL_INVALID "ws://" ADDR "/invalid"
38#define TRANSPORT_PROTOCOL "echo"
39#define TRANSPORT_PROTOCOL_INVALID "invalid"
40#define TRANSPORT_TIMEOUT 2000
41
42AST_TEST_DEFINE(transport_create_invalid)
43{
45
46 switch (cmd) {
47 case TEST_INIT:
48 info->name = __func__;
49 info->explicit_only = 0;
50 info->category = CATEGORY;
51 info->summary = "test creating an AEAP invalid transport type";
52 info->description = info->summary;
53 return AST_TEST_NOT_RUN;
54 case TEST_EXECUTE:
55 break;
56 }
57
58 /* Transport is expected to be NULL here */
59 ast_test_validate(test, !(transport = aeap_transport_create("invalid")));
60
61 return AST_TEST_PASS;
62}
63
65{
67
68 switch (cmd) {
69 case TEST_INIT:
70 info->name = __func__;
71 info->explicit_only = 0;
72 info->category = CATEGORY;
73 info->summary = "test creating an AEAP transport";
74 info->description = info->summary;
75 return AST_TEST_NOT_RUN;
76 case TEST_EXECUTE:
77 break;
78 }
79
80 /* Type is based off the scheme, so just pass in the URL here */
81 ast_test_validate(test, (transport = aeap_transport_create(TRANSPORT_URL)));
82
83 return AST_TEST_PASS;
84}
85
86AST_TEST_DEFINE(transport_connect)
87{
89
90 switch (cmd) {
91 case TEST_INIT:
92 info->name = __func__;
93 info->explicit_only = 0;
94 info->category = CATEGORY;
95 info->summary = "test connecting to an AEAP transport";
96 info->description = info->summary;
97 return AST_TEST_NOT_RUN;
98 case TEST_EXECUTE:
99 break;
100 }
101
102 /* Type is based off the scheme, so just pass in the URL for the type */
103 ast_test_validate(test, (transport = aeap_transport_create_and_connect(
105
106 ast_test_validate(test, aeap_transport_is_connected(transport));
107 ast_test_validate(test, !aeap_transport_disconnect(transport));
108 ast_test_validate(test, !aeap_transport_is_connected(transport));
109
110 return AST_TEST_PASS;
111}
112
113AST_TEST_DEFINE(transport_connect_fail)
114{
115 RAII_VAR(struct aeap_transport *, transport, NULL, aeap_transport_destroy);
116
117 switch (cmd) {
118 case TEST_INIT:
119 info->name = __func__;
120 info->explicit_only = 0;
121 info->category = CATEGORY;
122 info->summary = "test connecting failure for an AEAP transport";
123 info->description = info->summary;
124 return AST_TEST_NOT_RUN;
125 case TEST_EXECUTE:
126 break;
127 }
128
129 /* Test invalid address */
130 ast_test_validate(test, (transport = aeap_transport_create(TRANSPORT_URL)));
131
132 ast_test_validate(test, aeap_transport_connect(transport,
134
135 ast_test_validate(test, !aeap_transport_is_connected(transport));
136
137 aeap_transport_destroy(transport);
138
139 /* /\* Test invalid protocol *\/ */
140 ast_test_validate(test, (transport = aeap_transport_create(TRANSPORT_URL)));
141
142 ast_test_validate(test, aeap_transport_connect(transport,
144
145 ast_test_validate(test, !aeap_transport_is_connected(transport));
146
147 return AST_TEST_PASS;
148}
149
150AST_TEST_DEFINE(transport_binary)
151{
152 RAII_VAR(struct aeap_transport *, transport, NULL, aeap_transport_destroy);
153 int num = 38;
154 enum AST_AEAP_DATA_TYPE rtype;
155
156 switch (cmd) {
157 case TEST_INIT:
158 info->name = __func__;
159 info->explicit_only = 0;
160 info->category = CATEGORY;
161 info->summary = "test binary I/O from an AEAP transport";
162 info->description = info->summary;
163 return AST_TEST_NOT_RUN;
164 case TEST_EXECUTE:
165 break;
166 }
167
168 ast_test_validate(test, (transport = aeap_transport_create_and_connect(
170
171 ast_test_validate(test, aeap_transport_write(transport, &num, sizeof(num),
172 AST_AEAP_DATA_TYPE_BINARY) == sizeof(num));
173 ast_test_validate(test, aeap_transport_read(transport, &num,
174 sizeof(num), &rtype) == sizeof(num));
175 ast_test_validate(test, rtype == AST_AEAP_DATA_TYPE_BINARY);
176 ast_test_validate(test, num == 38);
177
178 return AST_TEST_PASS;
179}
180
181AST_TEST_DEFINE(transport_string)
182{
183 RAII_VAR(struct aeap_transport *, transport, NULL, aeap_transport_destroy);
184 char buf[16];
185 enum AST_AEAP_DATA_TYPE rtype;
186
187 switch (cmd) {
188 case TEST_INIT:
189 info->name = __func__;
190 info->explicit_only = 0;
191 info->category = CATEGORY;
192 info->summary = "test string I/O from an AEAP transport";
193 info->description = info->summary;
194 return AST_TEST_NOT_RUN;
195 case TEST_EXECUTE:
196 break;
197 }
198
199 ast_test_validate(test, (transport = aeap_transport_create_and_connect(
201
202 ast_test_validate(test, aeap_transport_write(transport, "foo bar baz", 11,
204 ast_test_validate(test, aeap_transport_read(transport, buf,
205 sizeof(buf) / sizeof(char), &rtype) == 11);
206 ast_test_validate(test, rtype == AST_AEAP_DATA_TYPE_STRING);
207 ast_test_validate(test, !strcmp(buf, "foo bar baz"));
208
209 return AST_TEST_PASS;
210}
211
213
214static int load_module(void)
215{
216 if (!(http_server = ast_http_test_server_get("aeap transport http server", NULL))) {
218 }
219
220 AST_TEST_REGISTER(transport_string);
221 AST_TEST_REGISTER(transport_binary);
222 AST_TEST_REGISTER(transport_connect_fail);
223 AST_TEST_REGISTER(transport_connect);
225 AST_TEST_REGISTER(transport_create_invalid);
226
228}
229
230static int unload_module(void)
231{
232 AST_TEST_UNREGISTER(transport_string);
233 AST_TEST_UNREGISTER(transport_binary);
234 AST_TEST_UNREGISTER(transport_connect_fail);
235 AST_TEST_UNREGISTER(transport_connect);
237 AST_TEST_UNREGISTER(transport_create_invalid);
238
239 ast_http_test_server_discard(http_server);
240
241 return 0;
242}
243
244AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk External Application Protocol Transport Tests",
245 .support_level = AST_MODULE_SUPPORT_CORE,
246 .load = load_module,
247 .unload = unload_module,
248 .requires = "res_aeap",
Asterisk main include file. File version handling, generic pbx functions.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Support for Private Asterisk HTTP Servers.
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:315
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
def info(msg)
AST_AEAP_DATA_TYPE
Supported Asterisk external application data types.
Definition: res_aeap.h:135
@ AST_AEAP_DATA_TYPE_BINARY
Definition: res_aeap.h:137
@ AST_AEAP_DATA_TYPE_STRING
Definition: res_aeap.h:138
static int transport_create(void *data)
Create a pjsip transport.
#define NULL
Definition: resample.c:96
Asterisk external application transport structure to be "derived" by specific transport implementatio...
Definition: transport.h:98
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_NOT_RUN
Definition: test.h:194
#define TRANSPORT_URL
#define TRANSPORT_PROTOCOL_INVALID
#define CATEGORY
static int load_module(void)
#define TRANSPORT_URL_INVALID
#define TRANSPORT_TIMEOUT
static int unload_module(void)
AST_TEST_DEFINE(transport_create_invalid)
static struct ast_http_server * http_server
#define TRANSPORT_PROTOCOL
intmax_t aeap_transport_write(struct aeap_transport *transport, const void *buf, intmax_t size, enum AST_AEAP_DATA_TYPE wtype)
Write data to the transport.
Definition: transport.c:146
int aeap_transport_connect(struct aeap_transport *transport, const char *url, const char *protocol, int timeout)
Connect a transport.
Definition: transport.c:48
void aeap_transport_destroy(struct aeap_transport *transport)
Destroy a transport.
Definition: transport.c:117
intmax_t aeap_transport_read(struct aeap_transport *transport, void *buf, intmax_t size, enum AST_AEAP_DATA_TYPE *rtype)
Read data from the transport.
Definition: transport.c:134
int aeap_transport_is_connected(struct aeap_transport *transport)
Whether or not the transport is in a connected state.
Definition: transport.c:85
struct aeap_transport * aeap_transport_create(const char *type)
Create an Asterisk external application transport.
Definition: transport.c:27
struct aeap_transport * aeap_transport_create_and_connect(const char *type, const char *url, const char *protocol, int timeout)
Create an Asterisk external application transport, and connect it.
Definition: transport.c:68
int aeap_transport_disconnect(struct aeap_transport *transport)
Disconnect a transport.
Definition: transport.c:94
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941