tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestContentUtils.cpp (10424B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 #include "js/PropertyAndElement.h"  // JS_DefineProperty
      9 #include "jsapi.h"
     10 #include "mozilla/CycleCollectedJSContext.h"
     11 #include "mozilla/dom/ScriptSettings.h"
     12 #include "mozilla/dom/SimpleGlobalObject.h"
     13 #include "nsContentUtils.h"
     14 #include "nsNetUtil.h"
     15 
     16 using namespace mozilla::dom;
     17 
     18 struct IsURIInListMatch {
     19  nsLiteralCString pattern;
     20  bool firstMatch, secondMatch;
     21 };
     22 
     23 std::ostream& operator<<(std::ostream& aStream,
     24                         const nsContentUtils::ParsedRange& aParsedRange) {
     25  if (aParsedRange.Start()) {
     26    aStream << *aParsedRange.Start();
     27  }
     28 
     29  aStream << "-";
     30 
     31  if (aParsedRange.End()) {
     32    aStream << *aParsedRange.End();
     33  }
     34 
     35  return aStream;
     36 }
     37 
     38 TEST(DOM_Base_ContentUtils, IsURIInList)
     39 {
     40  nsCOMPtr<nsIURI> uri, subURI;
     41  nsresult rv = NS_NewURI(getter_AddRefs(uri),
     42                          "https://example.com/path/favicon.ico#"_ns);
     43  ASSERT_TRUE(rv == NS_OK);
     44 
     45  rv = NS_NewURI(getter_AddRefs(subURI),
     46                 "http://sub.example.com/favicon.ico?"_ns);
     47  ASSERT_TRUE(rv == NS_OK);
     48 
     49  static constexpr IsURIInListMatch patterns[] = {
     50      {"bar.com,*.example.com,example.com,foo.com"_ns, true, true},
     51      {"bar.com,example.com,*.example.com,foo.com"_ns, true, true},
     52      {"*.example.com,example.com,foo.com"_ns, true, true},
     53      {"example.com,*.example.com,foo.com"_ns, true, true},
     54      {"*.example.com,example.com"_ns, true, true},
     55      {"example.com,*.example.com"_ns, true, true},
     56      {"*.example.com/,example.com/"_ns, true, true},
     57      {"example.com/,*.example.com/"_ns, true, true},
     58      {"*.example.com/pa,example.com/pa"_ns, false, false},
     59      {"example.com/pa,*.example.com/pa"_ns, false, false},
     60      {"*.example.com/pa/,example.com/pa/"_ns, false, false},
     61      {"example.com/pa/,*.example.com/pa/"_ns, false, false},
     62      {"*.example.com/path,example.com/path"_ns, false, false},
     63      {"example.com/path,*.example.com/path"_ns, false, false},
     64      {"*.example.com/path/,example.com/path/"_ns, true, false},
     65      {"example.com/path/,*.example.com/path/"_ns, true, false},
     66      {"*.example.com/favicon.ico"_ns, false, true},
     67      {"example.com/path/favicon.ico"_ns, true, false},
     68      {"*.example.com"_ns, false, true},
     69      {"example.com"_ns, true, false},
     70      {"foo.com"_ns, false, false},
     71      {"*.foo.com"_ns, false, false},
     72  };
     73 
     74  for (auto& entry : patterns) {
     75    bool result = nsContentUtils::IsURIInList(uri, entry.pattern);
     76    ASSERT_EQ(result, entry.firstMatch) << "Matching " << entry.pattern;
     77 
     78    result = nsContentUtils::IsURIInList(subURI, entry.pattern);
     79    ASSERT_EQ(result, entry.secondMatch) << "Matching " << entry.pattern;
     80  }
     81 }
     82 
     83 TEST(DOM_Base_ContentUtils,
     84     StringifyJSON_EmptyValue_UndefinedIsNullStringLiteral)
     85 {
     86  JS::Rooted<JSObject*> globalObject(
     87      mozilla::dom::RootingCx(),
     88      mozilla::dom::SimpleGlobalObject::Create(
     89          mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail));
     90  mozilla::dom::AutoJSAPI jsAPI;
     91  ASSERT_TRUE(jsAPI.Init(globalObject));
     92  JSContext* cx = jsAPI.cx();
     93  nsAutoString serializedValue;
     94 
     95  ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, JS::UndefinedHandleValue,
     96                                            serializedValue,
     97                                            UndefinedIsNullStringLiteral));
     98  ASSERT_TRUE(serializedValue.EqualsLiteral("null"));
     99 }
    100 
    101 TEST(DOM_Base_ContentUtils, StringifyJSON_Object_UndefinedIsNullStringLiteral)
    102 {
    103  JS::Rooted<JSObject*> globalObject(
    104      mozilla::dom::RootingCx(),
    105      mozilla::dom::SimpleGlobalObject::Create(
    106          mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail));
    107  mozilla::dom::AutoJSAPI jsAPI;
    108  ASSERT_TRUE(jsAPI.Init(globalObject));
    109  JSContext* cx = jsAPI.cx();
    110  nsAutoString serializedValue;
    111 
    112  JS::Rooted<JSObject*> jsObj(cx, JS_NewPlainObject(cx));
    113  JS::Rooted<JSString*> valueStr(cx, JS_NewStringCopyZ(cx, "Hello World!"));
    114  ASSERT_TRUE(JS_DefineProperty(cx, jsObj, "key1", valueStr, JSPROP_ENUMERATE));
    115  JS::Rooted<JS::Value> jsValue(cx, JS::ObjectValue(*jsObj));
    116 
    117  ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, jsValue, serializedValue,
    118                                            UndefinedIsNullStringLiteral));
    119 
    120  ASSERT_TRUE(serializedValue.EqualsLiteral("{\"key1\":\"Hello World!\"}"));
    121 }
    122 
    123 TEST(DOM_Base_ContentUtils, StringifyJSON_EmptyValue_UndefinedIsVoidString)
    124 {
    125  JS::Rooted<JSObject*> globalObject(
    126      mozilla::dom::RootingCx(),
    127      mozilla::dom::SimpleGlobalObject::Create(
    128          mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail));
    129  mozilla::dom::AutoJSAPI jsAPI;
    130  ASSERT_TRUE(jsAPI.Init(globalObject));
    131  JSContext* cx = jsAPI.cx();
    132  nsAutoString serializedValue;
    133 
    134  ASSERT_TRUE(nsContentUtils::StringifyJSON(
    135      cx, JS::UndefinedHandleValue, serializedValue, UndefinedIsVoidString));
    136 
    137  ASSERT_TRUE(serializedValue.IsVoid());
    138 }
    139 
    140 TEST(DOM_Base_ContentUtils, StringifyJSON_Object_UndefinedIsVoidString)
    141 {
    142  JS::Rooted<JSObject*> globalObject(
    143      mozilla::dom::RootingCx(),
    144      mozilla::dom::SimpleGlobalObject::Create(
    145          mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail));
    146  mozilla::dom::AutoJSAPI jsAPI;
    147  ASSERT_TRUE(jsAPI.Init(globalObject));
    148  JSContext* cx = jsAPI.cx();
    149  nsAutoString serializedValue;
    150 
    151  JS::Rooted<JSObject*> jsObj(cx, JS_NewPlainObject(cx));
    152  JS::Rooted<JSString*> valueStr(cx, JS_NewStringCopyZ(cx, "Hello World!"));
    153  ASSERT_TRUE(JS_DefineProperty(cx, jsObj, "key1", valueStr, JSPROP_ENUMERATE));
    154  JS::Rooted<JS::Value> jsValue(cx, JS::ObjectValue(*jsObj));
    155 
    156  ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, jsValue, serializedValue,
    157                                            UndefinedIsVoidString));
    158 
    159  ASSERT_TRUE(serializedValue.EqualsLiteral("{\"key1\":\"Hello World!\"}"));
    160 }
    161 
    162 TEST(DOM_Base_ContentUtils, ParseSingleRangeHeader)
    163 {
    164  // Parsing a simple range should succeed
    165  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=0-42"_ns, false),
    166            mozilla::Some(nsContentUtils::ParsedRange(mozilla::Some(0),
    167                                                      mozilla::Some(42))));
    168 
    169  // Range containing a invalid rangeStart should fail
    170  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes= t-200"_ns, true),
    171            mozilla::Nothing());
    172 
    173  // Range containing whitespace, with allowWhitespace=false should fail.
    174  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes= 2-200"_ns, false),
    175            mozilla::Nothing());
    176 
    177  // Range containing whitespace, with allowWhitespace=true should succeed
    178  EXPECT_EQ(
    179      nsContentUtils::ParseSingleRangeRequest("bytes \t= 2 - 200"_ns, true),
    180      mozilla::Some(
    181          nsContentUtils::ParsedRange(mozilla::Some(2), mozilla::Some(200))));
    182 
    183  // Range containing invalid whitespace should fail
    184  EXPECT_EQ(
    185      nsContentUtils::ParseSingleRangeRequest("bytes \r= 2 - 200"_ns, true),
    186      mozilla::Nothing());
    187 
    188  // Range without a rangeStart should succeed
    189  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes\t=\t-200"_ns, true),
    190            mozilla::Some(nsContentUtils::ParsedRange(mozilla::Nothing(),
    191                                                      mozilla::Some(200))));
    192 
    193  // Range without a rangeEnd should succeed
    194  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=55-"_ns, true),
    195            mozilla::Some(nsContentUtils::ParsedRange(mozilla::Some(55),
    196                                                      mozilla::Nothing())));
    197 
    198  // Range without a rangeStart or rangeEnd should fail
    199  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes\t=\t-"_ns, true),
    200            mozilla::Nothing());
    201 
    202  // Range with extra characters should fail
    203  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=0-42 "_ns, true),
    204            mozilla::Nothing());
    205 
    206  // Range with rangeStart > rangeEnd should fail
    207  EXPECT_EQ(nsContentUtils::ParseSingleRangeRequest("bytes=42-0 "_ns, true),
    208            mozilla::Nothing());
    209 }
    210 
    211 TEST(DOM_Base_ContentUtils, IsAllowedNonCorsRange)
    212 {
    213  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=-200"_ns), false);
    214  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes= 200-"_ns), false);
    215  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=201-200"_ns), false);
    216  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=200-201 "_ns), false);
    217  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=200-"_ns), true);
    218  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=200-201"_ns), true);
    219  EXPECT_EQ(nsContentUtils::IsAllowedNonCorsRange("bytes=-200 "_ns), false);
    220 }
    221 
    222 TEST(DOM_Base_ContentUtils, MaybeFixIPv6Host)
    223 {
    224  struct TestCase {
    225    const char* input;
    226    const char* expectedOutput;
    227  };
    228 
    229  const TestCase testCases[] = {
    230      // Hosts containing colons without brackets - brackets should be added
    231      {"2001:db8::1", "[2001:db8::1]"},  // IPv6 address
    232      {"::1", "[::1]"},                  // IPv6 loopback
    233      {"::", "[::]"},                    // IPv6 unspecified address
    234      // Hosts containing colons but already with brackets - should remain
    235      // unchanged
    236      {"[2001:db8::1]", "[2001:db8::1]"},
    237      {"[::1]", "[::1]"},
    238      // Hosts without colons - should remain unchanged
    239      {"example.com", "example.com"},
    240      {"localhost", "localhost"},
    241      {"", ""},  // Empty string
    242      // Single colon - length less than 2, so brackets are not added
    243      {":", ":"},
    244      // Hosts starting with '[' or ending with ']', but not both - no brackets
    245      // are added.
    246      {"[example.com", "[example.com"},
    247      {"example.com]", "example.com]"},
    248      // Hosts with misordered brackets - no brackets are added
    249      {"]example[", "]example["},
    250      // Hosts with length less than 2 - no brackets added
    251      {"a", "a"},
    252      // Hosts with colons but length less than 2 - no brackets added
    253      {":", ":"},
    254  };
    255 
    256  for (const auto& testCase : testCases) {
    257    nsCString host(testCase.input);
    258    nsContentUtils::MaybeFixIPv6Host(host);
    259    ASSERT_TRUE(host.Equals(testCase.expectedOutput))
    260    << "Input: '" << testCase.input << "', Expected: '"
    261    << testCase.expectedOutput << "', Got: '" << host.get() << "'";
    262  }
    263 }