Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
test_websocket_client.c File Reference

Websocket Client Unit Tests. More...

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"
#include "asterisk/pbx.h"
#include "asterisk/http_websocket.h"
Include dependency graph for test_websocket_client.c:

Go to the source code of this file.

Macros

#define CATEGORY   "/res/websocket/"
 
#define REMOTE_URL   "ws://127.0.0.1:8088/ws"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (websocket_client_create_and_connect)
 
 AST_TEST_DEFINE (websocket_client_bad_url)
 
 AST_TEST_DEFINE (websocket_client_unsupported_protocol)
 
 AST_TEST_DEFINE (websocket_client_multiple_protocols)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Websocket client test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_http_websocket", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Websocket Client Unit Tests.

Author
Kevin Harwell kharw.nosp@m.ell@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file test_websocket_client.c.

Macro Definition Documentation

◆ CATEGORY

#define CATEGORY   "/res/websocket/"

Definition at line 41 of file test_websocket_client.c.

Referenced by AST_TEST_DEFINE().

◆ REMOTE_URL

#define REMOTE_URL   "ws://127.0.0.1:8088/ws"

Definition at line 42 of file test_websocket_client.c.

Referenced by AST_TEST_DEFINE().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 165 of file test_websocket_client.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 165 of file test_websocket_client.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 165 of file test_websocket_client.c.

◆ AST_TEST_DEFINE() [1/4]

AST_TEST_DEFINE ( websocket_client_create_and_connect  )

Definition at line 44 of file test_websocket_client.c.

References ao2_cleanup, ast_free, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_websocket_client_create(), ast_websocket_read_string(), ast_websocket_write_string(), CATEGORY, sip_to_pjsip::info(), NULL, RAII_VAR, REMOTE_URL, result, TEST_EXECUTE, TEST_INIT, and write_buf().

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 }
#define REMOTE_URL
#define CATEGORY
ast_websocket_result
Result code for a websocket client.
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
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
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)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static PGresult * result
Definition: cel_pgsql.c:88

◆ AST_TEST_DEFINE() [2/4]

AST_TEST_DEFINE ( websocket_client_bad_url  )

Definition at line 74 of file test_websocket_client.c.

References ao2_cleanup, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_websocket_client_create(), CATEGORY, sip_to_pjsip::info(), NULL, RAII_VAR, result, TEST_EXECUTE, and TEST_INIT.

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 }
#define CATEGORY
ast_websocket_result
Result code for a websocket client.
#define NULL
Definition: resample.c:96
#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
def info(msg)
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)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static PGresult * result
Definition: cel_pgsql.c:88

◆ AST_TEST_DEFINE() [3/4]

AST_TEST_DEFINE ( websocket_client_unsupported_protocol  )

Definition at line 95 of file test_websocket_client.c.

References ao2_cleanup, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_websocket_client_create(), CATEGORY, sip_to_pjsip::info(), NULL, RAII_VAR, REMOTE_URL, result, TEST_EXECUTE, and TEST_INIT.

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 }
#define REMOTE_URL
#define CATEGORY
ast_websocket_result
Result code for a websocket client.
#define NULL
Definition: resample.c:96
#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
def info(msg)
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)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static PGresult * result
Definition: cel_pgsql.c:88

◆ AST_TEST_DEFINE() [4/4]

AST_TEST_DEFINE ( websocket_client_multiple_protocols  )

Definition at line 116 of file test_websocket_client.c.

References ao2_cleanup, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_websocket_client_accept_protocol(), ast_websocket_client_create(), CATEGORY, sip_to_pjsip::info(), NULL, RAII_VAR, REMOTE_URL, result, TEST_EXECUTE, and TEST_INIT.

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 }
#define REMOTE_URL
#define CATEGORY
ast_websocket_result
Result code for a websocket client.
#define NULL
Definition: resample.c:96
const char *AST_OPTIONAL_API_NAME() ast_websocket_client_accept_protocol(struct ast_websocket *ws)
#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
def info(msg)
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)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static PGresult * result
Definition: cel_pgsql.c:88

◆ load_module()

static int load_module ( void  )
static

Definition at line 142 of file test_websocket_client.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

Referenced by unload_module().

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 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 151 of file test_websocket_client.c.

References AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, AST_TEST_UNREGISTER, ASTERISK_GPL_KEY, and load_module().

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 }
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Websocket client test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_http_websocket", }
static

Definition at line 165 of file test_websocket_client.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 165 of file test_websocket_client.c.