caps/tests/gtest/TestOriginAttributes.cpp
author Liang-Heng Chen <xeonchen@gmail.com>
Wed, 04 Dec 2019 09:46:51 +0000
changeset 505289 6ee737ea06ad42c9c5a5bf06253b49ad16b204af
parent 501923 9f6892c03bd8dfcc604461ea90e71430c220fb74
permissions -rw-r--r--
Bug 1534339 - make OriginAttributes deserializable; r=baku `CreateSuffix` is irreversible by `PopulateFromSuffix` because it uses a multi-to-one mapping. Since only ':' will happen in a IPv6 format, we can make it a 1-to-1 mapping so that the `firstPartyDomain` is consistent after `CreateSuffix` and `PopulateFromSuffix`. Differential Revision: https://phabricator.services.mozilla.com/D47910

/* 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/Preferences.h"
#include "nsNetUtil.h"

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

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& fpd) {
  OriginAttributes attrs;
  nsCOMPtr<nsIURI> url;
  ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
  attrs.SetFirstPartyDomain(true, url);
  EXPECT_TRUE(attrs.mFirstPartyDomain.Equals(fpd));

  TestSuffix(attrs);
}

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

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

TEST(OriginAttributes, FirstPartyDomain_default)
{
  static const char prefKey[] = "privacy.firstparty.isolate";
  bool oldPref = Preferences::GetBool(prefKey);
  Preferences::SetBool(prefKey, true);
  TestFPD(NS_LITERAL_STRING("http://www.example.com"),
          NS_LITERAL_STRING("example.com"));
  TestFPD(NS_LITERAL_STRING("http://s3.amazonaws.com"),
          NS_LITERAL_STRING("s3.amazonaws.com"));
  TestFPD(NS_LITERAL_STRING("http://com"), NS_LITERAL_STRING("com"));
  TestFPD(NS_LITERAL_STRING("http://.com"), NS_LITERAL_STRING(""));
  TestFPD(NS_LITERAL_STRING("http://..com"), NS_LITERAL_STRING(""));
  TestFPD(NS_LITERAL_STRING("http://127.0.0.1"),
          NS_LITERAL_STRING("127.0.0.1"));
  TestFPD(NS_LITERAL_STRING("http://[::1]"), NS_LITERAL_STRING("[::1]"));
  Preferences::SetBool(prefKey, oldPref);
}