From 90b855e1aa23886dbb4bd794e2527a26973dd5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 31 Mar 2024 09:49:10 +0200 Subject: relay: add connection to remote (issue #2066) Connection to remote: - handshake: offer support for all supported hash algorithms - network connect with a socket - upgrade to websocket and authenticate with remote (password/TOTP) - check websocket response - get list of buffers (not used yet) Note: connection to remote with TLS or a proxy is not yet supported. --- tests/CMakeLists.txt | 1 + .../relay/api/remote/test-relay-remote-network.cpp | 241 +++++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 tests/unit/plugins/relay/api/remote/test-relay-remote-network.cpp (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4667b7a32..41a338574 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -143,6 +143,7 @@ if (ENABLE_RELAY) unit/plugins/relay/api/test-relay-api.cpp unit/plugins/relay/api/test-relay-api-msg.cpp unit/plugins/relay/api/test-relay-api-protocol.cpp + unit/plugins/relay/api/remote/test-relay-remote-network.cpp ) endif() endif() diff --git a/tests/unit/plugins/relay/api/remote/test-relay-remote-network.cpp b/tests/unit/plugins/relay/api/remote/test-relay-remote-network.cpp new file mode 100644 index 000000000..0883b62c6 --- /dev/null +++ b/tests/unit/plugins/relay/api/remote/test-relay-remote-network.cpp @@ -0,0 +1,241 @@ +/* + * test-relay-remote-network.cpp - test network functions for relay remote + * + * Copyright (C) 2024 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#include "CppUTest/TestHarness.h" + +#include "tests/tests.h" + +extern "C" +{ +#include +#include "src/core/core-config-file.h" +#include "src/plugins/relay/relay.h" +#include "src/plugins/relay/relay-auth.h" +#include "src/plugins/relay/relay-config.h" +#include "src/plugins/relay/relay-remote.h" +#include "src/plugins/relay/api/remote/relay-remote-network.h" + +extern char *relay_remote_network_get_url_resource (struct t_relay_remote *remote, + const char *resource); +extern char *relay_remote_network_get_handshake_request (); +} + +struct t_relay_remote *ptr_relay_remote = NULL; +struct t_relay_remote *ptr_relay_remote2 = NULL; + +TEST_GROUP(RelayRemoteNetwork) +{ +}; + +TEST_GROUP(RelayRemoteNetworkWithRemote) +{ + void setup () + { + /* disable auto-open of relay buffer */ + config_file_option_set (relay_config_look_auto_open_buffer, "off", 1); + + /* create two relay remotes */ + ptr_relay_remote = relay_remote_new ("remote", "http://localhost:9000", + NULL, "secret", "secretbase32"); + ptr_relay_remote2 = relay_remote_new ("remote2", "https://localhost:9001/", + "my_proxy", "secret", "secretbase32"); + } + + void teardown () + { + relay_remote_free (ptr_relay_remote); + ptr_relay_remote = NULL; + + /* restore auto-open of relay buffer */ + config_file_option_reset (relay_config_look_auto_open_buffer, 1); + } +}; + +/* + * Tests functions: + * relay_remote_network_get_url_resource + */ + +TEST(RelayRemoteNetworkWithRemote, GetUrlResource) +{ + char *str; + + WEE_TEST_STR(NULL, relay_remote_network_get_url_resource (NULL, NULL)); + WEE_TEST_STR(NULL, relay_remote_network_get_url_resource (NULL, "")); + WEE_TEST_STR(NULL, relay_remote_network_get_url_resource (NULL, "/api/buffers")); + + WEE_TEST_STR( + "http://localhost:9000/api/buffers", + relay_remote_network_get_url_resource (ptr_relay_remote, "buffers")); + WEE_TEST_STR( + "https://localhost:9001/api/buffers", + relay_remote_network_get_url_resource (ptr_relay_remote2, "buffers")); +} + +/* + * Tests functions: + * relay_remote_network_close_connection + */ + +TEST(RelayRemoteNetwork, CloseConnection) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_disconnect + */ + +TEST(RelayRemoteNetwork, Disconnect) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_check_auth + */ + +TEST(RelayRemoteNetwork, CheckAuth) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_send_data + */ + +TEST(RelayRemoteNetwork, SendData) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_send + */ + +TEST(RelayRemoteNetwork, Send) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_recv_text + */ + +TEST(RelayRemoteNetwork, RecvText) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_read_websocket_frames + */ + +TEST(RelayRemoteNetwork, ReadWebsocketFrames) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_recv_buffer + */ + +TEST(RelayRemoteNetwork, RecvBuffer) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_recv_cb + */ + +TEST(RelayRemoteNetwork, RecvCb) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_connect_ws_auth + */ + +TEST(RelayRemoteNetwork, ConnectWsAuth) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_connect_cb + */ + +TEST(RelayRemoteNetwork, ConnectCb) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_url_handshake_cb + */ + +TEST(RelayRemoteNetwork, UrlHandshakeCb) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * relay_remote_network_get_handshake_request + */ + +TEST(RelayRemoteNetwork, GetHandshakeRequest) +{ + const char *str_start = "{\"password_hash_algo\": [\""; + char *str; + int i; + + str = relay_remote_network_get_handshake_request (); + CHECK(str); + STRNCMP_EQUAL(str_start, str, strlen (str_start)); + for (i = 0; i < RELAY_NUM_PASSWORD_HASH_ALGOS; i++) + { + CHECK(strstr (str, relay_auth_password_hash_algo_name[i])); + } +} + +/* + * Tests functions: + * relay_remote_network_connect + */ + +TEST(RelayRemoteNetwork, Connect) +{ + /* TODO: write tests */ +} -- cgit v1.2.3