1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/* -*- 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 "js/PropertyAndElement.h" // JS_DefineProperty
#include "jsapi.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/SimpleGlobalObject.h"
#include "nsContentUtils.h"
#include "nsNetUtil.h"
using namespace mozilla::dom;
struct IsURIInListMatch {
nsLiteralCString pattern;
bool firstMatch, secondMatch;
};
std::ostream& operator<<(std::ostream& aStream,
const nsContentUtils::ParsedRange& aParsedRange) {
if (aParsedRange.Start()) {
aStream << *aParsedRange.Start();
}
aStream << "-";
if (aParsedRange.End()) {
aStream << *aParsedRange.End();
}
return aStream;
}
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!\"}"));
}
TEST(DOM_Base_ContentUtils, ParseSingleRangeHeader)
{
// Parsing a simple range should succeed
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=0-42"_ns, false),
mozilla::Some(nsContentUtils::ParsedRange(mozilla::Some(0),
mozilla::Some(42))));
// Range containing a invalid rangeStart should fail
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes= t-200"_ns, true),
mozilla::Nothing());
// Range containing whitespace, with allowWhitespace=false should fail.
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes= 2-200"_ns, false),
mozilla::Nothing());
// Range containing whitespace, with allowWhitespace=true should succeed
EXPECT_EQ(
nsContentUtils::ParseSingleRangeRequest("bytes \t= 2 - 200"_ns, true),
mozilla::Some(
nsContentUtils::ParsedRange(mozilla::Some(2), mozilla::Some(200))));
// Range containing invalid whitespace should fail
EXPECT_EQ(
nsContentUtils::ParseSingleRangeRequest("bytes \r= 2 - 200"_ns, true),
mozilla::Nothing());
// Range without a rangeStart should succeed
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes\t=\t-200"_ns, true),
mozilla::Some(nsContentUtils::ParsedRange(mozilla::Nothing(),
mozilla::Some(200))));
// Range without a rangeEnd should succeed
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=55-"_ns, true),
mozilla::Some(nsContentUtils::ParsedRange(mozilla::Some(55),
mozilla::Nothing())));
// Range without a rangeStart or rangeEnd should fail
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes\t=\t-"_ns, true),
mozilla::Nothing());
// Range with extra characters should fail
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=0-42 "_ns, true),
mozilla::Nothing());
// Range with rangeStart > rangeEnd should fail
EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=42-0 "_ns, true),
mozilla::Nothing());
}
TEST(DOM_Base_ContentUtils, IsAllowedNonCorsRange)
{
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=-200"_ns), false);
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes= 200-"_ns), false);
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=201-200"_ns), false);
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=200-201 "_ns), false);
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=200-"_ns), true);
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=200-201"_ns), true);
EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=-200 "_ns), false);
}
TEST(DOM_Base_ContentUtils, MaybeFixIPv6Host)
{
struct TestCase {
const char* input;
const char* expectedOutput;
};
const TestCase testCases[] = {
// Hosts containing colons without brackets - brackets should be added
{"2001:db8::1", "[2001:db8::1]"}, // IPv6 address
{"::1", "[::1]"}, // IPv6 loopback
{"::", "[::]"}, // IPv6 unspecified address
// Hosts containing colons but already with brackets - should remain
// unchanged
{"[2001:db8::1]", "[2001:db8::1]"},
{"[::1]", "[::1]"},
// Hosts without colons - should remain unchanged
{"example.com", "example.com"},
{"localhost", "localhost"},
{"", ""}, // Empty string
// Single colon - length less than 2, so brackets are not added
{":", ":"},
// Hosts starting with '[' or ending with ']', but not both - no brackets
// are added.
{"[example.com", "[example.com"},
{"example.com]", "example.com]"},
// Hosts with misordered brackets - no brackets are added
{"]example[", "]example["},
// Hosts with length less than 2 - no brackets added
{"a", "a"},
// Hosts with colons but length less than 2 - no brackets added
{":", ":"},
};
for (const auto& testCase : testCases) {
nsCString host(testCase.input);
nsContentUtils::MaybeFixIPv6Host(host);
ASSERT_TRUE(host.Equals(testCase.expectedOutput))
<< "Input: '" << testCase.input << "', Expected: '"
<< testCase.expectedOutput << "', Got: '" << host.get() << "'";
}
}
|