blob: 0cebe4e8e9a8d16b163b01843b049a98faf6cbb8 [file] [log] [blame]
Elliott Hughesd3b9d112013-02-13 08:22:07 -08001/*
2 * Copyright (C) 2013 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 <sys/types.h>
20#include <sys/socket.h>
21#include <netdb.h>
Elliott Hughesd8213bb2013-02-13 09:49:33 -080022#include <netinet/in.h>
Elliott Hughesd3b9d112013-02-13 08:22:07 -080023
Elliott Hughes32fea142014-11-16 10:14:54 -080024TEST(netdb, getaddrinfo_NULL_host) {
25 // It's okay for the host argument to be NULL, as long as service isn't.
26 addrinfo* ai = NULL;
27 ASSERT_EQ(0, getaddrinfo(NULL, "smtp", NULL, &ai));
28 // (sockaddr_in::sin_port and sockaddr_in6::sin6_port overlap.)
29 ASSERT_EQ(25U, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
30 freeaddrinfo(ai);
31}
32
33TEST(netdb, getaddrinfo_NULL_service) {
34 // It's okay for the service argument to be NULL, as long as host isn't.
35 addrinfo* ai = NULL;
36 ASSERT_EQ(0, getaddrinfo("localhost", NULL, NULL, &ai));
37 ASSERT_TRUE(ai != NULL);
38 freeaddrinfo(ai);
39}
40
Elliott Hughesd3b9d112013-02-13 08:22:07 -080041TEST(netdb, getaddrinfo_NULL_hints) {
42 addrinfo* ai = NULL;
43 ASSERT_EQ(0, getaddrinfo("localhost", "9999", NULL, &ai));
Derek Xueba811122014-08-13 14:19:17 +010044
45 bool saw_tcp = false;
46 bool saw_udp = false;
47 for (addrinfo* p = ai; p != NULL; p = p->ai_next) {
48 ASSERT_TRUE(p->ai_family == AF_INET || p->ai_family == AF_INET6);
49 if (p->ai_socktype == SOCK_STREAM) {
50 ASSERT_EQ(IPPROTO_TCP, p->ai_protocol);
51 saw_tcp = true;
52 } else if (p->ai_socktype == SOCK_DGRAM) {
53 ASSERT_EQ(IPPROTO_UDP, p->ai_protocol);
54 saw_udp = true;
55 }
56 }
57 ASSERT_TRUE(saw_tcp);
58 ASSERT_TRUE(saw_udp);
59
60 freeaddrinfo(ai);
61}
62
63TEST(netdb, getaddrinfo_service_lookup) {
64 addrinfo* ai = NULL;
65 ASSERT_EQ(0, getaddrinfo("localhost", "smtp", NULL, &ai));
66 ASSERT_EQ(SOCK_STREAM, ai->ai_socktype);
67 ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol);
68 ASSERT_EQ(25, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
69 freeaddrinfo(ai);
70}
71
72TEST(netdb, getaddrinfo_hints) {
73 addrinfo hints;
74 memset(&hints, 0, sizeof(hints));
75 hints.ai_family = AF_UNSPEC;
76 hints.ai_socktype = SOCK_STREAM;
77 hints.ai_protocol = IPPROTO_TCP;
78
79 addrinfo* ai = NULL;
80 ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai));
81 ASSERT_EQ(AF_INET, ai->ai_family);
82 ASSERT_EQ(SOCK_STREAM, ai->ai_socktype);
83 ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol);
84 ASSERT_TRUE(ai->ai_next == NULL);
Elliott Hughesd3b9d112013-02-13 08:22:07 -080085 freeaddrinfo(ai);
86}
Elliott Hughesd8213bb2013-02-13 09:49:33 -080087
88TEST(netdb, getnameinfo_salen) {
89 sockaddr_storage ss;
90 memset(&ss, 0, sizeof(ss));
91 sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
92 char tmp[16];
93
94 ss.ss_family = AF_INET;
95 socklen_t too_much = sizeof(ss);
96 socklen_t just_right = sizeof(sockaddr_in);
97 socklen_t too_little = sizeof(sockaddr_in) - 1;
98
99 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
100 ASSERT_STREQ("0.0.0.0", tmp);
101 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
102 ASSERT_STREQ("0.0.0.0", tmp);
103 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
104
105 ss.ss_family = AF_INET6;
106 just_right = sizeof(sockaddr_in6);
107 too_little = sizeof(sockaddr_in6) - 1;
108 too_much = just_right + 1;
109
110 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
111 ASSERT_STREQ("::", tmp);
112 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
113 ASSERT_STREQ("::", tmp);
114 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
115}
Derek Xue4912fc72014-08-13 14:19:17 +0100116
117TEST(netdb, gethostbyname) {
118 hostent* hent = gethostbyname("localhost");
119 ASSERT_TRUE(hent != NULL);
120 ASSERT_EQ(hent->h_addrtype, AF_INET);
121 ASSERT_EQ(hent->h_addr[0], 127);
122 ASSERT_EQ(hent->h_addr[1], 0);
123 ASSERT_EQ(hent->h_addr[2], 0);
124 ASSERT_EQ(hent->h_addr[3], 1);
125}
Derek Xueba811122014-08-13 14:19:17 +0100126
127TEST(netdb, getservbyname) {
128 // smtp is TCP-only, so we know we'll get 25/tcp back.
129 servent* s = getservbyname("smtp", NULL);
130 ASSERT_TRUE(s != NULL);
131 ASSERT_EQ(25, ntohs(s->s_port));
132 ASSERT_STREQ("tcp", s->s_proto);
133
134 // We get the same result by explicitly asking for tcp.
135 s = getservbyname("smtp", "tcp");
136 ASSERT_TRUE(s != NULL);
137 ASSERT_EQ(25, ntohs(s->s_port));
138 ASSERT_STREQ("tcp", s->s_proto);
139
140 // And we get a failure if we explicitly ask for udp.
141 s = getservbyname("smtp", "udp");
142 ASSERT_TRUE(s == NULL);
143
144 // But there are actually udp services.
145 s = getservbyname("echo", "udp");
146 ASSERT_TRUE(s != NULL);
147 ASSERT_EQ(7, ntohs(s->s_port));
148 ASSERT_STREQ("udp", s->s_proto);
149}