tor-browser

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

TestLocalStorage.cpp (4732B)


      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 file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 #include "mozilla/NullPrincipal.h"
      9 #include "mozilla/dom/LocalStorageCommon.h"
     10 #include "mozilla/dom/StorageUtils.h"
     11 #include "mozilla/ipc/BackgroundUtils.h"
     12 #include "mozilla/ipc/PBackgroundSharedTypes.h"
     13 #include "nsCOMPtr.h"
     14 #include "nsContentUtils.h"
     15 #include "nsIURI.h"
     16 #include "nsNetUtil.h"
     17 
     18 using namespace mozilla;
     19 using namespace mozilla::dom;
     20 using namespace mozilla::dom::StorageUtils;
     21 using namespace mozilla::ipc;
     22 
     23 namespace {
     24 
     25 struct OriginKeyTest {
     26  const char* mSpec;
     27  const char* mOriginKey;
     28  const char* mQuotaKey;
     29 };
     30 
     31 already_AddRefed<nsIPrincipal> GetContentPrincipal(const char* aSpec) {
     32  nsCOMPtr<nsIURI> uri;
     33  nsresult rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(aSpec));
     34  if (NS_WARN_IF(NS_FAILED(rv))) {
     35    return nullptr;
     36  }
     37 
     38  OriginAttributes attrs;
     39 
     40  nsCOMPtr<nsIPrincipal> principal =
     41      BasePrincipal::CreateContentPrincipal(uri, attrs);
     42 
     43  return principal.forget();
     44 }
     45 
     46 void CheckGeneratedOriginKey(nsIPrincipal* aPrincipal, const char* aOriginKey,
     47                             const char* aQuotaKey) {
     48  nsCString originAttrSuffix;
     49  nsCString originKey;
     50  nsCString quotaKey;
     51 
     52  aPrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
     53 
     54  nsresult rv = aPrincipal->GetStorageOriginKey(originKey);
     55  if (aOriginKey) {
     56    ASSERT_EQ(rv, NS_OK) << "GetStorageOriginKey should not fail";
     57    EXPECT_TRUE(originKey == nsDependentCString(aOriginKey));
     58  } else {
     59    ASSERT_NE(rv, NS_OK) << "GetStorageOriginKey should fail";
     60  }
     61 
     62  rv = aPrincipal->GetLocalStorageQuotaKey(quotaKey);
     63  if (aQuotaKey) {
     64    ASSERT_EQ(rv, NS_OK) << "GetLocalStorageQuotaKey should not fail";
     65    EXPECT_TRUE(quotaKey == nsDependentCString(aQuotaKey));
     66  } else {
     67    ASSERT_NE(rv, NS_OK) << "GetLocalStorageQuotaKey should fail";
     68  }
     69 
     70  PrincipalInfo principalInfo;
     71  rv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo);
     72  ASSERT_EQ(rv, NS_OK) << "PrincipalToPrincipalInfo should not fail";
     73 
     74  const auto res = GenerateOriginKey2(principalInfo);
     75  if (aOriginKey) {
     76    ASSERT_TRUE(res.isOk())
     77    << "GenerateOriginKey2 should not fail";
     78    EXPECT_TRUE(res.inspect().second == nsDependentCString(aOriginKey));
     79  } else {
     80    ASSERT_TRUE(res.isErr())
     81    << "GenerateOriginKey2 should fail";
     82  }
     83 }
     84 
     85 }  // namespace
     86 
     87 TEST(LocalStorage, OriginKey)
     88 {
     89  // Check the system principal.
     90  nsCOMPtr<nsIScriptSecurityManager> secMan =
     91      nsContentUtils::GetSecurityManager();
     92  ASSERT_TRUE(secMan)
     93  << "GetSecurityManager() should not fail";
     94 
     95  nsCOMPtr<nsIPrincipal> principal;
     96  secMan->GetSystemPrincipal(getter_AddRefs(principal));
     97  ASSERT_TRUE(principal)
     98  << "GetSystemPrincipal() should not fail";
     99 
    100  CheckGeneratedOriginKey(principal, nullptr, nullptr);
    101 
    102  // Check the null principal.
    103  principal = NullPrincipal::CreateWithoutOriginAttributes();
    104  ASSERT_TRUE(principal)
    105  << "CreateWithoutOriginAttributes() should not fail";
    106 
    107  CheckGeneratedOriginKey(principal, nullptr, nullptr);
    108 
    109  // Check content principals.
    110  static const OriginKeyTest tests[] = {
    111      {"http://localhost", "tsohlacol.:http:80", ":tsohlacol."},
    112      {"http://www.mozilla.org", "gro.allizom.www.:http:80", ":gro.allizom."},
    113      {"https://www.mozilla.org", "gro.allizom.www.:https:443",
    114       ":gro.allizom."},
    115      {"http://www.mozilla.org:32400", "gro.allizom.www.:http:32400",
    116       ":gro.allizom."},
    117      {"file:///Users/Joe/Sites/", "/setiS/eoJ/sresU/.:file",
    118       ":/setiS/eoJ/sresU/."},
    119      {"file:///Users/Joe/Sites/#foo", "/setiS/eoJ/sresU/.:file",
    120       ":/setiS/eoJ/sresU/."},
    121      {"file:///Users/Joe/Sites/?foo", "/setiS/eoJ/sresU/.:file",
    122       ":/setiS/eoJ/sresU/."},
    123      {"file:///Users/Joe/Sites", "/eoJ/sresU/.:file", ":/eoJ/sresU/."},
    124      {"file:///Users/Joe/Sites#foo", "/eoJ/sresU/.:file", ":/eoJ/sresU/."},
    125      {"file:///Users/Joe/Sites?foo", "/eoJ/sresU/.:file", ":/eoJ/sresU/."},
    126      {"moz-extension://53711a8f-65ed-e742-9671-1f02e267c0bc/"
    127       "_generated_background_page.html",
    128       "cb0c762e20f1-1769-247e-de56-f8a11735.:moz-extension",
    129       ":cb0c762e20f1-1769-247e-de56-f8a11735."},
    130      {"http://[::1]:8/test.html", "1::.:http:8", ":1::."},
    131  };
    132 
    133  for (const auto& test : tests) {
    134    principal = GetContentPrincipal(test.mSpec);
    135    ASSERT_TRUE(principal)
    136    << "GetContentPrincipal() should not fail";
    137 
    138    CheckGeneratedOriginKey(principal, test.mOriginKey, test.mQuotaKey);
    139  }
    140 }