blob: 48f438c5325bf502fefaba1ea5c09c4e1de4b2f5 [file] [log] [blame]
Elliott Hughes0c485da2016-02-03 14:13:52 -08001/*
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>
Colin Cross4c5595c2021-08-16 15:51:59 -070020#include <sys/cdefs.h>
Elliott Hughes0c485da2016-02-03 14:13:52 -080021
22#include <gtest/gtest.h>
23
Elliott Hughesba267f42017-02-24 16:19:53 -080024#include <android-base/macros.h>
25
Elliott Hughes95646e62023-09-21 14:11:19 -070026#include "utils.h"
27
Elliott Hughesba267f42017-02-24 16:19:53 -080028static constexpr uint16_t le16 = 0x1234;
29static constexpr uint32_t le32 = 0x12345678;
30static constexpr uint64_t le64 = 0x123456789abcdef0;
31
32static constexpr uint16_t be16 = 0x3412;
33static constexpr uint32_t be32 = 0x78563412;
34static constexpr uint64_t be64 = 0xf0debc9a78563412;
35
Elliott Hughes0c485da2016-02-03 14:13:52 -080036TEST(netinet_in, bindresvport) {
Colin Cross4c5595c2021-08-16 15:51:59 -070037#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes7cebf832020-08-12 14:25:41 -070038 // This isn't something we can usually test (because you need to be root),
39 // so just check the symbol's there.
Elliott Hughes0c485da2016-02-03 14:13:52 -080040 ASSERT_EQ(-1, bindresvport(-1, nullptr));
Elliott Hughes7cebf832020-08-12 14:25:41 -070041
42 // Only AF_INET is supported.
43 sockaddr_in sin = {.sin_family = AF_INET6};
44 errno = 0;
45 ASSERT_EQ(-1, bindresvport(-1, &sin));
Elliott Hughes95646e62023-09-21 14:11:19 -070046 ASSERT_ERRNO(EPFNOSUPPORT);
Colin Cross7da20342021-07-28 11:18:11 -070047#else
48 GTEST_SKIP() << "musl doesn't support bindresvport";
49#endif
Elliott Hughes0c485da2016-02-03 14:13:52 -080050}
51
52TEST(netinet_in, in6addr_any) {
53 in6_addr any = IN6ADDR_ANY_INIT;
54 ASSERT_EQ(0, memcmp(&any, &in6addr_any, sizeof(in6addr_any)));
55}
56
57TEST(netinet_in, in6addr_loopback) {
58 in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
59 ASSERT_EQ(0, memcmp(&loopback, &in6addr_loopback, sizeof(in6addr_loopback)));
60}
Elliott Hughesba267f42017-02-24 16:19:53 -080061
62TEST(netinet_in, htons_function) {
63 ASSERT_EQ(be16, (htons)(le16));
64}
65
66TEST(netinet_in, htonl_function) {
67 ASSERT_EQ(be32, (htonl)(le32));
68}
69
70TEST(netinet_in, htonq_macro) {
71#if defined(__BIONIC__)
72 ASSERT_EQ(be64, htonq(le64));
73#else
74 UNUSED(be64);
75#endif
76}
77
78TEST(netinet_in, ntohs_function) {
79 ASSERT_EQ(le16, (ntohs)(be16));
80}
81
82TEST(netinet_in, ntohl_function) {
83 ASSERT_EQ(le32, (ntohl)(be32));
84}
85
86TEST(netinet_in, ntohq_macro) {
87#if defined(__BIONIC__)
88 ASSERT_EQ(le64, ntohq(be64));
89#else
90 UNUSED(le64);
91#endif
92}
Elliott Hughes64f355f2017-08-30 16:10:24 -070093
94TEST(netinet_in, ip_mreq_source_fields) {
95 // https://issuetracker.google.com/36987220
96 ip_mreq_source mreq;
97 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
98 mreq.imr_multiaddr.s_addr = htonl(INADDR_ANY);
99 mreq.imr_sourceaddr.s_addr = htonl(INADDR_ANY);
100}