WIP: BG95/sequencer sanitizers

This commit is contained in:
2021-09-23 19:07:56 +03:00
parent 2c67fb6069
commit bab3fd04fe
4 changed files with 91 additions and 215 deletions
+36 -29
View File
@@ -46,11 +46,14 @@
namespace test_bg95_base {
using namespace tbx;
using Q = equeue<char, 512, true>;
// BG95 implementer mock
class BG95 : public BG95_base<BG95, Q, 256> {
using base_type = BG95_base<BG95, Q, 256>;
template<size_t N>
class BG95 : public BG95_base<BG95<N>, equeue<char, N, true>, N> {
using Q = equeue<char, N, true>;
using base_type = BG95_base<BG95<N>, Q, N>;
public:
enum class event {
@@ -172,18 +175,18 @@ namespace test_bg95_base {
* Test inetd in non blocking mode
*/
TEST(TBG95_base, inetd_non_blocking) {
BG95 modem;
BG95<256> modem;
char buffer[256];
const BG95::async_handlers<2> async = {{
{"+QMTOPEN:", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
{"+QMT", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
const BG95<256>::async_handlers<2> async = {{
{"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
{"+QMT", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
}};
clear_flag();
modem.inetd(false, &async);
EXPECT_EQ (handler_flag, false);
modem.async(BG95::event::MQTT_DISCONNECT);
modem.async(BG95<256>::event::MQTT_DISCONNECT);
modem.inetd(false, &async); // parse "\r\n"
EXPECT_EQ (handler_flag, false);
modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
@@ -220,47 +223,51 @@ namespace test_bg95_base {
}
TEST(TBG95_base, run) {
BG95 modem;
BG95<256> modem;
const BG95::async_handlers<2> async = {{
{"+QMTOPEN:", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
{"+QMT", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
const BG95<256>::async_handlers<2> async = {{
{"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
{"+QMT", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
}};
const BG95::script_t<5> script = {{
/* 0 */{BG95::control_t::NOP, {"", BG95::match_t::NO, nullptr, BG95::action_t::GOTO, 1}, 1000},
/* 1 */{BG95::control_t::SEND, {"ATE0\r\n", BG95::match_t::NO, nullptr, BG95::action_t::NEXT, 0}, 0},
/* 2 */{BG95::control_t::EXPECT, {{
{"OK\r\n", BG95::match_t::ENDS_WITH, nullptr, BG95::action_t::NEXT, 0},
{"ERROR", BG95::match_t::CONTAINS, nullptr, BG95::action_t::EXIT_ERROR, 0} }},
const BG95<256>::script_t<5> script = {{
/* 0 */{BG95<256>::control_t::NOP, {"", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::GOTO, 1}, 1000},
/* 1 */{BG95<256>::control_t::SEND, {"ATE0\r\n", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::NEXT, 0}, 0},
/* 2 */{BG95<256>::control_t::EXPECT, {{
{"OK\r\n", BG95<256>::match_t::ENDS_WITH, nullptr, BG95<256>::action_t::NEXT, 0},
{"ERROR", BG95<256>::match_t::CONTAINS, nullptr, BG95<256>::action_t::EXIT_ERROR, 0} }},
1000
},
/* 3 */{BG95::control_t::SEND, {"AT+CSQ\r\n", BG95::match_t::NO, nullptr, BG95::action_t::NEXT, 0}, 0},
/* 4 */{BG95::control_t::EXPECT, {{
{"OK\r\n", BG95::match_t::ENDS_WITH, nullptr, BG95::action_t::EXIT_OK, 0},
{"ERROR", BG95::match_t::CONTAINS, nullptr, BG95::action_t::EXIT_ERROR, 0} }},
/* 3 */{BG95<256>::control_t::SEND, {"AT+CSQ\r\n", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::NEXT, 0}, 0},
/* 4 */{BG95<256>::control_t::EXPECT, {{
{"OK\r\n", BG95<256>::match_t::ENDS_WITH, nullptr, BG95<256>::action_t::EXIT_OK, 0},
{"ERROR", BG95<256>::match_t::CONTAINS, nullptr, BG95<256>::action_t::EXIT_ERROR, 0} }},
1000
},
}};
std::atomic<bool> lock(true);
std::mutex m;
m.lock();
std::thread th1 ([&](){
do
modem.inetd(false, &async);
while (lock.load(std::memory_order_acquire));
while (!m.try_lock());
m.unlock();
});
EXPECT_EQ (modem.run(script), true);
lock.store(false, std::memory_order_acq_rel);
m.unlock(); // stop and join inetd
th1.join();
}
TEST(TBG95_base, command) {
BG95 modem;
BG95<256> modem;
std::atomic<bool> lock(true);
std::mutex m;
m.lock();
std::thread th1 ([&](){
do
modem.inetd(false);
while (lock.load(std::memory_order_acquire));
while (!m.try_lock());
m.unlock();
});
int status;
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", status), true);
@@ -270,7 +277,7 @@ namespace test_bg95_base {
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n%\r\n\r\nOK\r\n", substr), true);
EXPECT_EQ (strcmp("+CREG: 0,5", substr), 0);
lock.store(false, std::memory_order_acq_rel); // stop and join inetd
m.unlock(); // stop and join inetd
th1.join();
}