Elliott Hughes | 0c485da | 2016-02-03 14:13:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <netinet/in.h> |
| 18 | |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Elliott Hughes | ba267f4 | 2017-02-24 16:19:53 -0800 | [diff] [blame] | 23 | #include <android-base/macros.h> |
| 24 | |
| 25 | static constexpr uint16_t le16 = 0x1234; |
| 26 | static constexpr uint32_t le32 = 0x12345678; |
| 27 | static constexpr uint64_t le64 = 0x123456789abcdef0; |
| 28 | |
| 29 | static constexpr uint16_t be16 = 0x3412; |
| 30 | static constexpr uint32_t be32 = 0x78563412; |
| 31 | static constexpr uint64_t be64 = 0xf0debc9a78563412; |
| 32 | |
Elliott Hughes | 0c485da | 2016-02-03 14:13:52 -0800 | [diff] [blame] | 33 | TEST(netinet_in, bindresvport) { |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 34 | // This isn't something we can usually test (because you need to be root), |
| 35 | // so just check the symbol's there. |
Elliott Hughes | 0c485da | 2016-02-03 14:13:52 -0800 | [diff] [blame] | 36 | ASSERT_EQ(-1, bindresvport(-1, nullptr)); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 37 | |
| 38 | // Only AF_INET is supported. |
| 39 | sockaddr_in sin = {.sin_family = AF_INET6}; |
| 40 | errno = 0; |
| 41 | ASSERT_EQ(-1, bindresvport(-1, &sin)); |
| 42 | ASSERT_EQ(EPFNOSUPPORT, errno); |
Elliott Hughes | 0c485da | 2016-02-03 14:13:52 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(netinet_in, in6addr_any) { |
| 46 | in6_addr any = IN6ADDR_ANY_INIT; |
| 47 | ASSERT_EQ(0, memcmp(&any, &in6addr_any, sizeof(in6addr_any))); |
| 48 | } |
| 49 | |
| 50 | TEST(netinet_in, in6addr_loopback) { |
| 51 | in6_addr loopback = IN6ADDR_LOOPBACK_INIT; |
| 52 | ASSERT_EQ(0, memcmp(&loopback, &in6addr_loopback, sizeof(in6addr_loopback))); |
| 53 | } |
Elliott Hughes | ba267f4 | 2017-02-24 16:19:53 -0800 | [diff] [blame] | 54 | |
| 55 | TEST(netinet_in, htons_function) { |
| 56 | ASSERT_EQ(be16, (htons)(le16)); |
| 57 | } |
| 58 | |
| 59 | TEST(netinet_in, htonl_function) { |
| 60 | ASSERT_EQ(be32, (htonl)(le32)); |
| 61 | } |
| 62 | |
| 63 | TEST(netinet_in, htonq_macro) { |
| 64 | #if defined(__BIONIC__) |
| 65 | ASSERT_EQ(be64, htonq(le64)); |
| 66 | #else |
| 67 | UNUSED(be64); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | TEST(netinet_in, ntohs_function) { |
| 72 | ASSERT_EQ(le16, (ntohs)(be16)); |
| 73 | } |
| 74 | |
| 75 | TEST(netinet_in, ntohl_function) { |
| 76 | ASSERT_EQ(le32, (ntohl)(be32)); |
| 77 | } |
| 78 | |
| 79 | TEST(netinet_in, ntohq_macro) { |
| 80 | #if defined(__BIONIC__) |
| 81 | ASSERT_EQ(le64, ntohq(be64)); |
| 82 | #else |
| 83 | UNUSED(le64); |
| 84 | #endif |
| 85 | } |
Elliott Hughes | 64f355f | 2017-08-30 16:10:24 -0700 | [diff] [blame] | 86 | |
| 87 | TEST(netinet_in, ip_mreq_source_fields) { |
| 88 | // https://issuetracker.google.com/36987220 |
| 89 | ip_mreq_source mreq; |
| 90 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 91 | mreq.imr_multiaddr.s_addr = htonl(INADDR_ANY); |
| 92 | mreq.imr_sourceaddr.s_addr = htonl(INADDR_ANY); |
| 93 | } |