Leosac  0.8.0
Open Source Access Control
CredentialValidator.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 
24 #include "gtest/gtest.h"
25 
26 using namespace Leosac;
27 using namespace Leosac::Cred;
28 
29 namespace Leosac
30 {
31 namespace Test
32 {
33 
34 TEST(TestCredentialValidator, alias_length)
35 {
36  RFIDCard c;
37 
38  ASSERT_NO_THROW({ c.alias("long_enough"); });
39 
40  ASSERT_NO_THROW({ c.alias("a"); });
41 
42  ASSERT_THROW(
43  { c.alias("this_is_so_long_this_alias_is_clearly_to_long_to_be_valid"); },
45 
46  std::string alias;
47  for (int i = 0; i < 51; ++i)
48  {
49  ASSERT_NO_THROW(c.alias(alias));
50  alias += '_';
51  }
52  ASSERT_THROW({ c.alias(alias); }, ModelException);
53 }
54 
55 TEST(TestWiegandCardValidator, card_id)
56 {
57  RFIDCard c;
58 
59  ASSERT_THROW({ c.card_id("11:22:33:4"); }, ModelException);
60  ASSERT_THROW({ c.card_id("11:22:334"); }, ModelException);
61  ASSERT_THROW({ c.card_id("lama"); }, ModelException);
62  ASSERT_THROW({ c.card_id("1a:2-b"); }, ModelException);
63 
64  ASSERT_NO_THROW({ c.card_id("11:22:33:44"); });
65  ASSERT_NO_THROW({ c.card_id("aa:bb:cc"); });
66 }
67 
68 TEST(TestWiegandCardValidator, nb_bits)
69 {
70  RFIDCard c;
71  ASSERT_THROW(c.nb_bits(-1), ModelException);
72  ASSERT_THROW(c.nb_bits(-42), ModelException);
73  ASSERT_THROW(c.nb_bits(-1024), ModelException);
74 
75  ASSERT_NO_THROW(c.nb_bits(26));
76  ASSERT_NO_THROW(c.nb_bits(37));
77  ASSERT_NO_THROW(c.nb_bits(500)); // Makes no sense, but is not negative.
78 }
79 
80 TEST(TestWiegandPinValidator, pin_is_numeric)
81 {
82  PinCode c;
83  ASSERT_THROW(c.pin_code("-1"), ModelException);
84  ASSERT_THROW(c.pin_code("-42"), ModelException);
85  ASSERT_THROW(c.pin_code("-1024"), ModelException);
86 
87  ASSERT_NO_THROW(c.pin_code("26"));
88  ASSERT_NO_THROW(c.pin_code("37"));
89  ASSERT_NO_THROW(c.pin_code("500"));
90  ASSERT_NO_THROW(c.pin_code("12345678"));
91 }
92 }
93 }
Leosac::Cred::PinCode
A PinCode credential.
Definition: PinCode.hpp:33
Leosac::Test::TEST
TEST(TestWiegandPinValidator, pin_is_numeric)
Definition: CredentialValidator.cpp:80
Leosac::Cred::RFIDCard::nb_bits
virtual int nb_bits() const override
Definition: RFIDCard.cpp:39
RFIDCard.hpp
ICredential.hpp
Leosac::Cred::RFIDCard
An RFID card credential.
Definition: RFIDCard.hpp:33
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Cred::PinCode::pin_code
const std::string & pin_code() const override
Definition: PinCode.cpp:28
ModelException.hpp
PinCode.hpp
Leosac::Cred::RFIDCard::card_id
virtual const std::string & card_id() const override
Definition: RFIDCard.cpp:34
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::Cred::Credential::alias
virtual std::string alias() const override
An alias for the credential.
Definition: Credential.cpp:43
Leosac::Cred
Definition: Credential.hpp:31