user_cloud_external_data_manager_browsertest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6
7#include "base/bind.h"
8#include "base/callback.h"
9#include "base/files/file_path.h"
10#include "base/files/file_util.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/path_service.h"
13#include "base/run_loop.h"
14#include "base/values.h"
15#include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h"
16#include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
17#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
18#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
19#include "chrome/browser/policy/profile_policy_connector.h"
20#include "chrome/browser/policy/profile_policy_connector_factory.h"
21#include "chrome/browser/profiles/profile.h"
22#include "chrome/browser/ui/browser.h"
23#include "chrome/common/chrome_paths.h"
24#include "chrome/test/base/in_process_browser_test.h"
25#include "components/policy/core/common/cloud/cloud_policy_core.h"
26#include "components/policy/core/common/external_data_fetcher.h"
27#include "components/policy/core/common/policy_map.h"
28#include "components/policy/core/common/policy_service.h"
29#include "content/public/test/test_utils.h"
30#include "net/test/embedded_test_server/embedded_test_server.h"
31#include "policy/policy_constants.h"
32#include "testing/gtest/include/gtest/gtest.h"
33#include "url/gurl.h"
34
35namespace policy {
36
37namespace {
38
39const char kExternalDataPath[] = "policy/blank.html";
40
41}  // namespace
42
43typedef InProcessBrowserTest UserCloudExternalDataManagerTest;
44
45IN_PROC_BROWSER_TEST_F(UserCloudExternalDataManagerTest, FetchExternalData) {
46  CloudExternalDataManagerBase::SetMaxExternalDataSizeForTesting(1000);
47
48  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
49  const GURL url =
50      embedded_test_server()->GetURL(std::string("/") + kExternalDataPath);
51
52  base::FilePath test_dir;
53  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
54  std::string external_data;
55  ASSERT_TRUE(base::ReadFileToString(test_dir.AppendASCII(kExternalDataPath),
56                                     &external_data));
57  ASSERT_FALSE(external_data.empty());
58
59  scoped_ptr<base::DictionaryValue> metadata =
60      test::ConstructExternalDataReference(url.spec(), external_data);
61#if defined(OS_CHROMEOS)
62  UserCloudPolicyManagerChromeOS* policy_manager =
63      UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
64          browser()->profile());
65#else
66  UserCloudPolicyManager* policy_manager =
67      UserCloudPolicyManagerFactory::GetForBrowserContext(browser()->profile());
68#endif
69  ASSERT_TRUE(policy_manager);
70  // TODO(bartfab): The test injects an ExternalDataFetcher for an arbitrary
71  // policy. This is only done because there are no policies that reference
72  // external data yet. Once the first such policy is added, switch the test to
73  // that policy and stop injecting a manually instantiated ExternalDataFetcher.
74  test::SetExternalDataReference(policy_manager->core(),
75                                 key::kHomepageLocation,
76                                 make_scoped_ptr(metadata->DeepCopy()));
77  content::RunAllPendingInMessageLoop();
78
79  ProfilePolicyConnector* policy_connector =
80      ProfilePolicyConnectorFactory::GetForProfile(browser()->profile());
81  ASSERT_TRUE(policy_connector);
82  const PolicyMap& policies = policy_connector->policy_service()->GetPolicies(
83      PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
84  const PolicyMap::Entry* policy_entry = policies.Get(key::kHomepageLocation);
85  ASSERT_TRUE(policy_entry);
86  EXPECT_TRUE(base::Value::Equals(metadata.get(), policy_entry->value));
87  ASSERT_TRUE(policy_entry->external_data_fetcher);
88
89  base::RunLoop run_loop;
90  scoped_ptr<std::string> fetched_external_data;
91  policy_entry->external_data_fetcher->Fetch(base::Bind(
92      &test::ExternalDataFetchCallback,
93      &fetched_external_data,
94      run_loop.QuitClosure()));
95  run_loop.Run();
96
97  ASSERT_TRUE(fetched_external_data);
98  EXPECT_EQ(external_data, *fetched_external_data);
99}
100
101}  // namespace policy
102