• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/base64.h"
6 #include "base/json/json_reader.h"
7 #include "base/test/test_simple_task_runner.h"
8 #include "base/values.h"
9 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
10 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
11 #include "components/policy/core/common/cloud/policy_header_service.h"
12 #include "net/http/http_request_headers.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace policy {
17 using enterprise_management::PolicyData;
18 
19 namespace {
20 const char kDMServerURL[] = "http://server_url";
21 const char kPolicyHeaderName[] = "Chrome-Policy-Posture";
22 
23 class TestCloudPolicyStore : public MockCloudPolicyStore {
24  public:
SetPolicy(scoped_ptr<PolicyData> policy)25   void SetPolicy(scoped_ptr<PolicyData> policy) {
26     policy_ = policy.Pass();
27     // Notify observers.
28     NotifyStoreLoaded();
29   }
30 };
31 
32 class PolicyHeaderServiceTest : public testing::Test {
33  public:
PolicyHeaderServiceTest()34   PolicyHeaderServiceTest() {
35     task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner());
36   }
~PolicyHeaderServiceTest()37   virtual ~PolicyHeaderServiceTest() {}
38 
SetUp()39   virtual void SetUp() OVERRIDE {
40     service_.reset(new PolicyHeaderService(
41         kDMServerURL, &user_store_, &device_store_));
42     helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass();
43   }
44 
TearDown()45   virtual void TearDown() OVERRIDE {
46     task_runner_->RunUntilIdle();
47     // Helper should outlive the service.
48     service_.reset();
49     helper_.reset();
50   }
51 
ValidateHeader(const net::HttpRequestHeaders & headers,const std::string & expected_dmtoken)52   void ValidateHeader(const net::HttpRequestHeaders& headers,
53                       const std::string& expected_dmtoken) {
54     if (expected_dmtoken.empty()) {
55       EXPECT_TRUE(headers.IsEmpty());
56     } else {
57       // Read header.
58       std::string header;
59       EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header));
60       // Decode the base64 value into JSON.
61       std::string decoded;
62       base::Base64Decode(header, &decoded);
63       // Parse the JSON.
64       scoped_ptr<Value> value(base::JSONReader::Read(decoded));
65       ASSERT_TRUE(value);
66       DictionaryValue* dict;
67       EXPECT_TRUE(value->GetAsDictionary(&dict));
68       // Read the values and verify them vs the expected values.
69       std::string dm_token;
70       dict->GetString("user_dmtoken", &dm_token);
71       EXPECT_EQ(dm_token, expected_dmtoken);
72     }
73   }
74 
75   base::MessageLoop loop_;
76   scoped_ptr<PolicyHeaderService> service_;
77   TestCloudPolicyStore user_store_;
78   TestCloudPolicyStore device_store_;
79   scoped_ptr<PolicyHeaderIOHelper> helper_;
80   scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
81 };
82 
83 }  // namespace
84 
TEST_F(PolicyHeaderServiceTest,TestCreationAndShutdown)85 TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) {
86   // Just tests that the objects can be created and shutdown properly.
87   EXPECT_TRUE(service_);
88   EXPECT_TRUE(helper_);
89 }
90 
TEST_F(PolicyHeaderServiceTest,TestWithAndWithoutPolicyHeader)91 TEST_F(PolicyHeaderServiceTest, TestWithAndWithoutPolicyHeader) {
92   // Set policy - this should push a header to the PolicyHeaderIOHelper.
93   scoped_ptr<PolicyData> policy(new PolicyData());
94   std::string expected_token = "expected_token";
95   policy->set_request_token(expected_token);
96   user_store_.SetPolicy(policy.Pass());
97   task_runner_->RunUntilIdle();
98 
99   net::TestURLRequestContext context;
100   net::TestURLRequest request(
101       GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context);
102   helper_->AddPolicyHeaders(&request);
103   ValidateHeader(request.extra_request_headers(), expected_token);
104 
105   // Now blow away the policy data.
106   user_store_.SetPolicy(scoped_ptr<PolicyData>());
107   task_runner_->RunUntilIdle();
108 
109   net::TestURLRequest request2(
110       GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context);
111   helper_->AddPolicyHeaders(&request2);
112   ValidateHeader(request2.extra_request_headers(), "");
113 }
114 
115 }  // namespace policy
116