extensions/permissions/test/gtest/PermissionManagerTest.cpp
author Ehsan Akhgari <ehsan@mozilla.com>
Tue, 30 Apr 2019 14:06:53 +0000
changeset 530767 f3c896ea6e1693af54eb6515aad514869d68f057
parent 518356 extensions/cookie/test/gtest/PermissionManagerTest.cpp@c7260676a0f722ca69f7f99074679eb9f1f75cb7
child 542160 19acdaa55d35e394762897901955c92fd98fb4d8
permissions -rw-r--r--
Bug 1547397 - Part 3: Move the permission manager code to extensions/permissions; r=baku Differential Revision: https://phabricator.services.mozilla.com/D29116

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsNetUtil.h"
#include "nsPermissionManager.h"
#include "mozilla/OriginAttributes.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Unused.h"
#include "gtest/gtest.h"
#include "gtest/MozGTestBench.h"

using namespace mozilla;

class PermissionManager : public ::testing::Test {
 protected:
  PermissionManager()
      : mNonExistentType(
            NS_LITERAL_CSTRING("permissionTypeThatIsGuaranteedToNeverExist")) {}
  void SetUp() override {
    mPermissionManager = nsPermissionManager::GetInstance();
    nsCOMPtr<nsIURI> uri;
    nsresult rv = NS_NewURI(
        getter_AddRefs(uri),
        NS_LITERAL_CSTRING("https://test.origin.with.subdomains.example.com"));
    MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
    mPrincipal = mozilla::BasePrincipal::CreateCodebasePrincipal(
        uri, OriginAttributes());
  }

  void TearDown() override {
    mPermissionManager = nullptr;
    mPrincipal = nullptr;
  }

  static const unsigned kNumIterations = 100000;

  nsLiteralCString mNonExistentType;
  RefPtr<nsPermissionManager> mPermissionManager;
  nsCOMPtr<nsIPrincipal> mPrincipal;
};

MOZ_GTEST_BENCH_F(PermissionManager, TestNonExistentPermissionFromPrincipal,
                  [this] {
                    for (unsigned i = 0; i < kNumIterations; ++i) {
                      uint32_t result = 0;
                      Unused << mPermissionManager->TestPermissionFromPrincipal(
                          mPrincipal, mNonExistentType, &result);
                    }
                  });