#9 #12 fix escaping and checksum calculation

pull/32/head
Geoffrey Merck 4 years ago
parent 6cc1a50c79
commit a9953aec29

@ -23,11 +23,9 @@ std::vector<char> CRSMS1AMessageBuilder::m_charsToEscape = {-17, 0, 17, 19, -2,
void CRSMS1AMessageBuilder::buildMessage(std::string& message, const std::string& sender, const std::string& recipient, const std::string body)
{
std::string bodyTmp(body);
auto bodyCrc = calculateBodyCRC(body);
bodyTmp.push_back(bodyCrc);
escapeBody(bodyTmp, std::string(bodyTmp));
std::string bodyTmp;
escapeBody(bodyTmp, body + bodyCrc);
std::string header = CStringUtils::string_format("%s,%s,0011", sender.c_str(), recipient.c_str());
char c1, c2;
@ -47,15 +45,15 @@ char CRSMS1AMessageBuilder::calculateBodyCRC(const std::string& body)
num += c;
}
return (char)((num & 255) /*- 128*/);
return (char)((num & 255) - 128);
}
void CRSMS1AMessageBuilder::escapeBody(std::string output, const std::string& body)
void CRSMS1AMessageBuilder::escapeBody(std::string& output, const std::string& body)
{
output.clear();
for(char c : body) {
if(std::find(m_charsToEscape.begin(), m_charsToEscape.end(), c) != m_charsToEscape.end()) {
output.push_back(-17);
output.push_back('o');
}
output.push_back(c);
}

@ -28,7 +28,7 @@ public:
private:
static void calcMsgIcomCRC(const std::string& msg, char& c1, char& c2);
static void escapeBody(std::string output, const std::string& body);
static void escapeBody(std::string& output, const std::string& body);
static void escapeBytes(std::vector<char> output, const std::vector<char> input);
static char calculateBodyCRC(const std::string& body);
static char doWhatever(char b2);

@ -27,7 +27,7 @@ namespace RSMS1AMessageBuilder
};
TEST_F(RSMS1AMessageBuilder_buildMessage, testABC)
TEST_F(RSMS1AMessageBuilder_buildMessage, ABC)
{
std::string message;
CRSMS1AMessageBuilder::buildMessage(message, "KC3FRA", "F4FXL", "ABC");
@ -35,7 +35,7 @@ namespace RSMS1AMessageBuilder
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118ABCF\n");
}
TEST_F(RSMS1AMessageBuilder_buildMessage, testA)
TEST_F(RSMS1AMessageBuilder_buildMessage, A)
{
std::string message;
CRSMS1AMessageBuilder::buildMessage(message, "KC3FRA", "F4FXL", "A");
@ -43,7 +43,7 @@ namespace RSMS1AMessageBuilder
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118AA\n");
}
TEST_F(RSMS1AMessageBuilder_buildMessage, testAA)
TEST_F(RSMS1AMessageBuilder_buildMessage, AA)
{
std::string message;
CRSMS1AMessageBuilder::buildMessage(message, "KC3FRA", "F4FXL", "AA");
@ -51,11 +51,27 @@ namespace RSMS1AMessageBuilder
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118AA\02\n");
}
TEST_F(RSMS1AMessageBuilder_buildMessage, testSalutCommentVasTu)
TEST_F(RSMS1AMessageBuilder_buildMessage, SalutCommentVasTu)
{
std::string message;
CRSMS1AMessageBuilder::buildMessage(message, "KC3FRA", "F4FXL", "Salut, comment vas tu?");
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118Saluto, comment vas tu?\x7A\n");
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118Saluto, comment vas tu?z\n");
}
TEST_F(RSMS1AMessageBuilder_buildMessage, escapeComma)
{
std::string message;
CRSMS1AMessageBuilder::buildMessage(message, "KC3FRA", "F4FXL", ",");
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118o,o,\n");
}
TEST_F(RSMS1AMessageBuilder_buildMessage, INeedMoreDollars)
{
std::string message;
CRSMS1AMessageBuilder::buildMessage(message, "KC3FRA", "F4FXL", "I need more $$$$");
EXPECT_STREQ(message.c_str(), "$$Msg,KC3FRA,F4FXL,001118I need more o$o$o$o$\x08\n");
}
};
Loading…
Cancel
Save

Powered by TurnKey Linux.