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