File: settings_override_permission_unittest.cc

package info (click to toggle)
chromium-browser 37.0.2062.120-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,707,260 kB
  • sloc: cpp: 8,976,677; ansic: 3,473,199; python: 586,578; asm: 449,013; xml: 184,195; java: 142,924; sh: 118,496; perl: 81,467; makefile: 27,557; yacc: 10,506; objc: 8,886; tcl: 3,186; cs: 2,252; lex: 2,213; sql: 1,198; pascal: 1,170; lisp: 790; awk: 407; ruby: 155; php: 83; sed: 52; exp: 11
file content (173 lines) | stat: -rw-r--r-- 7,102 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// These tests make sure SettingsOverridePermission values are set correctly.

#include "base/basictypes.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permission_set.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/permissions/settings_override_permission.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace {

class SettingsOverridePermissionTest : public ExtensionManifestTest {
 protected:
  enum Flags {
    kHomepage = 1,
    kStartupPages = 1 << 1,
    kSearchProvider = 1 << 2,
  };

  scoped_refptr<Extension> GetPermissionSet(uint32 flags) {
    base::DictionaryValue ext_manifest;
    ext_manifest.SetString(manifest_keys::kName, "test");
    ext_manifest.SetString(manifest_keys::kVersion, "0.1");
    ext_manifest.SetInteger(manifest_keys::kManifestVersion, 2);

    scoped_ptr<base::DictionaryValue> settings_override(
        new base::DictionaryValue);
    if (flags & kHomepage)
      settings_override->SetString("homepage", "http://www.google.com");
    if (flags & kStartupPages) {
      scoped_ptr<base::ListValue> startup_pages(new base::ListValue);
      startup_pages->AppendString("http://startup.com/startup.html");
      settings_override->Set("startup_pages", startup_pages.release());
    }
    if (flags & kSearchProvider) {
      scoped_ptr<base::DictionaryValue> search_provider(
          new base::DictionaryValue);
      search_provider->SetString("search_url", "http://google.com/search.html");
      search_provider->SetString("name", "test");
      search_provider->SetString("keyword", "lock");
      search_provider->SetString("encoding", "UTF-8");
      search_provider->SetBoolean("is_default", true);
      search_provider->SetString("favicon_url",
                                 "http://wikipedia.org/wiki/Favicon");
      settings_override->Set("search_provider", search_provider.release());
    }
    ext_manifest.Set(
        manifest_keys::kSettingsOverride, settings_override.release());

    Manifest manifest(&ext_manifest, "test");
    return LoadAndExpectSuccess(manifest);
  }
};

TEST_F(SettingsOverridePermissionTest, HomePage) {
  scoped_refptr<Extension> extension(GetPermissionSet(kHomepage));
  scoped_refptr<const PermissionSet> permission_set(
      extension->permissions_data()->active_permissions());

#if defined(OS_WIN)
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  std::vector<base::string16> warnings =
      extension->permissions_data()->GetPermissionMessageStrings();
  ASSERT_EQ(1u, warnings.size());
  EXPECT_EQ("Change your home page to: google.com/",
            base::UTF16ToUTF8(warnings[0]));

  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  EXPECT_FALSE(permission_set->HasAPIPermission(
      APIPermission::kSearchProvider));
#else
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  EXPECT_FALSE(
      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
#endif
}

TEST_F(SettingsOverridePermissionTest, StartupPages) {
  scoped_refptr<Extension> extension(GetPermissionSet(kStartupPages));
  scoped_refptr<const PermissionSet> permission_set(
      extension->permissions_data()->active_permissions());

#if defined(OS_WIN)
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  std::vector<base::string16> warnings =
      extension->permissions_data()->GetPermissionMessageStrings();
  ASSERT_EQ(1u, warnings.size());
  EXPECT_EQ("Change your start page to: startup.com/startup.html",
            base::UTF16ToUTF8(warnings[0]));

  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(
      APIPermission::kSearchProvider));
#else
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  EXPECT_FALSE(
      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
#endif
}

TEST_F(SettingsOverridePermissionTest, SearchSettings) {
  scoped_refptr<Extension> extension(GetPermissionSet(kSearchProvider));
  scoped_refptr<const PermissionSet> permission_set(
      extension->permissions_data()->active_permissions());

#if defined(OS_WIN)
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
  std::vector<base::string16> warnings =
      extension->permissions_data()->GetPermissionMessageStrings();
  ASSERT_EQ(1u, warnings.size());
  EXPECT_EQ("Change your search settings to: google.com",
            base::UTF16ToUTF8(warnings[0]));

  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
#else
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  EXPECT_FALSE(
      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
#endif
}

TEST_F(SettingsOverridePermissionTest, All) {
  scoped_refptr<Extension> extension(GetPermissionSet(
      kSearchProvider | kStartupPages | kHomepage));
  scoped_refptr<const PermissionSet> permission_set(
      extension->permissions_data()->active_permissions());

#if defined(OS_WIN)
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
#else
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  EXPECT_FALSE(
      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
#endif
}

TEST_F(SettingsOverridePermissionTest, Some) {
  scoped_refptr<Extension> extension(GetPermissionSet(
      kSearchProvider | kHomepage));
  scoped_refptr<const PermissionSet> permission_set(
      extension->permissions_data()->active_permissions());

#if defined(OS_WIN)
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
#else
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
  EXPECT_FALSE(
      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
#endif
}

}  // namespace

}  // namespace extensions