Bug 1736543 - Tests, r=xpcom-reviewers,nika

Depends on D128630

Differential Revision: https://phabricator.services.mozilla.com/D128631
This commit is contained in:
Paul Zuehlcke 2022-01-17 12:09:52 +00:00
Родитель 0c8f85e0c6
Коммит 7ab30db7fe
4 изменённых файлов: 91 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,41 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsExternalHelperAppService.h"
#include "gtest/gtest.h"
#include "nsNetUtil.h"
using namespace mozilla;
TEST(ExternalHelperAppService, EscapeURI)
{
nsCString input("myproto://hello world");
nsCString expected("myproto://hello%20world");
nsCOMPtr<nsIIOService> ios(do_GetIOService());
nsCOMPtr<nsIURI> uri;
nsresult rv = ios->NewURI(input, nullptr, nullptr, getter_AddRefs(uri));
EXPECT_EQ(rv, NS_OK);
nsAutoCString spec;
rv = uri->GetSpec(spec);
EXPECT_EQ(rv, NS_OK);
// Constructing the URI does not escape the space character.
ASSERT_TRUE(spec.Equals(input));
// Created an escaped version of the URI.
nsCOMPtr<nsIURI> escapedURI;
rv = nsExternalHelperAppService::EscapeURI(uri, getter_AddRefs(escapedURI));
EXPECT_EQ(rv, NS_OK);
nsAutoCString escapedSpec;
rv = escapedURI->GetSpec(escapedSpec);
EXPECT_EQ(rv, NS_OK);
// Escaped URI should have an escaped spec.
ASSERT_TRUE(escapedSpec.Equals(expected));
}

Просмотреть файл

@ -0,0 +1,11 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
UNIFIED_SOURCES += [
"ExternalHelperAppServiceTest.cpp",
]
FINAL_LIBRARY = "xul-gtest"

Просмотреть файл

@ -10,6 +10,10 @@ XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell.ini"]
BROWSER_CHROME_MANIFESTS += ["mochitest/browser.ini"]
TEST_DIRS += [
"gtest",
]
TESTING_JS_MODULES += [
"HandlerServiceTestUtils.jsm",
]

Просмотреть файл

@ -201,3 +201,38 @@ TEST(Escape, AppleNSURLEscapeLists)
EXPECT_STREQ(toEscape.BeginReading(), escaped.BeginReading());
}
}
// Test external handler URLs are properly escaped.
TEST(Escape, EscapeURLExternalHandlerURLs)
{
const nsCString input[] = {
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;/?:@&=+$,!'()*-._~#[]"_ns,
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"_ns,
"custom_proto:Hello World"_ns,
"custom_proto:Hello%20World"_ns,
"myApp://\"foo\" 'bar' `foo`"_ns,
"translator://en-de?view=übersicht"_ns,
"foo:some\\path\\here"_ns,
"web+foo://user:1234@example.com:8080?foo=bar"_ns,
"ext+bar://id='myId'"_ns};
const nsCString expected[] = {
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;/?:@&=+$,!'()*-._~#[]"_ns,
"%20!%22#$%&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[%5C]%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"_ns,
"custom_proto:Hello%20World"_ns,
"custom_proto:Hello%20World"_ns,
"myApp://%22foo%22%20'bar'%20%60foo%60"_ns,
"translator://en-de?view=%C3%BCbersicht"_ns,
"foo:some%5Cpath%5Chere"_ns,
"web+foo://user:1234@example.com:8080?foo=bar"_ns,
"ext+bar://id='myId'"_ns};
for (size_t i = 0; i < ArrayLength(input); i++) {
nsCString src(input[i]);
nsCString dst;
nsresult rv =
NS_EscapeURL(src, esc_ExtHandler | esc_AlwaysCopy, dst, fallible);
EXPECT_EQ(rv, NS_OK);
ASSERT_TRUE(dst.Equals(expected[i]));
}
}