rename test case file and add somre test cases

pull/32/head
Geoffrey Merck 4 years ago
parent 775805eaf7
commit 088259daae

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -16,23 +16,30 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include <gtest/gtest.h> #include "APRSFrame.h"
#include "../../APRSParser.h" CAPRSFrame::CAPRSFrame() :
m_source(),
m_destination(),
m_path(),
m_type(APFT_UNKNOWN)
{
class APRSParser_parseAPRSFrame_Tests : public ::testing::Test { }
};
TEST_F(APRSParser_parseAPRSFrame_Tests, EmpyString) { CAPRSFrame::CAPRSFrame(const std::string& source, const std::string& destination, const std::vector<std::string>& path, APRS_FRAME_TYPE type) :
m_source(source),
m_destination(destination),
m_path(),
m_type(type)
{
m_path.assign(path.begin(), path.end());
}
TAPRSFrame aprsFrame; void CAPRSFrame::clear()
bool retVal = CAPRSParser::parseFrame("", aprsFrame); {
m_source.clear();
EXPECT_FALSE(retVal); m_destination.clear();
EXPECT_STRCASEEQ(aprsFrame.m_body.c_str(), ""); m_path.clear();
EXPECT_STRCASEEQ(aprsFrame.m_dest.c_str(), ""); m_type = APFT_UNKNOWN;
EXPECT_STRCASEEQ(aprsFrame.m_source.c_str(), "");
EXPECT_EQ(aprsFrame.m_type, APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.m_path.size(), 0U);
} }

@ -27,10 +27,22 @@ enum APRS_FRAME_TYPE {
APFT_MESSAGE, APFT_MESSAGE,
}; };
typedef struct { class CAPRSFrame {
public:
CAPRSFrame();
CAPRSFrame(const std::string& source, const std::string& destination, const std::vector<std::string>& path, APRS_FRAME_TYPE type);
void clear();
std::string& getSource() { return m_source; }
std::string& getDestination() { return m_destination; }
std::vector<std::string>& getPath() { return m_path; }
std::string& getBody() { return m_body; }
APRS_FRAME_TYPE& getType() { return m_type; }
private:
std::string m_source; std::string m_source;
std::string m_dest; std::string m_destination;
std::vector<std::string> m_path; std::vector<std::string> m_path;
std::string m_body; std::string m_body;
APRS_FRAME_TYPE m_type; APRS_FRAME_TYPE m_type;
} TAPRSFrame; };

@ -17,42 +17,66 @@
*/ */
#include "APRSParser.h" #include "APRSParser.h"
#include "Log.h"
bool CAPRSParser::parseFrame(const std::string& frameStr, TAPRSFrame& frame) bool CAPRSParser::parseFrame(const std::string& frameStr, CAPRSFrame& frame)
{ {
frame.m_body.clear(); frame.clear();
frame.m_dest.clear(); bool ret = false;
frame.m_path.clear();
frame.m_source.clear();
frame.m_type = APFT_UNKNOWN;
if(!frameStr.empty()) if(!frameStr.empty()) {
return false; auto pos = frameStr.find_first_of(':');
if(pos != std::string::npos && pos != frameStr.length() - 1) {
auto header = frameStr.substr(0, pos); // contains source, dest and path
auto body = frameStr.substr(pos +1);
auto pos = frameStr.find_first_of(':'); std::vector<std::string> headerSplits;
if(pos == std::string::npos || pos == frameStr.length() - 1) boost::split(headerSplits, header, [](char c) { return c == ',' || c == '>';});
return false;
auto header = frameStr.substr(0, pos); // contains sours, dest and path //we need at least source and dest to form a valid frame, also headers shall not contain empty strings
auto body = frameStr.substr(pos +1); if(headerSplits.size() >= 2 && std::none_of(headerSplits.begin(), headerSplits.end(), [](std::string s){ return s.empty(); })) {
frame.getSource().assign(headerSplits[0]);
frame.getDestination().assign(headerSplits[1]);
std::vector<std::string> headerSplits; for(unsigned int i = 2; i < headerSplits.size(); i++) {
boost::split(headerSplits, header, [](char c) { return c == ',' || c == '>';}); frame.getPath().push_back(headerSplits[i]);
}
if(headerSplits.size() < 2) //we need at least source and dest to form a valid frame frame.getBody().assign(body);
return false;
frame.m_source.assign(headerSplits[0]); setFrameType(frame);
frame.m_dest.assign(headerSplits[1]); if(frame.getType() == APFT_UNKNOWN) {
CLog::logInfo("Invalid or unsupported APRS frame : %s", frameStr);
for(unsigned int i = 2; i < headerSplits.size(); i++) { }
frame.m_path.push_back(headerSplits[i]); else {
ret = true;
}
}
}
} }
frame.m_body.assign(body); return ret;
}
frame.m_type = body[0] == ':' ? APFT_MESSAGE : APFT_UNKNOWN; void CAPRSParser::setFrameType(CAPRSFrame& frame)
{
APRS_FRAME_TYPE type = APFT_UNKNOWN;
std::string body(frame.getBody());
return true; if(!body.empty()) {
} switch (body[0])
{
case ':':
if(body[10] == ':')
type = APFT_MESSAGE;
break;
default:
break;
}
}
frame.getType() = type;
if(type == APFT_UNKNOWN)
frame.clear();
}

@ -26,6 +26,8 @@
class CAPRSParser class CAPRSParser
{ {
public: public:
//TODO 2022-01-02 move these to own class ? static bool parseFrame(const std::string& frameStr, CAPRSFrame& frame);
static bool parseFrame(const std::string& frameStr, TAPRSFrame& frame);
private:
static void setFrameType(CAPRSFrame& frame);
}; };

@ -0,0 +1,92 @@
/*
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <gtest/gtest.h>
#include "../../APRSParser.h"
class APRSParser_parseAPRSFrame : public ::testing::Test {
};
TEST_F(APRSParser_parseAPRSFrame, EmpyString) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("", aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), "");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "");
EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.getPath().size(), 0U);
}
TEST_F(APRSParser_parseAPRSFrame, NoSourceCallsign) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame(">APRS::F4ABC Test Message", aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), "");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "");
EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.getPath().size(), 0U);
}
TEST_F(APRSParser_parseAPRSFrame, NoDestCallsign) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("N0CALL>::F4ABC Test Message", aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), "");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "");
EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.getPath().size(), 0U);
}
TEST_F(APRSParser_parseAPRSFrame, CorrectMessageFrameWithDigipeater) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("N0CALL>APRS,WIDE1-1,WIDE2-2::F4ABC :Test Message", aprsFrame);
EXPECT_TRUE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), ":F4ABC :Test Message");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "APRS");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "N0CALL");
EXPECT_EQ(aprsFrame.getType(), APFT_MESSAGE);
EXPECT_EQ(aprsFrame.getPath().size(), 2);
EXPECT_STREQ(aprsFrame.getPath()[0].c_str(), "WIDE1-1");
EXPECT_STREQ(aprsFrame.getPath()[1].c_str(), "WIDE2-2");
}
TEST_F(APRSParser_parseAPRSFrame, CorrectMessageFrameWithoutDigipeater) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("N0CALL>APRS::F4ABC :Test Message", aprsFrame);
EXPECT_TRUE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), ":F4ABC :Test Message");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "APRS");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "N0CALL");
EXPECT_EQ(aprsFrame.getType(), APFT_MESSAGE);
EXPECT_EQ(aprsFrame.getPath().size(), 0);
}
Loading…
Cancel
Save

Powered by TurnKey Linux.