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 em::PolicyFetchResponse&, |
| 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 } // namespace |
| 73 |
| 74 class DevicePolicyCacheTest : public testing::Test { |
| 75 protected: |
| 76 DevicePolicyCacheTest() { |
| 77 } |
| 78 |
| 79 virtual void SetUp() { |
| 80 cache_.reset(new DevicePolicyCache(&signed_settings_helper_)); |
| 81 } |
| 82 |
| 83 virtual void TearDown() { |
| 84 EXPECT_CALL(signed_settings_helper_, CancelCallback(_)); |
| 85 cache_.reset(); |
| 86 } |
| 87 |
| 88 const PolicyMap& mandatory_policy(const DevicePolicyCache& cache) { |
| 89 return cache.mandatory_policy_; |
| 90 } |
| 91 |
| 92 scoped_ptr<DevicePolicyCache> cache_; |
| 93 MockSignedSettingsHelper signed_settings_helper_; |
| 94 |
| 95 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); |
| 97 }; |
| 98 |
| 99 TEST_F(DevicePolicyCacheTest, Startup) { |
| 100 scoped_ptr<em::PolicyFetchResponse> policy_response( |
| 101 CreateProxyPolicy("proxy.server")); |
| 102 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 103 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 104 *policy_response)); |
| 105 cache_->Load(); |
| 106 // TODO(jkummerow): This will be EXPECT_GT once policy decoding is |
| 107 // implemented in DevicePolicyCache::DecodeDevicePolicy(...). |
| 108 EXPECT_EQ(mandatory_policy(*cache_).size(), 0U); |
| 109 } |
| 110 |
| 111 TEST_F(DevicePolicyCacheTest, SetPolicy) { |
| 112 InSequence s; |
| 113 // Startup. |
| 114 scoped_ptr<em::PolicyFetchResponse> policy_response( |
| 115 CreateProxyPolicy("proxy.server.old")); |
| 116 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 117 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 118 *policy_response)); |
| 119 cache_->Load(); |
| 120 scoped_ptr<Value> expected(Value::CreateStringValue("proxy.server.old")); |
| 121 // TODO(jkummerow): This will be EXPECT_TRUE once policy decoding is |
| 122 // implemented in DevicePolicyCache::DecodeDevicePolicy(...). |
| 123 EXPECT_FALSE(Value::Equals( |
| 124 mandatory_policy(*cache_).Get(kPolicyProxyServer), expected.get())); |
| 125 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 126 // Set new policy information. |
| 127 scoped_ptr<em::PolicyFetchResponse> new_policy_response( |
| 128 CreateProxyPolicy("proxy.server.new")); |
| 129 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).WillOnce( |
| 130 MockSignedSettingsHelperStorePolicy(chromeos::SignedSettings::SUCCESS)); |
| 131 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 132 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 133 *new_policy_response)); |
| 134 cache_->SetPolicy(*new_policy_response); |
| 135 expected.reset(Value::CreateStringValue("proxy.server.new")); |
| 136 // TODO(jkummerow): This will be EXPECT_TRUE once policy decoding is |
| 137 // implemented in DevicePolicyCache::DecodeDevicePolicy(...). |
| 138 EXPECT_FALSE(Value::Equals( |
| 139 mandatory_policy(*cache_).Get(kPolicyProxyServer), expected.get())); |
| 140 } |
| 141 |
| 142 } // namespace policy |
OLD | NEW |