36 #include <gtest/gtest.h>
42 static std::string gl_data_path;
63 , my_card_(new Cred::RFIDCard())
64 , my_card2_(new Cred::RFIDCard())
65 , unknown_card_(new Cred::RFIDCard())
66 , my_pin_(new Cred::PinCode())
68 my_card_->card_id(
"aa:bb:cc:dd");
69 my_card_->nb_bits(32);
71 my_card2_->card_id(
"cc:dd:ee:ff");
72 my_card2_->nb_bits(32);
74 unknown_card_->card_id(
"00:00:00:00");
75 unknown_card_->nb_bits(32);
77 my_pin_->pin_code(
"1234");
79 card_and_pin_ = std::make_shared<Cred::RFIDCardPin>();
80 card_and_pin_->card().card_id(
"cc:dd:ee:ff");
81 card_and_pin_->card().nb_bits(32);
82 card_and_pin_->pin().pin_code(
"1234");
93 std::tm date = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
98 std::time_t time_temp;
105 date5 = date4 = date3 = date2 = date;
107 time_temp = std::mktime(&date);
108 date_monday_12_00 = std::chrono::system_clock::from_time_t(time_temp);
113 time_temp = std::mktime(&date2);
114 date_monday_16_31 = std::chrono::system_clock::from_time_t(time_temp);
120 time_temp = std::mktime(&date3);
121 date_sunday_18_50 = std::chrono::system_clock::from_time_t(time_temp);
127 time_temp = std::mktime(&date4);
128 date_wednesday_23_42 = std::chrono::system_clock::from_time_t(time_temp);
134 time_temp = std::mktime(&date4);
135 date_thursday_14_00 = std::chrono::system_clock::from_time_t(time_temp);
149 bool is_in_group(
const std::string &user_name,
const std::string &group_name,
152 auto search_lambda = [&](
UserPtr u) ->
bool {
153 return u->username() == boost::algorithm::to_lower_copy(user_name);
156 for (
const auto &group : mapper->
groups())
158 if (group->name() == group_name)
160 return std::find_if(group->members().begin(), group->members().end(),
161 search_lambda) != group->members().end();
199 ASSERT_FALSE(my_card_->owner().get());
200 mapper_->mapToUser(my_card_);
201 ASSERT_TRUE(my_card_->owner().get());
202 ASSERT_EQ(
"my_user", my_card_->owner()->username());
205 ASSERT_FALSE(my_pin_->owner().get());
206 mapper_->mapToUser(my_pin_);
207 ASSERT_TRUE(my_pin_->owner().get());
208 ASSERT_EQ(
"my_user", my_pin_->owner()->username());
210 ASSERT_FALSE(my_card2_->owner().get());
211 mapper_->mapToUser(my_card2_);
212 ASSERT_TRUE(my_card2_->owner().get());
213 ASSERT_EQ(
"toto", my_card2_->owner()->username());
222 mapper_->mapToUser(my_card_);
223 ASSERT_TRUE(my_card_->owner().get());
226 ASSERT_TRUE(profile.get());
228 ASSERT_TRUE(profile->isAccessGranted(date_monday_12_00, doorA_));
229 ASSERT_TRUE(profile->isAccessGranted(date_monday_16_31, doorA_));
230 ASSERT_FALSE(profile->isAccessGranted(date_thursday_14_00, doorA_));
232 ASSERT_TRUE(profile->isAccessGranted(date_monday_12_00, doorB_));
233 ASSERT_TRUE(profile->isAccessGranted(date_monday_16_31, doorB_));
234 ASSERT_FALSE(profile->isAccessGranted(date_thursday_14_00, doorB_));
236 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorA_));
237 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorB_));
240 mapper_->mapToUser(my_pin_);
241 ASSERT_TRUE(my_pin_->owner().get());
244 ASSERT_TRUE(profile_from_pin.get());
246 ASSERT_TRUE(profile_from_pin->isAccessGranted(date_monday_12_00, doorA_));
247 ASSERT_TRUE(profile_from_pin->isAccessGranted(date_monday_16_31, doorA_));
248 ASSERT_FALSE(profile_from_pin->isAccessGranted(date_thursday_14_00, doorA_));
250 ASSERT_TRUE(profile_from_pin->isAccessGranted(date_monday_12_00, doorB_));
251 ASSERT_TRUE(profile_from_pin->isAccessGranted(date_monday_16_31, doorB_));
252 ASSERT_FALSE(profile_from_pin->isAccessGranted(date_thursday_14_00, doorB_));
254 ASSERT_TRUE(profile_from_pin->isAccessGranted(date_sunday_18_50, doorA_));
255 ASSERT_TRUE(profile_from_pin->isAccessGranted(date_sunday_18_50, doorB_));
263 mapper_->mapToUser(my_card_);
264 mapper_->mapToUser(my_card2_);
265 ASSERT_TRUE(my_card_->owner().get());
266 ASSERT_TRUE(my_card2_->owner().get());
269 ASSERT_TRUE(profile_myuser.get());
270 ASSERT_TRUE(profile_toto.get());
273 ASSERT_TRUE(profile_myuser->isAccessGranted(date_sunday_18_50, doorC_));
274 ASSERT_TRUE(profile_myuser->isAccessGranted(date_monday_16_31, doorC_));
276 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorA_));
277 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorB_));
278 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorC_));
279 ASSERT_FALSE(profile_toto->isAccessGranted(date_monday_16_31, doorC_));
287 ASSERT_FALSE(unknown_card_->owner().get());
288 mapper_->mapToUser(unknown_card_);
289 ASSERT_FALSE(unknown_card_->owner().get());
297 ASSERT_THROW(std::unique_ptr<IAuthSourceMapper> faulty_mapper(
309 std::unique_ptr<IAuthSourceMapper> faulty_mapper(
318 ASSERT_TRUE(is_in_group(
"my_user",
"Admins", mapper2_));
319 ASSERT_TRUE(is_in_group(
"toto",
"Admins", mapper2_));
321 ASSERT_FALSE(is_in_group(
"useless",
"Admins", mapper2_));
322 ASSERT_TRUE(is_in_group(
"useless",
"random_group", mapper2_));
328 ASSERT_TRUE(is_in_group(
"my_user",
"Admins", mapper3_));
329 ASSERT_TRUE(is_in_group(
"my_user",
"Operators", mapper3_));
330 ASSERT_TRUE(is_in_group(
"my_user",
"Users", mapper3_));
331 ASSERT_TRUE(is_in_group(
"my_user",
"YetAnotherGroup", mapper3_));
332 ASSERT_FALSE(is_in_group(
"my_user",
"group_doesnt_exists", mapper3_));
334 ASSERT_FALSE(is_in_group(
"toto",
"Operators", mapper3_));
335 ASSERT_TRUE(is_in_group(
"toto",
"Admins", mapper3_));
336 ASSERT_TRUE(is_in_group(
"toto",
"Users", mapper3_));
344 mapper2_->mapToUser(my_card_);
346 auto profile = mapper2_->buildProfile(my_card_);
347 ASSERT_TRUE(profile.get());
348 ASSERT_TRUE(is_in_group(
"my_user",
"Admins", mapper2_));
350 ASSERT_TRUE(profile->isAccessGranted(date_monday_12_00, doorA_));
351 ASSERT_FALSE(profile->isAccessGranted(date_monday_16_31, doorA_));
353 ASSERT_FALSE(profile->isAccessGranted(date_monday_12_00, doorB_));
354 ASSERT_TRUE(profile->isAccessGranted(date_monday_16_31, doorB_));
356 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorA_));
357 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorB_));
359 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorC_));
360 ASSERT_FALSE(profile->isAccessGranted(date_monday_16_31, doorC_));
369 mapper3_->mapToUser(my_card_);
370 mapper3_->mapToUser(my_card2_);
372 auto profile = mapper3_->buildProfile(my_card_);
373 auto profile_toto = mapper3_->buildProfile(my_card2_);
374 ASSERT_TRUE(profile.get());
375 ASSERT_TRUE(profile_toto.get());
377 ASSERT_TRUE(profile->isAccessGranted(date_monday_12_00, doorA_));
378 ASSERT_FALSE(profile->isAccessGranted(date_monday_16_31, doorA_));
380 ASSERT_FALSE(profile->isAccessGranted(date_monday_12_00, doorB_));
381 ASSERT_TRUE(profile->isAccessGranted(date_monday_16_31, doorB_));
383 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorA_));
384 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorB_));
386 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorC_));
387 ASSERT_TRUE(profile->isAccessGranted(date_monday_16_31, doorC_));
390 ASSERT_TRUE(profile->isAccessGranted(date_wednesday_23_42, doorA_));
391 ASSERT_TRUE(profile->isAccessGranted(date_wednesday_23_42, doorB_));
392 ASSERT_TRUE(profile->isAccessGranted(date_wednesday_23_42, doorC_));
395 ASSERT_TRUE(profile->isAccessGranted(date_thursday_14_00, doorA_));
396 ASSERT_TRUE(profile->isAccessGranted(date_thursday_14_00, doorB_));
397 ASSERT_FALSE(profile->isAccessGranted(date_thursday_14_00, doorC_));
400 ASSERT_TRUE(profile_toto->isAccessGranted(date_thursday_14_00, doorA_));
401 ASSERT_FALSE(profile_toto->isAccessGranted(date_thursday_14_00, doorB_));
402 ASSERT_FALSE(profile_toto->isAccessGranted(date_thursday_14_00, doorC_));
411 mapper4_->mapToUser(my_card_);
412 mapper4_->mapToUser(my_card2_);
414 auto profile = mapper4_->buildProfile(my_card_);
415 auto profile_toto = mapper4_->buildProfile(my_card2_);
416 ASSERT_TRUE(profile.get());
417 ASSERT_TRUE(profile_toto.get());
419 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorB_));
420 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorB_));
422 ASSERT_TRUE(profile->isAccessGranted(date_thursday_14_00, doorA_));
423 ASSERT_TRUE(profile_toto->isAccessGranted(date_thursday_14_00, doorA_));
425 ASSERT_TRUE(profile->isAccessGranted(date_sunday_18_50, doorA_));
426 ASSERT_FALSE(profile_toto->isAccessGranted(date_sunday_18_50, doorA_));
431 mapper4_->mapToUser(unknown_card_);
432 auto profile = mapper4_->buildProfile(unknown_card_);
433 ASSERT_FALSE(profile.get());
438 mapper5_->mapToUser(card_and_pin_);
439 auto profile_toto = mapper5_->buildProfile(card_and_pin_);
440 ASSERT_TRUE(profile_toto.get());
442 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorA_));
443 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorB_));
444 ASSERT_TRUE(profile_toto->isAccessGranted(date_sunday_18_50, doorC_));
445 ASSERT_FALSE(profile_toto->isAccessGranted(date_monday_16_31, doorC_));
450 mapper5_->mapToUser(my_card_);
451 mapper5_->mapToUser(my_card2_);
452 mapper5_->mapToUser(my_pin_);
454 auto profile_llamasticot = mapper5_->buildProfile(my_card_);
455 ASSERT_FALSE(profile_llamasticot.get());
457 auto profile_llama1 = mapper5_->buildProfile(my_card2_);
458 ASSERT_TRUE(profile_llama1.get());
460 auto profile_llama2 = mapper5_->buildProfile(my_pin_);
461 ASSERT_FALSE(profile_llama2.get());
466 mapper6_->mapToUser(my_card_);
467 mapper6_->mapToUser(my_card2_);
468 mapper6_->mapToUser(my_pin_);
470 auto profile_llamasticot = mapper6_->buildProfile(my_card_);
471 ASSERT_FALSE(profile_llamasticot.get());
473 auto profile_llama1 = mapper6_->buildProfile(my_card2_);
474 ASSERT_TRUE(profile_llama1.get());
476 auto profile_llama2 = mapper6_->buildProfile(my_pin_);
477 ASSERT_FALSE(profile_llama2.get());
482 mapper7_->mapToUser(my_card_);
483 mapper7_->mapToUser(my_pin_);
484 auto profile = mapper7_->buildProfile(my_card_);
488 auto profile_magic_cred = mapper7_->buildProfile(my_pin_);
490 ASSERT_TRUE(profile.get());
491 ASSERT_TRUE(profile_magic_cred.get());
494 ASSERT_TRUE(profile->isAccessGranted(date_monday_12_00,
nullptr));
495 ASSERT_TRUE(profile->isAccessGranted(date_monday_16_31,
nullptr));
497 ASSERT_TRUE(profile_magic_cred->isAccessGranted(date_monday_12_00,
nullptr));
498 ASSERT_TRUE(profile_magic_cred->isAccessGranted(date_monday_16_31,
nullptr));
500 ASSERT_FALSE(profile->isAccessGranted(date_sunday_18_50,
nullptr));
501 ASSERT_FALSE(profile->isAccessGranted(date_wednesday_23_42, doorB_));
502 ASSERT_FALSE(profile->isAccessGranted(date_wednesday_23_42, doorC_));
503 ASSERT_FALSE(profile->isAccessGranted(date_wednesday_23_42,
nullptr));
505 ASSERT_FALSE(profile_magic_cred->isAccessGranted(date_sunday_18_50,
nullptr));
506 ASSERT_FALSE(profile_magic_cred->isAccessGranted(date_wednesday_23_42, doorB_));
507 ASSERT_FALSE(profile_magic_cred->isAccessGranted(date_wednesday_23_42, doorC_));
508 ASSERT_FALSE(profile_magic_cred->isAccessGranted(date_wednesday_23_42,
nullptr));
511 ASSERT_TRUE(profile_magic_cred->isAccessGranted(date_wednesday_23_42, doorA_));
512 ASSERT_TRUE(profile_magic_cred->isAccessGranted(date_thursday_14_00,
nullptr));
514 ASSERT_FALSE(profile->isAccessGranted(date_wednesday_23_42, doorA_));
515 ASSERT_FALSE(profile->isAccessGranted(date_thursday_14_00,
nullptr));
526 std::unique_ptr<IAuthSourceMapper> faulty_mapper(
535 int main(
int argc,
char **argv)
537 ::testing::InitGoogleTest(&argc, argv);
542 gl_data_path = std::string(argv[1]) +
'/';
543 return RUN_ALL_TESTS();