|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020 Project CHIP Authors |
| 4 | + * Copyright (c) 2016-2017 Nest Labs, Inc. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef __STDC_FORMAT_MACROS |
| 21 | +#define __STDC_FORMAT_MACROS |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef __STDC_LIMIT_MACROS |
| 25 | +#define __STDC_LIMIT_MACROS |
| 26 | +#endif |
| 27 | + |
| 28 | +#include <inttypes.h> |
| 29 | +#include <stdint.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include <inet/IPAddress.h> |
| 33 | +#include <lib/core/DataModelTypes.h> |
| 34 | +#include <lib/core/PeerId.h> |
| 35 | +#include <lib/support/UnitTestRegistration.h> |
| 36 | +#include <transport/raw/PeerAddress.h> |
| 37 | + |
| 38 | +#include <nlunit-test.h> |
| 39 | + |
| 40 | +using namespace chip; |
| 41 | + |
| 42 | +/** |
| 43 | + * Test correct identification of IPv6 multicast addresses. |
| 44 | + */ |
| 45 | +void TestPeerAddressMulticast(nlTestSuite * inSuite, void * inContext) |
| 46 | +{ |
| 47 | + constexpr chip::FabricId fabric = 0xa1a2a4a8b1b2b4b8; |
| 48 | + constexpr chip::GroupId group = 0xe10f; |
| 49 | + chip::Transport::PeerAddress addr = chip::Transport::PeerAddress::Multicast(fabric, group); |
| 50 | + NL_TEST_ASSERT(inSuite, chip::Transport::Type::kUdp == addr.GetTransportType()); |
| 51 | + NL_TEST_ASSERT(inSuite, addr.IsMulticast()); |
| 52 | + |
| 53 | + const Inet::IPAddress & ip = addr.GetIPAddress(); |
| 54 | + NL_TEST_ASSERT(inSuite, ip.IsIPv6Multicast()); |
| 55 | + NL_TEST_ASSERT(inSuite, chip::Inet::IPAddressType::kIPv6 == ip.Type()); |
| 56 | + |
| 57 | + constexpr uint8_t expected[NL_INET_IPV6_ADDR_LEN_IN_BYTES] = { 0xff, 0x35, 0x00, 0x40, 0xfd, 0xa1, 0xa2, 0xa4, |
| 58 | + 0xa8, 0xb1, 0xb2, 0xb4, 0xb8, 0x00, 0xe1, 0x0f }; |
| 59 | + uint8_t result[NL_INET_IPV6_ADDR_LEN_IN_BYTES]; |
| 60 | + uint8_t * p = result; |
| 61 | + ip.WriteAddress(p); |
| 62 | + NL_TEST_ASSERT(inSuite, !memcmp(expected, result, NL_INET_IPV6_ADDR_LEN_IN_BYTES)); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Test Suite. It lists all the test functions. |
| 67 | + */ |
| 68 | + |
| 69 | +// clang-format off |
| 70 | +static const nlTest sTests[] = |
| 71 | +{ |
| 72 | + NL_TEST_DEF("PeerAddress Multicast", TestPeerAddressMulticast), |
| 73 | + NL_TEST_SENTINEL() |
| 74 | +}; |
| 75 | +// clang-format on |
| 76 | + |
| 77 | +int TestPeerAddress(void) |
| 78 | +{ |
| 79 | + // clang-format off |
| 80 | + nlTestSuite theSuite = |
| 81 | + { |
| 82 | + "PeerAddress", |
| 83 | + &sTests[0], |
| 84 | + nullptr, |
| 85 | + nullptr |
| 86 | + }; |
| 87 | + // clang-format on |
| 88 | + |
| 89 | + // Run test suit againt one context. |
| 90 | + nlTestRunner(&theSuite, nullptr); |
| 91 | + |
| 92 | + return (nlTestRunnerStats(&theSuite)); |
| 93 | +} |
| 94 | + |
| 95 | +CHIP_REGISTER_TEST_SUITE(TestPeerAddress) |
0 commit comments