Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / common / cloud / policy_header_service_unittest.cc
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/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"
18
19 namespace policy {
20 using enterprise_management::PolicyData;
21
22 namespace {
23 const char kDMServerURL[] = "http://server_url";
24 const char kPolicyHeaderName[] = "Chrome-Policy-Posture";
25
26 class TestCloudPolicyStore : public MockCloudPolicyStore {
27  public:
28   void SetPolicy(scoped_ptr<PolicyData> policy) {
29     policy_ = policy.Pass();
30     // Notify observers.
31     NotifyStoreLoaded();
32   }
33 };
34
35 class PolicyHeaderServiceTest : public testing::Test {
36  public:
37   PolicyHeaderServiceTest() {
38     task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner());
39   }
40   virtual ~PolicyHeaderServiceTest() {}
41
42   virtual void SetUp() OVERRIDE {
43     service_.reset(new PolicyHeaderService(kDMServerURL,
44                                            kPolicyVerificationKeyHash,
45                                            &user_store_,
46                                            &device_store_));
47     helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass();
48   }
49
50   virtual void TearDown() OVERRIDE {
51     task_runner_->RunUntilIdle();
52     // Helper should outlive the service.
53     service_.reset();
54     helper_.reset();
55   }
56
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());
62     } else {
63       // Read header.
64       std::string header;
65       EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header));
66       // Decode the base64 value into JSON.
67       std::string decoded;
68       base::Base64Decode(header, &decoded);
69       // Parse the JSON.
70       scoped_ptr<base::Value> value(base::JSONReader::Read(decoded));
71       ASSERT_TRUE(value);
72       base::DictionaryValue* dict;
73       EXPECT_TRUE(value->GetAsDictionary(&dict));
74       // Read the values and verify them vs the expected values.
75       std::string dm_token;
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);
81     }
82   }
83
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_;
90 };
91
92 }  // namespace
93
94 TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) {
95   // Just tests that the objects can be created and shutdown properly.
96   EXPECT_TRUE(service_);
97   EXPECT_TRUE(helper_);
98 }
99
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();
109
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);
116
117   // Now blow away the policy data.
118   user_store_.SetPolicy(scoped_ptr<PolicyData>());
119   task_runner_->RunUntilIdle();
120
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(), "", "");
125 }
126
127 }  // namespace policy