Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 <[email protected]>
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 
44 AST_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 
74 AST_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 
95 AST_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 
116 AST_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 
142 static 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 
151 static 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 
160 AST_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",
165 );
AST_TEST_DEFINE(websocket_client_create_and_connect)
#define REMOTE_URL
Asterisk main include file. File version handling, generic pbx functions.
#define CATEGORY
ast_websocket_result
Result code for a websocket client.
static int load_module(void)
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
int AST_OPTIONAL_API_NAME() ast_websocket_write_string(struct ast_websocket *ws, const char *buf)
int AST_OPTIONAL_API_NAME() ast_websocket_read_string(struct ast_websocket *ws, char **buf)
#define NULL
Definition: resample.c:96
static int unload_module(void)
const char *AST_OPTIONAL_API_NAME() ast_websocket_client_accept_protocol(struct ast_websocket *ws)
void write_buf(int file, char *buffer, int num)
Definition: eagi_proxy.c:312
#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:911
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
Core PBX routines and definitions.
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
#define ast_free(a)
Definition: astmm.h:182
Structure definition for session.
struct ast_websocket *AST_OPTIONAL_API_NAME() ast_websocket_client_create(const char *uri, const char *protocols, struct ast_tls_config *tls_cfg, enum ast_websocket_result *result)
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static PGresult * result
Definition: cel_pgsql.c:88
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.