| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
|
| 21 |
#if INCLUDE_WEBSOCK_TEST |
| 22 |
|
| 23 |
#define THIS_FILE "websock_test" |
| 24 |
|
| 25 |
#include <pjlib.h> |
| 26 |
#include <pjlib-util.h> |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define SERVER_BUF_SIZE 8192 |
| 39 |
#define WS_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
| 40 |
|
| 41 |
static struct echo_server_t |
| 42 |
{ |
| 43 |
pj_sock_t sock; |
| 44 |
pj_uint16_t port; |
| 45 |
pj_thread_t *thread; |
| 46 |
pj_bool_t quit; |
| 47 |
pj_pool_t *pool; |
| 48 |
} g_server; |
| 49 |
|
| 50 |
|
| 51 |
static void b64_encode(const pj_uint8_t *in, int in_len, |
| 52 |
char *out, int *out_len) |
| 53 |
{ |
| 54 |
pj_base64_encode(in, in_len, out, out_len); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
static int build_server_handshake(const char *req, char *resp, |
| 59 |
int resp_size) |
| 60 |
{ |
| 61 |
const char *key_hdr; |
| 62 |
char ws_key[64]; |
| 63 |
int key_len; |
| 64 |
char concat[128]; |
| 65 |
int concat_len; |
| 66 |
pj_sha1_context ctx; |
| 67 |
pj_uint8_t hash[20]; |
| 68 |
char accept_b64[32]; |
| 69 |
int accept_len = sizeof(accept_b64); |
| 70 |
const char *key_end; |
| 71 |
|
| 72 |
key_hdr = strstr(req, "Sec-WebSocket-Key: "); |
| 73 |
if (!key_hdr) return -1; |
| 74 |
key_hdr += 19; |
| 75 |
key_end = strstr(key_hdr, "\r\n"); |
| 76 |
if (!key_end) return -1; |
| 77 |
key_len = (int)(key_end - key_hdr); |
| 78 |
if (key_len >= (int)sizeof(ws_key)) return -1; |
| 79 |
pj_memcpy(ws_key, key_hdr, key_len); |
| 80 |
ws_key[key_len] = '\0'; |
| 81 |
|
| 82 |
concat_len = pj_ansi_snprintf(concat, sizeof(concat), "%s%s", |
| 83 |
ws_key, WS_GUID); |
| 84 |
pj_sha1_init(&ctx); |
| 85 |
pj_sha1_update(&ctx, (pj_uint8_t*)concat, concat_len); |
| 86 |
pj_sha1_final(&ctx, hash); |
| 87 |
b64_encode(hash, 20, accept_b64, &accept_len); |
| 88 |
accept_b64[accept_len] = '\0'; |
| 89 |
|
| 90 |
return pj_ansi_snprintf(resp, resp_size, |
| 91 |
"HTTP/1.1 101 Switching Protocols\r\n" |
| 92 |
"Upgrade: websocket\r\n" |
| 93 |
"Connection: Upgrade\r\n" |
| 94 |
"Sec-WebSocket-Accept: %s\r\n" |
| 95 |
"\r\n", accept_b64); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static pj_size_t server_decode_frame(const pj_uint8_t *data, pj_size_t len, |
| 102 |
pj_uint8_t *opcode, pj_bool_t *fin, |
| 103 |
pj_uint8_t *payload, pj_size_t *plen) |
| 104 |
{ |
| 105 |
pj_size_t pos = 0; |
| 106 |
pj_size_t pl; |
| 107 |
pj_uint8_t mask[4]; |
| 108 |
pj_size_t i; |
| 109 |
|
| 110 |
if (len < 2) return 0; |
| 111 |
*fin = (data[0] & 0x80) != 0; |
| 112 |
*opcode = data[0] & 0x0F; |
| 113 |
pl = data[1] & 0x7F; |
| 114 |
pos = 2; |
| 115 |
if (pl == 126) { |
| 116 |
if (len < 4) return 0; |
| 117 |
pl = ((pj_size_t)data[2] << 8) | data[3]; |
| 118 |
pos = 4; |
| 119 |
} else if (pl == 127) { |
| 120 |
if (len < 10) return 0; |
| 121 |
pl = ((pj_size_t)data[6] << 24) | ((pj_size_t)data[7] << 16) | |
| 122 |
((pj_size_t)data[8] << 8) | data[9]; |
| 123 |
pos = 10; |
| 124 |
} |
| 125 |
|
| 126 |
if (!(data[1] & 0x80)) return 0; |
| 127 |
if (len < pos + 4 + pl) return 0; |
| 128 |
|
| 129 |
if (pl > SERVER_BUF_SIZE) return 0; |
| 130 |
pj_memcpy(mask, &data[pos], 4); |
| 131 |
pos += 4; |
| 132 |
for (i = 0; i < pl; ++i) |
| 133 |
payload[i] = data[pos + i] ^ mask[i & 3]; |
| 134 |
*plen = pl; |
| 135 |
return pos + pl; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static pj_size_t server_encode_frame(pj_uint8_t *buf, pj_uint8_t opcode, |
| 140 |
const pj_uint8_t *payload, |
| 141 |
pj_size_t plen) |
| 142 |
{ |
| 143 |
pj_size_t pos = 0; |
| 144 |
buf[pos++] = (pj_uint8_t)(0x80 | opcode); |
| 145 |
if (plen < 126) { |
| 146 |
buf[pos++] = (pj_uint8_t)plen; |
| 147 |
} else if (plen <= 0xFFFF) { |
| 148 |
buf[pos++] = 126; |
| 149 |
buf[pos++] = (pj_uint8_t)((plen >> 8) & 0xFF); |
| 150 |
buf[pos++] = (pj_uint8_t)(plen & 0xFF); |
| 151 |
} else { |
| 152 |
buf[pos++] = 127; |
| 153 |
buf[pos++] = 0; buf[pos++] = 0; buf[pos++] = 0; buf[pos++] = 0; |
| 154 |
buf[pos++] = (pj_uint8_t)((plen >> 24) & 0xFF); |
| 155 |
buf[pos++] = (pj_uint8_t)((plen >> 16) & 0xFF); |
| 156 |
buf[pos++] = (pj_uint8_t)((plen >> 8) & 0xFF); |
| 157 |
buf[pos++] = (pj_uint8_t)(plen & 0xFF); |
| 158 |
} |
| 159 |
if (plen > 0) |
| 160 |
pj_memcpy(&buf[pos], payload, plen); |
| 161 |
return pos + plen; |
| 162 |
} |
| 163 |
|
| 164 |
static int server_thread(void *p) |
| 165 |
{ |
| 166 |
struct echo_server_t *srv = (struct echo_server_t*)p; |
| 167 |
pj_uint8_t buf[SERVER_BUF_SIZE]; |
| 168 |
pj_uint8_t frame_payload[SERVER_BUF_SIZE]; |
| 169 |
pj_uint8_t out_buf[SERVER_BUF_SIZE]; |
| 170 |
|
| 171 |
while (!srv->quit) { |
| 172 |
pj_sock_t csock = PJ_INVALID_SOCKET; |
| 173 |
pj_ssize_t rlen; |
| 174 |
int rc; |
| 175 |
pj_fd_set_t rset; |
| 176 |
pj_time_val timeout; |
| 177 |
pj_size_t total; |
| 178 |
pj_bool_t handshake_done; |
| 179 |
|
| 180 |
|
| 181 |
timeout.sec = 0; |
| 182 |
timeout.msec = 200; |
| 183 |
PJ_FD_ZERO(&rset); |
| 184 |
PJ_FD_SET(srv->sock, &rset); |
| 185 |
rc = pj_sock_select((int)srv->sock + 1, &rset, NULL, NULL, |
| 186 |
&timeout); |
| 187 |
if (rc != 1 || srv->quit) |
| 188 |
continue; |
| 189 |
|
| 190 |
if (pj_sock_accept(srv->sock, &csock, NULL, NULL) != PJ_SUCCESS) |
| 191 |
continue; |
| 192 |
|
| 193 |
|
| 194 |
total = 0; |
| 195 |
handshake_done = PJ_FALSE; |
| 196 |
|
| 197 |
while (!srv->quit) { |
| 198 |
timeout.sec = 5; |
| 199 |
timeout.msec = 0; |
| 200 |
PJ_FD_ZERO(&rset); |
| 201 |
PJ_FD_SET(csock, &rset); |
| 202 |
rc = pj_sock_select((int)csock + 1, &rset, NULL, NULL, |
| 203 |
&timeout); |
| 204 |
if (rc != 1) |
| 205 |
break; |
| 206 |
|
| 207 |
rlen = sizeof(buf) - total; |
| 208 |
if (pj_sock_recv(csock, buf + total, &rlen, 0) != PJ_SUCCESS |
| 209 |
|| rlen <= 0) |
| 210 |
{ |
| 211 |
break; |
| 212 |
} |
| 213 |
total += rlen; |
| 214 |
|
| 215 |
if (!handshake_done) { |
| 216 |
char resp[512]; |
| 217 |
int resp_len; |
| 218 |
pj_ssize_t slen; |
| 219 |
|
| 220 |
|
| 221 |
if (!strstr((char*)buf, "\r\n\r\n")) |
| 222 |
continue; |
| 223 |
|
| 224 |
resp_len = build_server_handshake((char*)buf, resp, |
| 225 |
sizeof(resp)); |
| 226 |
if (resp_len < 0) break; |
| 227 |
slen = resp_len; |
| 228 |
pj_sock_send(csock, resp, &slen, 0); |
| 229 |
handshake_done = PJ_TRUE; |
| 230 |
total = 0; |
| 231 |
continue; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
while (total > 0) { |
| 236 |
pj_uint8_t opcode; |
| 237 |
pj_bool_t fin; |
| 238 |
pj_size_t plen = 0; |
| 239 |
pj_size_t consumed; |
| 240 |
pj_size_t out_len; |
| 241 |
pj_ssize_t slen; |
| 242 |
|
| 243 |
consumed = server_decode_frame(buf, total, &opcode, &fin, |
| 244 |
frame_payload, &plen); |
| 245 |
if (consumed == 0) |
| 246 |
break; |
| 247 |
|
| 248 |
if (opcode == 0x8) { |
| 249 |
|
| 250 |
out_len = server_encode_frame(out_buf, 0x8, |
| 251 |
frame_payload, plen); |
| 252 |
slen = (pj_ssize_t)out_len; |
| 253 |
pj_sock_send(csock, out_buf, &slen, 0); |
| 254 |
goto client_done; |
| 255 |
} else if (opcode == 0x9) { |
| 256 |
|
| 257 |
out_len = server_encode_frame(out_buf, 0xA, |
| 258 |
frame_payload, plen); |
| 259 |
slen = (pj_ssize_t)out_len; |
| 260 |
pj_sock_send(csock, out_buf, &slen, 0); |
| 261 |
} else { |
| 262 |
|
| 263 |
out_len = server_encode_frame(out_buf, opcode, |
| 264 |
frame_payload, plen); |
| 265 |
slen = (pj_ssize_t)out_len; |
| 266 |
pj_sock_send(csock, out_buf, &slen, 0); |
| 267 |
} |
| 268 |
|
| 269 |
if (consumed < total) |
| 270 |
pj_memmove(buf, buf + consumed, total - consumed); |
| 271 |
total -= consumed; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
client_done: |
| 276 |
if (csock != PJ_INVALID_SOCKET) |
| 277 |
pj_sock_close(csock); |
| 278 |
} |
| 279 |
|
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
static pj_status_t start_echo_server(void) |
| 284 |
{ |
| 285 |
pj_sockaddr_in addr; |
| 286 |
pj_status_t status; |
| 287 |
int addr_len; |
| 288 |
|
| 289 |
g_server.quit = PJ_FALSE; |
| 290 |
|
| 291 |
status = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, |
| 292 |
&g_server.sock); |
| 293 |
if (status != PJ_SUCCESS) return status; |
| 294 |
|
| 295 |
pj_sockaddr_in_init(&addr, NULL, 0); |
| 296 |
status = pj_sock_bind(g_server.sock, &addr, sizeof(addr)); |
| 297 |
if (status != PJ_SUCCESS) { |
| 298 |
pj_sock_close(g_server.sock); |
| 299 |
return status; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
addr_len = sizeof(addr); |
| 304 |
pj_sock_getsockname(g_server.sock, &addr, &addr_len); |
| 305 |
g_server.port = pj_sockaddr_in_get_port(&addr); |
| 306 |
|
| 307 |
status = pj_sock_listen(g_server.sock, 1); |
| 308 |
if (status != PJ_SUCCESS) { |
| 309 |
pj_sock_close(g_server.sock); |
| 310 |
return status; |
| 311 |
} |
| 312 |
|
| 313 |
g_server.pool = pj_pool_create(mem, "srv", 512, 256, NULL); |
| 314 |
if (!g_server.pool) { |
| 315 |
pj_sock_close(g_server.sock); |
| 316 |
return PJ_ENOMEM; |
| 317 |
} |
| 318 |
|
| 319 |
status = pj_thread_create(g_server.pool, "ws_echo_srv", &server_thread, |
| 320 |
&g_server, 0, 0, &g_server.thread); |
| 321 |
if (status != PJ_SUCCESS) { |
| 322 |
pj_sock_close(g_server.sock); |
| 323 |
pj_pool_release(g_server.pool); |
| 324 |
g_server.pool = NULL; |
| 325 |
return status; |
| 326 |
} |
| 327 |
|
| 328 |
PJ_LOG(3, (THIS_FILE, "Echo server started on port %d", |
| 329 |
g_server.port)); |
| 330 |
return PJ_SUCCESS; |
| 331 |
} |
| 332 |
|
| 333 |
static void stop_echo_server(void) |
| 334 |
{ |
| 335 |
g_server.quit = PJ_TRUE; |
| 336 |
if (g_server.thread) { |
| 337 |
pj_thread_join(g_server.thread); |
| 338 |
pj_thread_destroy(g_server.thread); |
| 339 |
g_server.thread = NULL; |
| 340 |
} |
| 341 |
if (g_server.sock != PJ_INVALID_SOCKET) { |
| 342 |
pj_sock_close(g_server.sock); |
| 343 |
g_server.sock = PJ_INVALID_SOCKET; |
| 344 |
} |
| 345 |
if (g_server.pool) { |
| 346 |
pj_pool_release(g_server.pool); |
| 347 |
g_server.pool = NULL; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
#define MAX_RX_MSGS 16 |
| 355 |
#define MAX_MSG_SIZE 4096 |
| 356 |
|
| 357 |
typedef struct test_state_t |
| 358 |
{ |
| 359 |
pj_pool_t *pool; |
| 360 |
pj_ioqueue_t *ioq; |
| 361 |
pj_timer_heap_t *timer; |
| 362 |
pj_websock *ws; |
| 363 |
|
| 364 |
|
| 365 |
pj_status_t connect_status; |
| 366 |
pj_bool_t connected; |
| 367 |
pj_bool_t closed; |
| 368 |
pj_uint16_t close_code; |
| 369 |
|
| 370 |
unsigned rx_count; |
| 371 |
pj_websock_opcode rx_opcode[MAX_RX_MSGS]; |
| 372 |
pj_size_t rx_len[MAX_RX_MSGS]; |
| 373 |
pj_uint8_t rx_data[MAX_RX_MSGS][MAX_MSG_SIZE]; |
| 374 |
|
| 375 |
pj_bool_t done; |
| 376 |
} test_state_t; |
| 377 |
|
| 378 |
|
| 379 |
static void poll_events_ex(test_state_t *st, unsigned timeout_ms, |
| 380 |
pj_bool_t break_on_connect) |
| 381 |
{ |
| 382 |
pj_time_val end, now, poll_delay; |
| 383 |
|
| 384 |
pj_gettimeofday(&end); |
| 385 |
end.msec += timeout_ms; |
| 386 |
pj_time_val_normalize(&end); |
| 387 |
|
| 388 |
for (;;) { |
| 389 |
poll_delay.sec = 0; |
| 390 |
poll_delay.msec = 10; |
| 391 |
pj_timer_heap_poll(st->timer, NULL); |
| 392 |
pj_ioqueue_poll(st->ioq, &poll_delay); |
| 393 |
pj_gettimeofday(&now); |
| 394 |
if (PJ_TIME_VAL_GTE(now, end) || st->done) |
| 395 |
break; |
| 396 |
if (break_on_connect && st->connected) |
| 397 |
break; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
static void poll_events(test_state_t *st, unsigned timeout_ms) |
| 403 |
{ |
| 404 |
poll_events_ex(st, timeout_ms, PJ_FALSE); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static void on_connect(pj_websock *ws, pj_status_t status) |
| 410 |
{ |
| 411 |
test_state_t *st = (test_state_t*)pj_websock_get_user_data(ws); |
| 412 |
st->connect_status = status; |
| 413 |
st->connected = (status == PJ_SUCCESS); |
| 414 |
PJ_LOG(4, (THIS_FILE, "on_connect: status=%d", status)); |
| 415 |
} |
| 416 |
|
| 417 |
static void on_rx_msg(pj_websock *ws, pj_websock_opcode opcode, |
| 418 |
const void *data, pj_size_t len) |
| 419 |
{ |
| 420 |
test_state_t *st = (test_state_t*)pj_websock_get_user_data(ws); |
| 421 |
PJ_LOG(4, (THIS_FILE, "on_rx_msg: opcode=%d len=%lu", |
| 422 |
opcode, (unsigned long)len)); |
| 423 |
if (st->rx_count < MAX_RX_MSGS) { |
| 424 |
unsigned idx = st->rx_count; |
| 425 |
st->rx_opcode[idx] = opcode; |
| 426 |
st->rx_len[idx] = len; |
| 427 |
if (len > MAX_MSG_SIZE) len = MAX_MSG_SIZE; |
| 428 |
pj_memcpy(st->rx_data[idx], data, len); |
| 429 |
st->rx_count++; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
static void on_close(pj_websock *ws, pj_uint16_t status_code, |
| 434 |
const pj_str_t *reason) |
| 435 |
{ |
| 436 |
test_state_t *st = (test_state_t*)pj_websock_get_user_data(ws); |
| 437 |
PJ_UNUSED_ARG(reason); |
| 438 |
st->closed = PJ_TRUE; |
| 439 |
st->close_code = status_code; |
| 440 |
st->done = PJ_TRUE; |
| 441 |
PJ_LOG(4, (THIS_FILE, "on_close: code=%d", status_code)); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static int test_create_destroy(void) |
| 447 |
{ |
| 448 |
pj_pool_t *pool; |
| 449 |
pj_ioqueue_t *ioq; |
| 450 |
pj_timer_heap_t *timer; |
| 451 |
pj_websock *ws = NULL; |
| 452 |
pj_websock_param param; |
| 453 |
pj_status_t status; |
| 454 |
|
| 455 |
PJ_LOG(3, (THIS_FILE, " test_create_destroy")); |
| 456 |
|
| 457 |
pool = pj_pool_create(mem, "wscrt", 4096, 1024, NULL); |
| 458 |
PJ_TEST_NOT_NULL(pool, NULL, return -100); |
| 459 |
|
| 460 |
PJ_TEST_SUCCESS(pj_ioqueue_create(pool, 4, &ioq), NULL, { |
| 461 |
pj_pool_release(pool); return -110; |
| 462 |
}); |
| 463 |
PJ_TEST_SUCCESS(pj_timer_heap_create(pool, 4, &timer), NULL, { |
| 464 |
pj_ioqueue_destroy(ioq); pj_pool_release(pool); return -120; |
| 465 |
}); |
| 466 |
|
| 467 |
pj_websock_param_default(¶m); |
| 468 |
PJ_TEST_EQ(param.ping_interval, PJ_WEBSOCK_DEFAULT_PING_INTERVAL, |
| 469 |
NULL, return -130); |
| 470 |
|
| 471 |
param.ioqueue = ioq; |
| 472 |
param.timer_heap = timer; |
| 473 |
|
| 474 |
status = pj_websock_create(pool, ¶m, &ws); |
| 475 |
PJ_TEST_SUCCESS(status, NULL, { |
| 476 |
pj_ioqueue_destroy(ioq); pj_pool_release(pool); return -140; |
| 477 |
}); |
| 478 |
PJ_TEST_NOT_NULL(ws, NULL, return -150); |
| 479 |
PJ_TEST_EQ(pj_websock_get_state(ws), PJ_WEBSOCK_STATE_CLOSED, |
| 480 |
NULL, return -160); |
| 481 |
|
| 482 |
pj_websock_destroy(ws); |
| 483 |
pj_ioqueue_destroy(ioq); |
| 484 |
pj_timer_heap_destroy(timer); |
| 485 |
pj_pool_release(pool); |
| 486 |
|
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
static pj_status_t setup_test_state(test_state_t *st) |
| 493 |
{ |
| 494 |
pj_websock_param param; |
| 495 |
pj_status_t status; |
| 496 |
|
| 497 |
pj_bzero(st, sizeof(*st)); |
| 498 |
st->pool = pj_pool_create(mem, "wstest", 8192, 4096, NULL); |
| 499 |
if (!st->pool) return PJ_ENOMEM; |
| 500 |
|
| 501 |
status = pj_ioqueue_create(st->pool, 8, &st->ioq); |
| 502 |
if (status != PJ_SUCCESS) return status; |
| 503 |
|
| 504 |
status = pj_timer_heap_create(st->pool, 8, &st->timer); |
| 505 |
if (status != PJ_SUCCESS) return status; |
| 506 |
|
| 507 |
pj_websock_param_default(¶m); |
| 508 |
param.ioqueue = st->ioq; |
| 509 |
param.timer_heap = st->timer; |
| 510 |
param.ping_interval = 0; |
| 511 |
param.cb.on_connect = &on_connect; |
| 512 |
param.cb.on_rx_msg = &on_rx_msg; |
| 513 |
param.cb.on_close = &on_close; |
| 514 |
param.user_data = st; |
| 515 |
|
| 516 |
return pj_websock_create(st->pool, ¶m, &st->ws); |
| 517 |
} |
| 518 |
|
| 519 |
static void teardown_test_state(test_state_t *st) |
| 520 |
{ |
| 521 |
if (st->ws) |
| 522 |
pj_websock_destroy(st->ws); |
| 523 |
if (st->ioq) |
| 524 |
pj_ioqueue_destroy(st->ioq); |
| 525 |
if (st->timer) |
| 526 |
pj_timer_heap_destroy(st->timer); |
| 527 |
if (st->pool) |
| 528 |
pj_pool_release(st->pool); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
static int test_text_echo(void) |
| 534 |
{ |
| 535 |
test_state_t st; |
| 536 |
pj_str_t url; |
| 537 |
char url_buf[64]; |
| 538 |
const char *json_msg = "{\"type\":\"session.create\"," |
| 539 |
"\"model\":\"gpt-4o-realtime\"}"; |
| 540 |
pj_status_t status; |
| 541 |
|
| 542 |
PJ_LOG(3, (THIS_FILE, " test_text_echo (JSON control message)")); |
| 543 |
|
| 544 |
PJ_TEST_SUCCESS(setup_test_state(&st), NULL, return -200); |
| 545 |
|
| 546 |
pj_ansi_snprintf(url_buf, sizeof(url_buf), |
| 547 |
"ws://127.0.0.1:%d/v1/realtime", g_server.port); |
| 548 |
pj_cstr(&url, url_buf); |
| 549 |
|
| 550 |
status = pj_websock_connect(st.ws, &url, NULL); |
| 551 |
PJ_TEST_TRUE(status == PJ_SUCCESS || status == PJ_EPENDING, NULL, { |
| 552 |
teardown_test_state(&st); return -210; |
| 553 |
}); |
| 554 |
|
| 555 |
|
| 556 |
poll_events_ex(&st, 2000, PJ_TRUE); |
| 557 |
PJ_TEST_TRUE(st.connected, "WebSocket connect failed", { |
| 558 |
teardown_test_state(&st); return -220; |
| 559 |
}); |
| 560 |
|
| 561 |
|
| 562 |
status = pj_websock_send(st.ws, PJ_WEBSOCK_OP_TEXT, |
| 563 |
json_msg, pj_ansi_strlen(json_msg)); |
| 564 |
PJ_TEST_SUCCESS(status, NULL, { |
| 565 |
teardown_test_state(&st); return -230; |
| 566 |
}); |
| 567 |
|
| 568 |
|
| 569 |
poll_events(&st, 1000); |
| 570 |
|
| 571 |
PJ_TEST_GTE(st.rx_count, 1U, "No message received", { |
| 572 |
teardown_test_state(&st); return -240; |
| 573 |
}); |
| 574 |
PJ_TEST_EQ(st.rx_opcode[0], PJ_WEBSOCK_OP_TEXT, |
| 575 |
"Expected text frame", { |
| 576 |
teardown_test_state(&st); return -250; |
| 577 |
}); |
| 578 |
PJ_TEST_EQ(st.rx_len[0], pj_ansi_strlen(json_msg), |
| 579 |
"Echo length mismatch", { |
| 580 |
teardown_test_state(&st); return -260; |
| 581 |
}); |
| 582 |
PJ_TEST_EQ(pj_memcmp(st.rx_data[0], json_msg, st.rx_len[0]), 0, |
| 583 |
"Echo content mismatch", { |
| 584 |
teardown_test_state(&st); return -270; |
| 585 |
}); |
| 586 |
|
| 587 |
|
| 588 |
pj_websock_close(st.ws, 1000, NULL); |
| 589 |
poll_events(&st, 1000); |
| 590 |
|
| 591 |
teardown_test_state(&st); |
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
static int test_binary_echo(void) |
| 598 |
{ |
| 599 |
test_state_t st; |
| 600 |
pj_str_t url; |
| 601 |
char url_buf[64]; |
| 602 |
pj_uint8_t pcm_buf[960]; |
| 603 |
unsigned i; |
| 604 |
pj_status_t status; |
| 605 |
|
| 606 |
PJ_LOG(3, (THIS_FILE, " test_binary_echo (PCM audio frame)")); |
| 607 |
|
| 608 |
PJ_TEST_SUCCESS(setup_test_state(&st), NULL, return -300); |
| 609 |
|
| 610 |
pj_ansi_snprintf(url_buf, sizeof(url_buf), |
| 611 |
"ws://127.0.0.1:%d/audio", g_server.port); |
| 612 |
pj_cstr(&url, url_buf); |
| 613 |
|
| 614 |
status = pj_websock_connect(st.ws, &url, NULL); |
| 615 |
PJ_TEST_TRUE(status == PJ_SUCCESS || status == PJ_EPENDING, NULL, { |
| 616 |
teardown_test_state(&st); return -310; |
| 617 |
}); |
| 618 |
|
| 619 |
poll_events_ex(&st, 2000, PJ_TRUE); |
| 620 |
PJ_TEST_TRUE(st.connected, "WebSocket connect failed", { |
| 621 |
teardown_test_state(&st); return -320; |
| 622 |
}); |
| 623 |
|
| 624 |
|
| 625 |
for (i = 0; i < sizeof(pcm_buf); ++i) { |
| 626 |
pcm_buf[i] = (pj_uint8_t)(i & 0xFF); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
status = pj_websock_send(st.ws, PJ_WEBSOCK_OP_BIN, |
| 631 |
pcm_buf, sizeof(pcm_buf)); |
| 632 |
PJ_TEST_SUCCESS(status, NULL, { |
| 633 |
teardown_test_state(&st); return -330; |
| 634 |
}); |
| 635 |
|
| 636 |
poll_events(&st, 1000); |
| 637 |
|
| 638 |
PJ_TEST_GTE(st.rx_count, 1U, "No audio echo received", { |
| 639 |
teardown_test_state(&st); return -340; |
| 640 |
}); |
| 641 |
PJ_TEST_EQ(st.rx_opcode[0], PJ_WEBSOCK_OP_BIN, |
| 642 |
"Expected binary frame", { |
| 643 |
teardown_test_state(&st); return -350; |
| 644 |
}); |
| 645 |
PJ_TEST_EQ(st.rx_len[0], sizeof(pcm_buf), |
| 646 |
"Audio echo length mismatch", { |
| 647 |
teardown_test_state(&st); return -360; |
| 648 |
}); |
| 649 |
PJ_TEST_EQ(pj_memcmp(st.rx_data[0], pcm_buf, sizeof(pcm_buf)), 0, |
| 650 |
"Audio echo content mismatch", { |
| 651 |
teardown_test_state(&st); return -370; |
| 652 |
}); |
| 653 |
|
| 654 |
pj_websock_close(st.ws, 1000, NULL); |
| 655 |
poll_events(&st, 1000); |
| 656 |
|
| 657 |
teardown_test_state(&st); |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
static int test_streaming(void) |
| 664 |
{ |
| 665 |
test_state_t st; |
| 666 |
pj_str_t url; |
| 667 |
char url_buf[64]; |
| 668 |
pj_uint8_t pcm_buf[480]; |
| 669 |
unsigned i; |
| 670 |
unsigned num_frames = 5; |
| 671 |
pj_status_t status; |
| 672 |
|
| 673 |
PJ_LOG(3, (THIS_FILE, " test_streaming (%d audio frames)", num_frames)); |
| 674 |
|
| 675 |
PJ_TEST_SUCCESS(setup_test_state(&st), NULL, return -400); |
| 676 |
|
| 677 |
pj_ansi_snprintf(url_buf, sizeof(url_buf), |
| 678 |
"ws://127.0.0.1:%d/stream", g_server.port); |
| 679 |
pj_cstr(&url, url_buf); |
| 680 |
|
| 681 |
status = pj_websock_connect(st.ws, &url, NULL); |
| 682 |
PJ_TEST_TRUE(status == PJ_SUCCESS || status == PJ_EPENDING, NULL, { |
| 683 |
teardown_test_state(&st); return -410; |
| 684 |
}); |
| 685 |
|
| 686 |
poll_events_ex(&st, 2000, PJ_TRUE); |
| 687 |
PJ_TEST_TRUE(st.connected, NULL, { |
| 688 |
teardown_test_state(&st); return -420; |
| 689 |
}); |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
for (i = 0; i < num_frames; ++i) { |
| 694 |
pj_memset(pcm_buf, (pj_uint8_t)i, sizeof(pcm_buf)); |
| 695 |
status = pj_websock_send(st.ws, PJ_WEBSOCK_OP_BIN, |
| 696 |
pcm_buf, sizeof(pcm_buf)); |
| 697 |
PJ_TEST_SUCCESS(status, "Send failed mid-stream", { |
| 698 |
teardown_test_state(&st); return -430; |
| 699 |
}); |
| 700 |
poll_events(&st, 20); |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
poll_events(&st, 3000); |
| 705 |
|
| 706 |
PJ_TEST_EQ(st.rx_count, num_frames, "Not all frames echoed", { |
| 707 |
teardown_test_state(&st); return -440; |
| 708 |
}); |
| 709 |
|
| 710 |
|
| 711 |
for (i = 0; i < num_frames; ++i) { |
| 712 |
PJ_TEST_EQ(st.rx_opcode[i], PJ_WEBSOCK_OP_BIN, NULL, { |
| 713 |
teardown_test_state(&st); return -450; |
| 714 |
}); |
| 715 |
PJ_TEST_EQ(st.rx_len[i], sizeof(pcm_buf), NULL, { |
| 716 |
teardown_test_state(&st); return -460; |
| 717 |
}); |
| 718 |
|
| 719 |
PJ_TEST_EQ(st.rx_data[i][0], (pj_uint8_t)i, NULL, { |
| 720 |
teardown_test_state(&st); return -470; |
| 721 |
}); |
| 722 |
} |
| 723 |
|
| 724 |
pj_websock_close(st.ws, 1000, NULL); |
| 725 |
poll_events(&st, 1000); |
| 726 |
|
| 727 |
teardown_test_state(&st); |
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
static int test_close(void) |
| 734 |
{ |
| 735 |
test_state_t st; |
| 736 |
pj_str_t url; |
| 737 |
char url_buf[64]; |
| 738 |
pj_status_t status; |
| 739 |
|
| 740 |
PJ_LOG(3, (THIS_FILE, " test_close (graceful close handshake)")); |
| 741 |
|
| 742 |
PJ_TEST_SUCCESS(setup_test_state(&st), NULL, return -500); |
| 743 |
|
| 744 |
pj_ansi_snprintf(url_buf, sizeof(url_buf), |
| 745 |
"ws://127.0.0.1:%d/close", g_server.port); |
| 746 |
pj_cstr(&url, url_buf); |
| 747 |
|
| 748 |
status = pj_websock_connect(st.ws, &url, NULL); |
| 749 |
PJ_TEST_TRUE(status == PJ_SUCCESS || status == PJ_EPENDING, NULL, { |
| 750 |
teardown_test_state(&st); return -510; |
| 751 |
}); |
| 752 |
|
| 753 |
poll_events_ex(&st, 2000, PJ_TRUE); |
| 754 |
PJ_TEST_TRUE(st.connected, NULL, { |
| 755 |
teardown_test_state(&st); return -520; |
| 756 |
}); |
| 757 |
|
| 758 |
|
| 759 |
status = pj_websock_close(st.ws, 1000, NULL); |
| 760 |
PJ_TEST_SUCCESS(status, NULL, { |
| 761 |
teardown_test_state(&st); return -530; |
| 762 |
}); |
| 763 |
|
| 764 |
poll_events(&st, 2000); |
| 765 |
|
| 766 |
PJ_TEST_TRUE(st.closed, "Close callback not received", { |
| 767 |
teardown_test_state(&st); return -540; |
| 768 |
}); |
| 769 |
PJ_TEST_EQ(st.close_code, 1000, "Expected close code 1000", { |
| 770 |
teardown_test_state(&st); return -550; |
| 771 |
}); |
| 772 |
PJ_TEST_EQ(pj_websock_get_state(st.ws), PJ_WEBSOCK_STATE_CLOSED, |
| 773 |
NULL, { |
| 774 |
teardown_test_state(&st); return -560; |
| 775 |
}); |
| 776 |
|
| 777 |
teardown_test_state(&st); |
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
#if INCLUDE_WEBSOCK_ONLINE_TEST |
| 785 |
|
| 786 |
#define PUBLIC_WSS_URL "wss://echo.websocket.org" |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
static int test_public_wss_echo(void) |
| 795 |
{ |
| 796 |
#if PJ_HAS_SSL_SOCK |
| 797 |
test_state_t st; |
| 798 |
pj_str_t url; |
| 799 |
const char *json_msg = "{\"type\":\"test\",\"msg\":\"hello\"}"; |
| 800 |
pj_uint8_t bin_buf[320]; |
| 801 |
unsigned i, base_rx; |
| 802 |
pj_status_t status; |
| 803 |
|
| 804 |
PJ_LOG(3, (THIS_FILE, " test_public_wss_echo (%s)", PUBLIC_WSS_URL)); |
| 805 |
|
| 806 |
PJ_TEST_SUCCESS(setup_test_state(&st), NULL, return -800); |
| 807 |
|
| 808 |
pj_cstr(&url, PUBLIC_WSS_URL); |
| 809 |
status = pj_websock_connect(st.ws, &url, NULL); |
| 810 |
PJ_TEST_TRUE(status == PJ_SUCCESS || status == PJ_EPENDING, NULL, { |
| 811 |
teardown_test_state(&st); return -810; |
| 812 |
}); |
| 813 |
|
| 814 |
|
| 815 |
poll_events_ex(&st, 15000, PJ_TRUE); |
| 816 |
PJ_TEST_TRUE(st.connected, "Public wss:// connect failed", { |
| 817 |
teardown_test_state(&st); return -820; |
| 818 |
}); |
| 819 |
|
| 820 |
|
| 821 |
poll_events(&st, 2000); |
| 822 |
base_rx = st.rx_count; |
| 823 |
|
| 824 |
|
| 825 |
status = pj_websock_send(st.ws, PJ_WEBSOCK_OP_TEXT, |
| 826 |
json_msg, pj_ansi_strlen(json_msg)); |
| 827 |
PJ_TEST_SUCCESS(status, NULL, { |
| 828 |
teardown_test_state(&st); return -830; |
| 829 |
}); |
| 830 |
|
| 831 |
poll_events(&st, 5000); |
| 832 |
PJ_TEST_GTE(st.rx_count, base_rx + 1, |
| 833 |
"No text echo from wss:// server", { |
| 834 |
teardown_test_state(&st); return -840; |
| 835 |
}); |
| 836 |
PJ_TEST_EQ(st.rx_opcode[base_rx], PJ_WEBSOCK_OP_TEXT, NULL, { |
| 837 |
teardown_test_state(&st); return -850; |
| 838 |
}); |
| 839 |
PJ_TEST_EQ(st.rx_len[base_rx], pj_ansi_strlen(json_msg), |
| 840 |
"WSS text echo length mismatch", { |
| 841 |
teardown_test_state(&st); return -860; |
| 842 |
}); |
| 843 |
PJ_TEST_EQ(pj_memcmp(st.rx_data[base_rx], json_msg, |
| 844 |
st.rx_len[base_rx]), 0, |
| 845 |
"WSS text echo content mismatch", { |
| 846 |
teardown_test_state(&st); return -870; |
| 847 |
}); |
| 848 |
|
| 849 |
|
| 850 |
for (i = 0; i < sizeof(bin_buf); ++i) |
| 851 |
bin_buf[i] = (pj_uint8_t)(i & 0xFF); |
| 852 |
|
| 853 |
status = pj_websock_send(st.ws, PJ_WEBSOCK_OP_BIN, |
| 854 |
bin_buf, sizeof(bin_buf)); |
| 855 |
PJ_TEST_SUCCESS(status, NULL, { |
| 856 |
teardown_test_state(&st); return -875; |
| 857 |
}); |
| 858 |
|
| 859 |
poll_events(&st, 5000); |
| 860 |
PJ_TEST_GTE(st.rx_count, base_rx + 2, |
| 861 |
"No binary echo from wss:// server", { |
| 862 |
teardown_test_state(&st); return -876; |
| 863 |
}); |
| 864 |
PJ_TEST_EQ(st.rx_opcode[base_rx + 1], PJ_WEBSOCK_OP_BIN, NULL, { |
| 865 |
teardown_test_state(&st); return -877; |
| 866 |
}); |
| 867 |
PJ_TEST_EQ(st.rx_len[base_rx + 1], sizeof(bin_buf), |
| 868 |
"WSS binary echo length mismatch", { |
| 869 |
teardown_test_state(&st); return -878; |
| 870 |
}); |
| 871 |
PJ_TEST_EQ(pj_memcmp(st.rx_data[base_rx + 1], bin_buf, |
| 872 |
sizeof(bin_buf)), 0, |
| 873 |
"WSS binary echo content mismatch", { |
| 874 |
teardown_test_state(&st); return -879; |
| 875 |
}); |
| 876 |
|
| 877 |
|
| 878 |
pj_websock_close(st.ws, 1000, NULL); |
| 879 |
poll_events(&st, 5000); |
| 880 |
|
| 881 |
PJ_TEST_TRUE(st.closed, "WSS close not received", { |
| 882 |
teardown_test_state(&st); return -880; |
| 883 |
}); |
| 884 |
|
| 885 |
teardown_test_state(&st); |
| 886 |
return 0; |
| 887 |
|
| 888 |
#else |
| 889 |
PJ_LOG(3, (THIS_FILE, " test_public_wss_echo SKIPPED " |
| 890 |
"(SSL not available)")); |
| 891 |
return 0; |
| 892 |
#endif |
| 893 |
} |
| 894 |
|
| 895 |
#endif |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
int websock_test(void) |
| 900 |
{ |
| 901 |
int rc; |
| 902 |
|
| 903 |
PJ_LOG(3, (THIS_FILE, "WebSocket test (real-time speech AI)")); |
| 904 |
|
| 905 |
PJ_TEST_SUCCESS(start_echo_server(), "Failed to start echo server", |
| 906 |
return -10); |
| 907 |
|
| 908 |
rc = test_create_destroy(); |
| 909 |
if (rc) goto on_return; |
| 910 |
|
| 911 |
rc = test_text_echo(); |
| 912 |
if (rc) goto on_return; |
| 913 |
|
| 914 |
rc = test_binary_echo(); |
| 915 |
if (rc) goto on_return; |
| 916 |
|
| 917 |
rc = test_streaming(); |
| 918 |
if (rc) goto on_return; |
| 919 |
|
| 920 |
rc = test_close(); |
| 921 |
if (rc) goto on_return; |
| 922 |
|
| 923 |
on_return: |
| 924 |
stop_echo_server(); |
| 925 |
|
| 926 |
#if INCLUDE_WEBSOCK_ONLINE_TEST |
| 927 |
if (rc == 0) { |
| 928 |
PJ_LOG(3, (THIS_FILE, "Online WebSocket tests")); |
| 929 |
rc = test_public_wss_echo(); |
| 930 |
} |
| 931 |
#endif |
| 932 |
|
| 933 |
return rc; |
| 934 |
} |
| 935 |
|
| 936 |
#endif |