diff --git a/libs/acn/PDU.cpp b/libs/acn/PDU.cpp index 0f256eca1..33e40f720 100644 --- a/libs/acn/PDU.cpp +++ b/libs/acn/PDU.cpp @@ -50,6 +50,16 @@ unsigned int PDU::Size() const { * @return false on error, true otherwise */ bool PDU::Pack(uint8_t *buffer, unsigned int *length) const { + if (!buffer) { + OLA_WARN << "PDU::Pack: missing buffer"; + return false; + } + + if (!length) { + OLA_WARN << "PDU::Pack: missing length"; + return false; + } + unsigned int size = Size(); unsigned int offset = 0; diff --git a/libs/acn/PDU.h b/libs/acn/PDU.h index 2be285dea..531a8e96b 100644 --- a/libs/acn/PDU.h +++ b/libs/acn/PDU.h @@ -22,6 +22,7 @@ #define LIBS_ACN_PDU_H_ #include +#include #include #include #include @@ -160,6 +161,16 @@ class PDUBlock { */ template bool PDUBlock::Pack(uint8_t *data, unsigned int *length) const { + if (!data) { + OLA_WARN << "PDUBlock::Pack: missing buffer"; + return false; + } + + if (!length) { + OLA_WARN << "PDUBock::Pack: missing length"; + return false; + } + bool status = true; unsigned int i = 0; typename std::vector::const_iterator iter; diff --git a/libs/acn/PDUTest.cpp b/libs/acn/PDUTest.cpp index a5d86cc5d..fd6e9de05 100644 --- a/libs/acn/PDUTest.cpp +++ b/libs/acn/PDUTest.cpp @@ -35,11 +35,13 @@ using ola::io::OutputStream; class PDUTest: public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(PDUTest); + CPPUNIT_TEST(testPDU); CPPUNIT_TEST(testPDUBlock); CPPUNIT_TEST(testBlockToOutputStream); CPPUNIT_TEST_SUITE_END(); public: + void testPDU(); void testPDUBlock(); void testBlockToOutputStream(); @@ -51,6 +53,41 @@ class PDUTest: public CppUnit::TestFixture { CPPUNIT_TEST_SUITE_REGISTRATION(PDUTest); +/* + * Test that packing a PDUBlock works. + */ +void PDUTest::testPDU() { + MockPDU pdu(0x1234, 0x2468); + + OLA_ASSERT_EQ(4u, pdu.HeaderSize()); + OLA_ASSERT_EQ(4u, pdu.DataSize()); + OLA_ASSERT_EQ(14u, pdu.Size()); + + unsigned int size = pdu.Size(); + uint8_t *data = new uint8_t[size]; + unsigned int bytes_used = size; + OLA_ASSERT(pdu.Pack(data, &bytes_used)); + OLA_ASSERT_EQ(size, bytes_used); + + // test null data + OLA_ASSERT_FALSE(pdu.Pack(NULL, &bytes_used)); + + // test a null length + OLA_ASSERT_FALSE(pdu.Pack(data, NULL)); + + // test undersized buffer + bytes_used = size - 1; + OLA_ASSERT_FALSE(pdu.Pack(data, &bytes_used)); + OLA_ASSERT_EQ(0u, bytes_used); + + // test oversized buffer + bytes_used = size + 1; + OLA_ASSERT(pdu.Pack(data, &bytes_used)); + OLA_ASSERT_EQ(size, bytes_used); + delete[] data; +} + + /* * Test that packing a PDUBlock works. */ @@ -74,6 +111,24 @@ void PDUTest::testPDUBlock() { OLA_ASSERT_EQ(1u, *test++); OLA_ASSERT_EQ(2u, *test++); OLA_ASSERT_EQ(42u, *test); + + // test null data + OLA_ASSERT_FALSE(block.Pack(NULL, &bytes_used)); + + // test a null length + OLA_ASSERT_FALSE(block.Pack(data, NULL)); + + // test undersized buffer + bytes_used = block_size - 1; + OLA_ASSERT_FALSE(block.Pack(data, &bytes_used)); + // TODO(Peter): Work out what behaviour we want for the bytes used, it's + // currently the actual total used, not zero like the PDU::Pack returns + // OLA_ASSERT_EQ(0u, bytes_used); + + // test oversized buffer + bytes_used = block_size + 1; + OLA_ASSERT(block.Pack(data, &bytes_used)); + OLA_ASSERT_EQ(block_size, bytes_used); delete[] data; block.Clear();