Add test for parse_url.

This commit is contained in:
levlam 2023-01-30 15:29:01 +03:00
parent e2d079860d
commit 9555bfc16c
1 changed files with 88 additions and 0 deletions

View File

@ -6,10 +6,98 @@
//
#include "td/utils/common.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.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;
}
char 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));