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.
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/cloud_policy_constants.h"
10 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
11 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
12 #include "components/policy/core/common/cloud/policy_header_service.h"
13 #include "net/http/http_request_headers.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using enterprise_management::PolicyData;
21 const char kDMServerURL[] = "http://server_url";
22 const char kPolicyHeaderName[] = "Chrome-Policy-Posture";
24 class TestCloudPolicyStore : public MockCloudPolicyStore {
26 void SetPolicy(scoped_ptr<PolicyData> policy) {
27 policy_ = policy.Pass();
33 class PolicyHeaderServiceTest : public testing::Test {
35 PolicyHeaderServiceTest() {
36 task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner());
38 virtual ~PolicyHeaderServiceTest() {}
40 virtual void SetUp() OVERRIDE {
41 service_.reset(new PolicyHeaderService(kDMServerURL,
42 kPolicyVerificationKeyHash,
45 helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass();
48 virtual void TearDown() OVERRIDE {
49 task_runner_->RunUntilIdle();
50 // Helper should outlive the service.
55 void ValidateHeader(const net::HttpRequestHeaders& headers,
56 const std::string& expected_dmtoken,
57 const std::string& expected_policy_token) {
58 if (expected_dmtoken.empty()) {
59 EXPECT_TRUE(headers.IsEmpty());
63 EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header));
64 // Decode the base64 value into JSON.
66 base::Base64Decode(header, &decoded);
68 scoped_ptr<base::Value> value(base::JSONReader::Read(decoded));
70 base::DictionaryValue* dict;
71 EXPECT_TRUE(value->GetAsDictionary(&dict));
72 // Read the values and verify them vs the expected values.
74 dict->GetString("user_dmtoken", &dm_token);
75 EXPECT_EQ(dm_token, expected_dmtoken);
76 std::string policy_token;
77 dict->GetString("user_policy_token", &policy_token);
78 EXPECT_EQ(policy_token, expected_policy_token);
82 base::MessageLoop loop_;
83 scoped_ptr<PolicyHeaderService> service_;
84 TestCloudPolicyStore user_store_;
85 TestCloudPolicyStore device_store_;
86 scoped_ptr<PolicyHeaderIOHelper> helper_;
87 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
92 TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) {
93 // Just tests that the objects can be created and shutdown properly.
94 EXPECT_TRUE(service_);
98 TEST_F(PolicyHeaderServiceTest, TestWithAndWithoutPolicyHeader) {
99 // Set policy - this should push a header to the PolicyHeaderIOHelper.
100 scoped_ptr<PolicyData> policy(new PolicyData());
101 std::string expected_dmtoken = "expected_dmtoken";
102 std::string expected_policy_token = "expected_dmtoken";
103 policy->set_request_token(expected_dmtoken);
104 policy->set_policy_token(expected_policy_token);
105 user_store_.SetPolicy(policy.Pass());
106 task_runner_->RunUntilIdle();
108 net::TestURLRequestContext context;
109 net::TestURLRequest request(
110 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context);
111 helper_->AddPolicyHeaders(&request);
112 ValidateHeader(request.extra_request_headers(), expected_dmtoken,
113 expected_policy_token);
115 // Now blow away the policy data.
116 user_store_.SetPolicy(scoped_ptr<PolicyData>());
117 task_runner_->RunUntilIdle();
119 net::TestURLRequest request2(
120 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context);
121 helper_->AddPolicyHeaders(&request2);
122 ValidateHeader(request2.extra_request_headers(), "", "");
125 } // namespace policy