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

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

Issue 2644233002: cros: Added a fake fingerprint storage class. (Closed)
Patch Set: Changed fake fingerprint from string to string vec. 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"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "chromeos/dbus/biod/biod_test_utils.h"
12 #include "dbus/mock_bus.h" 13 #include "dbus/mock_bus.h"
13 #include "dbus/mock_object_proxy.h" 14 #include "dbus/mock_object_proxy.h"
14 #include "dbus/object_path.h" 15 #include "dbus/object_path.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 using ::testing::_; 19 using ::testing::_;
19 using ::testing::Invoke; 20 using ::testing::Invoke;
20 using ::testing::Return; 21 using ::testing::Return;
21 22
22 namespace chromeos { 23 namespace chromeos {
23 24
24 namespace { 25 namespace {
25 26
26 // Shorthand for a commonly-used constant. 27 // Shorthand for a commonly-used constant.
27 const char* kInterface = biod::kBiometricsManagerInterface; 28 const char* kInterface = biod::kBiometricsManagerInterface;
28 29
29 // Value used to intialize dbus::ObjectPath objects in tests to make it easier 30 // Value used to intialize dbus::ObjectPath objects in tests to make it easier
30 // to determine when empty values have been assigned. 31 // to determine when empty values have been assigned.
31 const char kInvalidTestPath[] = "/invalid/test/path"; 32 const char kInvalidTestPath[] = "/invalid/test/path";
32 33
33 // Value used to intialize string objects in tests to make it easier to 34 // Value used to intialize string objects in tests to make it easier to
34 // determine when empty values have been assigned. 35 // determine when empty values have been assigned.
35 const char kInvalidString[] = "invalidString"; 36 const char kInvalidString[] = "invalidString";
36 37
37 void CopyObjectPath(dbus::ObjectPath* dest_path,
38 const dbus::ObjectPath& src_path) {
39 CHECK(dest_path);
40 *dest_path = src_path;
41 }
42
43 void CopyObjectPathArray(
44 std::vector<dbus::ObjectPath>* dest_object_paths,
45 const std::vector<dbus::ObjectPath>& src_object_paths) {
46 CHECK(dest_object_paths);
47 *dest_object_paths = src_object_paths;
48 }
49
50 void CopyString(std::string* dest_str, const std::string& src_str) {
51 CHECK(dest_str);
52 *dest_str = src_str;
53 }
54
55 // Matcher that verifies that a dbus::Message has member |name|. 38 // Matcher that verifies that a dbus::Message has member |name|.
56 MATCHER_P(HasMember, name, "") { 39 MATCHER_P(HasMember, name, "") {
57 if (arg->GetMember() != name) { 40 if (arg->GetMember() != name) {
58 *result_listener << "has member " << arg->GetMember(); 41 *result_listener << "has member " << arg->GetMember();
59 return false; 42 return false;
60 } 43 }
61 return true; 44 return true;
62 } 45 }
63 46
64 // Runs |callback| with |response|. Needed due to ResponseCallback expecting a 47 // Runs |callback| with |response|. Needed due to ResponseCallback expecting a
65 // bare pointer rather than an std::unique_ptr. 48 // bare pointer rather than an std::unique_ptr.
66 void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback, 49 void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback,
67 std::unique_ptr<dbus::Response> response) { 50 std::unique_ptr<dbus::Response> response) {
68 callback.Run(response.get()); 51 callback.Run(response.get());
69 } 52 }
70 53
71 // Implementation of BiodClient::Observer.
72 class TestBiodObserver : public BiodClient::Observer {
73 public:
74 TestBiodObserver() {}
75 ~TestBiodObserver() override {}
76
77 int num_enroll_scans_received() const { return num_enroll_scans_received_; }
78 int num_auth_scans_received() const { return num_auth_scans_received_; }
79 int num_failures_received() const { return num_failures_received_; }
80
81 // BiodClient::Observer:
82 void BiodServiceRestarted() override {}
83
84 void BiodEnrollScanDoneReceived(biod::ScanResult scan_result,
85 bool enroll_sesion_complete) override {
86 num_enroll_scans_received_++;
87 }
88
89 void BiodAuthScanDoneReceived(biod::ScanResult scan_result,
90 const AuthScanMatches& matches) override {
91 num_auth_scans_received_++;
92 }
93
94 void BiodSessionFailedReceived() override { num_failures_received_++; }
95
96 private:
97 int num_enroll_scans_received_ = 0;
98 int num_auth_scans_received_ = 0;
99 int num_failures_received_ = 0;
100
101 DISALLOW_COPY_AND_ASSIGN(TestBiodObserver);
102 };
103
104 } // namespace 54 } // namespace
105 55
106 class BiodClientTest : public testing::Test { 56 class BiodClientTest : public testing::Test {
107 public: 57 public:
108 BiodClientTest() {} 58 BiodClientTest() {}
109 ~BiodClientTest() override {} 59 ~BiodClientTest() override {}
110 60
111 void SetUp() override { 61 void SetUp() override {
112 dbus::Bus::Options options; 62 dbus::Bus::Options options;
113 options.bus_type = dbus::Bus::SYSTEM; 63 options.bus_type = dbus::Bus::SYSTEM;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 195
246 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 196 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
247 dbus::MessageWriter writer(response.get()); 197 dbus::MessageWriter writer(response.get());
248 writer.AppendObjectPath(kFakeObjectPath); 198 writer.AppendObjectPath(kFakeObjectPath);
249 199
250 // Create a fake response with a fake object path. The start enroll 200 // Create a fake response with a fake object path. The start enroll
251 // call should return this object path. 201 // call should return this object path.
252 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, 202 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
253 std::move(response)); 203 std::move(response));
254 dbus::ObjectPath returned_path(kInvalidTestPath); 204 dbus::ObjectPath returned_path(kInvalidTestPath);
255 client_->StartEnrollSession(kFakeId, kFakeLabel, 205 client_->StartEnrollSession(
256 base::Bind(&CopyObjectPath, &returned_path)); 206 kFakeId, kFakeLabel,
207 base::Bind(&test_utils::CopyObjectPath, &returned_path));
257 base::RunLoop().RunUntilIdle(); 208 base::RunLoop().RunUntilIdle();
258 EXPECT_EQ(kFakeObjectPath, returned_path); 209 EXPECT_EQ(kFakeObjectPath, returned_path);
259 210
260 // Verify that by sending a empty reponse or a improperly formatted one, the 211 // 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. 212 // response is an empty object path. Also, logs will get printed.
262 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, 213 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
263 nullptr); 214 nullptr);
264 returned_path = dbus::ObjectPath(kInvalidTestPath); 215 returned_path = dbus::ObjectPath(kInvalidTestPath);
265 client_->StartEnrollSession(kFakeId, kFakeLabel, 216 client_->StartEnrollSession(
266 base::Bind(&CopyObjectPath, &returned_path)); 217 kFakeId, kFakeLabel,
218 base::Bind(&test_utils::CopyObjectPath, &returned_path));
267 base::RunLoop().RunUntilIdle(); 219 base::RunLoop().RunUntilIdle();
268 EXPECT_EQ(dbus::ObjectPath(), returned_path); 220 EXPECT_EQ(dbus::ObjectPath(), returned_path);
269 221
270 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty()); 222 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty());
271 dbus::MessageWriter bad_writer(bad_response.get()); 223 dbus::MessageWriter bad_writer(bad_response.get());
272 bad_writer.AppendString(""); 224 bad_writer.AppendString("");
273 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod, 225 AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
274 std::move(bad_response)); 226 std::move(bad_response));
275 returned_path = dbus::ObjectPath(kInvalidTestPath); 227 returned_path = dbus::ObjectPath(kInvalidTestPath);
276 client_->StartEnrollSession(kFakeId, kFakeLabel, 228 client_->StartEnrollSession(
277 base::Bind(&CopyObjectPath, &returned_path)); 229 kFakeId, kFakeLabel,
230 base::Bind(&test_utils::CopyObjectPath, &returned_path));
278 base::RunLoop().RunUntilIdle(); 231 base::RunLoop().RunUntilIdle();
279 EXPECT_EQ(dbus::ObjectPath(), returned_path); 232 EXPECT_EQ(dbus::ObjectPath(), returned_path);
280 } 233 }
281 234
282 TEST_F(BiodClientTest, TestGetRecordsForUser) { 235 TEST_F(BiodClientTest, TestGetRecordsForUser) {
283 const std::string kFakeId("fakeId"); 236 const std::string kFakeId("fakeId");
284 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path")); 237 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
285 const dbus::ObjectPath kFakeObjectPath2(std::string("/fake/object/path2")); 238 const dbus::ObjectPath kFakeObjectPath2(std::string("/fake/object/path2"));
286 const std::vector<dbus::ObjectPath> kFakeObjectPaths = {kFakeObjectPath, 239 const std::vector<dbus::ObjectPath> kFakeObjectPaths = {kFakeObjectPath,
287 kFakeObjectPath2}; 240 kFakeObjectPath2};
288 241
289 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 242 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
290 dbus::MessageWriter writer(response.get()); 243 dbus::MessageWriter writer(response.get());
291 writer.AppendArrayOfObjectPaths(kFakeObjectPaths); 244 writer.AppendArrayOfObjectPaths(kFakeObjectPaths);
292 245
293 // Create a fake response with an array of fake object paths. The get 246 // Create a fake response with an array of fake object paths. The get
294 // records for user call should return this array of object paths. 247 // records for user call should return this array of object paths.
295 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, 248 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
296 std::move(response)); 249 std::move(response));
297 std::vector<dbus::ObjectPath> returned_object_paths = { 250 std::vector<dbus::ObjectPath> returned_object_paths = {
298 dbus::ObjectPath(kInvalidTestPath)}; 251 dbus::ObjectPath(kInvalidTestPath)};
299 client_->GetRecordsForUser( 252 client_->GetRecordsForUser(
300 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); 253 kFakeId,
254 base::Bind(&test_utils::CopyObjectPathArray, &returned_object_paths));
301 base::RunLoop().RunUntilIdle(); 255 base::RunLoop().RunUntilIdle();
302 EXPECT_EQ(kFakeObjectPaths, returned_object_paths); 256 EXPECT_EQ(kFakeObjectPaths, returned_object_paths);
303 257
304 // Verify that by sending a empty reponse the response is an empty array of 258 // Verify that by sending a empty reponse the response is an empty array of
305 // object paths. Also, logs will get printed. 259 // object paths. Also, logs will get printed.
306 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod, 260 AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
307 nullptr); 261 nullptr);
308 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)}; 262 returned_object_paths = {dbus::ObjectPath(kInvalidTestPath)};
309 client_->GetRecordsForUser( 263 client_->GetRecordsForUser(
310 kFakeId, base::Bind(&CopyObjectPathArray, &returned_object_paths)); 264 kFakeId,
265 base::Bind(&test_utils::CopyObjectPathArray, &returned_object_paths));
311 base::RunLoop().RunUntilIdle(); 266 base::RunLoop().RunUntilIdle();
312 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths); 267 EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths);
313 } 268 }
314 269
315 TEST_F(BiodClientTest, TestStartAuthentication) { 270 TEST_F(BiodClientTest, TestStartAuthentication) {
316 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path")); 271 const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
317 272
318 // Create a fake response with a fake object path. The start authentication 273 // Create a fake response with a fake object path. The start authentication
319 // call should return this object path. 274 // call should return this object path.
320 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 275 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
321 dbus::MessageWriter writer(response.get()); 276 dbus::MessageWriter writer(response.get());
322 writer.AppendObjectPath(kFakeObjectPath); 277 writer.AppendObjectPath(kFakeObjectPath);
323 278
324 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, 279 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
325 std::move(response)); 280 std::move(response));
326 dbus::ObjectPath returned_path(kInvalidTestPath); 281 dbus::ObjectPath returned_path(kInvalidTestPath);
327 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 282 client_->StartAuthSession(
283 base::Bind(&test_utils::CopyObjectPath, &returned_path));
328 base::RunLoop().RunUntilIdle(); 284 base::RunLoop().RunUntilIdle();
329 EXPECT_EQ(kFakeObjectPath, returned_path); 285 EXPECT_EQ(kFakeObjectPath, returned_path);
330 286
331 // Verify that by sending a empty reponse or a improperly formatted one, the 287 // 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. 288 // response is an empty object path. Also, logs will get printed.
333 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, nullptr); 289 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, nullptr);
334 returned_path = dbus::ObjectPath(kInvalidTestPath); 290 returned_path = dbus::ObjectPath(kInvalidTestPath);
335 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 291 client_->StartAuthSession(
292 base::Bind(&test_utils::CopyObjectPath, &returned_path));
336 base::RunLoop().RunUntilIdle(); 293 base::RunLoop().RunUntilIdle();
337 EXPECT_EQ(dbus::ObjectPath(), returned_path); 294 EXPECT_EQ(dbus::ObjectPath(), returned_path);
338 295
339 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty()); 296 std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty());
340 dbus::MessageWriter bad_writer(bad_response.get()); 297 dbus::MessageWriter bad_writer(bad_response.get());
341 bad_writer.AppendString(""); 298 bad_writer.AppendString("");
342 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, 299 AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
343 std::move(bad_response)); 300 std::move(bad_response));
344 returned_path = dbus::ObjectPath(kInvalidTestPath); 301 returned_path = dbus::ObjectPath(kInvalidTestPath);
345 client_->StartAuthSession(base::Bind(&CopyObjectPath, &returned_path)); 302 client_->StartAuthSession(
303 base::Bind(&test_utils::CopyObjectPath, &returned_path));
346 base::RunLoop().RunUntilIdle(); 304 base::RunLoop().RunUntilIdle();
347 EXPECT_EQ(dbus::ObjectPath(), returned_path); 305 EXPECT_EQ(dbus::ObjectPath(), returned_path);
348 } 306 }
349 307
350 // Verify when signals are mocked, an observer will catch the signals as 308 // Verify when signals are mocked, an observer will catch the signals as
351 // expected. 309 // expected.
352 TEST_F(BiodClientTest, TestNotifyObservers) { 310 TEST_F(BiodClientTest, TestNotifyObservers) {
353 TestBiodObserver observer; 311 test_utils::TestBiodObserver observer;
354 client_->AddObserver(&observer); 312 client_->AddObserver(&observer);
355 EXPECT_TRUE(client_->HasObserver(&observer)); 313 EXPECT_TRUE(client_->HasObserver(&observer));
356 314
357 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS; 315 const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS;
358 const bool enroll_session_complete = false; 316 const bool enroll_session_complete = false;
359 const AuthScanMatches test_attempt; 317 const AuthScanMatches test_attempt;
360 EXPECT_EQ(0, observer.num_enroll_scans_received()); 318 EXPECT_EQ(0, observer.NumEnrollScansReceived());
361 EXPECT_EQ(0, observer.num_auth_scans_received()); 319 EXPECT_EQ(0, observer.NumAuthScansReceived());
362 EXPECT_EQ(0, observer.num_failures_received()); 320 EXPECT_EQ(0, observer.num_failures_received());
363 321
364 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete); 322 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete);
365 EXPECT_EQ(1, observer.num_enroll_scans_received()); 323 EXPECT_EQ(1, observer.NumEnrollScansReceived());
366 324
367 EmitAuthScanDoneSignal(scan_signal, test_attempt); 325 EmitAuthScanDoneSignal(scan_signal, test_attempt);
368 EXPECT_EQ(1, observer.num_auth_scans_received()); 326 EXPECT_EQ(1, observer.NumAuthScansReceived());
369 327
370 EmitScanFailedSignal(); 328 EmitScanFailedSignal();
371 EXPECT_EQ(1, observer.num_failures_received()); 329 EXPECT_EQ(1, observer.num_failures_received());
372 330
373 client_->RemoveObserver(&observer); 331 client_->RemoveObserver(&observer);
374 332
375 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete); 333 EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete);
376 EmitAuthScanDoneSignal(scan_signal, test_attempt); 334 EmitAuthScanDoneSignal(scan_signal, test_attempt);
377 EXPECT_EQ(1, observer.num_enroll_scans_received()); 335 EXPECT_EQ(1, observer.NumEnrollScansReceived());
378 EXPECT_EQ(1, observer.num_auth_scans_received()); 336 EXPECT_EQ(1, observer.NumAuthScansReceived());
379 EXPECT_EQ(1, observer.num_failures_received()); 337 EXPECT_EQ(1, observer.num_failures_received());
380 } 338 }
381 339
382 TEST_F(BiodClientTest, TestGetRecordLabel) { 340 TEST_F(BiodClientTest, TestGetRecordLabel) {
383 const std::string kFakeLabel("fakeLabel"); 341 const std::string kFakeLabel("fakeLabel");
384 const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path")); 342 const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path"));
385 343
386 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 344 std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
387 dbus::MessageWriter writer(response.get()); 345 dbus::MessageWriter writer(response.get());
388 writer.AppendString(kFakeLabel); 346 writer.AppendString(kFakeLabel);
389 347
390 // Create a fake response with string. The get label call should return this 348 // Create a fake response with string. The get label call should return this
391 // exact string. 349 // exact string.
392 std::string returned_label = kInvalidString; 350 std::string returned_label = kInvalidString;
393 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response)); 351 AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
394 client_->RequestRecordLabel(kFakeRecordPath, 352 client_->RequestRecordLabel(
395 base::Bind(&CopyString, &returned_label)); 353 kFakeRecordPath, base::Bind(&test_utils::CopyLabel, &returned_label));
396 base::RunLoop().RunUntilIdle(); 354 base::RunLoop().RunUntilIdle();
397 EXPECT_EQ(kFakeLabel, returned_label); 355 EXPECT_EQ(kFakeLabel, returned_label);
398 356
399 // Verify that by sending a empty reponse the response is an empty string. 357 // Verify that by sending a empty reponse the response is an empty string.
400 // Also, logs will get printed. 358 // Also, logs will get printed.
401 returned_label = kInvalidString; 359 returned_label = kInvalidString;
402 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr); 360 AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
403 client_->RequestRecordLabel(kFakeRecordPath, 361 client_->RequestRecordLabel(
404 base::Bind(&CopyString, &returned_label)); 362 kFakeRecordPath, base::Bind(&test_utils::CopyLabel, &returned_label));
405 base::RunLoop().RunUntilIdle(); 363 base::RunLoop().RunUntilIdle();
406 EXPECT_EQ("", returned_label); 364 EXPECT_EQ("", returned_label);
407 } 365 }
408 } // namespace chromeos 366 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698