From a414fb9da549835a3f2feed862f8e3eca811e2a9 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Sat, 16 Nov 2024 19:27:11 +0100 Subject: [PATCH] tests: add tests for auth via Sec-WebSocket-Protocol --- tests/unit/plugins/relay/test-relay-http.cpp | 21 +++++++++++++++++++ .../plugins/relay/test-relay-websocket.cpp | 13 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/tests/unit/plugins/relay/test-relay-http.cpp b/tests/unit/plugins/relay/test-relay-http.cpp index 9b3558883..72f816b92 100644 --- a/tests/unit/plugins/relay/test-relay-http.cpp +++ b/tests/unit/plugins/relay/test-relay-http.cpp @@ -867,6 +867,27 @@ TEST(RelayHttp, GetAuthStatus) config_file_option_reset (relay_config_network_totp_secret, 1); config_file_option_reset (relay_config_network_totp_window, 1); + /* test invalid plain-text password ("test") via Sec-WebSocket-Protocol */ + hashtable_remove (client->http_req->headers, "authorization"); + hashtable_set (client->http_req->headers, "sec-websocket-protocol", + WEBSOCKET_SUB_PROTOCOL_API_WEECHAT + ", base64url.bearer.authorization.weechat.cGxhaW46dGVzdA"); + LONGS_EQUAL(-2, relay_http_get_auth_status (client)); + + /* test valid plain-text password ("secret_password") via Sec-WebSocket-Protocol */ + hashtable_set (client->http_req->headers, "sec-websocket-protocol", + WEBSOCKET_SUB_PROTOCOL_API_WEECHAT + ", base64url.bearer.authorization.weechat.cGxhaW46c2VjcmV0X3Bhc3N3b3Jk"); + LONGS_EQUAL(0, relay_http_get_auth_status (client)); + + /* test auth via Sec-WebSocket-Protocol with base64url specific characters */ + config_file_option_set (relay_config_network_password, "..>..?.", 1); + hashtable_set (client->http_req->headers, "sec-websocket-protocol", + WEBSOCKET_SUB_PROTOCOL_API_WEECHAT + ", base64url.bearer.authorization.weechat.cGxhaW46Li4-Li4_Lg"); + LONGS_EQUAL(0, relay_http_get_auth_status (client)); + config_file_option_set (relay_config_network_password, good_pwd, 1); + config_file_option_reset (relay_config_network_password, 1); relay_http_request_free (client->http_req); diff --git a/tests/unit/plugins/relay/test-relay-websocket.cpp b/tests/unit/plugins/relay/test-relay-websocket.cpp index b4e129789..4a6237de3 100644 --- a/tests/unit/plugins/relay/test-relay-websocket.cpp +++ b/tests/unit/plugins/relay/test-relay-websocket.cpp @@ -248,6 +248,19 @@ TEST(RelayWebsocket, ClientHandshakeValid) "\r\n", relay_websocket_build_handshake (request)); + relay_websocket_deflate_reinit (request->ws_deflate); + hashtable_set (request->headers, "sec-websocket-protocol", + WEBSOCKET_SUB_PROTOCOL_API_WEECHAT + ", base64url.bearer.authorization.weechat.cGxhaW46c2VjcmV0X3Bhc3N3b3Jk"); + WEE_TEST_STR( + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: fhLJYtv//ugX2vQXpifQgByRZ5Y=\r\n" + "Sec-WebSocket-Protocol: " WEBSOCKET_SUB_PROTOCOL_API_WEECHAT "\r\n" + "\r\n", + relay_websocket_build_handshake (request)); + relay_http_request_free (request); }