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/memory/scoped_ptr.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "base/values.h"
10 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
11 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
12 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
13 #include "components/policy/core/common/cloud/policy_header_service.h"
14 #include "net/http/http_request_headers.h"
15 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 using enterprise_management::PolicyData;
23 const char kDMServerURL[] = "http://server_url";
24 const char kPolicyHeaderName[] = "Chrome-Policy-Posture";
26 class TestCloudPolicyStore : public MockCloudPolicyStore {
28 void SetPolicy(scoped_ptr<PolicyData> policy) {
29 policy_ = policy.Pass();
35 class PolicyHeaderServiceTest : public testing::Test {
37 PolicyHeaderServiceTest() {
38 task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner());
40 virtual ~PolicyHeaderServiceTest() {}
42 virtual void SetUp() OVERRIDE {
43 service_.reset(new PolicyHeaderService(kDMServerURL,
44 kPolicyVerificationKeyHash,
47 helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass();
50 virtual void TearDown() OVERRIDE {
51 task_runner_->RunUntilIdle();
52 // Helper should outlive the service.
57 void ValidateHeader(const net::HttpRequestHeaders& headers,
58 const std::string& expected_dmtoken,
59 const std::string& expected_policy_token) {
60 if (expected_dmtoken.empty()) {
61 EXPECT_TRUE(headers.IsEmpty());
65 EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header));
66 // Decode the base64 value into JSON.
68 base::Base64Decode(header, &decoded);
70 scoped_ptr<base::Value> value(base::JSONReader::Read(decoded));
72 base::DictionaryValue* dict;
73 EXPECT_TRUE(value->GetAsDictionary(&dict));
74 // Read the values and verify them vs the expected values.
76 dict->GetString("user_dmtoken", &dm_token);
77 EXPECT_EQ(dm_token, expected_dmtoken);
78 std::string policy_token;
79 dict->GetString("user_policy_token", &policy_token);
80 EXPECT_EQ(policy_token, expected_policy_token);
84 base::MessageLoop loop_;
85 scoped_ptr<PolicyHeaderService> service_;
86 TestCloudPolicyStore user_store_;
87 TestCloudPolicyStore device_store_;
88 scoped_ptr<PolicyHeaderIOHelper> helper_;
89 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
94 TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) {
95 // Just tests that the objects can be created and shutdown properly.
96 EXPECT_TRUE(service_);
100 TEST_F(PolicyHeaderServiceTest, TestWithAndWithoutPolicyHeader) {
101 // Set policy - this should push a header to the PolicyHeaderIOHelper.
102 scoped_ptr<PolicyData> policy(new PolicyData());
103 std::string expected_dmtoken = "expected_dmtoken";
104 std::string expected_policy_token = "expected_dmtoken";
105 policy->set_request_token(expected_dmtoken);
106 policy->set_policy_token(expected_policy_token);
107 user_store_.SetPolicy(policy.Pass());
108 task_runner_->RunUntilIdle();
110 net::TestURLRequestContext context;
111 scoped_ptr<net::URLRequest> request(context.CreateRequest(
112 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, NULL));
113 helper_->AddPolicyHeaders(request->url(), request.get());
114 ValidateHeader(request->extra_request_headers(), expected_dmtoken,
115 expected_policy_token);
117 // Now blow away the policy data.
118 user_store_.SetPolicy(scoped_ptr<PolicyData>());
119 task_runner_->RunUntilIdle();
121 scoped_ptr<net::URLRequest> request2(context.CreateRequest(
122 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, NULL));
123 helper_->AddPolicyHeaders(request2->url(), request2.get());
124 ValidateHeader(request2->extra_request_headers(), "", "");
127 } // namespace policy