]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/arpa_inet_test.cpp
Merge "Sync with upstream for gethnamaddr.c."
[android-sdk/platform-bionic.git] / tests / arpa_inet_test.cpp
1 /*
2  * Copyright (C) 2014 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  */
17 #include <gtest/gtest.h>
19 #include <arpa/inet.h>
21 TEST(arpa_inet, inet_addr) {
22   ASSERT_EQ((htonl)(0x7f000001), inet_addr("127.0.0.1"));
23 }
25 TEST(arpa_inet, inet_aton) {
26   in_addr a;
27   ASSERT_EQ(1, inet_aton("127.0.0.1", &a));
28   ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
29 }
31 TEST(arpa_inet, inet_lnaof) {
32   in_addr a = { htonl(0x12345678) };
33   ASSERT_EQ(0x00345678U, inet_lnaof(a));
34 }
36 TEST(arpa_inet, inet_makeaddr) {
37   in_addr a = inet_makeaddr(0x12U, 0x345678);
38   ASSERT_EQ((htonl)(0x12345678), a.s_addr);
39 }
41 TEST(arpa_inet, inet_netof) {
42   in_addr a = { htonl(0x12345678) };
43   ASSERT_EQ(0x12U, inet_netof(a));
44 }
46 TEST(arpa_inet, inet_network) {
47   ASSERT_EQ(0x7f000001U, inet_network("127.0.0.1"));
48 }
50 TEST(arpa_inet, inet_ntoa) {
51   in_addr a = { (htonl)(0x7f000001) };
52   ASSERT_STREQ("127.0.0.1", inet_ntoa(a));
53 }
55 TEST(arpa_inet, inet_pton__inet_ntop) {
56   sockaddr_storage ss;
57   ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss));
59   char s[INET_ADDRSTRLEN];
60   ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss, s, INET_ADDRSTRLEN));
61 }
63 TEST(arpa_inet, inet_ntop_overflow) {
64   // OpenBSD's inet_ntop had a bug where passing a 'size' larger than INET_ADDRSTRLEN
65   // for AF_INET or INET6_ADDRSTRLEN for AF_INET6 would cause inet_ntop to overflow an
66   // internal buffer.
68   sockaddr_storage ss4;
69   ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss4));
71   sockaddr_storage ss6;
72   ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &ss6));
74   char s4[INET_ADDRSTRLEN];
75   char s6[INET6_ADDRSTRLEN];
76   ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, INET_ADDRSTRLEN));
77   ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, 2*INET_ADDRSTRLEN));
78   ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET_ADDRSTRLEN));
79   ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET6_ADDRSTRLEN));
80   ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, 2*INET6_ADDRSTRLEN));
81 }