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