diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /dom/base/test/gtest/TestContentUtils.cpp | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/base/test/gtest/TestContentUtils.cpp')
-rw-r--r-- | dom/base/test/gtest/TestContentUtils.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/dom/base/test/gtest/TestContentUtils.cpp b/dom/base/test/gtest/TestContentUtils.cpp new file mode 100644 index 0000000000..d38cb1bdca --- /dev/null +++ b/dom/base/test/gtest/TestContentUtils.cpp @@ -0,0 +1,146 @@ +/* -*- 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 "gtest/gtest.h" + +#include "jsapi.h" +#include "js/PropertyAndElement.h" // JS_DefineProperty +#include "nsContentUtils.h" +#include "nsNetUtil.h" +#include "mozilla/CycleCollectedJSContext.h" +#include "mozilla/dom/ScriptSettings.h" +#include "mozilla/dom/SimpleGlobalObject.h" + +using namespace mozilla::dom; + +struct IsURIInListMatch { + nsLiteralCString pattern; + bool firstMatch, secondMatch; +}; + +TEST(DOM_Base_ContentUtils, IsURIInList) +{ + nsCOMPtr<nsIURI> uri, subURI; + nsresult rv = NS_NewURI(getter_AddRefs(uri), + "https://example.com/path/favicon.ico#"_ns); + ASSERT_TRUE(rv == NS_OK); + + rv = NS_NewURI(getter_AddRefs(subURI), + "http://sub.example.com/favicon.ico?"_ns); + ASSERT_TRUE(rv == NS_OK); + + static constexpr IsURIInListMatch patterns[] = { + {"bar.com,*.example.com,example.com,foo.com"_ns, true, true}, + {"bar.com,example.com,*.example.com,foo.com"_ns, true, true}, + {"*.example.com,example.com,foo.com"_ns, true, true}, + {"example.com,*.example.com,foo.com"_ns, true, true}, + {"*.example.com,example.com"_ns, true, true}, + {"example.com,*.example.com"_ns, true, true}, + {"*.example.com/,example.com/"_ns, true, true}, + {"example.com/,*.example.com/"_ns, true, true}, + {"*.example.com/pa,example.com/pa"_ns, false, false}, + {"example.com/pa,*.example.com/pa"_ns, false, false}, + {"*.example.com/pa/,example.com/pa/"_ns, false, false}, + {"example.com/pa/,*.example.com/pa/"_ns, false, false}, + {"*.example.com/path,example.com/path"_ns, false, false}, + {"example.com/path,*.example.com/path"_ns, false, false}, + {"*.example.com/path/,example.com/path/"_ns, true, false}, + {"example.com/path/,*.example.com/path/"_ns, true, false}, + {"*.example.com/favicon.ico"_ns, false, true}, + {"example.com/path/favicon.ico"_ns, true, false}, + {"*.example.com"_ns, false, true}, + {"example.com"_ns, true, false}, + {"foo.com"_ns, false, false}, + {"*.foo.com"_ns, false, false}, + }; + + for (auto& entry : patterns) { + bool result = nsContentUtils::IsURIInList(uri, entry.pattern); + ASSERT_EQ(result, entry.firstMatch) << "Matching " << entry.pattern; + + result = nsContentUtils::IsURIInList(subURI, entry.pattern); + ASSERT_EQ(result, entry.secondMatch) << "Matching " << entry.pattern; + } +} + +TEST(DOM_Base_ContentUtils, + StringifyJSON_EmptyValue_UndefinedIsNullStringLiteral) +{ + JS::Rooted<JSObject*> globalObject( + mozilla::dom::RootingCx(), + mozilla::dom::SimpleGlobalObject::Create( + mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail)); + mozilla::dom::AutoJSAPI jsAPI; + ASSERT_TRUE(jsAPI.Init(globalObject)); + JSContext* cx = jsAPI.cx(); + nsAutoString serializedValue; + + ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, JS::UndefinedHandleValue, + serializedValue, + UndefinedIsNullStringLiteral)); + ASSERT_TRUE(serializedValue.EqualsLiteral("null")); +} + +TEST(DOM_Base_ContentUtils, StringifyJSON_Object_UndefinedIsNullStringLiteral) +{ + JS::Rooted<JSObject*> globalObject( + mozilla::dom::RootingCx(), + mozilla::dom::SimpleGlobalObject::Create( + mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail)); + mozilla::dom::AutoJSAPI jsAPI; + ASSERT_TRUE(jsAPI.Init(globalObject)); + JSContext* cx = jsAPI.cx(); + nsAutoString serializedValue; + + JS::Rooted<JSObject*> jsObj(cx, JS_NewPlainObject(cx)); + JS::Rooted<JSString*> valueStr(cx, JS_NewStringCopyZ(cx, "Hello World!")); + ASSERT_TRUE(JS_DefineProperty(cx, jsObj, "key1", valueStr, JSPROP_ENUMERATE)); + JS::Rooted<JS::Value> jsValue(cx, JS::ObjectValue(*jsObj)); + + ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, jsValue, serializedValue, + UndefinedIsNullStringLiteral)); + + ASSERT_TRUE(serializedValue.EqualsLiteral("{\"key1\":\"Hello World!\"}")); +} + +TEST(DOM_Base_ContentUtils, StringifyJSON_EmptyValue_UndefinedIsVoidString) +{ + JS::Rooted<JSObject*> globalObject( + mozilla::dom::RootingCx(), + mozilla::dom::SimpleGlobalObject::Create( + mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail)); + mozilla::dom::AutoJSAPI jsAPI; + ASSERT_TRUE(jsAPI.Init(globalObject)); + JSContext* cx = jsAPI.cx(); + nsAutoString serializedValue; + + ASSERT_TRUE(nsContentUtils::StringifyJSON( + cx, JS::UndefinedHandleValue, serializedValue, UndefinedIsVoidString)); + + ASSERT_TRUE(serializedValue.IsVoid()); +} + +TEST(DOM_Base_ContentUtils, StringifyJSON_Object_UndefinedIsVoidString) +{ + JS::Rooted<JSObject*> globalObject( + mozilla::dom::RootingCx(), + mozilla::dom::SimpleGlobalObject::Create( + mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail)); + mozilla::dom::AutoJSAPI jsAPI; + ASSERT_TRUE(jsAPI.Init(globalObject)); + JSContext* cx = jsAPI.cx(); + nsAutoString serializedValue; + + JS::Rooted<JSObject*> jsObj(cx, JS_NewPlainObject(cx)); + JS::Rooted<JSString*> valueStr(cx, JS_NewStringCopyZ(cx, "Hello World!")); + ASSERT_TRUE(JS_DefineProperty(cx, jsObj, "key1", valueStr, JSPROP_ENUMERATE)); + JS::Rooted<JS::Value> jsValue(cx, JS::ObjectValue(*jsObj)); + + ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, jsValue, serializedValue, + UndefinedIsVoidString)); + + ASSERT_TRUE(serializedValue.EqualsLiteral("{\"key1\":\"Hello World!\"}")); +} |