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

Side by Side Diff: chromeos/attestation/attestation_flow_unittest.cc

Issue 11932004: Implemented attestation message flow for Chrome OS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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
(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 "base/bind.h"
6 #include "base/message_loop.h"
7 #include "chromeos/attestation/mock_attestation_flow.h"
8 #include "chromeos/cryptohome/mock_async_method_caller.h"
9 #include "chromeos/dbus/mock_cryptohome_client.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 using testing::_;
14 using testing::Invoke;
15 using testing::StrictMock;
16 using testing::WithArgs;
17
18 namespace chromeos {
19 namespace attestation {
20
21 namespace {
22
23 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) {
24 MessageLoop::current()->PostTask(
25 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false));
26 }
27
28 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) {
29 MessageLoop::current()->PostTask(
30 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
31 }
32
33 void DBusCallbackFail(const BoolDBusMethodCallback& callback) {
34 MessageLoop::current()->PostTask(
35 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_FAILURE, false));
36 }
37
38 void AsyncCallbackFalse(cryptohome::AsyncMethodCaller::Callback callback) {
39 callback.Run(false, cryptohome::MOUNT_ERROR_NONE);
40 }
41
42 } // namespace
43
44 TEST(AttestationTest, GetCertificate) {
45 // Use StrictMock when it is important that calls get triggered exactly once.
Mattias Nissler (ping if slow) 2013/01/18 13:38:46 This comment is not accurate: "Exactly once" you s
dkrahn 2013/01/22 22:50:11 Yes - comment corrected.
46 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
47 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE);
48 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_))
49 .Times(1);
50 std::string fake_enroll_response =
51 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest;
52 fake_enroll_response += "_response";
53 EXPECT_CALL(async_caller, AsyncTpmAttestationEnroll(fake_enroll_response, _))
54 .Times(1);
55 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(false, _))
56 .Times(1);
57 std::string fake_cert_response =
58 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest;
59 fake_cert_response += "_response";
60 EXPECT_CALL(async_caller,
61 AsyncTpmAttestationFinishCertRequest(fake_cert_response, _))
62 .Times(1);
Mattias Nissler (ping if slow) 2013/01/18 13:38:46 Suggestion: It looks like that you might want to c
dkrahn 2013/01/22 22:50:11 Good idea. Done.
63
64 chromeos::MockCryptohomeClient client;
65 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
66 .WillRepeatedly(Invoke(DBusCallbackFalse));
67
68 StrictMock<MockServerProxy> proxy;
69 proxy.DeferToFake(true);
70 EXPECT_CALL(proxy, SendEnrollRequest(
71 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest,
72 _)).Times(1);
73 EXPECT_CALL(proxy, SendCertificateRequest(
74 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest,
75 _)).Times(1);
76
77 StrictMock<MockObserver> observer;
78 EXPECT_CALL(observer, MockCertificateCallback(
79 true,
80 cryptohome::MockAsyncMethodCaller::kFakeAttestationCert)).Times(1);
81 AttestationFlow::CertificateCallback mock_callback = base::Bind(
82 &MockObserver::MockCertificateCallback,
83 base::Unretained(&observer));
84
85 MessageLoop message_loop;
86 AttestationFlow flow(&async_caller, &client, &proxy);
87 flow.GetCertificate("test", mock_callback);
88 message_loop.RunUntilIdle();
Mattias Nissler (ping if slow) 2013/01/18 13:38:46 according to base/message_loop.h you should use ba
dkrahn 2013/01/22 22:50:11 Done.
89 }
90
91 TEST(AttestationTest, GetCertificate_NoEK) {
92 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
93 async_caller.SetUp(false, cryptohome::MOUNT_ERROR_NONE);
94 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_))
95 .Times(1);
96
97 chromeos::MockCryptohomeClient client;
98 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
99 .WillRepeatedly(Invoke(DBusCallbackFalse));
100
101 // We're not expecting any server calls in this case.
102 StrictMock<MockServerProxy> proxy;
103
104 StrictMock<MockObserver> observer;
105 EXPECT_CALL(observer, MockCertificateCallback(false, ""))
106 .Times(1);
107 AttestationFlow::CertificateCallback mock_callback = base::Bind(
108 &MockObserver::MockCertificateCallback,
109 base::Unretained(&observer));
110
111 MessageLoop message_loop;
112 AttestationFlow flow(&async_caller, &client, &proxy);
113 flow.GetCertificate("test", mock_callback);
114 message_loop.RunUntilIdle();
115 }
116
117 TEST(AttestationTest, GetCertificate_EKRejected) {
118 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
119 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE);
120 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_))
121 .Times(1);
122
123 chromeos::MockCryptohomeClient client;
124 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
125 .WillRepeatedly(Invoke(DBusCallbackFalse));
126
127 StrictMock<MockServerProxy> proxy;
128 proxy.DeferToFake(false);
129 EXPECT_CALL(proxy, SendEnrollRequest(
130 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest,
131 _)).Times(1);
132
133 StrictMock<MockObserver> observer;
134 EXPECT_CALL(observer, MockCertificateCallback(false, ""))
135 .Times(1);
136 AttestationFlow::CertificateCallback mock_callback = base::Bind(
137 &MockObserver::MockCertificateCallback,
138 base::Unretained(&observer));
139
140 MessageLoop message_loop;
141 AttestationFlow flow(&async_caller, &client, &proxy);
142 flow.GetCertificate("test", mock_callback);
143 message_loop.RunUntilIdle();
144 }
145
146 TEST(AttestationTest, GetCertificate_FailEnroll) {
147 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
148 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE);
149 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_))
150 .Times(1);
151 std::string fake_enroll_response =
152 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest;
153 fake_enroll_response += "_response";
154 EXPECT_CALL(async_caller, AsyncTpmAttestationEnroll(fake_enroll_response, _))
155 .WillOnce(WithArgs<1>(Invoke(AsyncCallbackFalse)));
156
157 chromeos::MockCryptohomeClient client;
158 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
159 .WillRepeatedly(Invoke(DBusCallbackFalse));
160
161 StrictMock<MockServerProxy> proxy;
162 proxy.DeferToFake(true);
163 EXPECT_CALL(proxy, SendEnrollRequest(
164 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest,
165 _)).Times(1);
166
167 StrictMock<MockObserver> observer;
168 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1);
169 AttestationFlow::CertificateCallback mock_callback = base::Bind(
170 &MockObserver::MockCertificateCallback,
171 base::Unretained(&observer));
172
173 MessageLoop message_loop;
174 AttestationFlow flow(&async_caller, &client, &proxy);
175 flow.GetCertificate("test", mock_callback);
176 message_loop.RunUntilIdle();
177 }
178
179 TEST(AttestationTest, GetOwnerCertificateAlreadyEnrolled) {
180 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
181 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE);
182 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(true, _))
183 .Times(1);
184 std::string fake_cert_response =
185 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest;
186 fake_cert_response += "_response";
187 EXPECT_CALL(async_caller,
188 AsyncTpmAttestationFinishCertRequest(fake_cert_response, _))
189 .Times(1);
190
191 chromeos::MockCryptohomeClient client;
192 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
193 .WillRepeatedly(Invoke(DBusCallbackTrue));
194
195 StrictMock<MockServerProxy> proxy;
196 proxy.DeferToFake(true);
197 EXPECT_CALL(proxy, SendCertificateRequest(
198 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest,
199 _)).Times(1);
200
201 StrictMock<MockObserver> observer;
202 EXPECT_CALL(observer, MockCertificateCallback(
203 true,
204 cryptohome::MockAsyncMethodCaller::kFakeAttestationCert)).Times(1);
205 AttestationFlow::CertificateCallback mock_callback = base::Bind(
206 &MockObserver::MockCertificateCallback,
207 base::Unretained(&observer));
208
209 MessageLoop message_loop;
210 AttestationFlow flow(&async_caller, &client, &proxy);
211 flow.GetCertificate("attest-ent-machine", mock_callback);
212 message_loop.RunUntilIdle();
213 }
214
215 TEST(AttestationTest, GetCertificate_FailCreateCertRequest) {
216 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
217 async_caller.SetUp(false, cryptohome::MOUNT_ERROR_NONE);
218 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(false, _))
219 .Times(1);
220
221 chromeos::MockCryptohomeClient client;
222 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
223 .WillRepeatedly(Invoke(DBusCallbackTrue));
224
225 // We're not expecting any server calls in this case.
226 StrictMock<MockServerProxy> proxy;
227
228 StrictMock<MockObserver> observer;
229 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1);
230 AttestationFlow::CertificateCallback mock_callback = base::Bind(
231 &MockObserver::MockCertificateCallback,
232 base::Unretained(&observer));
233
234 MessageLoop message_loop;
235 AttestationFlow flow(&async_caller, &client, &proxy);
236 flow.GetCertificate("test", mock_callback);
237 message_loop.RunUntilIdle();
238 }
239
240 TEST(AttestationTest, GetCertificate_CertRequestRejected) {
241 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
242 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE);
243 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(false, _))
244 .Times(1);
245
246 chromeos::MockCryptohomeClient client;
247 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
248 .WillRepeatedly(Invoke(DBusCallbackTrue));
249
250 StrictMock<MockServerProxy> proxy;
251 proxy.DeferToFake(false);
252 EXPECT_CALL(proxy, SendCertificateRequest(
253 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest,
254 _)).Times(1);
255
256 StrictMock<MockObserver> observer;
257 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1);
258 AttestationFlow::CertificateCallback mock_callback = base::Bind(
259 &MockObserver::MockCertificateCallback,
260 base::Unretained(&observer));
261
262 MessageLoop message_loop;
263 AttestationFlow flow(&async_caller, &client, &proxy);
264 flow.GetCertificate("test", mock_callback);
265 message_loop.RunUntilIdle();
266 }
267
268 TEST(AttestationTest, GetCertificate_FailIsEnrolled) {
Mattias Nissler (ping if slow) 2013/01/18 13:38:46 What's the difference between this test and GetCer
dkrahn 2013/01/22 22:50:11 The difference is the use of 'DBusCallbackFail' as
269 // Not expecting any calls in this case.
270 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller;
271
272 chromeos::MockCryptohomeClient client;
273 EXPECT_CALL(client, TpmAttestationIsEnrolled(_))
274 .WillRepeatedly(Invoke(DBusCallbackFail));
275
276 // Not expecting any calls in this case.
277 StrictMock<MockServerProxy> proxy;
278
279 StrictMock<MockObserver> observer;
280 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1);
281 AttestationFlow::CertificateCallback mock_callback = base::Bind(
282 &MockObserver::MockCertificateCallback,
283 base::Unretained(&observer));
284
285 MessageLoop message_loop;
286 AttestationFlow flow(&async_caller, &client, &proxy);
287 flow.GetCertificate("test", mock_callback);
288 message_loop.RunUntilIdle();
289 }
290
291 } // namespace attestation
292 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698