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