diff --git a/libs/acn/PDU.cpp b/libs/acn/PDU.cpp index 33e40f720..0fa3ef744 100644 --- a/libs/acn/PDU.cpp +++ b/libs/acn/PDU.cpp @@ -131,6 +131,11 @@ bool PDU::Pack(uint8_t *buffer, unsigned int *length) const { * Write this PDU to an OutputStream. */ void PDU::Write(OutputStream *stream) const { + if (!stream) { + OLA_WARN << "PDU::Write: missing stream"; + return; + } + unsigned int size = Size(); if (size <= TWOB_LENGTH_LIMIT && !m_force_length_flag) { @@ -173,6 +178,11 @@ void PDU::Write(OutputStream *stream) const { void PDU::PrependFlagsAndLength(ola::io::OutputBufferInterface *output, uint8_t flags, bool force_length_flag) { + if (!output) { + OLA_WARN << "PDU::PrependFlagsAndLength: missing output"; + return; + } + PrependFlagsAndLength(output, output->Size(), flags, force_length_flag); } @@ -184,6 +194,11 @@ void PDU::PrependFlagsAndLength(ola::io::OutputBufferInterface *output, unsigned int size, uint8_t flags, bool force_length_flag) { + if (!output) { + OLA_WARN << "PDU::PrependFlagsAndLength: missing output"; + return; + } + if (size + 2 <= TWOB_LENGTH_LIMIT && !force_length_flag) { size += 2; uint16_t flags_and_length = static_cast(size); diff --git a/libs/acn/PDU.h b/libs/acn/PDU.h index 531a8e96b..18193c204 100644 --- a/libs/acn/PDU.h +++ b/libs/acn/PDU.h @@ -192,6 +192,11 @@ bool PDUBlock::Pack(uint8_t *data, unsigned int *length) const { */ template void PDUBlock::Write(ola::io::OutputStream *stream) const { + if (!stream) { + OLA_WARN << "PDUBlock::Write: missing stream"; + return; + } + typename std::vector::const_iterator iter; for (iter = m_pdus.begin(); iter != m_pdus.end(); ++iter) { // TODO(simon): optimize repeated headers & vectors here diff --git a/libs/acn/PDUTest.cpp b/libs/acn/PDUTest.cpp index fd6e9de05..cdeec1832 100644 --- a/libs/acn/PDUTest.cpp +++ b/libs/acn/PDUTest.cpp @@ -37,12 +37,14 @@ class PDUTest: public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(PDUTest); CPPUNIT_TEST(testPDU); CPPUNIT_TEST(testPDUBlock); + CPPUNIT_TEST(testPDUToOutputStream); CPPUNIT_TEST(testBlockToOutputStream); CPPUNIT_TEST_SUITE_END(); public: void testPDU(); void testPDUBlock(); + void testPDUToOutputStream(); void testBlockToOutputStream(); void setUp() { @@ -136,6 +138,36 @@ void PDUTest::testPDUBlock() { } +/* + * Test that writing a PDU to an OutputStream works. + */ +void PDUTest::testPDUToOutputStream() { + MockPDU pdu(0x1234, 0x2468); + + IOQueue output; + OutputStream stream(&output); + pdu.Write(&stream); + OLA_ASSERT_EQ(14u, output.Size()); + + uint8_t *data = new uint8_t[output.Size()]; + unsigned int data_size = output.Peek(data, output.Size()); + OLA_ASSERT_EQ(output.Size(), data_size); + + uint8_t EXPECTED[] = { + 0x70, 0x0e, 0, 0, + 0, 0x2b, + 0x34, 0x12, 0, 0, + 0x68, 0x24, 0, 0 + }; + OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), data, data_size); + output.Pop(output.Size()); + + // test null stream + pdu.Write(NULL); + delete[] data; +} + + /* * Test that writing to an OutputStream works. */ @@ -164,6 +196,9 @@ void PDUTest::testBlockToOutputStream() { }; OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), block_data, block_size); output.Pop(output.Size()); + + // test null stream + block.Write(NULL); delete[] block_data; } } // namespace acn