Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/browser/policy/cloud_policy_service_unittest.cc

Issue 10919131: Break out base functionality of UserCloudPolicyManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase, address comments, fix compilation. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 : service_(&client_, &store_) {}
25 service_(scoped_ptr<CloudPolicyClient>(client_), &store_) {}
26 25
27 MOCK_METHOD0(OnPolicyRefresh, void(void)); 26 MOCK_METHOD0(OnPolicyRefresh, void(void));
28 27
29 protected: 28 protected:
30 MockCloudPolicyClient* client_; 29 MockCloudPolicyClient client_;
31 MockCloudPolicyStore store_; 30 MockCloudPolicyStore store_;
32 CloudPolicyService service_; 31 CloudPolicyService service_;
33 }; 32 };
34 33
35 MATCHER_P(ProtoMatches, proto, "") { 34 MATCHER_P(ProtoMatches, proto, "") {
36 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); 35 return arg.SerializePartialAsString() == proto.SerializePartialAsString();
37 } 36 }
38 37
39 TEST_F(CloudPolicyServiceTest, ManagedByEmptyPolicy) { 38 TEST_F(CloudPolicyServiceTest, ManagedByEmptyPolicy) {
40 EXPECT_EQ("", service_.ManagedBy()); 39 EXPECT_EQ("", service_.ManagedBy());
41 } 40 }
42 41
43 TEST_F(CloudPolicyServiceTest, ManagedByValidPolicy) { 42 TEST_F(CloudPolicyServiceTest, ManagedByValidPolicy) {
44 store_.policy_.reset(new em::PolicyData()); 43 store_.policy_.reset(new em::PolicyData());
45 store_.policy_->set_username("user@example.com"); 44 store_.policy_->set_username("user@example.com");
46 EXPECT_EQ("example.com", service_.ManagedBy()); 45 EXPECT_EQ("example.com", service_.ManagedBy());
47 } 46 }
48 47
49 TEST_F(CloudPolicyServiceTest, PolicyUpdateSuccess) { 48 TEST_F(CloudPolicyServiceTest, PolicyUpdateSuccess) {
50 em::PolicyFetchResponse policy; 49 em::PolicyFetchResponse policy;
51 policy.set_policy_data("fake policy"); 50 policy.set_policy_data("fake policy");
52 client_->SetPolicy(policy); 51 client_.SetPolicy(policy);
53 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); 52 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1);
54 client_->NotifyPolicyFetched(); 53 client_.NotifyPolicyFetched();
55 54
56 // After |store_| initializes, credentials and other meta data should be 55 // After |store_| initializes, credentials and other meta data should be
57 // transferred to |client_|. 56 // transferred to |client_|.
58 store_.policy_.reset(new em::PolicyData()); 57 store_.policy_.reset(new em::PolicyData());
59 store_.policy_->set_request_token("fake token"); 58 store_.policy_->set_request_token("fake token");
60 store_.policy_->set_device_id("fake client id"); 59 store_.policy_->set_device_id("fake client id");
61 store_.policy_->set_timestamp(32); 60 store_.policy_->set_timestamp(32);
62 store_.policy_->set_valid_serial_number_missing(true); 61 store_.policy_->set_valid_serial_number_missing(true);
63 store_.policy_->set_public_key_version(17); 62 store_.policy_->set_public_key_version(17);
64 EXPECT_CALL(*client_, 63 EXPECT_CALL(client_,
65 SetupRegistration(store_.policy_->request_token(), 64 SetupRegistration(store_.policy_->request_token(),
66 store_.policy_->device_id())).Times(1); 65 store_.policy_->device_id())).Times(1);
67 store_.NotifyStoreLoaded(); 66 store_.NotifyStoreLoaded();
68 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(32), 67 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(32),
69 client_->last_policy_timestamp_); 68 client_.last_policy_timestamp_);
70 EXPECT_TRUE(client_->submit_machine_id_); 69 EXPECT_TRUE(client_.submit_machine_id_);
71 EXPECT_TRUE(client_->public_key_version_valid_); 70 EXPECT_TRUE(client_.public_key_version_valid_);
72 EXPECT_EQ(17, client_->public_key_version_); 71 EXPECT_EQ(17, client_.public_key_version_);
73 } 72 }
74 73
75 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) { 74 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) {
76 client_->SetStatus(DM_STATUS_REQUEST_FAILED); 75 client_.SetStatus(DM_STATUS_REQUEST_FAILED);
77 EXPECT_CALL(store_, Store(_)).Times(0); 76 EXPECT_CALL(store_, Store(_)).Times(0);
78 client_->NotifyPolicyFetched(); 77 client_.NotifyPolicyFetched();
79 } 78 }
80 79
81 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) { 80 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) {
82 testing::InSequence seq; 81 testing::InSequence seq;
83 82
84 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 83 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
85 client_->SetDMToken("fake token"); 84 client_.SetDMToken("fake token");
86 85
87 // Trigger a fetch on the client. 86 // Trigger a fetch on the client.
88 EXPECT_CALL(*client_, FetchPolicy()).Times(1); 87 EXPECT_CALL(client_, FetchPolicy()).Times(1);
89 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 88 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
90 base::Unretained(this))); 89 base::Unretained(this)));
91 90
92 // Client responds, push policy to store. 91 // Client responds, push policy to store.
93 em::PolicyFetchResponse policy; 92 em::PolicyFetchResponse policy;
94 policy.set_policy_data("fake policy"); 93 policy.set_policy_data("fake policy");
95 client_->SetPolicy(policy); 94 client_.SetPolicy(policy);
96 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); 95 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1);
97 client_->NotifyPolicyFetched(); 96 client_.NotifyPolicyFetched();
98 97
99 // Store reloads policy, callback gets triggered. 98 // Store reloads policy, callback gets triggered.
100 store_.policy_.reset(new em::PolicyData()); 99 store_.policy_.reset(new em::PolicyData());
101 store_.policy_->set_request_token("token"); 100 store_.policy_->set_request_token("token");
102 store_.policy_->set_device_id("device-id"); 101 store_.policy_->set_device_id("device-id");
103 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); 102 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
104 store_.NotifyStoreLoaded(); 103 store_.NotifyStoreLoaded();
105 } 104 }
106 105
107 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) { 106 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) {
108 // Clear the token so the client is not registered. 107 // Clear the token so the client is not registered.
109 client_->SetDMToken(""); 108 client_.SetDMToken("");
110 109
111 EXPECT_CALL(*client_, FetchPolicy()).Times(0); 110 EXPECT_CALL(client_, FetchPolicy()).Times(0);
112 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); 111 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
113 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 112 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
114 base::Unretained(this))); 113 base::Unretained(this)));
115 } 114 }
116 115
117 TEST_F(CloudPolicyServiceTest, RefreshPolicyClientError) { 116 TEST_F(CloudPolicyServiceTest, RefreshPolicyClientError) {
118 testing::InSequence seq; 117 testing::InSequence seq;
119 118
120 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 119 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
121 client_->SetDMToken("fake token"); 120 client_.SetDMToken("fake token");
122 121
123 // Trigger a fetch on the client. 122 // Trigger a fetch on the client.
124 EXPECT_CALL(*client_, FetchPolicy()).Times(1); 123 EXPECT_CALL(client_, FetchPolicy()).Times(1);
125 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 124 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
126 base::Unretained(this))); 125 base::Unretained(this)));
127 126
128 // Client responds with an error, which should trigger the callback. 127 // Client responds with an error, which should trigger the callback.
129 client_->SetStatus(DM_STATUS_REQUEST_FAILED); 128 client_.SetStatus(DM_STATUS_REQUEST_FAILED);
130 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); 129 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
131 client_->NotifyClientError(); 130 client_.NotifyClientError();
132 } 131 }
133 132
134 TEST_F(CloudPolicyServiceTest, RefreshPolicyStoreError) { 133 TEST_F(CloudPolicyServiceTest, RefreshPolicyStoreError) {
135 testing::InSequence seq; 134 testing::InSequence seq;
136 135
137 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 136 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
138 client_->SetDMToken("fake token"); 137 client_.SetDMToken("fake token");
139 138
140 // Trigger a fetch on the client. 139 // Trigger a fetch on the client.
141 EXPECT_CALL(*client_, FetchPolicy()).Times(1); 140 EXPECT_CALL(client_, FetchPolicy()).Times(1);
142 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 141 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
143 base::Unretained(this))); 142 base::Unretained(this)));
144 143
145 // Client responds, push policy to store. 144 // Client responds, push policy to store.
146 em::PolicyFetchResponse policy; 145 em::PolicyFetchResponse policy;
147 policy.set_policy_data("fake policy"); 146 policy.set_policy_data("fake policy");
148 client_->SetPolicy(policy); 147 client_.SetPolicy(policy);
149 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); 148 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1);
150 client_->NotifyPolicyFetched(); 149 client_.NotifyPolicyFetched();
151 150
152 // Store fails, which should trigger the callback. 151 // Store fails, which should trigger the callback.
153 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1); 152 EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
154 store_.NotifyStoreError(); 153 store_.NotifyStoreError();
155 } 154 }
156 155
157 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) { 156 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) {
158 testing::InSequence seq; 157 testing::InSequence seq;
159 158
160 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 159 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
161 client_->SetDMToken("fake token"); 160 client_.SetDMToken("fake token");
162 161
163 // Trigger a fetch on the client. 162 // Trigger a fetch on the client.
164 EXPECT_CALL(*client_, FetchPolicy()).Times(1); 163 EXPECT_CALL(client_, FetchPolicy()).Times(1);
165 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 164 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
166 base::Unretained(this))); 165 base::Unretained(this)));
167 166
168 // Triggering another policy refresh should generate a new fetch request. 167 // Triggering another policy refresh should generate a new fetch request.
169 EXPECT_CALL(*client_, FetchPolicy()).Times(1); 168 EXPECT_CALL(client_, FetchPolicy()).Times(1);
170 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 169 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
171 base::Unretained(this))); 170 base::Unretained(this)));
172 171
173 // Client responds, push policy to store. 172 // Client responds, push policy to store.
174 em::PolicyFetchResponse policy; 173 em::PolicyFetchResponse policy;
175 policy.set_policy_data("fake policy"); 174 policy.set_policy_data("fake policy");
176 client_->SetPolicy(policy); 175 client_.SetPolicy(policy);
177 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); 176 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1);
178 client_->NotifyPolicyFetched(); 177 client_.NotifyPolicyFetched();
179 178
180 // Trigger another policy fetch. 179 // Trigger another policy fetch.
181 EXPECT_CALL(*client_, FetchPolicy()).Times(1); 180 EXPECT_CALL(client_, FetchPolicy()).Times(1);
182 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, 181 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
183 base::Unretained(this))); 182 base::Unretained(this)));
184 183
185 // The store finishing the first load should not generate callbacks. 184 // The store finishing the first load should not generate callbacks.
186 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 185 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
187 store_.NotifyStoreLoaded(); 186 store_.NotifyStoreLoaded();
188 187
189 // Second policy fetch finishes. 188 // Second policy fetch finishes.
190 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); 189 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1);
191 client_->NotifyPolicyFetched(); 190 client_.NotifyPolicyFetched();
192 191
193 // Corresponding store operation finishes, all _three_ callbacks fire. 192 // Corresponding store operation finishes, all _three_ callbacks fire.
194 EXPECT_CALL(*this, OnPolicyRefresh()).Times(3); 193 EXPECT_CALL(*this, OnPolicyRefresh()).Times(3);
195 store_.NotifyStoreLoaded(); 194 store_.NotifyStoreLoaded();
196 } 195 }
197 196
198 } // namespace policy 197 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698