Leosac  0.8.0
Open Source Access Control
Rpleth.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 
20 #include "core/Scheduler.hpp"
21 #include "core/auth/Auth.hpp"
23 #include "helper/TestHelper.hpp"
26 #include "tools/log.hpp"
27 #include <memory>
28 
29 using namespace Leosac::Module::Rpleth;
30 using namespace Leosac::Test::Helper;
31 
32 namespace Leosac
33 {
34 namespace Test
35 {
36 
38 {
39  public:
40  virtual bool run_module(zmqpp::socket *pipe)
41  {
42  boost::property_tree::ptree cfg, module_cfg;
43 
44  module_cfg.add("port", "4242");
45  module_cfg.add("reader", "WIEGAND1");
46  module_cfg.add("stream_mode", "true");
47 
48  cfg.add("name", "RPLETH");
49  cfg.add_child("module_config", module_cfg);
50  return test_run_module<RplethModule>(&ctx_, pipe, cfg);
51  }
52 
54  : w1(ctx_, "WIEGAND1")
55  , w1_actor(std::bind(&FakeWiegandReader::run, &w1, std::placeholders::_1))
56  {
57  }
58 
59  virtual ~RplethTest()
60  {
61  }
62 
63  RplethPacket extract_packet(const std::string &data)
64  {
65  ciruclar_buf_.write(reinterpret_cast<const unsigned char *>(data.c_str()),
66  data.size()); // write those to circular buffer
67  RplethPacket rpleth_packet =
68  RplethProtocol::decodeCommand(ciruclar_buf_, true);
69  return rpleth_packet;
70  }
71 
76 
77  zmqpp::socket connect_to_rpleth()
78  {
79  zmqpp::message msg;
80  std::string connection_identity, data;
81 
82  zmqpp::socket client(ctx_, zmqpp::socket_type::stream);
83  client.connect("tcp://127.0.0.1:4242");
84  client.receive(msg);
85 
86  assert(msg.parts() == 2);
87  msg >> connection_identity;
88  assert(msg.size(1) == 0);
89  return std::move(client);
90  }
91 
95  void check_rpleth_card_msg(zmqpp::socket &source,
96  const std::vector<uint8_t> card_binary)
97  {
98  zmqpp::message msg;
99  std::string connection_identity, data;
100 
101  source.receive(msg);
102  msg >> connection_identity;
103  msg >> data; // data we would read from socket
104  RplethPacket rpleth_packet = extract_packet(data);
105 
106  ASSERT_TRUE(rpleth_packet.isGood);
107  ASSERT_EQ(8, +rpleth_packet.dataLen);
108  ASSERT_EQ(8, +rpleth_packet.data.size());
109 
110  ASSERT_EQ(card_binary, rpleth_packet.data);
111  }
112 
114  // to run the fake reader in a thread.
115  zmqpp::actor w1_actor;
116 };
117 
118 TEST(Rpleth, TestConvertCard)
119 {
120  std::vector<uint8_t> out;
121  std::vector<uint8_t> card_binary = {0x00, 0x00, 0x00, 0x00,
122  0xff, 0xff, 0xff, 0xff};
123 
124  out = RplethModule::card_convert_from_text(std::make_pair("ff:ff:ff:ff", 32));
125  ASSERT_EQ(card_binary, out);
126 
127  card_binary = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
128  out = RplethModule::card_convert_from_text(std::make_pair("00:00:00:00", 32));
129  ASSERT_EQ(card_binary, out);
130 
131  card_binary = {0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x05, 0x85};
132  out = RplethModule::card_convert_from_text(std::make_pair("80:81:61:40", 26));
133  ASSERT_EQ(card_binary, out);
134 
135  card_binary = {0x00, 0x00, 0x00, 0x00, 0x80, 0x81, 0x61, 0x40};
136  out = RplethModule::card_convert_from_text(std::make_pair("80:81:61:40", 32));
137  ASSERT_EQ(card_binary, out);
138 }
139 
143 TEST_F(RplethTest, TestReceiveStreamCardsSimple)
144 {
145  zmqpp::socket client = connect_to_rpleth();
146 
147  // fake wiegand reader activity.
148  bus_push_.send(zmqpp::message() << "S_WIEGAND1"
150  << "ff:ab:cd:ef" << 32);
151  check_rpleth_card_msg(client, {0x00, 0x00, 0x00, 0x00, 0xff, 0xab, 0xcd, 0xef});
152 }
153 
154 TEST_F(RplethTest, TestReceiveStreamCards2)
155 {
156  zmqpp::message msg;
157  std::string connection_identity, data;
158  zmqpp::socket client = connect_to_rpleth();
159 
160  // fake wiegand reader activity.
161  bus_push_.send(zmqpp::message() << "S_WIEGAND1"
163  << "ff:ab:cd:ef" << 32);
164  check_rpleth_card_msg(client, {0x00, 0x00, 0x00, 0x00, 0xff, 0xab, 0xcd, 0xef});
165 
166  bus_push_.send(zmqpp::message() << "S_WIEGAND1"
168  << "11:22:33:44" << 32);
169  check_rpleth_card_msg(client, {0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44});
170 
171  bus_push_.send(zmqpp::message() << "S_WIEGAND1"
173  << "80:81:61:40" << 26);
174  check_rpleth_card_msg(client, {0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x05, 0x85});
175 
176  bus_push_.send(zmqpp::message() << "S_IGNORED_READER"
178  << "11:22:33:44" << 32);
179  std::this_thread::sleep_for(std::chrono::milliseconds(500));
180  // nothing to read anymore
181  ASSERT_FALSE(client.receive(msg, true));
182 }
183 }
184 }
Leosac::Test::RplethTest::RplethTest
RplethTest()
Definition: Rpleth.cpp:53
Leosac::Test::RplethTest::check_rpleth_card_msg
void check_rpleth_card_msg(zmqpp::socket &source, const std::vector< uint8_t > card_binary)
Check that we can read a rpleth from the socket and check that its valid.
Definition: Rpleth.cpp:95
Leosac::Test::RplethTest::extract_packet
RplethPacket extract_packet(const std::string &data)
Definition: Rpleth.cpp:63
RplethModule.hpp
Leosac::Test::RplethTest::ciruclar_buf_
CircularBuffer ciruclar_buf_
Simulate client buffer.
Definition: Rpleth.cpp:75
Auth.hpp
Leosac::Test::RplethTest::w1_actor
zmqpp::actor w1_actor
Definition: Rpleth.cpp:115
Leosac::Module::Rpleth::RplethProtocol::decodeCommand
static RplethPacket decodeCommand(CircularBuffer &buffer, bool from_server=false)
Decode a packet from a circular buffer object.
Definition: rplethprotocol.cpp:32
Leosac::Module::Rpleth::RplethPacket
Definition: rplethpacket.hpp:39
Leosac::Test::RplethTest::w1
FakeWiegandReader w1
Definition: Rpleth.cpp:113
Leosac::Test::Helper::FakeWiegandReader
A test helper that fake a WiegandReader (it read command on a socket and send OK)
Definition: FakeWiegandReader.hpp:33
Scheduler.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::Rpleth::RplethPacket::dataLen
Byte dataLen
Definition: rplethpacket.hpp:64
Leosac::Module::Rpleth
Namespace where implementation for Rpleth support takes place.
Definition: circularbuffer.hpp:38
Leosac::Test::RplethTest::connect_to_rpleth
zmqpp::socket connect_to_rpleth()
Definition: Rpleth.cpp:77
Leosac::Test::TEST
TEST(Rpleth, TestConvertCard)
Definition: Rpleth.cpp:118
TestHelper.hpp
Leosac::Test::RplethTest
Definition: Rpleth.cpp:37
Leosac::Module::Rpleth::RplethModule::card_convert_from_text
static std::vector< uint8_t > card_convert_from_text(const std::pair< std::string, int > &card_info)
Convert a card number from textual hexadecimal representation to a 8 bytes byte-vector in Network Byt...
Definition: RplethModule.cpp:386
Leosac::Test::Helper
Unit testing utility class that helps writing test.
Definition: FakeGPIO.hpp:28
log.hpp
rplethprotocol.hpp
Rpleth protocol implementation.
FakeWiegandReader.hpp
Leosac::Test::Helper::TestHelper
Base class for test fixtures, it defines a ZMQ context, a BUS and a sub socket connect to the bus.
Definition: TestHelper.hpp:116
Leosac::Module::Rpleth::CircularBuffer
Implementation of a ring buffer.
Definition: circularbuffer.hpp:43
Leosac::Test::RplethTest::~RplethTest
virtual ~RplethTest()
Definition: Rpleth.cpp:59
Leosac::Test::RplethTest::run_module
virtual bool run_module(zmqpp::socket *pipe)
Called in the module's actor's thread.
Definition: Rpleth.cpp:40
Leosac::Module::Rpleth::RplethPacket::data
std::vector< Byte > data
Definition: rplethpacket.hpp:65
Leosac::Module::Rpleth::RplethPacket::isGood
bool isGood
Definition: rplethpacket.hpp:67
Leosac::Auth::SourceType::SIMPLE_WIEGAND
@ SIMPLE_WIEGAND
This define message formatting for data source SIMPLE_WIEGAND.
Leosac::Test::TEST_F
TEST_F(RplethTest, TestReceiveStreamCards2)
Definition: Rpleth.cpp:154