ladybird/Tests/LibWebView/TestWebViewURL.cpp
Timothy Flynn 8dc25dffc2 LibWebView: Protect URL highlighting against partially-typed URLs
The current helpers assume that a valid URL is a full URL (i.e. contains
the "://" separator between the scheme and domain). This isn't true, as
"file:" alone is parsed as a valid URL.

We must also avoid simply searching for the parsed public suffix in the
original URL string. For example, "com" is a public suffix. If we search
for that in the URL "com.com", we will think the public suffix starts at
index 0.
2023-10-28 19:03:19 +02:00

77 lines
3.6 KiB
C++

/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <LibWebView/URL.h>
static void compare_url_parts(StringView url, WebView::URLParts const& expected)
{
auto result = WebView::break_url_into_parts(url);
VERIFY(result.has_value());
EXPECT_EQ(result->scheme_and_subdomain, expected.scheme_and_subdomain);
EXPECT_EQ(result->effective_tld_plus_one, expected.effective_tld_plus_one);
EXPECT_EQ(result->remainder, expected.remainder);
}
TEST_CASE(invalid_url)
{
EXPECT(!WebView::break_url_into_parts(""sv).has_value());
EXPECT(!WebView::break_url_into_parts(":"sv).has_value());
EXPECT(!WebView::break_url_into_parts(":/"sv).has_value());
EXPECT(!WebView::break_url_into_parts("://"sv).has_value());
EXPECT(!WebView::break_url_into_parts("f"sv).has_value());
EXPECT(!WebView::break_url_into_parts("fi"sv).has_value());
EXPECT(!WebView::break_url_into_parts("fil"sv).has_value());
EXPECT(!WebView::break_url_into_parts("file"sv).has_value());
EXPECT(!WebView::break_url_into_parts("file:"sv).has_value());
EXPECT(!WebView::break_url_into_parts("file:/"sv).has_value());
EXPECT(!WebView::break_url_into_parts("h"sv).has_value());
EXPECT(!WebView::break_url_into_parts("ht"sv).has_value());
EXPECT(!WebView::break_url_into_parts("htt"sv).has_value());
EXPECT(!WebView::break_url_into_parts("http"sv).has_value());
EXPECT(!WebView::break_url_into_parts("http:"sv).has_value());
EXPECT(!WebView::break_url_into_parts("http:/"sv).has_value());
EXPECT(!WebView::break_url_into_parts("http://"sv).has_value());
EXPECT(!WebView::break_url_into_parts("https"sv).has_value());
EXPECT(!WebView::break_url_into_parts("https:"sv).has_value());
EXPECT(!WebView::break_url_into_parts("https:/"sv).has_value());
EXPECT(!WebView::break_url_into_parts("https://"sv).has_value());
}
TEST_CASE(file_url)
{
compare_url_parts("file://"sv, { "file://"sv, ""sv, {} });
compare_url_parts("file://a"sv, { "file://"sv, "a"sv, {} });
compare_url_parts("file:///a"sv, { "file://"sv, "/a"sv, {} });
compare_url_parts("file:///abc"sv, { "file://"sv, "/abc"sv, {} });
}
TEST_CASE(http_url)
{
compare_url_parts("http://a"sv, { "http://"sv, "a"sv, {} });
compare_url_parts("http://abc"sv, { "http://"sv, "abc"sv, {} });
compare_url_parts("http://com"sv, { "http://"sv, "com"sv, {} });
compare_url_parts("http://abc."sv, { "http://"sv, "abc."sv, {} });
compare_url_parts("http://abc.c"sv, { "http://"sv, "abc.c"sv, {} });
compare_url_parts("http://abc.com"sv, { "http://"sv, "abc.com"sv, {} });
compare_url_parts("http://abc.com."sv, { "http://"sv, "abc.com."sv, {} });
compare_url_parts("http://abc.com."sv, { "http://"sv, "abc.com."sv, {} });
compare_url_parts("http://abc.com.org"sv, { "http://abc."sv, "com.org"sv, {} });
compare_url_parts("http://abc.com.org.gov"sv, { "http://abc.com."sv, "org.gov"sv, {} });
compare_url_parts("http://abc/path"sv, { "http://"sv, "abc"sv, "/path"sv });
compare_url_parts("http://abc#anchor"sv, { "http://"sv, "abc"sv, "#anchor"sv });
compare_url_parts("http://abc?query"sv, { "http://"sv, "abc"sv, "?query"sv });
compare_url_parts("http://abc.def.com"sv, { "http://abc."sv, "def.com"sv, {} });
compare_url_parts("http://abc.def.com/path"sv, { "http://abc."sv, "def.com"sv, "/path"sv });
compare_url_parts("http://abc.def.com#anchor"sv, { "http://abc."sv, "def.com"sv, "#anchor"sv });
compare_url_parts("http://abc.def.com?query"sv, { "http://abc."sv, "def.com"sv, "?query"sv });
}