Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/security/test/gtest/TestSecureContext.cpp
Line
Count
Source (jump to first uncovered line)
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
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "gtest/gtest.h"
8
9
#include <string.h>
10
#include <stdlib.h>
11
12
#include "nsContentSecurityManager.h"
13
#include "nsContentUtils.h"
14
#include "nsIPrincipal.h"
15
#include "nsScriptSecurityManager.h"
16
#include "mozilla/NullPrincipal.h"
17
18
using namespace mozilla;
19
20
static const uint32_t kURIMaxLength = 64;
21
22
struct TestExpectations {
23
  char uri[kURIMaxLength ];
24
  bool expectedResult;
25
};
26
27
// ============================= TestDirectives ========================
28
29
TEST(SecureContext, IsOriginPotentiallyTrustworthyWithCodeBasePrincipal)
30
0
{
31
0
  //boolean isOriginPotentiallyTrustworthy(in nsIPrincipal aPrincipal);
32
0
33
0
  static const TestExpectations uris[] = {
34
0
    { "http://example.com/", false },
35
0
    { "https://example.com/", true },
36
0
    { "ws://example.com/", false },
37
0
    { "wss://example.com/", true },
38
0
    { "file:///xyzzy", true },
39
0
    { "ftp://example.com", false },
40
0
    { "about:config", false },
41
0
    { "http://localhost", true },
42
0
    { "http://xyzzy.localhost", false },
43
0
    { "http://127.0.0.1", true },
44
0
    { "resource://xyzzy", true },
45
0
    { "moz-extension://xyzzy", true },
46
0
    { "data:data:text/plain;charset=utf-8;base64,eHl6enk=", false },
47
0
    { "blob://unique-id", false },
48
0
    { "mailto:foo@bar.com", false },
49
0
    { "moz-icon://example.com", false },
50
0
    { "javascript:42", false },
51
0
  };
52
0
53
0
  uint32_t numExpectations = sizeof(uris) / sizeof(TestExpectations);
54
0
  nsCOMPtr<nsIContentSecurityManager> csManager = do_GetService(NS_CONTENTSECURITYMANAGER_CONTRACTID);
55
0
  ASSERT_TRUE(!!csManager);
56
0
57
0
  nsresult rv;
58
0
  for (uint32_t i = 0; i < numExpectations; i++) {
59
0
    nsCOMPtr<nsIPrincipal> prin;
60
0
    nsAutoCString uri(uris[i].uri);
61
0
    rv = nsScriptSecurityManager::GetScriptSecurityManager()->
62
0
           CreateCodebasePrincipalFromOrigin(uri, getter_AddRefs(prin));
63
0
    bool isPotentiallyTrustworthy = false;
64
0
    rv = csManager->IsOriginPotentiallyTrustworthy(prin, &isPotentiallyTrustworthy);
65
0
    ASSERT_EQ(NS_OK, rv);
66
0
    ASSERT_EQ(isPotentiallyTrustworthy, uris[i].expectedResult);
67
0
  }
68
0
}
69
70
TEST(SecureContext, IsOriginPotentiallyTrustworthyWithSystemPrincipal)
71
0
{
72
0
  RefPtr<nsScriptSecurityManager> ssManager = nsScriptSecurityManager::GetScriptSecurityManager();
73
0
  ASSERT_TRUE(!!ssManager);
74
0
  nsCOMPtr<nsIContentSecurityManager> csManager = do_GetService(NS_CONTENTSECURITYMANAGER_CONTRACTID);
75
0
  ASSERT_TRUE(!!csManager);
76
0
77
0
  nsCOMPtr<nsIPrincipal> sysPrin = nsContentUtils::GetSystemPrincipal();
78
0
  bool isPotentiallyTrustworthy;
79
0
  nsresult rv = csManager->IsOriginPotentiallyTrustworthy(sysPrin, &isPotentiallyTrustworthy);
80
0
  ASSERT_EQ(rv, NS_OK);
81
0
  ASSERT_TRUE(isPotentiallyTrustworthy);
82
0
}
83
84
TEST(SecureContext, IsOriginPotentiallyTrustworthyWithNullPrincipal)
85
0
{
86
0
  RefPtr<nsScriptSecurityManager> ssManager = nsScriptSecurityManager::GetScriptSecurityManager();
87
0
  ASSERT_TRUE(!!ssManager);
88
0
  nsCOMPtr<nsIContentSecurityManager> csManager = do_GetService(NS_CONTENTSECURITYMANAGER_CONTRACTID);
89
0
  ASSERT_TRUE(!!csManager);
90
0
91
0
  RefPtr<NullPrincipal> nullPrin = NullPrincipal::CreateWithoutOriginAttributes();
92
0
  bool isPotentiallyTrustworthy;
93
0
  nsresult rv = csManager->IsOriginPotentiallyTrustworthy(nullPrin, &isPotentiallyTrustworthy);
94
0
  ASSERT_EQ(rv, NS_OK);
95
0
  ASSERT_TRUE(!isPotentiallyTrustworthy);
96
0
}