| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/cloud_policy_service.h" | 5 #include "chrome/browser/policy/cloud_policy_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "chrome/browser/policy/mock_cloud_policy_client.h" | 9 #include "chrome/browser/policy/mock_cloud_policy_client.h" |
| 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" | 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace em = enterprise_management; | 15 namespace em = enterprise_management; |
| 16 | 16 |
| 17 using testing::_; | 17 using testing::_; |
| 18 | 18 |
| 19 namespace policy { | 19 namespace policy { |
| 20 | 20 |
| 21 class CloudPolicyServiceTest : public testing::Test { | 21 class CloudPolicyServiceTest : public testing::Test { |
| 22 public: | 22 public: |
| 23 CloudPolicyServiceTest() | 23 CloudPolicyServiceTest() |
| 24 : client_(new MockCloudPolicyClient), | 24 : client_(new MockCloudPolicyClient), |
| 25 store_(new MockCloudPolicyStore), | 25 service_(scoped_ptr<CloudPolicyClient>(client_), &store_) {} |
| 26 service_(scoped_ptr<CloudPolicyClient>(client_), | |
| 27 scoped_ptr<CloudPolicyStore>(store_)) {} | |
| 28 | 26 |
| 29 MOCK_METHOD0(OnPolicyRefresh, void(void)); | 27 MOCK_METHOD0(OnPolicyRefresh, void(void)); |
| 30 | 28 |
| 31 protected: | 29 protected: |
| 32 MockCloudPolicyClient* client_; | 30 MockCloudPolicyClient* client_; |
| 33 MockCloudPolicyStore* store_; | 31 MockCloudPolicyStore store_; |
| 34 CloudPolicyService service_; | 32 CloudPolicyService service_; |
| 35 }; | 33 }; |
| 36 | 34 |
| 37 MATCHER_P(ProtoMatches, proto, "") { | 35 MATCHER_P(ProtoMatches, proto, "") { |
| 38 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | 36 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); |
| 39 } | 37 } |
| 40 | 38 |
| 41 TEST_F(CloudPolicyServiceTest, ManagedByEmptyPolicy) { | 39 TEST_F(CloudPolicyServiceTest, ManagedByEmptyPolicy) { |
| 42 EXPECT_EQ("", service_.ManagedBy()); | 40 EXPECT_EQ("", service_.ManagedBy()); |
| 43 } | 41 } |
| 44 | 42 |
| 45 TEST_F(CloudPolicyServiceTest, ManagedByValidPolicy) { | 43 TEST_F(CloudPolicyServiceTest, ManagedByValidPolicy) { |
| 46 store_->policy_.reset(new em::PolicyData()); | 44 store_.policy_.reset(new em::PolicyData()); |
| 47 store_->policy_->set_username("user@example.com"); | 45 store_.policy_->set_username("user@example.com"); |
| 48 EXPECT_EQ("example.com", service_.ManagedBy()); | 46 EXPECT_EQ("example.com", service_.ManagedBy()); |
| 49 } | 47 } |
| 50 | 48 |
| 51 TEST_F(CloudPolicyServiceTest, PolicyUpdateSuccess) { | 49 TEST_F(CloudPolicyServiceTest, PolicyUpdateSuccess) { |
| 52 em::PolicyFetchResponse policy; | 50 em::PolicyFetchResponse policy; |
| 53 policy.set_policy_data("fake policy"); | 51 policy.set_policy_data("fake policy"); |
| 54 client_->SetPolicy(policy); | 52 client_->SetPolicy(policy); |
| 55 EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1); | 53 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 56 client_->NotifyPolicyFetched(); | 54 client_->NotifyPolicyFetched(); |
| 57 | 55 |
| 58 // After |store_| initializes, credentials and other meta data should be | 56 // After |store_| initializes, credentials and other meta data should be |
| 59 // transferred to |client_|. | 57 // transferred to |client_|. |
| 60 store_->policy_.reset(new em::PolicyData()); | 58 store_.policy_.reset(new em::PolicyData()); |
| 61 store_->policy_->set_request_token("fake token"); | 59 store_.policy_->set_request_token("fake token"); |
| 62 store_->policy_->set_device_id("fake client id"); | 60 store_.policy_->set_device_id("fake client id"); |
| 63 store_->policy_->set_timestamp(32); | 61 store_.policy_->set_timestamp(32); |
| 64 store_->policy_->set_valid_serial_number_missing(true); | 62 store_.policy_->set_valid_serial_number_missing(true); |
| 65 store_->policy_->set_public_key_version(17); | 63 store_.policy_->set_public_key_version(17); |
| 66 EXPECT_CALL(*client_, | 64 EXPECT_CALL(*client_, |
| 67 SetupRegistration(store_->policy_->request_token(), | 65 SetupRegistration(store_.policy_->request_token(), |
| 68 store_->policy_->device_id())).Times(1); | 66 store_.policy_->device_id())).Times(1); |
| 69 store_->NotifyStoreLoaded(); | 67 store_.NotifyStoreLoaded(); |
| 70 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(32), | 68 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(32), |
| 71 client_->last_policy_timestamp_); | 69 client_->last_policy_timestamp_); |
| 72 EXPECT_TRUE(client_->submit_machine_id_); | 70 EXPECT_TRUE(client_->submit_machine_id_); |
| 73 EXPECT_TRUE(client_->public_key_version_valid_); | 71 EXPECT_TRUE(client_->public_key_version_valid_); |
| 74 EXPECT_EQ(17, client_->public_key_version_); | 72 EXPECT_EQ(17, client_->public_key_version_); |
| 75 } | 73 } |
| 76 | 74 |
| 77 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) { | 75 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) { |
| 78 client_->SetStatus(DM_STATUS_REQUEST_FAILED); | 76 client_->SetStatus(DM_STATUS_REQUEST_FAILED); |
| 79 EXPECT_CALL(*store_, Store(_)).Times(0); | 77 EXPECT_CALL(store_, Store(_)).Times(0); |
| 80 client_->NotifyPolicyFetched(); | 78 client_->NotifyPolicyFetched(); |
| 81 } | 79 } |
| 82 | 80 |
| 83 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) { | 81 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) { |
| 84 testing::InSequence seq; | 82 testing::InSequence seq; |
| 85 | 83 |
| 86 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 84 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 87 client_->SetDMToken("fake token"); | 85 client_->SetDMToken("fake token"); |
| 88 | 86 |
| 89 // Trigger a fetch on the client. | 87 // Trigger a fetch on the client. |
| 90 EXPECT_CALL(*client_, FetchPolicy()).Times(1); | 88 EXPECT_CALL(*client_, FetchPolicy()).Times(1); |
| 91 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 89 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 92 base::Unretained(this))); | 90 base::Unretained(this))); |
| 93 | 91 |
| 94 // Client responds, push policy to store. | 92 // Client responds, push policy to store. |
| 95 em::PolicyFetchResponse policy; | 93 em::PolicyFetchResponse policy; |
| 96 policy.set_policy_data("fake policy"); | 94 policy.set_policy_data("fake policy"); |
| 97 client_->SetPolicy(policy); | 95 client_->SetPolicy(policy); |
| 98 EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1); | 96 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 99 client_->NotifyPolicyFetched(); | 97 client_->NotifyPolicyFetched(); |
| 100 | 98 |
| 101 // Store reloads policy, callback gets triggered. | 99 // Store reloads policy, callback gets triggered. |
| 102 store_->policy_.reset(new em::PolicyData()); | 100 store_.policy_.reset(new em::PolicyData()); |
| 103 store_->policy_->set_request_token("token"); | 101 store_.policy_->set_request_token("token"); |
| 104 store_->policy_->set_device_id("device-id"); | 102 store_.policy_->set_device_id("device-id"); |
| 105 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 103 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); |
| 106 store_->NotifyStoreLoaded(); | 104 store_.NotifyStoreLoaded(); |
| 107 } | 105 } |
| 108 | 106 |
| 109 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) { | 107 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) { |
| 110 // Clear the token so the client is not registered. | 108 // Clear the token so the client is not registered. |
| 111 client_->SetDMToken(""); | 109 client_->SetDMToken(""); |
| 112 | 110 |
| 113 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | 111 EXPECT_CALL(*client_, FetchPolicy()).Times(0); |
| 114 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 112 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); |
| 115 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 113 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 116 base::Unretained(this))); | 114 base::Unretained(this))); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 141 | 139 |
| 142 // Trigger a fetch on the client. | 140 // Trigger a fetch on the client. |
| 143 EXPECT_CALL(*client_, FetchPolicy()).Times(1); | 141 EXPECT_CALL(*client_, FetchPolicy()).Times(1); |
| 144 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 142 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 145 base::Unretained(this))); | 143 base::Unretained(this))); |
| 146 | 144 |
| 147 // Client responds, push policy to store. | 145 // Client responds, push policy to store. |
| 148 em::PolicyFetchResponse policy; | 146 em::PolicyFetchResponse policy; |
| 149 policy.set_policy_data("fake policy"); | 147 policy.set_policy_data("fake policy"); |
| 150 client_->SetPolicy(policy); | 148 client_->SetPolicy(policy); |
| 151 EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1); | 149 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 152 client_->NotifyPolicyFetched(); | 150 client_->NotifyPolicyFetched(); |
| 153 | 151 |
| 154 // Store fails, which should trigger the callback. | 152 // Store fails, which should trigger the callback. |
| 155 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); | 153 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); |
| 156 store_->NotifyStoreError(); | 154 store_.NotifyStoreError(); |
| 157 } | 155 } |
| 158 | 156 |
| 159 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) { | 157 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) { |
| 160 testing::InSequence seq; | 158 testing::InSequence seq; |
| 161 | 159 |
| 162 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 160 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 163 client_->SetDMToken("fake token"); | 161 client_->SetDMToken("fake token"); |
| 164 | 162 |
| 165 // Trigger a fetch on the client. | 163 // Trigger a fetch on the client. |
| 166 EXPECT_CALL(*client_, FetchPolicy()).Times(1); | 164 EXPECT_CALL(*client_, FetchPolicy()).Times(1); |
| 167 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 165 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 168 base::Unretained(this))); | 166 base::Unretained(this))); |
| 169 | 167 |
| 170 // Triggering another policy refresh should generate a new fetch request. | 168 // Triggering another policy refresh should generate a new fetch request. |
| 171 EXPECT_CALL(*client_, FetchPolicy()).Times(1); | 169 EXPECT_CALL(*client_, FetchPolicy()).Times(1); |
| 172 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 170 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 173 base::Unretained(this))); | 171 base::Unretained(this))); |
| 174 | 172 |
| 175 // Client responds, push policy to store. | 173 // Client responds, push policy to store. |
| 176 em::PolicyFetchResponse policy; | 174 em::PolicyFetchResponse policy; |
| 177 policy.set_policy_data("fake policy"); | 175 policy.set_policy_data("fake policy"); |
| 178 client_->SetPolicy(policy); | 176 client_->SetPolicy(policy); |
| 179 EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1); | 177 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 180 client_->NotifyPolicyFetched(); | 178 client_->NotifyPolicyFetched(); |
| 181 | 179 |
| 182 // Trigger another policy fetch. | 180 // Trigger another policy fetch. |
| 183 EXPECT_CALL(*client_, FetchPolicy()).Times(1); | 181 EXPECT_CALL(*client_, FetchPolicy()).Times(1); |
| 184 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | 182 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, |
| 185 base::Unretained(this))); | 183 base::Unretained(this))); |
| 186 | 184 |
| 187 // The store finishing the first load should not generate callbacks. | 185 // The store finishing the first load should not generate callbacks. |
| 188 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 186 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 189 store_->NotifyStoreLoaded(); | 187 store_.NotifyStoreLoaded(); |
| 190 | 188 |
| 191 // Second policy fetch finishes. | 189 // Second policy fetch finishes. |
| 192 EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1); | 190 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); |
| 193 client_->NotifyPolicyFetched(); | 191 client_->NotifyPolicyFetched(); |
| 194 | 192 |
| 195 // Corresponding store operation finishes, all _three_ callbacks fire. | 193 // Corresponding store operation finishes, all _three_ callbacks fire. |
| 196 EXPECT_CALL(*this, OnPolicyRefresh()).Times(3); | 194 EXPECT_CALL(*this, OnPolicyRefresh()).Times(3); |
| 197 store_->NotifyStoreLoaded(); | 195 store_.NotifyStoreLoaded(); |
| 198 } | 196 } |
| 199 | 197 |
| 200 } // namespace policy | 198 } // namespace policy |
| OLD | NEW |