Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/policy/device_policy_cache.h" | |
| 6 | |
| 7 #include "policy/configuration_policy_type.h" | |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace policy { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 using ::chromeos::SignedSettings; | |
| 16 using ::chromeos::SignedSettingsHelper; | |
| 17 using ::testing::_; | |
| 18 using ::testing::InSequence; | |
| 19 | |
| 20 class MockSignedSettingsHelper : public SignedSettingsHelper { | |
| 21 public: | |
| 22 MockSignedSettingsHelper() {} | |
| 23 virtual ~MockSignedSettingsHelper() {} | |
| 24 | |
| 25 MOCK_METHOD2(StartStorePolicyOp, void(const std::string&, | |
| 26 SignedSettingsHelper::Callback*)); | |
| 27 MOCK_METHOD1(StartRetrievePolicyOp, void(SignedSettingsHelper::Callback*)); | |
| 28 MOCK_METHOD1(CancelCallback, void(SignedSettingsHelper::Callback*)); | |
| 29 | |
| 30 // This test doesn't need these methods, but since they're pure virtual in | |
| 31 // SignedSettingsHelper, they must be implemented: | |
| 32 MOCK_METHOD2(StartCheckWhitelistOp, void(const std::string&, | |
| 33 SignedSettingsHelper::Callback*)); | |
| 34 MOCK_METHOD3(StartWhitelistOp, void(const std::string&, bool, | |
| 35 SignedSettingsHelper::Callback*)); | |
| 36 MOCK_METHOD3(StartStorePropertyOp, void(const std::string&, | |
| 37 const std::string&, | |
| 38 SignedSettingsHelper::Callback*)); | |
| 39 MOCK_METHOD2(StartRetrieveProperty, void(const std::string&, | |
| 40 SignedSettingsHelper::Callback*)); | |
| 41 | |
| 42 private: | |
| 43 DISALLOW_COPY_AND_ASSIGN(MockSignedSettingsHelper); | |
| 44 }; | |
| 45 | |
| 46 ACTION_P(MockSignedSettingsHelperStorePolicy, status_code) { | |
| 47 arg1->OnStorePolicyCompleted(status_code); | |
| 48 } | |
| 49 | |
| 50 ACTION_P2(MockSignedSettingsHelperRetrievePolicy, status_code, policy) { | |
| 51 arg0->OnRetrievePolicyCompleted(status_code, policy); | |
| 52 } | |
| 53 | |
| 54 em::PolicyFetchResponse* CreateProxyPolicy(const std::string& proxy) { | |
| 55 // This method omits a few fields which currently aren't needed by tests: | |
| 56 // timestamp, machine_name, request_token, policy_type, public key info. | |
| 57 em::PolicyData signed_response; | |
| 58 em::ChromeDeviceSettingsProto settings; | |
| 59 em::DeviceProxySettingsProto* proxy_proto = | |
| 60 settings.mutable_device_proxy_settings(); | |
| 61 proxy_proto->set_proxy_server(proxy); | |
| 62 proxy_proto->set_proxy_mode("fixed_servers"); | |
| 63 EXPECT_TRUE( | |
| 64 settings.SerializeToString(signed_response.mutable_policy_value())); | |
| 65 std::string serialized_signed_response; | |
| 66 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); | |
| 67 em::PolicyFetchResponse* response = new em::PolicyFetchResponse; | |
| 68 response->set_policy_data(serialized_signed_response); | |
| 69 return response; | |
| 70 } | |
| 71 | |
| 72 std::string CreatePolicyBlob(const std::string& proxy) { | |
| 73 scoped_ptr<em::PolicyFetchResponse> response(CreateProxyPolicy(proxy)); | |
| 74 std::string blob; | |
| 75 EXPECT_TRUE(response->SerializeToString(&blob)); | |
| 76 return blob; | |
| 77 } | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 class DevicePolicyCacheTest : public testing::Test { | |
| 82 protected: | |
| 83 DevicePolicyCacheTest() { | |
| 84 } | |
| 85 | |
| 86 virtual void SetUp() { | |
| 87 cache_.reset(new DevicePolicyCache(&signed_settings_helper_)); | |
| 88 } | |
| 89 | |
| 90 virtual void TearDown() { | |
| 91 EXPECT_CALL(signed_settings_helper_, CancelCallback(_)); | |
| 92 cache_.reset(); | |
| 93 } | |
| 94 | |
| 95 const PolicyMap& mandatory_policy(const DevicePolicyCache& cache) { | |
| 96 return cache.mandatory_policy_; | |
| 97 } | |
| 98 | |
| 99 scoped_ptr<DevicePolicyCache> cache_; | |
| 100 MockSignedSettingsHelper signed_settings_helper_; | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); | |
| 104 }; | |
| 105 | |
| 106 TEST_F(DevicePolicyCacheTest, Startup) { | |
| 107 std::string policy_response(CreatePolicyBlob("proxy.server")); | |
| 108 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 109 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 110 policy_response)); | |
| 111 cache_->Load(); | |
| 112 EXPECT_GT(mandatory_policy(*cache_.get()).size(), 0U); | |
|
Mattias Nissler (ping if slow)
2011/03/24 18:55:57
can just write *cache (also below)
Jakob Kummerow
2011/03/28 13:53:53
Done.
| |
| 113 } | |
| 114 | |
| 115 TEST_F(DevicePolicyCacheTest, SetPolicy) { | |
| 116 InSequence s; | |
| 117 // Startup. | |
| 118 std::string policy_response(CreatePolicyBlob("proxy.server.old")); | |
| 119 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 120 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 121 policy_response)); | |
| 122 cache_->Load(); | |
| 123 scoped_ptr<Value> expected(Value::CreateStringValue("proxy.server.old")); | |
| 124 EXPECT_TRUE(mandatory_policy(*cache_.get()).Get(kPolicyProxyServer)->Equals( | |
| 125 expected.get())); | |
| 126 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 127 // Set new policy information. | |
| 128 scoped_ptr<em::PolicyFetchResponse> new_policy( | |
| 129 CreateProxyPolicy("proxy.server.new")); | |
| 130 std::string new_policy_response(CreatePolicyBlob("proxy.server.new")); | |
| 131 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).WillOnce( | |
| 132 MockSignedSettingsHelperStorePolicy(chromeos::SignedSettings::SUCCESS)); | |
| 133 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 134 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 135 new_policy_response)); | |
| 136 cache_->SetPolicy(*new_policy.get()); | |
| 137 expected.reset(Value::CreateStringValue("proxy.server.new")); | |
| 138 EXPECT_TRUE(mandatory_policy(*cache_.get()).Get(kPolicyProxyServer)->Equals( | |
| 139 expected.get())); | |
| 140 } | |
| 141 | |
| 142 // TODO(jkummerow): Test cases to add in the future: | |
| 143 | |
| 144 // DecodePolicy -- doesn't really make sense yet due to undefined constants. | |
| 145 // In particular, want to test decoding a whitelist with 2+ elements. | |
|
Mattias Nissler (ping if slow)
2011/03/24 18:55:57
So define the constants and write the test case?
Jakob Kummerow
2011/03/28 13:53:53
As discussed, punted to a later CL.
| |
| 146 | |
| 147 // StartupUnmanaged -- can't do it yet due to missing dependencies. | |
| 148 | |
| 149 } // namespace policy | |
| OLD | NEW |