arpa_inet_test.cpp revision 5c8c88dd8d0a371d30096aa107297ebc23e96a45
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 */ 16 17#include <gtest/gtest.h> 18 19#include <arpa/inet.h> 20 21TEST(arpa_inet, inet_addr) { 22 ASSERT_EQ((htonl)(0x7f000001), inet_addr("127.0.0.1")); 23} 24 25TEST(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} 30 31TEST(arpa_inet, inet_lnaof) { 32 in_addr a = { htonl(0x12345678) }; 33 ASSERT_EQ(0x00345678U, inet_lnaof(a)); 34} 35 36TEST(arpa_inet, inet_makeaddr) { 37 in_addr a = inet_makeaddr(0x12U, 0x345678); 38 ASSERT_EQ((htonl)(0x12345678), a.s_addr); 39} 40 41TEST(arpa_inet, inet_netof) { 42 in_addr a = { htonl(0x12345678) }; 43 ASSERT_EQ(0x12U, inet_netof(a)); 44} 45 46TEST(arpa_inet, inet_network) { 47 ASSERT_EQ(0x7f000001U, inet_network("127.0.0.1")); 48} 49 50TEST(arpa_inet, inet_ntoa) { 51 in_addr a = { (htonl)(0x7f000001) }; 52 ASSERT_STREQ("127.0.0.1", inet_ntoa(a)); 53} 54 55TEST(arpa_inet, inet_pton__inet_ntop) { 56 sockaddr_storage ss; 57 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss)); 58 59 char s[INET_ADDRSTRLEN]; 60 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss, s, INET_ADDRSTRLEN)); 61} 62 63TEST(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. 67 68 sockaddr_storage ss4; 69 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss4)); 70 71 sockaddr_storage ss6; 72 ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &ss6)); 73 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} 82