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

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: Fixed patch set 2 errors. 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
« no previous file with comments | « chromeos/dbus/biod/biod_client.cc ('k') | chromeos/dbus/biod/fake_biod_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 CHECK(dest_status);
58 *dest_status = src_status;
59 }
60
61 void CopyBiometricType(biod::BiometricType* dest_type,
62 biod::BiometricType src_type) {
63 CHECK(dest_type);
64 *dest_type = src_type;
65 }
66
55 // Matcher that verifies that a dbus::Message has member |name|. 67 // Matcher that verifies that a dbus::Message has member |name|.
56 MATCHER_P(HasMember, name, "") { 68 MATCHER_P(HasMember, name, "") {
57 if (arg->GetMember() != name) { 69 if (arg->GetMember() != name) {
58 *result_listener << "has member " << arg->GetMember(); 70 *result_listener << "has member " << arg->GetMember();
59 return false; 71 return false;
60 } 72 }
61 return true; 73 return true;
62 } 74 }
63 75
64 // Runs |callback| with |response|. Needed due to ResponseCallback expecting a 76 // Runs |callback| with |response|. Needed due to ResponseCallback expecting a
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 140
129 client_.reset(BiodClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION)); 141 client_.reset(BiodClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION));
130 client_->Init(bus_.get()); 142 client_->Init(bus_.get());
131 143
132 // Execute callbacks posted by Init(). 144 // Execute callbacks posted by Init().
133 base::RunLoop().RunUntilIdle(); 145 base::RunLoop().RunUntilIdle();
134 } 146 }
135 147
136 protected: 148 protected:
137 // Add an expectation for method with |method_name| to be called. When the 149 // Add an expectation for method with |method_name| to be called. When the
138 // method is called the response shoudl match |response|. 150 // method is called the response should match |response|.
139 void AddMethodExpectation(const std::string& method_name, 151 void AddMethodExpectation(const std::string& method_name,
140 std::unique_ptr<dbus::Response> response) { 152 std::unique_ptr<dbus::Response> response) {
141 ASSERT_FALSE(pending_method_calls_.count(method_name)); 153 ASSERT_FALSE(pending_method_calls_.count(method_name));
142 pending_method_calls_[method_name] = std::move(response); 154 pending_method_calls_[method_name] = std::move(response);
143 EXPECT_CALL(*proxy_.get(), CallMethod(HasMember(method_name), _, _)) 155 EXPECT_CALL(*proxy_.get(), CallMethod(HasMember(method_name), _, _))
144 .WillOnce(Invoke(this, &BiodClientTest::OnCallMethod)); 156 .WillOnce(Invoke(this, &BiodClientTest::OnCallMethod));
145 } 157 }
146 158
147 // Synchronously passes |signal| to |client_|'s handler, simulating the signal 159 // Synchronously passes |signal| to |client_|'s handler, simulating the signal
148 // from biometrics. 160 // from biometrics.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 // call should return this object path. 263 // call should return this object path.
252 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, 264 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
253 std::move(response)); 265 std::move(response));
254 dbus::ObjectPath returned_path(kInvalidTestPath); 266 dbus::ObjectPath returned_path(kInvalidTestPath);
255 client_->StartEnrollSession(kFakeId, kFakeLabel, 267 client_->StartEnrollSession(kFakeId, kFakeLabel,
256 base::Bind(&CopyObjectPath, &returned_path)); 268 base::Bind(&CopyObjectPath, &returned_path));
257 base::RunLoop().RunUntilIdle(); 269 base::RunLoop().RunUntilIdle();
258 EXPECT_EQ(kFakeObjectPath, returned_path); 270 EXPECT_EQ(kFakeObjectPath, returned_path);
259 271
260 // Verify that by sending a empty reponse or a improperly formatted one, the 272 // Verify that by sending a empty reponse or a improperly formatted one, the
261 // response is an empty object path. Also, logs will get printed. 273 // response is an empty object path.
262 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, 274 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
263 nullptr); 275 nullptr);
264 returned_path = dbus::ObjectPath(kInvalidTestPath); 276 returned_path = dbus::ObjectPath(kInvalidTestPath);
265 client_->StartEnrollSession(kFakeId, kFakeLabel, 277 client_->StartEnrollSession(kFakeId, kFakeLabel,
266 base::Bind(&CopyObjectPath, &returned_path)); 278 base::Bind(&CopyObjectPath, &returned_path));
267 base::RunLoop().RunUntilIdle(); 279 base::RunLoop().RunUntilIdle();
268 EXPECT_EQ(dbus::ObjectPath(), returned_path); 280 EXPECT_EQ(dbus::ObjectPath(), returned_path);
269 281
270 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty()); 282 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty());
271 dbus::MessageWriter bad_writer(bad_response.get()); 283 dbus::MessageWriter bad_writer(bad_response.get());
(...skipping 23 matching lines...) Expand all
295 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, 307 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
296 std::move(response)); 308 std::move(response));
297 std::vector<dbus::ObjectPath> returned_object_paths = { 309 std::vector<dbus::ObjectPath> returned_object_paths = {
298 dbus::ObjectPath(kInvalidTestPath)}; 310 dbus::ObjectPath(kInvalidTestPath)};
299 client_->GetRecordsForUser( 311 client_->GetRecordsForUser(
300 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); 312 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths));
301 base::RunLoop().RunUntilIdle(); 313 base::RunLoop().RunUntilIdle();
302 EXPECT_EQ(kFakeObjectPaths, returned_object_paths); 314 EXPECT_EQ(kFakeObjectPaths, returned_object_paths);
303 315
304 // Verify that by sending a empty reponse the response is an empty array of 316 // Verify that by sending a empty reponse the response is an empty array of
305 // object paths. Also, logs will get printed. 317 // object paths.
306 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, 318 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
307 nullptr); 319 nullptr);
308 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)}; 320 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)};
309 client_->GetRecordsForUser( 321 client_->GetRecordsForUser(
310 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); 322 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths));
311 base::RunLoop().RunUntilIdle(); 323 base::RunLoop().RunUntilIdle();
312 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths); 324 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths);
313 } 325 }
314 326
327 TEST_F(BiodClientTest, TestDestroyAllRecords) {
328 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
329 dbus::MessageWriter writer(response.get());
330
331 // Create an empty response to simulate success.
332 AddMethodExpectation(biod::kBiometricsManagerDestroyAllRecordsMethod,
333 std::move(response));
334 DBusMethodCallStatus returned_status = static_cast<DBusMethodCallStatus>(-1);
335 client_->DestroyAllRecords(
336 base::Bind(&CopyDBusMethodCallStatus, &returned_status));
337 base::RunLoop().RunUntilIdle();
338 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, returned_status);
339
340 // Return an empty response to simulate failure.
Daniel Erat 2017/04/08 14:55:55 s/an empty/a null/
sammiequon 2017/04/10 18:10:59 Done.
341 AddMethodExpectation(biod::kBiometricsManagerDestroyAllRecordsMethod,
342 nullptr);
343 returned_status = static_cast<DBusMethodCallStatus>(-1);
344 client_->DestroyAllRecords(
345 base::Bind(&CopyDBusMethodCallStatus, &returned_status));
346 base::RunLoop().RunUntilIdle();
347 EXPECT_EQ(DBUS_METHOD_CALL_FAILURE, returned_status);
348 }
349
315 TEST_F(BiodClientTest, TestStartAuthentication) { 350 TEST_F(BiodClientTest, TestStartAuthentication) {
316 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path")); 351 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
317 352
318 // Create a fake response with a fake object path. The start authentication 353 // Create a fake response with a fake object path. The start authentication
319 // call should return this object path. 354 // call should return this object path.
320 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 355 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
321 dbus::MessageWriter writer(response.get()); 356 dbus::MessageWriter writer(response.get());
322 writer.AppendObjectPath(kFakeObjectPath); 357 writer.AppendObjectPath(kFakeObjectPath);
323 358
324 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, 359 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
325 std::move(response)); 360 std::move(response));
326 dbus::ObjectPath returned_path(kInvalidTestPath); 361 dbus::ObjectPath returned_path(kInvalidTestPath);
327 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 362 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path));
328 base::RunLoop().RunUntilIdle(); 363 base::RunLoop().RunUntilIdle();
329 EXPECT_EQ(kFakeObjectPath, returned_path); 364 EXPECT_EQ(kFakeObjectPath, returned_path);
330 365
331 // Verify that by sending a empty reponse or a improperly formatted one, the 366 // Verify that by sending a empty reponse or a improperly formatted one, the
332 // response is an empty object path. Also, logs will get printed. 367 // response is an empty object path.
333 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, nullptr); 368 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, nullptr);
334 returned_path = dbus::ObjectPath(kInvalidTestPath); 369 returned_path = dbus::ObjectPath(kInvalidTestPath);
335 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 370 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path));
336 base::RunLoop().RunUntilIdle(); 371 base::RunLoop().RunUntilIdle();
337 EXPECT_EQ(dbus::ObjectPath(), returned_path); 372 EXPECT_EQ(dbus::ObjectPath(), returned_path);
338 373
339 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty()); 374 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty());
340 dbus::MessageWriter bad_writer(bad_response.get()); 375 dbus::MessageWriter bad_writer(bad_response.get());
341 bad_writer.AppendString(""); 376 bad_writer.AppendString("");
342 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, 377 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
343 std::move(bad_response)); 378 std::move(bad_response));
344 returned_path = dbus::ObjectPath(kInvalidTestPath); 379 returned_path = dbus::ObjectPath(kInvalidTestPath);
345 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 380 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path));
346 base::RunLoop().RunUntilIdle(); 381 base::RunLoop().RunUntilIdle();
347 EXPECT_EQ(dbus::ObjectPath(), returned_path); 382 EXPECT_EQ(dbus::ObjectPath(), returned_path);
348 } 383 }
349 384
385 TEST_F(BiodClientTest, TestRequestBiometricType) {
386 const biod::BiometricType kFakeBiometricType =
387 biod::BIOMETRIC_TYPE_FINGERPRINT;
388
389 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
390 dbus::MessageWriter writer(response.get());
391 writer.AppendVariantOfUint32(static_cast<uint32_t>(kFakeBiometricType));
392
393 // Create a fake response with biometric type. The get label call should
394 // return this exact biometric type.
395 biod::BiometricType returned_biometric_type = biod::BIOMETRIC_TYPE_MAX;
396 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
397 client_->RequestType(
398 base::Bind(&CopyBiometricType, &returned_biometric_type));
399 base::RunLoop().RunUntilIdle();
400 EXPECT_EQ(kFakeBiometricType, returned_biometric_type);
401
402 // Verify that by sending a empty reponse the response is an unknown biometric
Daniel Erat 2017/04/08 14:55:55 s/empty/null/
sammiequon 2017/04/10 18:10:59 Done.
403 // type.
404 returned_biometric_type = biod::BIOMETRIC_TYPE_MAX;
405 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
406 client_->RequestType(
407 base::Bind(&CopyBiometricType, &returned_biometric_type));
408 base::RunLoop().RunUntilIdle();
409 EXPECT_EQ(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN,
410 returned_biometric_type);
411 }
412
413 TEST_F(BiodClientTest, TestRequestRecordLabel) {
414 const std::string kFakeLabel("fakeLabel");
415 const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path"));
416
417 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
418 dbus::MessageWriter writer(response.get());
419 writer.AppendString(kFakeLabel);
420
421 // Create a fake response with string. The get label call should return this
422 // exact string.
423 std::string returned_label = kInvalidString;
424 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
425 client_->RequestRecordLabel(kFakeRecordPath,
426 base::Bind(&CopyString, &returned_label));
427 base::RunLoop().RunUntilIdle();
428 EXPECT_EQ(kFakeLabel, returned_label);
429
430 // Verify that by sending a empty reponse the result is an empty string.
Daniel Erat 2017/04/08 14:55:55 s/empty/null/
sammiequon 2017/04/10 18:10:59 Done.
431 returned_label = kInvalidString;
432 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
433 client_->RequestRecordLabel(kFakeRecordPath,
434 base::Bind(&CopyString, &returned_label));
435 base::RunLoop().RunUntilIdle();
436 EXPECT_EQ("", returned_label);
437 }
438
350 // Verify when signals are mocked, an observer will catch the signals as 439 // Verify when signals are mocked, an observer will catch the signals as
351 // expected. 440 // expected.
352 TEST_F(BiodClientTest, TestNotifyObservers) { 441 TEST_F(BiodClientTest, TestNotifyObservers) {
353 TestBiodObserver observer; 442 TestBiodObserver observer;
354 client_->AddObserver(&observer); 443 client_->AddObserver(&observer);
355 EXPECT_TRUE(client_->HasObserver(&observer)); 444 EXPECT_TRUE(client_->HasObserver(&observer));
356 445
357 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS; 446 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS;
358 const bool enroll_session_complete = false; 447 const bool enroll_session_complete = false;
359 const AuthScanMatches test_attempt; 448 const AuthScanMatches test_attempt;
(...skipping 11 matching lines...) Expand all
371 EXPECT_EQ(1, observer.num_failures_received()); 460 EXPECT_EQ(1, observer.num_failures_received());
372 461
373 client_->RemoveObserver(&observer); 462 client_->RemoveObserver(&observer);
374 463
375 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete); 464 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete);
376 EmitAuthScanDoneSignal(scan_signal, test_attempt); 465 EmitAuthScanDoneSignal(scan_signal, test_attempt);
377 EXPECT_EQ(1, observer.num_enroll_scans_received()); 466 EXPECT_EQ(1, observer.num_enroll_scans_received());
378 EXPECT_EQ(1, observer.num_auth_scans_received()); 467 EXPECT_EQ(1, observer.num_auth_scans_received());
379 EXPECT_EQ(1, observer.num_failures_received()); 468 EXPECT_EQ(1, observer.num_failures_received());
380 } 469 }
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 470 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/biod/biod_client.cc ('k') | chromeos/dbus/biod/fake_biod_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698