Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 |
| 147 // Synchronously passes |signal| to |client_|'s handler, simulating the signal | 157 // Synchronously passes |signal| to |client_|'s handler, simulating the signal |
| 148 // from biometrics. | 158 // from biometrics. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 // call should return this object path. | 261 // call should return this object path. |
| 252 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, | 262 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, |
| 253 std::move(response)); | 263 std::move(response)); |
| 254 dbus::ObjectPath returned_path(kInvalidTestPath); | 264 dbus::ObjectPath returned_path(kInvalidTestPath); |
| 255 client_->StartEnrollSession(kFakeId, kFakeLabel, | 265 client_->StartEnrollSession(kFakeId, kFakeLabel, |
| 256 base::Bind(&CopyObjectPath, &returned_path)); | 266 base::Bind(&CopyObjectPath, &returned_path)); |
| 257 base::RunLoop().RunUntilIdle(); | 267 base::RunLoop().RunUntilIdle(); |
| 258 EXPECT_EQ(kFakeObjectPath, returned_path); | 268 EXPECT_EQ(kFakeObjectPath, returned_path); |
| 259 | 269 |
| 260 // Verify that by sending a empty reponse or a improperly formatted one, the | 270 // 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. | 271 // response is an empty object path. |
| 262 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, | 272 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, |
| 263 nullptr); | 273 nullptr); |
| 264 returned_path = dbus::ObjectPath(kInvalidTestPath); | 274 returned_path = dbus::ObjectPath(kInvalidTestPath); |
| 265 client_->StartEnrollSession(kFakeId, kFakeLabel, | 275 client_->StartEnrollSession(kFakeId, kFakeLabel, |
| 266 base::Bind(&CopyObjectPath, &returned_path)); | 276 base::Bind(&CopyObjectPath, &returned_path)); |
| 267 base::RunLoop().RunUntilIdle(); | 277 base::RunLoop().RunUntilIdle(); |
| 268 EXPECT_EQ(dbus::ObjectPath(), returned_path); | 278 EXPECT_EQ(dbus::ObjectPath(), returned_path); |
| 269 | 279 |
| 270 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty()); | 280 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty()); |
| 271 dbus::MessageWriter bad_writer(bad_response.get()); | 281 dbus::MessageWriter bad_writer(bad_response.get()); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 295 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, | 305 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, |
| 296 std::move(response)); | 306 std::move(response)); |
| 297 std::vector<dbus::ObjectPath> returned_object_paths = { | 307 std::vector<dbus::ObjectPath> returned_object_paths = { |
| 298 dbus::ObjectPath(kInvalidTestPath)}; | 308 dbus::ObjectPath(kInvalidTestPath)}; |
| 299 client_->GetRecordsForUser( | 309 client_->GetRecordsForUser( |
| 300 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); | 310 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); |
| 301 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 302 EXPECT_EQ(kFakeObjectPaths, returned_object_paths); | 312 EXPECT_EQ(kFakeObjectPaths, returned_object_paths); |
| 303 | 313 |
| 304 // Verify that by sending a empty reponse the response is an empty array of | 314 // Verify that by sending a empty reponse the response is an empty array of |
| 305 // object paths. Also, logs will get printed. | 315 // object paths. |
| 306 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, | 316 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, |
| 307 nullptr); | 317 nullptr); |
| 308 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)}; | 318 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)}; |
| 309 client_->GetRecordsForUser( | 319 client_->GetRecordsForUser( |
| 310 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); | 320 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); |
| 311 base::RunLoop().RunUntilIdle(); | 321 base::RunLoop().RunUntilIdle(); |
| 312 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths); | 322 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths); |
| 313 } | 323 } |
| 314 | 324 |
| 325 TEST_F(BiodClientTest, TestDestroyAllRecords) { | |
| 326 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 327 dbus::MessageWriter writer(response.get()); | |
| 328 | |
| 329 // Create a fake response with nothing. The destroy all records call should | |
|
Daniel Erat
2017/04/08 01:13:32
"Create an empty response to simulate success."
sammiequon
2017/04/08 01:31:48
Done.
| |
| 330 // return success. | |
| 331 AddMethodExpectation(biod::kBiometricsManagerDestroyAllRecordsMethod, | |
| 332 std::move(response)); | |
| 333 DBusMethodCallStatus returned_status = static_cast<DBusMethodCallStatus>(-1); | |
| 334 client_->DestroyAllRecords( | |
| 335 base::Bind(&CopyDBusMethodCallStatus, &returned_status)); | |
| 336 base::RunLoop().RunUntilIdle(); | |
| 337 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, returned_status); | |
| 338 | |
| 339 // Create a fake response with nothing. The destroy all records call should | |
|
Daniel Erat
2017/04/08 01:13:32
"Return an empty response to simulate failure." wo
sammiequon
2017/04/08 01:31:48
Done.
| |
| 340 // return failure. | |
| 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::BiometricType::BIOMETRIC_TYPE_FINGERPRINT; | |
|
Daniel Erat
2017/04/08 01:13:32
oh, this isn't actually an enum class, right? i th
sammiequon
2017/04/08 01:31:48
Is it prefer not to. I usually just did it anyways
Daniel Erat
2017/04/08 01:39:23
yep, shorter's better. iirc the BIOMETRIC_TYPE_ pr
sammiequon
2017/04/08 04:43:46
Acknowledged.
| |
| 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 = | |
| 396 biod::BiometricType::BIOMETRIC_TYPE_MAX; | |
| 397 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response)); | |
| 398 client_->RequestType( | |
| 399 base::Bind(&CopyBiometricType, &returned_biometric_type)); | |
| 400 base::RunLoop().RunUntilIdle(); | |
| 401 EXPECT_EQ(kFakeBiometricType, returned_biometric_type); | |
| 402 | |
| 403 // Verify that by sending a empty reponse the response is an unknown biometric | |
| 404 // type. | |
| 405 returned_biometric_type = biod::BiometricType::BIOMETRIC_TYPE_MAX; | |
| 406 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr); | |
| 407 client_->RequestType( | |
| 408 base::Bind(&CopyBiometricType, &returned_biometric_type)); | |
| 409 base::RunLoop().RunUntilIdle(); | |
| 410 EXPECT_EQ(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN, | |
| 411 returned_biometric_type); | |
| 412 } | |
| 413 | |
| 414 TEST_F(BiodClientTest, TestRequestRecordLabel) { | |
| 415 const std::string kFakeLabel("fakeLabel"); | |
| 416 const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path")); | |
| 417 | |
| 418 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 419 dbus::MessageWriter writer(response.get()); | |
| 420 writer.AppendString(kFakeLabel); | |
| 421 | |
| 422 // Create a fake response with string. The get label call should return this | |
| 423 // exact string. | |
| 424 std::string returned_label = kInvalidString; | |
| 425 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response)); | |
| 426 client_->RequestRecordLabel(kFakeRecordPath, | |
| 427 base::Bind(&CopyString, &returned_label)); | |
| 428 base::RunLoop().RunUntilIdle(); | |
| 429 EXPECT_EQ(kFakeLabel, returned_label); | |
| 430 | |
| 431 // Verify that by sending a empty reponse the response is an empty string. | |
|
Daniel Erat
2017/04/08 01:13:32
nit: "the response is" -> "the result is" may be c
sammiequon
2017/04/08 01:31:48
Done.
| |
| 432 returned_label = kInvalidString; | |
| 433 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr); | |
| 434 client_->RequestRecordLabel(kFakeRecordPath, | |
| 435 base::Bind(&CopyString, &returned_label)); | |
| 436 base::RunLoop().RunUntilIdle(); | |
| 437 EXPECT_EQ("", returned_label); | |
| 438 } | |
| 439 | |
| 350 // Verify when signals are mocked, an observer will catch the signals as | 440 // Verify when signals are mocked, an observer will catch the signals as |
| 351 // expected. | 441 // expected. |
| 352 TEST_F(BiodClientTest, TestNotifyObservers) { | 442 TEST_F(BiodClientTest, TestNotifyObservers) { |
| 353 TestBiodObserver observer; | 443 TestBiodObserver observer; |
| 354 client_->AddObserver(&observer); | 444 client_->AddObserver(&observer); |
| 355 EXPECT_TRUE(client_->HasObserver(&observer)); | 445 EXPECT_TRUE(client_->HasObserver(&observer)); |
| 356 | 446 |
| 357 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS; | 447 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS; |
| 358 const bool enroll_session_complete = false; | 448 const bool enroll_session_complete = false; |
| 359 const AuthScanMatches test_attempt; | 449 const AuthScanMatches test_attempt; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 371 EXPECT_EQ(1, observer.num_failures_received()); | 461 EXPECT_EQ(1, observer.num_failures_received()); |
| 372 | 462 |
| 373 client_->RemoveObserver(&observer); | 463 client_->RemoveObserver(&observer); |
| 374 | 464 |
| 375 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete); | 465 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete); |
| 376 EmitAuthScanDoneSignal(scan_signal, test_attempt); | 466 EmitAuthScanDoneSignal(scan_signal, test_attempt); |
| 377 EXPECT_EQ(1, observer.num_enroll_scans_received()); | 467 EXPECT_EQ(1, observer.num_enroll_scans_received()); |
| 378 EXPECT_EQ(1, observer.num_auth_scans_received()); | 468 EXPECT_EQ(1, observer.num_auth_scans_received()); |
| 379 EXPECT_EQ(1, observer.num_failures_received()); | 469 EXPECT_EQ(1, observer.num_failures_received()); |
| 380 } | 470 } |
| 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 | 471 } // namespace chromeos |
| OLD | NEW |