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

Side by Side Diff: chromeos/dbus/biod/biod_client_unittest.cc

Issue 2799043007: CrOS: Add success/failure indicators for biod methods with no callback. (Closed)
Patch Set: Created 3 years, 8 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "chromeos/dbus/biod/biod_client.h" 5 #include "chromeos/dbus/biod/biod_client.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 const std::vector<dbus::ObjectPath>& src_object_paths) { 45 const std::vector<dbus::ObjectPath>& src_object_paths) {
46 CHECK(dest_object_paths); 46 CHECK(dest_object_paths);
47 *dest_object_paths = src_object_paths; 47 *dest_object_paths = src_object_paths;
48 } 48 }
49 49
50 void CopyString(std::string* dest_str, const std::string& src_str) { 50 void CopyString(std::string* dest_str, const std::string& src_str) {
51 CHECK(dest_str); 51 CHECK(dest_str);
52 *dest_str = src_str; 52 *dest_str = src_str;
53 } 53 }
54 54
55 void CopyDBusMethodCallStatus(DBusMethodCallStatus* dest_status,
56 DBusMethodCallStatus src_status) {
57 *dest_status = src_status;
58 }
59
60 void CopyBiometricType(biod::BiometricType* dest_type,
61 biod::BiometricType src_type) {
62 *dest_type = src_type;
63 }
64
55 // Matcher that verifies that a dbus::Message has member |name|. 65 // Matcher that verifies that a dbus::Message has member |name|.
56 MATCHER_P(HasMember, name, "") { 66 MATCHER_P(HasMember, name, "") {
57 if (arg->GetMember() != name) { 67 if (arg->GetMember() != name) {
58 *result_listener << "has member " << arg->GetMember(); 68 *result_listener << "has member " << arg->GetMember();
59 return false; 69 return false;
60 } 70 }
61 return true; 71 return true;
62 } 72 }
63 73
64 // Runs |callback| with |response|. Needed due to ResponseCallback expecting a 74 // Runs |callback| with |response|. Needed due to ResponseCallback expecting a
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 138
129 client_.reset(BiodClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION)); 139 client_.reset(BiodClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION));
130 client_->Init(bus_.get()); 140 client_->Init(bus_.get());
131 141
132 // Execute callbacks posted by Init(). 142 // Execute callbacks posted by Init().
133 base::RunLoop().RunUntilIdle(); 143 base::RunLoop().RunUntilIdle();
134 } 144 }
135 145
136 protected: 146 protected:
137 // Add an expectation for method with |method_name| to be called. When the 147 // Add an expectation for method with |method_name| to be called. When the
138 // method is called the response shoudl match |response|. 148 // method is called the response should match |response|.
139 void AddMethodExpectation(const std::string& method_name, 149 void AddMethodExpectation(const std::string& method_name,
140 std::unique_ptr<dbus::Response> response) { 150 std::unique_ptr<dbus::Response> response) {
141 ASSERT_FALSE(pending_method_calls_.count(method_name)); 151 ASSERT_FALSE(pending_method_calls_.count(method_name));
142 pending_method_calls_[method_name] = std::move(response); 152 pending_method_calls_[method_name] = std::move(response);
143 EXPECT_CALL(*proxy_.get(), CallMethod(HasMember(method_name), _, _)) 153 EXPECT_CALL(*proxy_.get(), CallMethod(HasMember(method_name), _, _))
144 .WillOnce(Invoke(this, &BiodClientTest::OnCallMethod)); 154 .WillOnce(Invoke(this, &BiodClientTest::OnCallMethod));
145 } 155 }
146 156
157 void AddMethodWithErrorCallbackExpectation(
Daniel Erat 2017/04/08 00:12:34 per earlier comment, you probably don't need this
sammiequon 2017/04/08 01:03:30 Done.
158 const std::string& method_name,
159 std::unique_ptr<dbus::Response> response) {
160 ASSERT_FALSE(pending_method_calls_.count(method_name));
161 pending_method_calls_[method_name] = std::move(response);
162 EXPECT_CALL(*proxy_.get(),
163 CallMethodWithErrorCallback(HasMember(method_name), _, _, _))
164 .WillOnce(Invoke(this, &BiodClientTest::OnCallMethodWithErrorCallback));
165 }
166
147 // Synchronously passes |signal| to |client_|'s handler, simulating the signal 167 // Synchronously passes |signal| to |client_|'s handler, simulating the signal
148 // from biometrics. 168 // from biometrics.
149 void EmitSignal(dbus::Signal* signal) { 169 void EmitSignal(dbus::Signal* signal) {
150 const std::string signal_name = signal->GetMember(); 170 const std::string signal_name = signal->GetMember();
151 const auto it = signal_callbacks_.find(signal_name); 171 const auto it = signal_callbacks_.find(signal_name);
152 ASSERT_TRUE(it != signal_callbacks_.end()) 172 ASSERT_TRUE(it != signal_callbacks_.end())
153 << "Client didn't register for signal " << signal_name; 173 << "Client didn't register for signal " << signal_name;
154 it->second.Run(signal); 174 it->second.Run(signal);
155 } 175 }
156 176
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 auto it = pending_method_calls_.find(method_call->GetMember()); 247 auto it = pending_method_calls_.find(method_call->GetMember());
228 ASSERT_TRUE(it != pending_method_calls_.end()); 248 ASSERT_TRUE(it != pending_method_calls_.end());
229 auto pending_response = std::move(it->second); 249 auto pending_response = std::move(it->second);
230 pending_method_calls_.erase(it); 250 pending_method_calls_.erase(it);
231 251
232 message_loop_.task_runner()->PostTask( 252 message_loop_.task_runner()->PostTask(
233 FROM_HERE, base::Bind(&RunResponseCallback, callback, 253 FROM_HERE, base::Bind(&RunResponseCallback, callback,
234 base::Passed(&pending_response))); 254 base::Passed(&pending_response)));
235 } 255 }
236 256
257 void OnCallMethodWithErrorCallback(
Daniel Erat 2017/04/08 00:12:34 or this
sammiequon 2017/04/08 01:03:30 Done.
258 dbus::MethodCall* method_call,
259 int timeout_ms,
260 const dbus::ObjectProxy::ResponseCallback& callback,
261 const dbus::ObjectProxy::ErrorCallback& error_callback) {
262 auto it = pending_method_calls_.find(method_call->GetMember());
263 ASSERT_TRUE(it != pending_method_calls_.end());
264 auto pending_response = std::move(it->second);
265 pending_method_calls_.erase(it);
266
267 message_loop_.task_runner()->PostTask(
268 FROM_HERE, base::Bind(&RunResponseCallback, callback,
269 base::Passed(&pending_response)));
270 }
271
237 DISALLOW_COPY_AND_ASSIGN(BiodClientTest); 272 DISALLOW_COPY_AND_ASSIGN(BiodClientTest);
238 }; 273 };
239 274
240 TEST_F(BiodClientTest, TestStartEnrollSession) { 275 TEST_F(BiodClientTest, TestStartEnrollSession) {
241 const std::string kFakeId("fakeId"); 276 const std::string kFakeId("fakeId");
242 const std::string kFakeLabel("fakeLabel"); 277 const std::string kFakeLabel("fakeLabel");
243 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path")); 278 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
244 const dbus::ObjectPath kFakeObjectPath2(std::string("/fake/object/path2")); 279 const dbus::ObjectPath kFakeObjectPath2(std::string("/fake/object/path2"));
245 280
246 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 281 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 // object paths. Also, logs will get printed. 340 // object paths. Also, logs will get printed.
306 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, 341 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
307 nullptr); 342 nullptr);
308 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)}; 343 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)};
309 client_->GetRecordsForUser( 344 client_->GetRecordsForUser(
310 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); 345 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths));
311 base::RunLoop().RunUntilIdle(); 346 base::RunLoop().RunUntilIdle();
312 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths); 347 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths);
313 } 348 }
314 349
350 TEST_F(BiodClientTest, TestDestroyAllRecords) {
351 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
352 dbus::MessageWriter writer(response.get());
353
354 // Create a fake response with nothing. The destroy all records call should
355 // return success.
356 AddMethodWithErrorCallbackExpectation(
357 biod::kBiometricsManagerDestroyAllRecordsMethod, std::move(response));
358 DBusMethodCallStatus returned_status = static_cast<DBusMethodCallStatus>(-1);
359 client_->DestroyAllRecords(
360 base::Bind(&CopyDBusMethodCallStatus, &returned_status));
361 base::RunLoop().RunUntilIdle();
362 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, returned_status);
363
364 // Create a fake response with nothing. The destroy all records call should
365 // return failure.
366 AddMethodWithErrorCallbackExpectation(
367 biod::kBiometricsManagerDestroyAllRecordsMethod, nullptr);
368 returned_status = static_cast<DBusMethodCallStatus>(-1);
369 client_->DestroyAllRecords(
370 base::Bind(&CopyDBusMethodCallStatus, &returned_status));
371 base::RunLoop().RunUntilIdle();
372 EXPECT_EQ(DBUS_METHOD_CALL_FAILURE, returned_status);
373 }
374
315 TEST_F(BiodClientTest, TestStartAuthentication) { 375 TEST_F(BiodClientTest, TestStartAuthentication) {
316 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path")); 376 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
317 377
318 // Create a fake response with a fake object path. The start authentication 378 // Create a fake response with a fake object path. The start authentication
319 // call should return this object path. 379 // call should return this object path.
320 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 380 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
321 dbus::MessageWriter writer(response.get()); 381 dbus::MessageWriter writer(response.get());
322 writer.AppendObjectPath(kFakeObjectPath); 382 writer.AppendObjectPath(kFakeObjectPath);
323 383
324 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, 384 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
(...skipping 15 matching lines...) Expand all
340 dbus::MessageWriter bad_writer(bad_response.get()); 400 dbus::MessageWriter bad_writer(bad_response.get());
341 bad_writer.AppendString(""); 401 bad_writer.AppendString("");
342 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, 402 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
343 std::move(bad_response)); 403 std::move(bad_response));
344 returned_path = dbus::ObjectPath(kInvalidTestPath); 404 returned_path = dbus::ObjectPath(kInvalidTestPath);
345 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 405 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path));
346 base::RunLoop().RunUntilIdle(); 406 base::RunLoop().RunUntilIdle();
347 EXPECT_EQ(dbus::ObjectPath(), returned_path); 407 EXPECT_EQ(dbus::ObjectPath(), returned_path);
348 } 408 }
349 409
410 TEST_F(BiodClientTest, TestRequestBiometricType) {
411 const biod::BiometricType kFakeBiometricType =
412 biod::BiometricType::BIOMETRIC_TYPE_FINGERPRINT;
413
414 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
415 dbus::MessageWriter writer(response.get());
416 writer.AppendVariantOfUint32(static_cast<uint32_t>(kFakeBiometricType));
417
418 // Create a fake response with biometric type. The get label call should
419 // return this exact biometric type.
420 biod::BiometricType returned_biometric_type =
421 biod::BiometricType::BIOMETRIC_TYPE_MAX;
422 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
423 client_->RequestType(
424 base::Bind(&CopyBiometricType, &returned_biometric_type));
425 base::RunLoop().RunUntilIdle();
426 EXPECT_EQ(kFakeBiometricType, returned_biometric_type);
427
428 // Verify that by sending a empty reponse the response is an unknown biometric
429 // type. Also, logs will get printed.
Daniel Erat 2017/04/08 00:12:35 what's the reason for mentioning that messages wil
sammiequon 2017/04/08 01:03:30 No, I thought it would be a good heads ups for fil
Daniel Erat 2017/04/08 01:06:03 i think that switching to CallMethod (and letting
sammiequon 2017/04/08 01:31:47 I see. I think think spam was the wrong word, it w
430 returned_biometric_type = biod::BiometricType::BIOMETRIC_TYPE_MAX;
431 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
432 client_->RequestType(
433 base::Bind(&CopyBiometricType, &returned_biometric_type));
434 base::RunLoop().RunUntilIdle();
435 EXPECT_EQ(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN,
436 returned_biometric_type);
437 }
438
439 TEST_F(BiodClientTest, TestRequestRecordLabel) {
440 const std::string kFakeLabel("fakeLabel");
441 const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path"));
442
443 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
444 dbus::MessageWriter writer(response.get());
445 writer.AppendString(kFakeLabel);
446
447 // Create a fake response with string. The get label call should return this
448 // exact string.
449 std::string returned_label = kInvalidString;
450 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
451 client_->RequestRecordLabel(kFakeRecordPath,
452 base::Bind(&CopyString, &returned_label));
453 base::RunLoop().RunUntilIdle();
454 EXPECT_EQ(kFakeLabel, returned_label);
455
456 // Verify that by sending a empty reponse the response is an empty string.
457 // Also, logs will get printed.
458 returned_label = kInvalidString;
459 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
460 client_->RequestRecordLabel(kFakeRecordPath,
461 base::Bind(&CopyString, &returned_label));
462 base::RunLoop().RunUntilIdle();
463 EXPECT_EQ("", returned_label);
464 }
465
350 // Verify when signals are mocked, an observer will catch the signals as 466 // Verify when signals are mocked, an observer will catch the signals as
351 // expected. 467 // expected.
352 TEST_F(BiodClientTest, TestNotifyObservers) { 468 TEST_F(BiodClientTest, TestNotifyObservers) {
353 TestBiodObserver observer; 469 TestBiodObserver observer;
354 client_->AddObserver(&observer); 470 client_->AddObserver(&observer);
355 EXPECT_TRUE(client_->HasObserver(&observer)); 471 EXPECT_TRUE(client_->HasObserver(&observer));
356 472
357 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS; 473 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS;
358 const bool enroll_session_complete = false; 474 const bool enroll_session_complete = false;
359 const AuthScanMatches test_attempt; 475 const AuthScanMatches test_attempt;
(...skipping 11 matching lines...) Expand all
371 EXPECT_EQ(1, observer.num_failures_received()); 487 EXPECT_EQ(1, observer.num_failures_received());
372 488
373 client_->RemoveObserver(&observer); 489 client_->RemoveObserver(&observer);
374 490
375 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete); 491 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete);
376 EmitAuthScanDoneSignal(scan_signal, test_attempt); 492 EmitAuthScanDoneSignal(scan_signal, test_attempt);
377 EXPECT_EQ(1, observer.num_enroll_scans_received()); 493 EXPECT_EQ(1, observer.num_enroll_scans_received());
378 EXPECT_EQ(1, observer.num_auth_scans_received()); 494 EXPECT_EQ(1, observer.num_auth_scans_received());
379 EXPECT_EQ(1, observer.num_failures_received()); 495 EXPECT_EQ(1, observer.num_failures_received());
380 } 496 }
381
382 TEST_F(BiodClientTest, TestGetRecordLabel) {
383 const std::string kFakeLabel("fakeLabel");
384 const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path"));
385
386 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
387 dbus::MessageWriter writer(response.get());
388 writer.AppendString(kFakeLabel);
389
390 // Create a fake response with string. The get label call should return this
391 // exact string.
392 std::string returned_label = kInvalidString;
393 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
394 client_->RequestRecordLabel(kFakeRecordPath,
395 base::Bind(&CopyString, &returned_label));
396 base::RunLoop().RunUntilIdle();
397 EXPECT_EQ(kFakeLabel, returned_label);
398
399 // Verify that by sending a empty reponse the response is an empty string.
400 // Also, logs will get printed.
401 returned_label = kInvalidString;
402 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
403 client_->RequestRecordLabel(kFakeRecordPath,
404 base::Bind(&CopyString, &returned_label));
405 base::RunLoop().RunUntilIdle();
406 EXPECT_EQ("", returned_label);
407 }
408 } // namespace chromeos 497 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698