Skip to content

Commit

Permalink
Fix NPE and add tests for E133StatusHelper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewman committed Apr 13, 2024
1 parent d6ff723 commit 5b8e4d7
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
23 changes: 23 additions & 0 deletions libs/acn/E133StatusHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <stdint.h>
#include <string>
#include "ola/Logging.h"
#include "ola/e133/E133StatusHelper.h"

namespace ola {
Expand All @@ -32,6 +33,11 @@ using std::string;
* Verify that the int is a valid E1.33 Status Code.
*/
bool IntToStatusCode(uint16_t input, E133StatusCode *status_code) {
if (!status_code) {
OLA_WARN << "ola:e133::IntToStatusCode: missing status_code";
return false;
}

switch (input) {
case ola::e133::SC_E133_ACK:
*status_code = ola::e133::SC_E133_ACK;
Expand Down Expand Up @@ -101,6 +107,12 @@ string StatusCodeToString(E133StatusCode status_code) {

bool IntToConnectStatusCode(uint16_t input,
E133ConnectStatusCode *connect_status_code) {
if (!connect_status_code) {
OLA_WARN << "ola:e133::IntToConnectStatusCode: missing "
<< "connect_status_code";
return false;
}

switch (input) {
case ola::e133::CONNECT_OK:
*connect_status_code = ola::e133::CONNECT_OK;
Expand Down Expand Up @@ -147,6 +159,11 @@ string ConnectStatusCodeToString(E133ConnectStatusCode connect_status_code) {

bool IntToRPTStatusCode(uint16_t input,
RPTStatusVector *rpt_status_code) {
if (!rpt_status_code) {
OLA_WARN << "ola:e133::IntToRPTStatusCode: missing rpt_status_code";
return false;
}

switch (input) {
case ola::acn::VECTOR_RPT_STATUS_UNKNOWN_RPT_UID:
*rpt_status_code = ola::acn::VECTOR_RPT_STATUS_UNKNOWN_RPT_UID;
Expand Down Expand Up @@ -208,6 +225,12 @@ string RPTStatusCodeToString(RPTStatusVector rpt_status_code) {

bool RPTStatusCodeToRDMStatusCode(RPTStatusVector rpt_status_code,
RDMStatusCode *rdm_status_code) {
if (!rdm_status_code) {
OLA_WARN << "ola:e133::RPTStatusCodeToRDMStatusCode: missing "
<< "rdm_status_code";
return false;
}

// TODO(Peter): Fill in the gaps, possibly adding additional RDMStatusCodes
// if required
switch (rpt_status_code) {
Expand Down
124 changes: 124 additions & 0 deletions libs/acn/E133StatusHelperTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*

Check failure on line 1 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / Check Licences

File does not start with, or not exact match of, " * This program is free software; you can redistribute it and/or modify..."
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* E133StatusHelperTest.cpp
* Test fixture for the E133 Status Helper code
* Copyright (C) 2024 Peter Newman
*/

#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
#include <string>

#include "ola/e133/E133Enums.h"
#include "ola/e133/E133StatusHelper.h"
#include "ola/testing/TestUtils.h"

using std::string;
using ola::e133::IntToStatusCode;
using ola::e133::IntToConnectStatusCode;
using ola::e133::IntToRPTStatusCode;
using ola::e133::RPTStatusCodeToRDMStatusCode;

class E133StatusHelperTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(E133StatusHelperTest);
CPPUNIT_TEST(testIntToStatusCode);
CPPUNIT_TEST(testIntToConnectStatusCode);
CPPUNIT_TEST(testIntToRPTStatusCode);
CPPUNIT_TEST(testRPTStatusCodeToRDMStatusCode);
CPPUNIT_TEST_SUITE_END();

public:
void testIntToStatusCode();
void testIntToConnectStatusCode();
void testIntToRPTStatusCode();
void testRPTStatusCodeToRDMStatusCode();
};

CPPUNIT_TEST_SUITE_REGISTRATION(E133StatusHelperTest);


/*
* Test the IntToStatusCode function.
*/
void E133StatusHelperTest::testIntToStatusCode() {
ola::e133::E133StatusCode value;
OLA_ASSERT_TRUE(IntToStatusCode(0, &value));
OLA_ASSERT_EQ(value, ola::e133::SC_E133_ACK);
OLA_ASSERT_TRUE(IntToStatusCode(1, &value));
OLA_ASSERT_EQ(value, ola::e133::SC_E133_RDM_TIMEOUT);
OLA_ASSERT_TRUE(IntToStatusCode(9, &value));
OLA_ASSERT_EQ(value, ola::e133::SC_E133_BROADCAST_COMPLETE);
// Update this if additional entries are added to the enum
OLA_ASSERT_FALSE(IntToStatusCode((ola::e133::SC_E133_BROADCAST_COMPLETE + 1), &value));

Check failure on line 65 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
// test null status code
OLA_ASSERT_FALSE(IntToStatusCode(0, NULL));
}


/*
* Test the IntToConnectStatusCode function.
*/
void E133StatusHelperTest::testIntToConnectStatusCode() {
ola::e133::E133ConnectStatusCode value;
OLA_ASSERT_TRUE(IntToConnectStatusCode(0, &value));
OLA_ASSERT_EQ(value, ola::e133::CONNECT_OK);
OLA_ASSERT_TRUE(IntToConnectStatusCode(1, &value));
OLA_ASSERT_EQ(value, ola::e133::CONNECT_SCOPE_MISMATCH);
OLA_ASSERT_TRUE(IntToConnectStatusCode(5, &value));
OLA_ASSERT_EQ(value, ola::e133::CONNECT_INVALID_UID);
// Update this if additional entries are added to the enum
OLA_ASSERT_FALSE(IntToConnectStatusCode((ola::e133::CONNECT_INVALID_UID + 1), &value));

Check failure on line 83 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
// test null status code
OLA_ASSERT_FALSE(IntToConnectStatusCode(0, NULL));
}


/*
* Test the IntToRPTStatusCode function.
*/
void E133StatusHelperTest::testIntToRPTStatusCode() {
ola::acn::RPTStatusVector value;
OLA_ASSERT_TRUE(IntToRPTStatusCode(1, &value));
OLA_ASSERT_EQ(value, ola::acn::VECTOR_RPT_STATUS_UNKNOWN_RPT_UID);
OLA_ASSERT_TRUE(IntToRPTStatusCode(2, &value));
OLA_ASSERT_EQ(value, ola::acn::VECTOR_RPT_STATUS_RDM_TIMEOUT);
OLA_ASSERT_TRUE(IntToRPTStatusCode(9, &value));
OLA_ASSERT_EQ(value, ola::acn::VECTOR_RPT_STATUS_INVALID_COMMAND_CLASS);
// Update this if additional entries are added to the enum
OLA_ASSERT_FALSE(IntToRPTStatusCode((ola::acn::VECTOR_RPT_STATUS_INVALID_COMMAND_CLASS + 1), &value));

Check failure on line 101 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
// test null status code
OLA_ASSERT_FALSE(IntToRPTStatusCode(1, NULL));
}


/*
* Test the RPTStatusCodeToRDMStatusCode function.
*/
void E133StatusHelperTest::testRPTStatusCodeToRDMStatusCode() {
ola::rdm::RDMStatusCode value;
// Update this if earlier entries are added to the conversion
OLA_ASSERT_TRUE(RPTStatusCodeToRDMStatusCode(ola::acn::VECTOR_RPT_STATUS_RDM_TIMEOUT, &value));

Check failure on line 113 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
OLA_ASSERT_EQ(value, ola::rdm::RDM_TIMEOUT);
OLA_ASSERT_TRUE(RPTStatusCodeToRDMStatusCode(ola::acn::VECTOR_RPT_STATUS_RDM_INVALID_RESPONSE, &value));

Check failure on line 115 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
OLA_ASSERT_EQ(value, ola::rdm::RDM_INVALID_RESPONSE);
OLA_ASSERT_TRUE(RPTStatusCodeToRDMStatusCode(ola::acn::VECTOR_RPT_STATUS_INVALID_COMMAND_CLASS, &value));

Check failure on line 117 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
OLA_ASSERT_EQ(value, ola::rdm::RDM_INVALID_COMMAND_CLASS);
// Because the function accepts an enum as input, the compile traps any out of range source entry

Check failure on line 119 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
// Remove this once we provide suitable mapping for all statuses
OLA_ASSERT_FALSE(RPTStatusCodeToRDMStatusCode(ola::acn::VECTOR_RPT_STATUS_UNKNOWN_RPT_UID, &value));

Check failure on line 121 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
// test null status code
OLA_ASSERT_FALSE(RPTStatusCodeToRDMStatusCode(ola::acn::VECTOR_RPT_STATUS_RDM_TIMEOUT, NULL));

Check failure on line 123 in libs/acn/E133StatusHelperTest.cpp

View workflow job for this annotation

GitHub Actions / cpplint

Lines should be <= 80 characters long [whitespace/line_length] [2]
}
1 change: 1 addition & 0 deletions libs/acn/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ libs_acn_E133Tester_SOURCES = \
libs/acn/BrokerNullInflatorTest.cpp \
libs/acn/BrokerNullPDUTest.cpp \
libs/acn/BrokerPDUTest.cpp \
libs/acn/E133StatusHelperTest.cpp \
libs/acn/E133InflatorTest.cpp \
libs/acn/E133PDUTest.cpp \
libs/acn/RDMPDUTest.cpp \
Expand Down

0 comments on commit 5b8e4d7

Please sign in to comment.