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