| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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" |
| 39 |
#include "asterisk/http_websocket.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 |
|
| 48 |
enum ast_websocket_result result; |
| 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); |
| 77 |
enum ast_websocket_result result; |
| 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); |
| 98 |
enum ast_websocket_result result; |
| 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; |
| 120 |
enum ast_websocket_result result; |
| 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); |
| 148 |
return AST_MODULE_LOAD_SUCCESS; |
| 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 |
); |