Asterisk - The Open Source Telephony Project GIT-master-f36a736
test_websocket_client.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Kevin Harwell <kharwell@digium.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/*!
20 * \file
21 * \brief Websocket Client Unit Tests
22 *
23 * \author Kevin Harwell <kharwell@digium.com>
24 *
25 */
26
27/*** MODULEINFO
28 <depend>TEST_FRAMEWORK</depend>
29 <depend>res_http_websocket</depend>
30 <support_level>core</support_level>
31 ***/
32
33#include "asterisk.h"
34
35#include "asterisk/test.h"
36#include "asterisk/module.h"
37#include "asterisk/astobj2.h"
38#include "asterisk/pbx.h"
40
41#define CATEGORY "/res/websocket/"
42#define REMOTE_URL "ws://127.0.0.1:8088/ws"
43
44AST_TEST_DEFINE(websocket_client_create_and_connect)
45{
46 RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
47
49 const char write_buf[] = "this is only a test";
50 RAII_VAR(char *, read_buf, NULL, ast_free);
51
52 switch (cmd) {
53 case TEST_INIT:
54 info->name = __func__;
55 info->explicit_only = 1;
56 info->category = CATEGORY;
57 info->summary = "test creation and connection of a client websocket";
58 info->description = "test creation and connection of a client websocket";
59 return AST_TEST_NOT_RUN;
60 case TEST_EXECUTE:
61 break;
62 }
63
64 ast_test_validate(test, (client = ast_websocket_client_create(
65 REMOTE_URL, "echo", NULL, &result)));
66
67 ast_test_validate(test, !ast_websocket_write_string(client, write_buf));
68 ast_test_validate(test, ast_websocket_read_string(client, &read_buf) > 0);
69 ast_test_validate(test, !strcmp(write_buf, read_buf));
70
71 return AST_TEST_PASS;
72}
73
74AST_TEST_DEFINE(websocket_client_bad_url)
75{
76 RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
78
79 switch (cmd) {
80 case TEST_INIT:
81 info->name = __func__;
82 info->category = CATEGORY;
83 info->summary = "websocket client - test bad url";
84 info->description = "pass a bad url and make sure it fails";
85 return AST_TEST_NOT_RUN;
86 case TEST_EXECUTE:
87 break;
88 }
89
90 ast_test_validate(test, !(client = ast_websocket_client_create(
91 "invalid", NULL, NULL, &result)));
92 return AST_TEST_PASS;
93}
94
95AST_TEST_DEFINE(websocket_client_unsupported_protocol)
96{
97 RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
99
100 switch (cmd) {
101 case TEST_INIT:
102 info->name = __func__;
103 info->category = CATEGORY;
104 info->summary = "websocket client - unsupported protocol";
105 info->description = "fails on an unsupported protocol";
106 return AST_TEST_NOT_RUN;
107 case TEST_EXECUTE:
108 break;
109 }
110
111 ast_test_validate(test, !(client = ast_websocket_client_create(
112 REMOTE_URL, "unsupported", NULL, &result)));
113 return AST_TEST_PASS;
114}
115
116AST_TEST_DEFINE(websocket_client_multiple_protocols)
117{
118 RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
119 const char *accept_protocol;
121
122 switch (cmd) {
123 case TEST_INIT:
124 info->name = __func__;
125 info->category = CATEGORY;
126 info->summary = "websocket client - test multiple protocols";
127 info->description = "test multi-protocol client";
128 return AST_TEST_NOT_RUN;
129 case TEST_EXECUTE:
130 break;
131 }
132
133 ast_test_validate(test, (client = ast_websocket_client_create(
134 REMOTE_URL, "echo,unsupported", NULL, &result)));
135
136 accept_protocol = ast_websocket_client_accept_protocol(client);
137 ast_test_validate(test, accept_protocol && !strcmp(accept_protocol, "echo"));
138
139 return AST_TEST_PASS;
140}
141
142static int load_module(void)
143{
144 AST_TEST_REGISTER(websocket_client_create_and_connect);
145 AST_TEST_REGISTER(websocket_client_bad_url);
146 AST_TEST_REGISTER(websocket_client_unsupported_protocol);
147 AST_TEST_REGISTER(websocket_client_multiple_protocols);
149}
150
151static int unload_module(void)
152{
153 AST_TEST_UNREGISTER(websocket_client_multiple_protocols);
154 AST_TEST_UNREGISTER(websocket_client_unsupported_protocol);
155 AST_TEST_UNREGISTER(websocket_client_bad_url);
156 AST_TEST_UNREGISTER(websocket_client_create_and_connect);
157 return 0;
158}
159
160AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Websocket client test module",
161 .support_level = AST_MODULE_SUPPORT_CORE,
162 .load = load_module,
163 .unload = unload_module,
164 .requires = "res_http_websocket",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
static PGresult * result
Definition: cel_pgsql.c:84
void write_buf(int file, char *buffer, int num)
Definition: eagi_proxy.c:312
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
int ast_websocket_read_string(struct ast_websocket *ws, char **buf)
Read a WebSocket frame containing string data.
ast_websocket_result
Result code for a websocket client.
struct ast_websocket * ast_websocket_client_create(const char *uri, const char *protocols, struct ast_tls_config *tls_cfg, enum ast_websocket_result *result)
Create, and connect, a websocket client.
const char * ast_websocket_client_accept_protocol(struct ast_websocket *ws)
Retrieve the server accepted sub-protocol on the client.
int ast_websocket_write_string(struct ast_websocket *ws, const char *buf)
Construct and transmit a WebSocket frame containing string data.
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ 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
def info(msg)
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
Structure definition for session.
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 REMOTE_URL
#define CATEGORY
AST_TEST_DEFINE(websocket_client_create_and_connect)
static int load_module(void)
static int unload_module(void)
#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