|
| 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 | + * BrokerFetchClientListPDUTest.cpp |
| 17 | + * Test fixture for the BrokerFetchClientListPDU 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/IOStack.h" |
| 26 | +#include "ola/io/OutputStream.h" |
| 27 | +#include "ola/network/NetworkUtils.h" |
| 28 | +#include "ola/testing/TestUtils.h" |
| 29 | +#include "libs/acn/PDUTestCommon.h" |
| 30 | +#include "libs/acn/BrokerFetchClientListPDU.h" |
| 31 | + |
| 32 | +namespace ola { |
| 33 | +namespace acn { |
| 34 | + |
| 35 | +using ola::acn::BrokerFetchClientListPDU; |
| 36 | +using ola::io::IOQueue; |
| 37 | +using ola::io::IOStack; |
| 38 | +using ola::io::OutputStream; |
| 39 | +using ola::network::HostToNetwork; |
| 40 | + |
| 41 | +class BrokerFetchClientListPDUTest: public CppUnit::TestFixture { |
| 42 | + CPPUNIT_TEST_SUITE(BrokerFetchClientListPDUTest); |
| 43 | + CPPUNIT_TEST(testSimpleBrokerFetchClientListPDU); |
| 44 | + CPPUNIT_TEST(testSimpleBrokerFetchClientListPDUToOutputStream); |
| 45 | + CPPUNIT_TEST(testPrepend); |
| 46 | + CPPUNIT_TEST_SUITE_END(); |
| 47 | + |
| 48 | + public: |
| 49 | + void testSimpleBrokerFetchClientListPDU(); |
| 50 | + void testSimpleBrokerFetchClientListPDUToOutputStream(); |
| 51 | + void testPrepend(); |
| 52 | + |
| 53 | + void setUp() { |
| 54 | + ola::InitLogging(ola::OLA_LOG_DEBUG, ola::OLA_LOG_STDERR); |
| 55 | + } |
| 56 | + |
| 57 | + private: |
| 58 | + static const uint16_t TEST_VECTOR; |
| 59 | +}; |
| 60 | + |
| 61 | +CPPUNIT_TEST_SUITE_REGISTRATION(BrokerFetchClientListPDUTest); |
| 62 | + |
| 63 | +const uint16_t BrokerFetchClientListPDUTest::TEST_VECTOR = 39; |
| 64 | + |
| 65 | + |
| 66 | +/* |
| 67 | + * Test that packing a BrokerFetchClientListPDU works. |
| 68 | + */ |
| 69 | +void BrokerFetchClientListPDUTest::testSimpleBrokerFetchClientListPDU() { |
| 70 | + BrokerFetchClientListPDU pdu(TEST_VECTOR); |
| 71 | + |
| 72 | + OLA_ASSERT_EQ(0u, pdu.HeaderSize()); |
| 73 | + OLA_ASSERT_EQ(0u, pdu.DataSize()); |
| 74 | + OLA_ASSERT_EQ(5u, pdu.Size()); |
| 75 | + |
| 76 | + unsigned int size = pdu.Size(); |
| 77 | + uint8_t *data = new uint8_t[size]; |
| 78 | + unsigned int bytes_used = size; |
| 79 | + OLA_ASSERT(pdu.Pack(data, &bytes_used)); |
| 80 | + OLA_ASSERT_EQ(size, bytes_used); |
| 81 | + |
| 82 | + // spot check the data |
| 83 | + OLA_ASSERT_EQ((uint8_t) 0xf0, data[0]); |
| 84 | + // bytes_used is technically data[1] and data[2] if > 255 |
| 85 | + OLA_ASSERT_EQ((uint8_t) bytes_used, data[2]); |
| 86 | + uint16_t actual_value; |
| 87 | + memcpy(&actual_value, data + 3, sizeof(actual_value)); |
| 88 | + OLA_ASSERT_EQ(HostToNetwork(TEST_VECTOR), actual_value); |
| 89 | + |
| 90 | + // test undersized buffer |
| 91 | + bytes_used = size - 1; |
| 92 | + OLA_ASSERT_FALSE(pdu.Pack(data, &bytes_used)); |
| 93 | + OLA_ASSERT_EQ(0u, bytes_used); |
| 94 | + |
| 95 | + // test oversized buffer |
| 96 | + bytes_used = size + 1; |
| 97 | + OLA_ASSERT(pdu.Pack(data, &bytes_used)); |
| 98 | + OLA_ASSERT_EQ(size, bytes_used); |
| 99 | + delete[] data; |
| 100 | +} |
| 101 | + |
| 102 | +/* |
| 103 | + * Test that writing to an output stream works. |
| 104 | + */ |
| 105 | +void BrokerFetchClientListPDUTest:: |
| 106 | + testSimpleBrokerFetchClientListPDUToOutputStream() { |
| 107 | + BrokerFetchClientListPDU pdu(TEST_VECTOR); |
| 108 | + |
| 109 | + OLA_ASSERT_EQ(0u, pdu.HeaderSize()); |
| 110 | + OLA_ASSERT_EQ(0u, pdu.DataSize()); |
| 111 | + OLA_ASSERT_EQ(5u, pdu.Size()); |
| 112 | + |
| 113 | + IOQueue output; |
| 114 | + OutputStream stream(&output); |
| 115 | + pdu.Write(&stream); |
| 116 | + OLA_ASSERT_EQ(5u, output.Size()); |
| 117 | + |
| 118 | + uint8_t *pdu_data = new uint8_t[output.Size()]; |
| 119 | + unsigned int pdu_size = output.Peek(pdu_data, output.Size()); |
| 120 | + OLA_ASSERT_EQ(output.Size(), pdu_size); |
| 121 | + |
| 122 | + uint8_t EXPECTED[] = { |
| 123 | + 0xf0, 0x00, 0x05, |
| 124 | + 0, 39 |
| 125 | + }; |
| 126 | + OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), pdu_data, pdu_size); |
| 127 | + output.Pop(output.Size()); |
| 128 | + delete[] pdu_data; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +void BrokerFetchClientListPDUTest::testPrepend() { |
| 133 | + IOStack stack; |
| 134 | + BrokerFetchClientListPDU::PrependPDU(&stack); |
| 135 | + |
| 136 | + unsigned int length = stack.Size(); |
| 137 | + uint8_t *buffer = new uint8_t[length]; |
| 138 | + OLA_ASSERT(stack.Read(buffer, length)); |
| 139 | + |
| 140 | + const uint8_t expected_data[] = { |
| 141 | + 0xf0, 0x00, 0x05, |
| 142 | + 0, 0x06 |
| 143 | + }; |
| 144 | + OLA_ASSERT_DATA_EQUALS(expected_data, sizeof(expected_data), buffer, length); |
| 145 | + delete[] buffer; |
| 146 | +} |
| 147 | +} // namespace acn |
| 148 | +} // namespace ola |
0 commit comments