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/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"
16
17 namespace policy {
18 using enterprise_management::PolicyData;
19
20 namespace {
21 const char kDMServerURL[] = "http://server_url";
22 const char kPolicyHeaderName[] = "Chrome-Policy-Posture";
23
24 class TestCloudPolicyStore : public MockCloudPolicyStore {
25 public:
SetPolicy(scoped_ptr<PolicyData> policy)26 void SetPolicy(scoped_ptr<PolicyData> policy) {
27 policy_ = policy.Pass();
28 // Notify observers.
29 NotifyStoreLoaded();
30 }
31 };
32
33 class PolicyHeaderServiceTest : public testing::Test {
34 public:
PolicyHeaderServiceTest()35 PolicyHeaderServiceTest() {
36 task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner());
37 }
~PolicyHeaderServiceTest()38 virtual ~PolicyHeaderServiceTest() {}
39
SetUp()40 virtual void SetUp() OVERRIDE {
41 service_.reset(new PolicyHeaderService(kDMServerURL,
42 kPolicyVerificationKeyHash,
43 &user_store_,
44 &device_store_));
45 helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass();
46 }
47
TearDown()48 virtual void TearDown() OVERRIDE {
49 task_runner_->RunUntilIdle();
50 // Helper should outlive the service.
51 service_.reset();
52 helper_.reset();
53 }
54
ValidateHeader(const net::HttpRequestHeaders & headers,const std::string & expected_dmtoken,const std::string & expected_policy_token)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());
60 } else {
61 // Read header.
62 std::string header;
63 EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header));
64 // Decode the base64 value into JSON.
65 std::string decoded;
66 base::Base64Decode(header, &decoded);
67 // Parse the JSON.
68 scoped_ptr<base::Value> value(base::JSONReader::Read(decoded));
69 ASSERT_TRUE(value);
70 base::DictionaryValue* dict;
71 EXPECT_TRUE(value->GetAsDictionary(&dict));
72 // Read the values and verify them vs the expected values.
73 std::string dm_token;
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);
79 }
80 }
81
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_;
88 };
89
90 } // namespace
91
TEST_F(PolicyHeaderServiceTest,TestCreationAndShutdown)92 TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) {
93 // Just tests that the objects can be created and shutdown properly.
94 EXPECT_TRUE(service_);
95 EXPECT_TRUE(helper_);
96 }
97
TEST_F(PolicyHeaderServiceTest,TestWithAndWithoutPolicyHeader)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();
107
108 net::TestURLRequestContext context;
109 net::TestURLRequest request(
110 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context);
111 helper_->AddPolicyHeaders(request.url(), &request);
112 ValidateHeader(request.extra_request_headers(), expected_dmtoken,
113 expected_policy_token);
114
115 // Now blow away the policy data.
116 user_store_.SetPolicy(scoped_ptr<PolicyData>());
117 task_runner_->RunUntilIdle();
118
119 net::TestURLRequest request2(
120 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context);
121 helper_->AddPolicyHeaders(request2.url(), &request2);
122 ValidateHeader(request2.extra_request_headers(), "", "");
123 }
124
125 } // namespace policy
126