blob: ef2c8dac2a62c859dd948a12925e0d98204a75fa [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
24TEST(netdb, getaddrinfo_NULL_hints) {
25 addrinfo* ai = NULL;
26 ASSERT_EQ(0, getaddrinfo("localhost", "9999", NULL, &ai));
Derek Xueba811122014-08-13 14:19:17 +010027
28 bool saw_tcp = false;
29 bool saw_udp = false;
30 for (addrinfo* p = ai; p != NULL; p = p->ai_next) {
31 ASSERT_TRUE(p->ai_family == AF_INET || p->ai_family == AF_INET6);
32 if (p->ai_socktype == SOCK_STREAM) {
33 ASSERT_EQ(IPPROTO_TCP, p->ai_protocol);
34 saw_tcp = true;
35 } else if (p->ai_socktype == SOCK_DGRAM) {
36 ASSERT_EQ(IPPROTO_UDP, p->ai_protocol);
37 saw_udp = true;
38 }
39 }
40 ASSERT_TRUE(saw_tcp);
41 ASSERT_TRUE(saw_udp);
42
43 freeaddrinfo(ai);
44}
45
46TEST(netdb, getaddrinfo_service_lookup) {
47 addrinfo* ai = NULL;
48 ASSERT_EQ(0, getaddrinfo("localhost", "smtp", NULL, &ai));
49 ASSERT_EQ(SOCK_STREAM, ai->ai_socktype);
50 ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol);
51 ASSERT_EQ(25, ntohs(reinterpret_cast<sockaddr_in*>(ai->ai_addr)->sin_port));
52 freeaddrinfo(ai);
53}
54
55TEST(netdb, getaddrinfo_hints) {
56 addrinfo hints;
57 memset(&hints, 0, sizeof(hints));
58 hints.ai_family = AF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
60 hints.ai_protocol = IPPROTO_TCP;
61
62 addrinfo* ai = NULL;
63 ASSERT_EQ(0, getaddrinfo( "localhost", "9999", &hints, &ai));
64 ASSERT_EQ(AF_INET, ai->ai_family);
65 ASSERT_EQ(SOCK_STREAM, ai->ai_socktype);
66 ASSERT_EQ(IPPROTO_TCP, ai->ai_protocol);
67 ASSERT_TRUE(ai->ai_next == NULL);
Elliott Hughesd3b9d112013-02-13 08:22:07 -080068 freeaddrinfo(ai);
69}
Elliott Hughesd8213bb2013-02-13 09:49:33 -080070
71TEST(netdb, getnameinfo_salen) {
72 sockaddr_storage ss;
73 memset(&ss, 0, sizeof(ss));
74 sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
75 char tmp[16];
76
77 ss.ss_family = AF_INET;
78 socklen_t too_much = sizeof(ss);
79 socklen_t just_right = sizeof(sockaddr_in);
80 socklen_t too_little = sizeof(sockaddr_in) - 1;
81
82 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
83 ASSERT_STREQ("0.0.0.0", tmp);
84 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
85 ASSERT_STREQ("0.0.0.0", tmp);
86 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
87
88 ss.ss_family = AF_INET6;
89 just_right = sizeof(sockaddr_in6);
90 too_little = sizeof(sockaddr_in6) - 1;
91 too_much = just_right + 1;
92
93 ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
94 ASSERT_STREQ("::", tmp);
95 ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
96 ASSERT_STREQ("::", tmp);
97 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
98}
Derek Xue4912fc72014-08-13 14:19:17 +010099
100TEST(netdb, gethostbyname) {
101 hostent* hent = gethostbyname("localhost");
102 ASSERT_TRUE(hent != NULL);
103 ASSERT_EQ(hent->h_addrtype, AF_INET);
104 ASSERT_EQ(hent->h_addr[0], 127);
105 ASSERT_EQ(hent->h_addr[1], 0);
106 ASSERT_EQ(hent->h_addr[2], 0);
107 ASSERT_EQ(hent->h_addr[3], 1);
108}
Derek Xueba811122014-08-13 14:19:17 +0100109
110TEST(netdb, getservbyname) {
111 // smtp is TCP-only, so we know we'll get 25/tcp back.
112 servent* s = getservbyname("smtp", NULL);
113 ASSERT_TRUE(s != NULL);
114 ASSERT_EQ(25, ntohs(s->s_port));
115 ASSERT_STREQ("tcp", s->s_proto);
116
117 // We get the same result by explicitly asking for tcp.
118 s = getservbyname("smtp", "tcp");
119 ASSERT_TRUE(s != NULL);
120 ASSERT_EQ(25, ntohs(s->s_port));
121 ASSERT_STREQ("tcp", s->s_proto);
122
123 // And we get a failure if we explicitly ask for udp.
124 s = getservbyname("smtp", "udp");
125 ASSERT_TRUE(s == NULL);
126
127 // But there are actually udp services.
128 s = getservbyname("echo", "udp");
129 ASSERT_TRUE(s != NULL);
130 ASSERT_EQ(7, ntohs(s->s_port));
131 ASSERT_STREQ("udp", s->s_proto);
132}