tor-browser

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

TestOriginAttributes.cpp (4039B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 #include "gtest/gtest.h"
      5 #include "mozilla/BasePrincipal.h"
      6 #include "mozilla/NullPrincipal.h"
      7 #include "mozilla/Preferences.h"
      8 #include "nsNetUtil.h"
      9 
     10 using mozilla::OriginAttributes;
     11 using mozilla::Preferences;
     12 
     13 #define FPI_PREF "privacy.firstparty.isolate"
     14 #define SITE_PREF "privacy.firstparty.isolate.use_site"
     15 
     16 #define TEST_FPD(_spec, _expected) \
     17  TestFPD(nsLiteralString(_spec), nsLiteralString(_expected))
     18 
     19 namespace mozilla {
     20 
     21 static void TestSuffix(const OriginAttributes& attrs) {
     22  nsAutoCString suffix;
     23  attrs.CreateSuffix(suffix);
     24 
     25  OriginAttributes attrsFromSuffix;
     26  bool success = attrsFromSuffix.PopulateFromSuffix(suffix);
     27  EXPECT_TRUE(success);
     28 
     29  EXPECT_EQ(attrs, attrsFromSuffix);
     30 }
     31 
     32 static void TestFPD(const nsAString& spec, const nsAString& expected) {
     33  OriginAttributes attrs;
     34  nsCOMPtr<nsIURI> url;
     35  ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     36  attrs.SetFirstPartyDomain(true, url);
     37  EXPECT_TRUE(attrs.mFirstPartyDomain.Equals(expected));
     38 
     39  TestSuffix(attrs);
     40 }
     41 
     42 TEST(OriginAttributes, Suffix_default)
     43 {
     44  OriginAttributes attrs;
     45  TestSuffix(attrs);
     46 }
     47 
     48 TEST(OriginAttributes, FirstPartyDomain_default)
     49 {
     50  bool oldFpiPref = Preferences::GetBool(FPI_PREF);
     51  Preferences::SetBool(FPI_PREF, true);
     52  bool oldSitePref = Preferences::GetBool(SITE_PREF);
     53  Preferences::SetBool(SITE_PREF, false);
     54 
     55  TEST_FPD(u"http://www.example.com", u"example.com");
     56  TEST_FPD(u"http://www.example.com:80", u"example.com");
     57  TEST_FPD(u"http://www.example.com:8080", u"example.com");
     58  TEST_FPD(u"http://s3.amazonaws.com", u"s3.amazonaws.com");
     59  TEST_FPD(u"http://com", u"com");
     60  TEST_FPD(u"http://com.", u"com.");
     61  TEST_FPD(u"http://com:8080", u"com");
     62  TEST_FPD(u"http://.com", u"");
     63  TEST_FPD(u"http://..com", u"");
     64  TEST_FPD(u"http://127.0.0.1", u"127.0.0.1");
     65  TEST_FPD(u"http://[::1]", u"[::1]");
     66  TEST_FPD(u"about:config",
     67           u"about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla");
     68  TEST_FPD(u"moz-extension://f5b6ca10-5bd4-4ed6-9baf-820dc5152bc1", u"");
     69  TEST_FPD(u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}",
     70           u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");
     71  TEST_FPD(
     72      u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}"
     73      u"?https://www.example.com",
     74      u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");
     75 
     76  Preferences::SetBool(FPI_PREF, oldFpiPref);
     77  Preferences::SetBool(SITE_PREF, oldSitePref);
     78 }
     79 
     80 TEST(OriginAttributes, FirstPartyDomain_site)
     81 {
     82  bool oldFpiPref = Preferences::GetBool(FPI_PREF);
     83  Preferences::SetBool(FPI_PREF, true);
     84  bool oldSitePref = Preferences::GetBool(SITE_PREF);
     85  Preferences::SetBool(SITE_PREF, true);
     86 
     87  TEST_FPD(u"http://www.example.com", u"(http,example.com)");
     88  TEST_FPD(u"http://www.example.com:80", u"(http,example.com)");
     89  TEST_FPD(u"http://www.example.com:8080", u"(http,example.com)");
     90  TEST_FPD(u"http://s3.amazonaws.com", u"(http,s3.amazonaws.com)");
     91  TEST_FPD(u"http://com", u"(http,com)");
     92  TEST_FPD(u"http://com.", u"(http,com.)");
     93  TEST_FPD(u"http://com:8080", u"(http,com)");
     94  TEST_FPD(u"http://.com", u"(http,.com)");
     95  TEST_FPD(u"http://..com", u"(http,..com)");
     96  TEST_FPD(u"http://127.0.0.1", u"(http,127.0.0.1)");
     97  TEST_FPD(u"http://[::1]", u"(http,[::1])");
     98  TEST_FPD(u"about:config",
     99           u"(about,about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla)");
    100  TEST_FPD(u"moz-extension://f5b6ca10-5bd4-4ed6-9baf-820dc5152bc1", u"");
    101  TEST_FPD(u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}",
    102           u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");
    103  TEST_FPD(
    104      u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}"
    105      u"?https://www.example.com",
    106      u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");
    107 
    108  Preferences::SetBool(FPI_PREF, oldFpiPref);
    109  Preferences::SetBool(SITE_PREF, oldSitePref);
    110 }
    111 
    112 }  // namespace mozilla