blob: 2606082101655467e3a206c9f91c54bb919a633d [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>
20
21#include <gtest/gtest.h>
22
Elliott Hughesba267f42017-02-24 16:19:53 -080023#include <android-base/macros.h>
24
25static constexpr uint16_t le16 = 0x1234;
26static constexpr uint32_t le32 = 0x12345678;
27static constexpr uint64_t le64 = 0x123456789abcdef0;
28
29static constexpr uint16_t be16 = 0x3412;
30static constexpr uint32_t be32 = 0x78563412;
31static constexpr uint64_t be64 = 0xf0debc9a78563412;
32
Elliott Hughes0c485da2016-02-03 14:13:52 -080033TEST(netinet_in, bindresvport) {
34 // This isn't something we can usually test, so just check the symbol's there.
35 ASSERT_EQ(-1, bindresvport(-1, nullptr));
36}
37
38TEST(netinet_in, in6addr_any) {
39 in6_addr any = IN6ADDR_ANY_INIT;
40 ASSERT_EQ(0, memcmp(&any, &in6addr_any, sizeof(in6addr_any)));
41}
42
43TEST(netinet_in, in6addr_loopback) {
44 in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
45 ASSERT_EQ(0, memcmp(&loopback, &in6addr_loopback, sizeof(in6addr_loopback)));
46}
Elliott Hughesba267f42017-02-24 16:19:53 -080047
48TEST(netinet_in, htons_function) {
49 ASSERT_EQ(be16, (htons)(le16));
50}
51
52TEST(netinet_in, htonl_function) {
53 ASSERT_EQ(be32, (htonl)(le32));
54}
55
56TEST(netinet_in, htonq_macro) {
57#if defined(__BIONIC__)
58 ASSERT_EQ(be64, htonq(le64));
59#else
60 UNUSED(be64);
61#endif
62}
63
64TEST(netinet_in, ntohs_function) {
65 ASSERT_EQ(le16, (ntohs)(be16));
66}
67
68TEST(netinet_in, ntohl_function) {
69 ASSERT_EQ(le32, (ntohl)(be32));
70}
71
72TEST(netinet_in, ntohq_macro) {
73#if defined(__BIONIC__)
74 ASSERT_EQ(le64, ntohq(be64));
75#else
76 UNUSED(le64);
77#endif
78}
Elliott Hughes64f355f2017-08-30 16:10:24 -070079
80TEST(netinet_in, ip_mreq_source_fields) {
81 // https://issuetracker.google.com/36987220
82 ip_mreq_source mreq;
83 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
84 mreq.imr_multiaddr.s_addr = htonl(INADDR_ANY);
85 mreq.imr_sourceaddr.s_addr = htonl(INADDR_ANY);
86}