|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation; either version 2 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU Library General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program; if not, write to the Free Software |
| 14 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 | + * |
| 16 | + * BrokerClientEntryPDUTest.cpp |
| 17 | + * Test fixture for the BrokerClientEntryPDU class |
| 18 | + * Copyright (C) 2023 Peter Newman |
| 19 | + */ |
| 20 | + |
| 21 | +#include <cppunit/extensions/HelperMacros.h> |
| 22 | + |
| 23 | +#include "ola/Logging.h" |
| 24 | +#include "ola/io/IOQueue.h" |
| 25 | +#include "ola/io/OutputStream.h" |
| 26 | +#include "ola/network/NetworkUtils.h" |
| 27 | +#include "ola/testing/TestUtils.h" |
| 28 | +#include "libs/acn/BrokerClientEntryPDU.h" |
| 29 | +#include "libs/acn/PDUTestCommon.h" |
| 30 | + |
| 31 | + |
| 32 | +namespace ola { |
| 33 | +namespace acn { |
| 34 | + |
| 35 | +using ola::io::IOQueue; |
| 36 | +using ola::io::OutputStream; |
| 37 | +using ola::network::HostToNetwork; |
| 38 | + |
| 39 | +class BrokerClientEntryPDUTest: public CppUnit::TestFixture { |
| 40 | + CPPUNIT_TEST_SUITE(BrokerClientEntryPDUTest); |
| 41 | + CPPUNIT_TEST(testSimpleBrokerClientEntryPDU); |
| 42 | + CPPUNIT_TEST(testSimpleBrokerClientEntryPDUToOutputStream); |
| 43 | + CPPUNIT_TEST_SUITE_END(); |
| 44 | + |
| 45 | + public: |
| 46 | + void testSimpleBrokerClientEntryPDU(); |
| 47 | + void testSimpleBrokerClientEntryPDUToOutputStream(); |
| 48 | + |
| 49 | + void setUp() { |
| 50 | + ola::InitLogging(ola::OLA_LOG_DEBUG, ola::OLA_LOG_STDERR); |
| 51 | + } |
| 52 | + |
| 53 | + private: |
| 54 | + static const unsigned int TEST_VECTOR; |
| 55 | + static const uint8_t TEST_DATA[]; |
| 56 | +}; |
| 57 | + |
| 58 | +const uint8_t BrokerClientEntryPDUTest::TEST_DATA[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
| 59 | + 12, 13, 14, 15}; |
| 60 | + |
| 61 | +CPPUNIT_TEST_SUITE_REGISTRATION(BrokerClientEntryPDUTest); |
| 62 | + |
| 63 | +const unsigned int BrokerClientEntryPDUTest::TEST_VECTOR = 39; |
| 64 | + |
| 65 | + |
| 66 | +/* |
| 67 | + * Test that packing a BrokerClientEntryPDU without data works. |
| 68 | + */ |
| 69 | +void BrokerClientEntryPDUTest::testSimpleBrokerClientEntryPDU() { |
| 70 | + const CID client_cid = CID::FromData(TEST_DATA); |
| 71 | + BrokerClientEntryHeader header(client_cid); |
| 72 | + BrokerClientEntryPDU pdu(TEST_VECTOR, header, NULL); |
| 73 | + |
| 74 | + OLA_ASSERT_EQ(16u, pdu.HeaderSize()); |
| 75 | + OLA_ASSERT_EQ(0u, pdu.DataSize()); |
| 76 | + OLA_ASSERT_EQ(23u, pdu.Size()); |
| 77 | + |
| 78 | + unsigned int size = pdu.Size(); |
| 79 | + uint8_t *data = new uint8_t[size]; |
| 80 | + unsigned int bytes_used = size; |
| 81 | + OLA_ASSERT(pdu.Pack(data, &bytes_used)); |
| 82 | + OLA_ASSERT_EQ(size, bytes_used); |
| 83 | + |
| 84 | + // spot check the data |
| 85 | + OLA_ASSERT_EQ((uint8_t) 0xf0, data[0]); |
| 86 | + // bytes_used is technically data[1] and data[2] if > 255 |
| 87 | + OLA_ASSERT_EQ((uint8_t) bytes_used, data[2]); |
| 88 | + unsigned int actual_value; |
| 89 | + memcpy(&actual_value, data + 3, sizeof(actual_value)); |
| 90 | + OLA_ASSERT_EQ(HostToNetwork(TEST_VECTOR), actual_value); |
| 91 | + |
| 92 | + uint8_t buffer[CID::CID_LENGTH]; |
| 93 | + client_cid.Pack(buffer); |
| 94 | + OLA_ASSERT_DATA_EQUALS(&data[7], CID::CID_LENGTH, buffer, sizeof(buffer)); |
| 95 | + |
| 96 | + // test undersized buffer |
| 97 | + bytes_used = size - 1; |
| 98 | + OLA_ASSERT_FALSE(pdu.Pack(data, &bytes_used)); |
| 99 | + OLA_ASSERT_EQ(0u, bytes_used); |
| 100 | + |
| 101 | + // test oversized buffer |
| 102 | + bytes_used = size + 1; |
| 103 | + OLA_ASSERT(pdu.Pack(data, &bytes_used)); |
| 104 | + OLA_ASSERT_EQ(size, bytes_used); |
| 105 | + delete[] data; |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +/* |
| 110 | + * Test that writing to an output stream works. |
| 111 | + */ |
| 112 | +void BrokerClientEntryPDUTest::testSimpleBrokerClientEntryPDUToOutputStream() { |
| 113 | + const ola::acn::CID client_cid = CID::FromData(TEST_DATA); |
| 114 | + BrokerClientEntryHeader header(client_cid); |
| 115 | + BrokerClientEntryPDU pdu(TEST_VECTOR, header, NULL); |
| 116 | + |
| 117 | + OLA_ASSERT_EQ(16u, pdu.HeaderSize()); |
| 118 | + OLA_ASSERT_EQ(0u, pdu.DataSize()); |
| 119 | + OLA_ASSERT_EQ(23u, pdu.Size()); |
| 120 | + |
| 121 | + IOQueue output; |
| 122 | + OutputStream stream(&output); |
| 123 | + pdu.Write(&stream); |
| 124 | + OLA_ASSERT_EQ(23u, output.Size()); |
| 125 | + |
| 126 | + uint8_t *pdu_data = new uint8_t[output.Size()]; |
| 127 | + unsigned int pdu_size = output.Peek(pdu_data, output.Size()); |
| 128 | + OLA_ASSERT_EQ(output.Size(), pdu_size); |
| 129 | + |
| 130 | + uint8_t EXPECTED[] = { |
| 131 | + 0xf0, 0x00, 0x17, |
| 132 | + 0, 0, 0, 39, |
| 133 | + 0, 1, 2, 3, 4, 5, 6, 7, |
| 134 | + 8, 9, 10, 11, 12, 13, 14, 15 |
| 135 | + }; |
| 136 | + OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), pdu_data, pdu_size); |
| 137 | + output.Pop(output.Size()); |
| 138 | + delete[] pdu_data; |
| 139 | +} |
| 140 | +} // namespace acn |
| 141 | +} // namespace ola |
0 commit comments