summaryrefslogtreecommitdiffstats
path: root/caps/tests/gtest/TestOriginAttributes.cpp
blob: fa759f80d5eea2ef68ec5b0af231b8b229c76108 (plain)
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
/* 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 "mozilla/BasePrincipal.h"
#include "mozilla/NullPrincipal.h"
#include "mozilla/Preferences.h"
#include "nsNetUtil.h"

using mozilla::OriginAttributes;
using mozilla::Preferences;

#define FPI_PREF "privacy.firstparty.isolate"
#define SITE_PREF "privacy.firstparty.isolate.use_site"

#define TEST_FPD(_spec, _expected) \
  TestFPD(nsLiteralString(_spec), nsLiteralString(_expected))

namespace mozilla {

static void TestSuffix(const OriginAttributes& attrs) {
  nsAutoCString suffix;
  attrs.CreateSuffix(suffix);

  OriginAttributes attrsFromSuffix;
  bool success = attrsFromSuffix.PopulateFromSuffix(suffix);
  EXPECT_TRUE(success);

  EXPECT_EQ(attrs, attrsFromSuffix);
}

static void TestFPD(const nsAString& spec, const nsAString& expected) {
  OriginAttributes attrs;
  nsCOMPtr<nsIURI> url;
  ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
  attrs.SetFirstPartyDomain(true, url);
  EXPECT_TRUE(attrs.mFirstPartyDomain.Equals(expected));

  TestSuffix(attrs);
}

TEST(OriginAttributes, Suffix_default)
{
  OriginAttributes attrs;
  TestSuffix(attrs);
}

TEST(OriginAttributes, Suffix_inIsolatedMozBrowser)
{
  OriginAttributes attrs(true);
  TestSuffix(attrs);
}

TEST(OriginAttributes, FirstPartyDomain_default)
{
  bool oldFpiPref = Preferences::GetBool(FPI_PREF);
  Preferences::SetBool(FPI_PREF, true);
  bool oldSitePref = Preferences::GetBool(SITE_PREF);
  Preferences::SetBool(SITE_PREF, false);

  TEST_FPD(u"http://www.example.com", u"example.com");
  TEST_FPD(u"http://www.example.com:80", u"example.com");
  TEST_FPD(u"http://www.example.com:8080", u"example.com");
  TEST_FPD(u"http://s3.amazonaws.com", u"s3.amazonaws.com");
  TEST_FPD(u"http://com", u"com");
  TEST_FPD(u"http://com.", u"com.");
  TEST_FPD(u"http://com:8080", u"com");
  TEST_FPD(u"http://.com", u"");
  TEST_FPD(u"http://..com", u"");
  TEST_FPD(u"http://127.0.0.1", u"127.0.0.1");
  TEST_FPD(u"http://[::1]", u"[::1]");
  TEST_FPD(u"about:config",
           u"about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla");
  TEST_FPD(u"moz-extension://f5b6ca10-5bd4-4ed6-9baf-820dc5152bc1", u"");
  TEST_FPD(u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}",
           u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");
  TEST_FPD(
      u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}"
      u"?https://www.example.com",
      u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");

  Preferences::SetBool(FPI_PREF, oldFpiPref);
  Preferences::SetBool(SITE_PREF, oldSitePref);
}

TEST(OriginAttributes, FirstPartyDomain_site)
{
  bool oldFpiPref = Preferences::GetBool(FPI_PREF);
  Preferences::SetBool(FPI_PREF, true);
  bool oldSitePref = Preferences::GetBool(SITE_PREF);
  Preferences::SetBool(SITE_PREF, true);

  TEST_FPD(u"http://www.example.com", u"(http,example.com)");
  TEST_FPD(u"http://www.example.com:80", u"(http,example.com)");
  TEST_FPD(u"http://www.example.com:8080", u"(http,example.com)");
  TEST_FPD(u"http://s3.amazonaws.com", u"(http,s3.amazonaws.com)");
  TEST_FPD(u"http://com", u"(http,com)");
  TEST_FPD(u"http://com.", u"(http,com.)");
  TEST_FPD(u"http://com:8080", u"(http,com,8080)");
  TEST_FPD(u"http://.com", u"(http,.com)");
  TEST_FPD(u"http://..com", u"(http,..com)");
  TEST_FPD(u"http://127.0.0.1", u"(http,127.0.0.1)");
  TEST_FPD(u"http://[::1]", u"(http,[::1])");
  TEST_FPD(u"about:config",
           u"(about,about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla)");
  TEST_FPD(u"moz-extension://f5b6ca10-5bd4-4ed6-9baf-820dc5152bc1", u"");
  TEST_FPD(u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}",
           u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");
  TEST_FPD(
      u"moz-nullprincipal:{9bebdabb-828a-4284-8b00-432a968c6e42}"
      u"?https://www.example.com",
      u"9bebdabb-828a-4284-8b00-432a968c6e42.mozilla");

  Preferences::SetBool(FPI_PREF, oldFpiPref);
  Preferences::SetBool(SITE_PREF, oldSitePref);
}

}  // namespace mozilla