Leosac  0.8.0
Open Source Access Control
AuthSourceBuilder.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 #include "core/auth/Auth.hpp"
27 #include <gtest/gtest.h>
28 
29 using namespace Leosac::Auth;
30 using namespace Leosac::Module::Auth;
31 
32 namespace Leosac
33 {
34 namespace Test
35 {
40 class AuthSourceBuilderTest : public ::testing::Test
41 {
42  public:
44  {
45  msg1_ << "S_MY_WIEGAND_1";
47  msg1_ << "af:bc:12:42";
48  msg1_ << 32;
49 
50  msg2_ << "S_MY_WIEGAND_1";
51  msg2_ << SourceType::WIEGAND_PIN;
52  msg2_ << "1234";
53 
54  msg3_ << "S_MY_WIEGAND_1";
56  msg3_ << "af:bc:12:42";
57  msg3_ << 32;
58  msg3_ << "1234";
59  }
60 
62  {
63  }
64 
69  zmqpp::message msg1_;
70 
74  zmqpp::message msg2_;
75 
79  zmqpp::message msg3_;
80 };
81 
82 TEST_F(AuthSourceBuilderTest, ExtractSourceName)
83 {
84  std::string out;
85 
86  ASSERT_TRUE(builder_.extract_source_name("S_MY_DEVICE", &out));
87  ASSERT_EQ(out, "MY_DEVICE");
88 
89  ASSERT_TRUE(builder_.extract_source_name("S___MY_DEVICE", &out));
90  ASSERT_EQ(out, "__MY_DEVICE");
91 
92  ASSERT_FALSE(builder_.extract_source_name("MY_DEVICE", &out));
93  ASSERT_FALSE(builder_.extract_source_name("", &out));
94  ASSERT_FALSE(builder_.extract_source_name("S_", &out));
95  ASSERT_FALSE(builder_.extract_source_name("D", &out));
96 }
97 
98 TEST_F(AuthSourceBuilderTest, BuildWiegandCard)
99 {
100  Cred::ICredentialPtr auth_source = builder_.create(&msg1_);
101  ASSERT_TRUE(auth_source.get());
102 
103  // ASSERT_EQ(auth_source->name(), "MY_WIEGAND_1");
104  Cred::RFIDCardPtr spec = std::dynamic_pointer_cast<Cred::RFIDCard>(auth_source);
105  ASSERT_TRUE(spec.get());
106  ASSERT_EQ("af:bc:12:42", spec->card_id());
107  ASSERT_EQ(32, spec->nb_bits());
108 }
109 
111 {
112  Cred::ICredentialPtr auth_source = builder_.create(&msg2_);
113  ASSERT_TRUE(auth_source.get());
114 
115  // ASSERT_EQ(auth_source->name(), "MY_WIEGAND_1");
116  Cred::PinCodePtr spec = std::dynamic_pointer_cast<Cred::PinCode>(auth_source);
117  ASSERT_TRUE(spec.get());
118  ASSERT_EQ("1234", spec->pin_code());
119 }
120 
121 TEST_F(AuthSourceBuilderTest, BuildCardAndPing)
122 {
123  Cred::ICredentialPtr auth_source = builder_.create(&msg3_);
124  ASSERT_TRUE(auth_source.get());
125 
126  // ASSERT_EQ(auth_source->name(), "MY_WIEGAND_1");
127  Cred::RFIDCardPinPtr spec =
128  std::dynamic_pointer_cast<Cred::RFIDCardPin>(auth_source);
129  ASSERT_TRUE(spec.get());
130  ASSERT_EQ("af:bc:12:42", spec->card().card_id());
131  ASSERT_EQ(32, spec->card().nb_bits());
132  ASSERT_EQ("1234", spec->pin().pin_code());
133 }
134 }
135 }
Leosac::Test::AuthSourceBuilderTest::builder_
AuthSourceBuilder builder_
Definition: AuthSourceBuilder.cpp:65
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::Test::AuthSourceBuilderTest
Test the AuthSourceBuilder ability to create AuthSource object from messages.
Definition: AuthSourceBuilder.cpp:40
Leosac::Test::AuthSourceBuilderTest::msg3_
zmqpp::message msg3_
Message to construct a cardId + pin credential.
Definition: AuthSourceBuilder.cpp:79
Leosac::Test::AuthSourceBuilderTest::AuthSourceBuilderTest
AuthSourceBuilderTest()
Definition: AuthSourceBuilder.cpp:43
Auth.hpp
Leosac::Cred::RFIDCardPtr
std::shared_ptr< RFIDCard > RFIDCardPtr
Definition: CredentialFwd.hpp:43
Leosac::Auth::SourceType::WIEGAND_PIN
@ WIEGAND_PIN
Message formatting when using a simple PIN code.
AuthSourceBuilder.hpp
RFIDCard.hpp
Leosac::Auth::SourceType::WIEGAND_CARD_PIN
@ WIEGAND_CARD_PIN
When reading both a card an a PIN code.
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Test::AuthSourceBuilderTest::msg2_
zmqpp::message msg2_
This looks like a message sent by MY_WIEGAND_1 with WIEGAND_PIN_4BITS data.
Definition: AuthSourceBuilder.cpp:74
RFIDCardPin.hpp
FileAuthSourceMapper.hpp
Leosac::Auth::AuthSourceBuilder
This class is some kind of factory to create IAuthenticationSource object from a zmqpp::message sent ...
Definition: AuthSourceBuilder.hpp:37
Leosac::Cred::PinCodePtr
std::shared_ptr< PinCode > PinCodePtr
Definition: CredentialFwd.hpp:47
PinCode.hpp
Leosac::Test::AuthSourceBuilderTest::msg1_
zmqpp::message msg1_
This looks like a message sent by MY_WIEGAND_1 with SIMPLE_WIEGAND data.
Definition: AuthSourceBuilder.cpp:69
Leosac::Cred::ICredentialPtr
std::shared_ptr< ICredential > ICredentialPtr
Definition: CredentialFwd.hpp:32
Leosac::Cred::RFIDCardPinPtr
std::shared_ptr< RFIDCardPin > RFIDCardPinPtr
Definition: CredentialFwd.hpp:50
IAuthSourceMapper.hpp
Leosac::Module::Auth
Authentication backend modules live here.
Leosac::Test::AuthSourceBuilderTest::~AuthSourceBuilderTest
~AuthSourceBuilderTest()
Definition: AuthSourceBuilder.cpp:61
Leosac::Auth::SourceType::SIMPLE_WIEGAND
@ SIMPLE_WIEGAND
This define message formatting for data source SIMPLE_WIEGAND.
Leosac::Test::TEST_F
TEST_F(AuthSourceBuilderTest, BuildCardAndPing)
Definition: AuthSourceBuilder.cpp:121