Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_apitest.cc

Issue 1306183007: Switch to id-based affiliation determination (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
Polina Bondarenko 2015/09/23 14:36:04 Do you use the PrefService anywhere? I don't see i
peletskyi 2015/09/25 12:15:58 local_state() call requires this include.
5 #include "base/prefs/pref_service.h"
5 #include "base/strings/stringprintf.h" 6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chromeos/policy/affiliation_test_helper.h"
6 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 9 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
7 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" 10 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
8 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" 11 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
9 #include "chrome/browser/extensions/extension_apitest.h" 12 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/net/url_request_mock_util.h" 13 #include "chrome/browser/net/url_request_mock_util.h"
14 #include "chrome/test/base/ui_test_utils.h"
11 #include "chromeos/dbus/fake_session_manager_client.h" 15 #include "chromeos/dbus/fake_session_manager_client.h"
12 #include "chromeos/login/user_names.h" 16 #include "chromeos/login/user_names.h"
13 #include "components/policy/core/common/mock_configuration_policy_provider.h" 17 #include "components/policy/core/common/mock_configuration_policy_provider.h"
18 #include "components/user_manager/user_manager.h"
14 #include "content/public/browser/notification_service.h" 19 #include "content/public/browser/notification_service.h"
15 #include "content/public/test/test_utils.h" 20 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/api_test_utils.h" 21 #include "extensions/browser/api_test_utils.h"
17 #include "extensions/browser/extension_registry.h" 22 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/test_extension_registry_observer.h" 23 #include "extensions/browser/test_extension_registry_observer.h"
24 #include "extensions/test/result_catcher.h"
19 #include "net/test/url_request/url_request_mock_http_job.h" 25 #include "net/test/url_request/url_request_mock_http_job.h"
20 #include "policy/policy_constants.h" 26 #include "policy/policy_constants.h"
21 27
22 namespace { 28 namespace {
23 29
24 const char kDeviceId[] = "device_id"; 30 const char kDeviceId[] = "device_id";
25 const base::FilePath::CharType kTestExtensionDir[] = 31 const base::FilePath::CharType kTestExtensionDir[] =
26 FILE_PATH_LITERAL("extensions/api_test/enterprise_device_attributes"); 32 FILE_PATH_LITERAL("extensions/api_test/enterprise_device_attributes");
27 const base::FilePath::CharType kUpdateManifestFileName[] = 33 const base::FilePath::CharType kUpdateManifestFileName[] =
28 FILE_PATH_LITERAL("update_manifest.xml"); 34 FILE_PATH_LITERAL("update_manifest.xml");
29 35
36 const char kAffiliatedUser[] = "user@example.com";
37 const char kAffiliationID[] = "some-affiliation-id";
38 const char kAnotherAffiliationID[] = "another-affiliation-id";
39
30 // The managed_storage extension has a key defined in its manifest, so that 40 // The managed_storage extension has a key defined in its manifest, so that
31 // its extension ID is well-known and the policy system can push policies for 41 // its extension ID is well-known and the policy system can push policies for
32 // the extension. 42 // the extension.
33 const char kTestExtensionID[] = "nbiliclbejdndfpchgkbmfoppjplbdok"; 43 const char kTestExtensionID[] = "nbiliclbejdndfpchgkbmfoppjplbdok";
34 44
45 struct Params {
46 explicit Params(bool affiliated) : affiliated_(affiliated) {}
47 bool affiliated_;
48 };
49
35 } // namespace 50 } // namespace
36 51
37 namespace extensions { 52 namespace extensions {
38 53
39 class EnterpriseDeviceAttributesTest : public ExtensionApiTest { 54 class EnterpriseDeviceAttributesTest :
55 public ExtensionApiTest,
56 public ::testing::WithParamInterface<Params> {
40 public: 57 public:
41 explicit EnterpriseDeviceAttributesTest(const std::string& domain) 58 EnterpriseDeviceAttributesTest() {
42 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient), 59 set_exit_when_last_browser_closes(false);
43 test_domain_(domain) {} 60 set_chromeos_user_ = false;
61 }
44 62
45 protected: 63 protected:
64 // ExtensionApiTest
65 void SetUpCommandLine(base::CommandLine* command_line) override {
66 ExtensionApiTest::SetUpCommandLine(command_line);
67 chromeos::affiliation_test_helper::
68 AppendCommandLineSwitchesForLoginManager(command_line);
69 }
70
46 void SetUpInProcessBrowserTestFixture() override { 71 void SetUpInProcessBrowserTestFixture() override {
72 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
73
74 chromeos::FakeSessionManagerClient* fake_session_manager_client =
75 new chromeos::FakeSessionManagerClient;
47 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( 76 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
48 make_scoped_ptr(fake_session_manager_client_)); 77 scoped_ptr<chromeos::SessionManagerClient>(
49 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); 78 fake_session_manager_client));
79
80 std::set<std::string> device_affiliation_ids;
81 device_affiliation_ids.insert(kAffiliationID);
82 chromeos::affiliation_test_helper::SetDeviceAffiliationID(
83 &test_helper_, fake_session_manager_client, device_affiliation_ids);
84
85 std::set<std::string> user_affiliation_ids;
86 if (GetParam().affiliated_) {
87 user_affiliation_ids.insert(kAffiliationID);
88 } else {
89 user_affiliation_ids.insert(kAnotherAffiliationID);
90 }
91 policy::UserPolicyBuilder user_policy;
92 chromeos::affiliation_test_helper::SetUserAffiliationIDs(
93 &user_policy, fake_session_manager_client, kAffiliatedUser,
94 user_affiliation_ids);
50 95
51 // Set up fake install attributes. 96 // Set up fake install attributes.
52 scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes( 97 scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes(
53 new policy::StubEnterpriseInstallAttributes()); 98 new policy::StubEnterpriseInstallAttributes());
54 99
55 attributes->SetDomain(test_domain_); 100 attributes->SetRegistrationUser(kAffiliatedUser);
56 attributes->SetRegistrationUser(chromeos::login::kStubUser);
57 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting( 101 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
58 attributes.release()); 102 attributes.release());
59 103
60 test_helper_.InstallOwnerKey(); 104 test_helper_.InstallOwnerKey();
61 // Init the device policy. 105 // Init the device policy.
62 policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy(); 106 policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy();
63 device_policy->SetDefaultSigningKey(); 107 device_policy->SetDefaultSigningKey();
64 device_policy->policy_data().set_directory_api_id(kDeviceId); 108 device_policy->policy_data().set_directory_api_id(kDeviceId);
65 device_policy->Build(); 109 device_policy->Build();
66 110
67 fake_session_manager_client_->set_device_policy(device_policy->GetBlob()); 111 fake_session_manager_client->set_device_policy(device_policy->GetBlob());
68 fake_session_manager_client_->OnPropertyChangeComplete(true); 112 fake_session_manager_client->OnPropertyChangeComplete(true);
69 113
70 // Init the user policy provider. 114 // Init the user policy provider.
71 EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_)) 115 EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
72 .WillRepeatedly(testing::Return(true)); 116 .WillRepeatedly(testing::Return(true));
73 policy_provider_.SetAutoRefresh(); 117 policy_provider_.SetAutoRefresh();
74 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( 118 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
75 &policy_provider_); 119 &policy_provider_);
76 } 120 }
77 121
78 void SetUpOnMainThread() override { 122 void SetUpOnMainThread() override {
123 const base::ListValue* users =
124 g_browser_process->local_state()->GetList("LoggedInUsers");
125 if (!users->empty()) {
126 chromeos::affiliation_test_helper::LoginUser(kAffiliatedUser);
127 }
128
79 ExtensionApiTest::SetUpOnMainThread(); 129 ExtensionApiTest::SetUpOnMainThread();
80
81 // Enable the URLRequestMock, which is required for force-installing the
82 // test extension through policy.
83 content::BrowserThread::PostTask(
84 content::BrowserThread::IO, FROM_HERE,
85 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
86
87 SetPolicy();
88 } 130 }
89 131
90 private:
91 void SetPolicy() { 132 void SetPolicy() {
92 // Extensions that are force-installed come from an update URL, which 133 // Extensions that are force-installed come from an update URL, which
93 // defaults to the webstore. Use a mock URL for this test with an update 134 // defaults to the webstore. Use a mock URL for this test with an update
94 // manifest that includes the crx file of the test extension. 135 // manifest that includes the crx file of the test extension.
95 base::FilePath update_manifest_path = 136 base::FilePath update_manifest_path =
96 base::FilePath(kTestExtensionDir).Append(kUpdateManifestFileName); 137 base::FilePath(kTestExtensionDir).Append(kUpdateManifestFileName);
97 GURL update_manifest_url( 138 GURL update_manifest_url(
98 net::URLRequestMockHTTPJob::GetMockUrl(update_manifest_path)); 139 net::URLRequestMockHTTPJob::GetMockUrl(update_manifest_path));
99 140
100 scoped_ptr<base::ListValue> forcelist(new base::ListValue); 141 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
101 forcelist->AppendString(base::StringPrintf( 142 forcelist->AppendString(base::StringPrintf(
102 "%s;%s", kTestExtensionID, update_manifest_url.spec().c_str())); 143 "%s;%s", kTestExtensionID, update_manifest_url.spec().c_str()));
103 144
104 policy::PolicyMap policy; 145 policy::PolicyMap policy;
105 policy.Set(policy::key::kExtensionInstallForcelist, 146 policy.Set(policy::key::kExtensionInstallForcelist,
106 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE, 147 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
107 forcelist.release(), NULL); 148 forcelist.release(), NULL);
108 149
109 // Set the policy and wait until the extension is installed. 150 // Set the policy and wait until the extension is installed.
110 extensions::TestExtensionRegistryObserver observer( 151 extensions::TestExtensionRegistryObserver observer(
111 ExtensionRegistry::Get(profile())); 152 ExtensionRegistry::Get(profile()));
112 policy_provider_.UpdateChromePolicy(policy); 153 policy_provider_.UpdateChromePolicy(policy);
113 observer.WaitForExtensionLoaded(); 154 observer.WaitForExtensionLoaded();
114 } 155 }
115 156
116 chromeos::FakeSessionManagerClient* const fake_session_manager_client_; 157 // Load |page_url| in |browser| and wait for PASSED or FAILED notification.
158 // The functionality of this function is reduced functionality of
159 // RunExtensionSubtest(), but we don't use it here because it requires
160 // function InProcessBrowserTest::browser() to return non-NULL pointer.
161 // Unfortunately it returns the value which is set in constructor and can't be
162 // modified. Because on login flow there is no browser, the function
163 // InProcessBrowserTest::browser() always returns NULL. Besides this we need
164 // only very little functionality from RunExtensionSubtest(). Thus so that
165 // don't make RunExtensionSubtest() to complex we just introduce a new
166 // function.
167 bool TestExtension(Browser* browser, const std::string& page_url) {
Polina Bondarenko 2015/09/23 14:36:04 /s/<<"/<< "
peletskyi 2015/09/25 12:15:58 Done.
168 DCHECK(!page_url.empty()) <<"page_url cannot be empty";
169
170 extensions::ResultCatcher catcher;
171 ui_test_utils::NavigateToURL(browser, GURL(page_url));
172
173 if (!catcher.GetNextResult()) {
174 message_ = catcher.message();
175 return false;
176 }
177 return true;
178 }
179
180 private:
117 policy::MockConfigurationPolicyProvider policy_provider_; 181 policy::MockConfigurationPolicyProvider policy_provider_;
118 policy::DevicePolicyCrosTestHelper test_helper_; 182 policy::DevicePolicyCrosTestHelper test_helper_;
119 const std::string test_domain_;
120 }; 183 };
121 184
122 // Creates affiliated user before browser initializes. 185 IN_PROC_BROWSER_TEST_P(EnterpriseDeviceAttributesTest, PRE_Success) {
123 class EnterpriseDeviceAttributesAffiliatedTest 186 chromeos::affiliation_test_helper::PreLoginUser(kAffiliatedUser);
124 : public EnterpriseDeviceAttributesTest { 187 }
125 public:
126 EnterpriseDeviceAttributesAffiliatedTest()
127 : EnterpriseDeviceAttributesTest("gmail.com") {}
128 };
129 188
Polina Bondarenko 2015/09/23 14:36:04 I'd add a comment that the both affiliated and non
peletskyi 2015/09/25 12:15:58 Done. Inserted the comment before INSTANTIATE_TEST
130 // Creates non-affiliated user before browser init. 189 IN_PROC_BROWSER_TEST_P(EnterpriseDeviceAttributesTest, Success) {
131 class EnterpriseDeviceAttributesNonAffiliatedTest 190 content::BrowserThread::PostTask(
132 : public EnterpriseDeviceAttributesTest { 191 content::BrowserThread::IO, FROM_HERE,
133 public: 192 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
134 EnterpriseDeviceAttributesNonAffiliatedTest()
135 : EnterpriseDeviceAttributesTest("example.com") {}
136 };
137 193
138 // Tests the case of an affiliated user and pre-installed extension. Fetches 194 SetPolicy();
139 // the valid cloud directory device id. 195
140 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Success) { 196 EXPECT_EQ(GetParam().affiliated_, user_manager::UserManager::Get()
197 ->FindUser(kAffiliatedUser)
198 ->is_affiliated());
199
200 // Device ID is available only for affiliated user.
201 std::string device_id = GetParam().affiliated_ ? kDeviceId : "";
202
141 // Pass the expected value (device_id) to test. 203 // Pass the expected value (device_id) to test.
142 ASSERT_TRUE(RunExtensionSubtest( 204 ASSERT_TRUE(TestExtension(CreateBrowser(profile()),
143 "", base::StringPrintf("chrome-extension://%s/basic.html?%s", 205 base::StringPrintf("chrome-extension://%s/basic.html?%s",
144 kTestExtensionID, kDeviceId))) 206 kTestExtensionID, device_id.c_str())))
145 << message_; 207 << message_;
146 } 208 }
147 209
148 // Test the case of non-affiliated user and pre-installed by policy extension.
149 // Extension API is available, but fetches the empty string.
150 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest,
151 EmptyString) {
152 // Pass the expected value (empty string) to test.
153 ASSERT_TRUE(RunExtensionSubtest(
154 "", base::StringPrintf("chrome-extension://%s/basic.html?%s",
155 kTestExtensionID, "")))
156 << message_;
157 }
158
159 // Ensure that extensions that are not pre-installed by policy throw an install 210 // Ensure that extensions that are not pre-installed by policy throw an install
160 // warning if they request the enterprise.deviceAttributes permission in the 211 // warning if they request the enterprise.deviceAttributes permission in the
161 // manifest and that such extensions don't see the 212 // manifest and that such extensions don't see the
162 // chrome.enterprise.deviceAttributes namespace. 213 // chrome.enterprise.deviceAttributes namespace.
163 IN_PROC_BROWSER_TEST_F( 214 IN_PROC_BROWSER_TEST_F(
164 ExtensionApiTest, 215 ExtensionApiTest,
165 EnterpriseDeviceAttributesIsRestrictedToPolicyExtension) { 216 EnterpriseDeviceAttributesIsRestrictedToPolicyExtension) {
166 ASSERT_TRUE(RunExtensionSubtest("enterprise_device_attributes", 217 ASSERT_TRUE(RunExtensionSubtest("enterprise_device_attributes",
167 "api_not_available.html", 218 "api_not_available.html",
168 kFlagIgnoreManifestWarnings)); 219 kFlagIgnoreManifestWarnings));
169 220
170 base::FilePath extension_path = 221 base::FilePath extension_path =
171 test_data_dir_.AppendASCII("enterprise_device_attributes"); 222 test_data_dir_.AppendASCII("enterprise_device_attributes");
172 extensions::ExtensionRegistry* registry = 223 extensions::ExtensionRegistry* registry =
173 extensions::ExtensionRegistry::Get(profile()); 224 extensions::ExtensionRegistry::Get(profile());
174 const extensions::Extension* extension = 225 const extensions::Extension* extension =
175 GetExtensionByPath(registry->enabled_extensions(), extension_path); 226 GetExtensionByPath(registry->enabled_extensions(), extension_path);
176 ASSERT_FALSE(extension->install_warnings().empty()); 227 ASSERT_FALSE(extension->install_warnings().empty());
177 EXPECT_EQ( 228 EXPECT_EQ(
178 "'enterprise.deviceAttributes' is not allowed for specified install " 229 "'enterprise.deviceAttributes' is not allowed for specified install "
179 "location.", 230 "location.",
180 extension->install_warnings()[0].message); 231 extension->install_warnings()[0].message);
181 } 232 }
182 233
234 INSTANTIATE_TEST_CASE_P(AffiliationCheck,
235 EnterpriseDeviceAttributesTest,
236 ::testing::Values(Params(true), Params(false)));
183 } // namespace extensions 237 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698