Commit 5a99b69f authored by Daniel Vogelheim's avatar Daniel Vogelheim Committed by Commit Bot

[Trusted Types] Remove unused functions.

Remove unused functions and associated unit tests. This prepares for changes
to rename & change the trusted_types_util.h interfaces.

Bug: 739170
Change-Id: I45ddf92d5946cb72a33ae4fba2bfb8d04f8c4d6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2078611
Commit-Queue: Daniel Vogelheim <vogelheim@chromium.org>
Reviewed-by: default avatarYifan Luo <lyf@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746359}
parent f92a893d
......@@ -27,7 +27,6 @@ namespace blink {
namespace {
enum TrustedTypeViolationKind {
kAnyTrustedTypeAssignment,
kTrustedHTMLAssignment,
kTrustedScriptAssignment,
kTrustedScriptURLAssignment,
......@@ -45,8 +44,6 @@ enum TrustedTypeViolationKind {
const char* GetMessage(TrustedTypeViolationKind kind) {
switch (kind) {
case kAnyTrustedTypeAssignment:
return "This document requires any trusted type assignment.";
case kTrustedHTMLAssignment:
return "This document requires 'TrustedHTML' assignment.";
case kTrustedScriptAssignment:
......@@ -230,31 +227,6 @@ bool RequireTrustedTypesCheck(const ExecutionContext* execution_context) {
!ContentSecurityPolicy::ShouldBypassMainWorld(execution_context);
}
String GetStringFromTrustedType(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&
string_or_trusted_type,
const ExecutionContext* execution_context,
ExceptionState& exception_state) {
DCHECK(!string_or_trusted_type.IsNull());
if (string_or_trusted_type.IsString() &&
RequireTrustedTypesCheck(execution_context)) {
TrustedTypeFail(
kAnyTrustedTypeAssignment, execution_context, exception_state,
GetStringFromTrustedTypeWithoutCheck(string_or_trusted_type));
return g_empty_string;
}
if (string_or_trusted_type.IsTrustedHTML())
return string_or_trusted_type.GetAsTrustedHTML()->toString();
if (string_or_trusted_type.IsTrustedScript())
return string_or_trusted_type.GetAsTrustedScript()->toString();
if (string_or_trusted_type.IsTrustedScriptURL())
return string_or_trusted_type.GetAsTrustedScriptURL()->toString();
return string_or_trusted_type.GetAsString();
}
String GetStringFromTrustedTypeWithoutCheck(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&
string_or_trusted_type) {
......
......@@ -25,14 +25,6 @@ enum class SpecificTrustedType {
kTrustedScriptURL,
};
String CORE_EXPORT GetStringFromTrustedType(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&,
const ExecutionContext*,
ExceptionState&);
String CORE_EXPORT GetStringFromTrustedTypeWithoutCheck(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&);
String CORE_EXPORT GetStringFromSpecificTrustedType(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&,
SpecificTrustedType,
......
......@@ -22,32 +22,6 @@
namespace blink {
// Functions for checking throwing cases.
void GetStringFromTrustedTypeThrows(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&
string_or_trusted_type) {
auto* document = MakeGarbageCollected<Document>();
document->GetContentSecurityPolicy()->DidReceiveHeader(
"trusted-types *", network::mojom::ContentSecurityPolicyType::kEnforce,
network::mojom::ContentSecurityPolicySource::kMeta);
DummyExceptionStateForTesting exception_state;
ASSERT_FALSE(exception_state.HadException());
String s = GetStringFromTrustedType(
string_or_trusted_type, document->ToExecutionContext(), exception_state);
EXPECT_FALSE(exception_state.HadException());
document->GetContentSecurityPolicy()->DidReceiveHeader(
"require-trusted-types-for 'script'",
network::mojom::ContentSecurityPolicyType::kEnforce,
network::mojom::ContentSecurityPolicySource::kMeta);
ASSERT_FALSE(exception_state.HadException());
String s1 = GetStringFromTrustedType(
string_or_trusted_type, document->ToExecutionContext(), exception_state);
EXPECT_TRUE(exception_state.HadException());
EXPECT_EQ(ESErrorType::kTypeError, exception_state.CodeAs<ESErrorType>());
exception_state.ClearException();
}
void GetStringFromTrustedHTMLThrows(
const StringOrTrustedHTML& string_or_trusted_html) {
auto dummy_page_holder = std::make_unique<DummyPageHolder>(IntSize(800, 600));
......@@ -129,20 +103,6 @@ void GetStringFromTrustedScriptURLThrows(
}
// Functions for checking non-throwing cases.
void GetStringFromTrustedTypeWorks(
const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL&
string_or_trusted_type,
String expected) {
auto* document = MakeGarbageCollected<Document>();
document->GetContentSecurityPolicy()->DidReceiveHeader(
"trusted-types *", network::mojom::ContentSecurityPolicyType::kEnforce,
network::mojom::ContentSecurityPolicySource::kMeta);
DummyExceptionStateForTesting exception_state;
String s = GetStringFromTrustedType(
string_or_trusted_type, document->ToExecutionContext(), exception_state);
ASSERT_EQ(s, expected);
}
void GetStringFromTrustedHTMLWorks(
const StringOrTrustedHTML& string_or_trusted_html,
String expected) {
......@@ -189,93 +149,6 @@ void GetStringFromTrustedScriptURLWorks(
ASSERT_EQ(s, expected);
}
// GetStringFromTrustedType() tests
TEST(TrustedTypesUtilTest, GetStringFromTrustedType_TrustedHTML) {
auto* html = MakeGarbageCollected<TrustedHTML>("A string");
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::FromTrustedHTML(
html);
GetStringFromTrustedTypeWorks(trusted_value, "A string");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedType_TrustedScript) {
auto* script = MakeGarbageCollected<TrustedScript>("A string");
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::FromTrustedScript(
script);
GetStringFromTrustedTypeWorks(trusted_value, "A string");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedType_TrustedScriptURL) {
String url_address = "http://www.example.com/";
auto* script_url = MakeGarbageCollected<TrustedScriptURL>(url_address);
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::
FromTrustedScriptURL(script_url);
GetStringFromTrustedTypeWorks(trusted_value, "http://www.example.com/");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedType_TrustedScriptURL_Relative) {
String url_address = "relative/url.html";
auto* script_url = MakeGarbageCollected<TrustedScriptURL>(url_address);
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::
FromTrustedScriptURL(script_url);
GetStringFromTrustedTypeWorks(trusted_value, "relative/url.html");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedType_String) {
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL string_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::FromString(
"A string");
GetStringFromTrustedTypeThrows(string_value);
}
// GetStringFromTrustedTypeWithoutCheck() tests
TEST(TrustedTypesUtilTest, GetStringFromTrustedTypeWithoutCheck_TrustedHTML) {
auto* html = MakeGarbageCollected<TrustedHTML>("A string");
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::FromTrustedHTML(
html);
String s = GetStringFromTrustedTypeWithoutCheck(trusted_value);
ASSERT_EQ(s, "A string");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedTypeWithoutCheck_TrustedScript) {
auto* script = MakeGarbageCollected<TrustedScript>("A string");
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::FromTrustedScript(
script);
String s = GetStringFromTrustedTypeWithoutCheck(trusted_value);
ASSERT_EQ(s, "A string");
}
TEST(TrustedTypesUtilTest,
GetStringFromTrustedTypeWithoutCheck_TrustedScriptURL) {
String url_address = "http://www.example.com/";
auto* script_url = MakeGarbageCollected<TrustedScriptURL>(url_address);
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL trusted_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::
FromTrustedScriptURL(script_url);
String s = GetStringFromTrustedTypeWithoutCheck(trusted_value);
ASSERT_EQ(s, "http://www.example.com/");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedTypeWithoutCheck_String) {
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL string_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL::FromString(
"A string");
String s = GetStringFromTrustedTypeWithoutCheck(string_value);
ASSERT_EQ(s, "A string");
}
TEST(TrustedTypesUtilTest, GetStringFromTrustedTypeWithoutCheck_Null) {
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL null_value =
StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURL();
String s = GetStringFromTrustedTypeWithoutCheck(null_value);
ASSERT_EQ(s, "");
}
// GetStringFromTrustedHTML tests
TEST(TrustedTypesUtilTest, GetStringFromTrustedHTML_TrustedHTML) {
auto* html = MakeGarbageCollected<TrustedHTML>("A string");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment