| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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/test/test_simple_task_runner.h" |
| 6 #include "chrome/browser/policy/cloud/policy_header_service.h" |
| 7 #include "components/policy/core/browser/policy_header_io_helper.h" |
| 8 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" |
| 9 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "net/http/http_request_headers.h" |
| 11 #include "net/url_request/url_request_test_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace policy { |
| 15 using enterprise_management::PolicyData; |
| 16 |
| 17 namespace { |
| 18 const char kDMServerURL[] = "http://server_url"; |
| 19 const char kPolicyHeaderName[] = "Chrome-Policy-Posture"; |
| 20 const char kExpectedPolicyHeader[] = "expected_header"; |
| 21 |
| 22 // Test version of the PolicyHeaderService that allows the tests to inject |
| 23 // their own header values. |
| 24 // TODO(atwilson): Remove this once PolicyHeaderService extracts the header |
| 25 // directly from policy. |
| 26 class TestPolicyHeaderService : public PolicyHeaderService { |
| 27 public: |
| 28 TestPolicyHeaderService(CloudPolicyStore* user_store, |
| 29 CloudPolicyStore* device_store) |
| 30 : PolicyHeaderService(kDMServerURL, user_store, device_store) { |
| 31 } |
| 32 |
| 33 virtual ~TestPolicyHeaderService() {} |
| 34 |
| 35 void set_header(const std::string& header) { header_ = header; } |
| 36 |
| 37 protected: |
| 38 virtual std::string CreateHeaderValue() OVERRIDE { return header_; } |
| 39 |
| 40 private: |
| 41 std::string header_; |
| 42 }; |
| 43 |
| 44 class TestCloudPolicyStore : public MockCloudPolicyStore { |
| 45 public: |
| 46 void SetPolicy(scoped_ptr<PolicyData> policy) { |
| 47 policy_ = policy.Pass(); |
| 48 // Notify observers. |
| 49 NotifyStoreLoaded(); |
| 50 } |
| 51 }; |
| 52 |
| 53 class PolicyHeaderServiceTest : public testing::Test { |
| 54 public: |
| 55 PolicyHeaderServiceTest() { |
| 56 task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner()); |
| 57 } |
| 58 virtual ~PolicyHeaderServiceTest() {} |
| 59 |
| 60 virtual void SetUp() OVERRIDE { |
| 61 service_.reset(new TestPolicyHeaderService(&user_store_, &device_store_)); |
| 62 service_->set_header(kExpectedPolicyHeader); |
| 63 helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass(); |
| 64 } |
| 65 |
| 66 virtual void TearDown() OVERRIDE { |
| 67 task_runner_->RunUntilIdle(); |
| 68 // Helper should outlive the service. |
| 69 service_.reset(); |
| 70 helper_.reset(); |
| 71 } |
| 72 |
| 73 void ValidateHeader(const net::HttpRequestHeaders& headers, |
| 74 bool should_exist) { |
| 75 if (should_exist) { |
| 76 std::string header; |
| 77 EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header)); |
| 78 EXPECT_EQ(header, kExpectedPolicyHeader); |
| 79 } else { |
| 80 EXPECT_TRUE(headers.IsEmpty()); |
| 81 } |
| 82 } |
| 83 |
| 84 scoped_ptr<TestPolicyHeaderService> service_; |
| 85 TestCloudPolicyStore user_store_; |
| 86 TestCloudPolicyStore device_store_; |
| 87 scoped_ptr<PolicyHeaderIOHelper> helper_; |
| 88 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 89 content::TestBrowserThreadBundle thread_bundle_; |
| 90 }; |
| 91 |
| 92 } // namespace |
| 93 |
| 94 TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) { |
| 95 // Just tests that the objects can be created and shutdown properly. |
| 96 DCHECK(service_); |
| 97 DCHECK(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 user_store_.SetPolicy(policy.Pass()); |
| 104 task_runner_->RunUntilIdle(); |
| 105 |
| 106 net::TestURLRequestContext context; |
| 107 net::TestURLRequest request( |
| 108 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context); |
| 109 helper_->AddPolicyHeaders(&request); |
| 110 ValidateHeader(request.extra_request_headers(), true); |
| 111 |
| 112 // Now blow away the policy data. |
| 113 service_->set_header(""); |
| 114 user_store_.SetPolicy(scoped_ptr<PolicyData>()); |
| 115 task_runner_->RunUntilIdle(); |
| 116 |
| 117 net::TestURLRequest request2( |
| 118 GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context); |
| 119 helper_->AddPolicyHeaders(&request2); |
| 120 ValidateHeader(request2.extra_request_headers(), false); |
| 121 } |
| 122 |
| 123 } // namespace policy |
| OLD | NEW |