diff options
author | George Hazan <george.hazan@gmail.com> | 2023-06-04 19:24:05 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2023-06-04 19:24:05 +0300 |
commit | efc336e60cf1331bf5f3213d296981b87b8b2a6c (patch) | |
tree | ea59ea1a324f45f6e8a06cc0887b376bfba90ca9 /protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp | |
parent | 6e83622d2af1cec3c759f4cff6efe4df2fe3328c (diff) |
fixes #3537 (Telegram: 32-разрядная версия падает в 64-разрядной Windows) + update to the fresh TDLIB
Diffstat (limited to 'protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp')
-rw-r--r-- | protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp b/protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp index 6e91d48803..6d97c08770 100644 --- a/protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp +++ b/protocols/Telegram/tdlib/td/tdutils/test/HttpUrl.cpp @@ -1,15 +1,104 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/utils/common.h" #include "td/utils/HttpUrl.h" +#include "td/utils/misc.h" +#include "td/utils/Slice.h" +#include "td/utils/SliceBuilder.h" +#include "td/utils/StringBuilder.h" #include "td/utils/tests.h" #include <utility> +static void test_parse_url(const td::string &url, td::string userinfo, td::string host, bool is_ipv6, + int specified_port, int port) { + for (auto query : {"", "/.com", "#", "?t=1"}) { + auto http_url = td::parse_url(url + query).move_as_ok(); + ASSERT_EQ(userinfo, http_url.userinfo_); + ASSERT_EQ(host, http_url.host_); + ASSERT_EQ(is_ipv6, http_url.is_ipv6_); + ASSERT_EQ(specified_port, http_url.specified_port_); + ASSERT_EQ(port, http_url.port_); + } +} + +static void test_parse_url(const td::string &url, td::Slice error_message) { + for (auto query : {"", "/.com", "#", "?t=1"}) { + auto error = td::parse_url(url + query).move_as_error(); + ASSERT_EQ(error_message, error.message()); + } +} + +TEST(HttpUrl, parse_url) { + test_parse_url("http://localhost:8080", "", "localhost", false, 8080, 8080); + test_parse_url("http://lOcAlhOsT:8080", "", "localhost", false, 8080, 8080); + test_parse_url("http://UsEr:PaSs@lOcAlhOsT:8080", "UsEr:PaSs", "localhost", false, 8080, 8080); + test_parse_url("http://example.com", "", "example.com", false, 0, 80); + test_parse_url("https://example.com", "", "example.com", false, 0, 443); + test_parse_url("https://example.com:65535", "", "example.com", false, 65535, 65535); + test_parse_url("https://example.com:00000071", "", "example.com", false, 71, 71); + test_parse_url("example.com?://", "", "example.com", false, 0, 80); + test_parse_url("example.com/://", "", "example.com", false, 0, 80); + test_parse_url("example.com#://", "", "example.com", false, 0, 80); + test_parse_url("@example.com#://", "", "example.com", false, 0, 80); + test_parse_url("test@example.com#://", "test", "example.com", false, 0, 80); + test_parse_url("test:pass@example.com#://", "test:pass", "example.com", false, 0, 80); + test_parse_url("te%ffst:pa%8Dss@examp%9Ele.com#://", "te%ffst:pa%8Dss", "examp%9ele.com", false, 0, 80); + test_parse_url("http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]", "", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]", true, 0, + 80); + test_parse_url("https://test@[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/", "test", + "[2001:db8:85a3:8d3:1319:8a2e:370:7348]", true, 443, 443); + test_parse_url("http://[64:ff9b::255.255.255.255]", "", "[64:ff9b::255.255.255.255]", true, 0, 80); + test_parse_url("http://255.255.255.255", "", "255.255.255.255", false, 0, 80); + test_parse_url("http://255.255.255.com", "", "255.255.255.com", false, 0, 80); + test_parse_url("https://exam%00ple.com", "", "exam%00ple.com", false, 0, 443); + + test_parse_url("example.com://", "Unsupported URL protocol"); + test_parse_url("https://example.com:65536", "Wrong port number specified in the URL"); + test_parse_url("https://example.com:0", "Wrong port number specified in the URL"); + test_parse_url("https://example.com:0x1", "Wrong port number specified in the URL"); + test_parse_url("https://example.com:", "Wrong port number specified in the URL"); + test_parse_url("https://example.com:-1", "Wrong port number specified in the URL"); + test_parse_url("example.com@://", "Wrong port number specified in the URL"); + test_parse_url("example.com@:1//", "URL host is empty"); + test_parse_url("example.com@.:1//", "Host is invalid"); + test_parse_url("exam%0gple.com", "Wrong percent-encoded symbol in URL host"); + test_parse_url("a%g0b@example.com", "Wrong percent-encoded symbol in URL userinfo"); + + for (int c = 1; c <= 255; c++) { + if (c == '%') { + continue; + } + auto ch = static_cast<char>(c); + if (td::is_alnum(ch) || c >= 128 || td::string(".-_!$,~*\'();&+=").find(ch) != td::string::npos) { + // allowed character + test_parse_url(PSTRING() << ch << "a@b" << ch, td::string(1, ch) + "a", "b" + td::string(1, td::to_lower(ch)), + false, 0, 80); + } else if (c == ':') { + // allowed in userinfo character + test_parse_url(PSTRING() << ch << "a@b" << ch << 1, td::string(1, ch) + "a", "b", false, 1, 1); + test_parse_url(PSTRING() << ch << "a@b" << ch, "Wrong port number specified in the URL"); + test_parse_url(PSTRING() << ch << "a@b", td::string(1, ch) + "a", "b", false, 0, 80); + } else if (c == '#' || c == '?' || c == '/') { + // special disallowed character + test_parse_url(PSTRING() << ch << "a@b" << ch, "URL host is empty"); + } else if (c == '@') { + // special disallowed character + test_parse_url(PSTRING() << ch << "a@b" << ch, "URL host is empty"); + test_parse_url(PSTRING() << ch << "a@b" << ch << '1', "Disallowed character in URL userinfo"); + } else { + // generic disallowed character + test_parse_url(PSTRING() << ch << "a@b" << ch, "Disallowed character in URL host"); + test_parse_url(PSTRING() << "a@b" << ch, "Disallowed character in URL host"); + test_parse_url(PSTRING() << ch << "a@b", "Disallowed character in URL userinfo"); + } + } +} + static void test_get_url_query_file_name(const char *prefix, const char *suffix, const char *file_name) { auto path = td::string(prefix) + td::string(file_name) + td::string(suffix); ASSERT_STREQ(file_name, td::get_url_query_file_name(path)); |