aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorYabin Cui2014-12-17 19:06:10 -0600
committerGerrit Code Review2014-12-17 19:06:10 -0600
commit370a3e573d1cdff6070c77322d4a07bd53e5db3c (patch)
treef191d716bc9b48001cc1366e95689ce7011d6b19 /tests
parent92b9cb2c899c386954b8f9ad8111aa6c8c63e306 (diff)
parent58d33a51f336d6823ef1ec915949a5884699ff5f (diff)
downloadplatform-bionic-370a3e573d1cdff6070c77322d4a07bd53e5db3c.tar.gz
platform-bionic-370a3e573d1cdff6070c77322d4a07bd53e5db3c.tar.xz
platform-bionic-370a3e573d1cdff6070c77322d4a07bd53e5db3c.zip
Merge "Sync with upstream for gethnamaddr.c."
Diffstat (limited to 'tests')
-rw-r--r--tests/netdb_test.cpp126
1 files changed, 123 insertions, 3 deletions
diff --git a/tests/netdb_test.cpp b/tests/netdb_test.cpp
index 0cebe4e8..ab5b4875 100644
--- a/tests/netdb_test.cpp
+++ b/tests/netdb_test.cpp
@@ -14,11 +14,13 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include <netdb.h>
18
17#include <gtest/gtest.h> 19#include <gtest/gtest.h>
18 20
21#include <arpa/inet.h>
19#include <sys/types.h> 22#include <sys/types.h>
20#include <sys/socket.h> 23#include <sys/socket.h>
21#include <netdb.h>
22#include <netinet/in.h> 24#include <netinet/in.h>
23 25
24TEST(netdb, getaddrinfo_NULL_host) { 26TEST(netdb, getaddrinfo_NULL_host) {
@@ -114,8 +116,7 @@ TEST(netdb, getnameinfo_salen) {
114 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST)); 116 ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
115} 117}
116 118
117TEST(netdb, gethostbyname) { 119void VerifyLocalhost(hostent *hent) {
118 hostent* hent = gethostbyname("localhost");
119 ASSERT_TRUE(hent != NULL); 120 ASSERT_TRUE(hent != NULL);
120 ASSERT_EQ(hent->h_addrtype, AF_INET); 121 ASSERT_EQ(hent->h_addrtype, AF_INET);
121 ASSERT_EQ(hent->h_addr[0], 127); 122 ASSERT_EQ(hent->h_addr[0], 127);
@@ -124,6 +125,125 @@ TEST(netdb, gethostbyname) {
124 ASSERT_EQ(hent->h_addr[3], 1); 125 ASSERT_EQ(hent->h_addr[3], 1);
125} 126}
126 127
128TEST(netdb, gethostbyname) {
129 hostent* hp = gethostbyname("localhost");
130 VerifyLocalhost(hp);
131}
132
133TEST(netdb, gethostbyname2) {
134 hostent* hp = gethostbyname2("localhost", AF_INET);
135 VerifyLocalhost(hp);
136}
137
138TEST(netdb, gethostbyname_r) {
139 hostent hent;
140 hostent *hp;
141 char buf[512];
142 int err;
143 int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
144 ASSERT_EQ(0, result);
145 VerifyLocalhost(hp);
146
147 // Change hp->h_addr to test reentrancy.
148 hp->h_addr[0] = 0;
149
150 hostent hent2;
151 hostent *hp2;
152 char buf2[512];
153 result = gethostbyname_r("localhost", &hent2, buf2, sizeof(buf2), &hp2, &err);
154 ASSERT_EQ(0, result);
155 VerifyLocalhost(hp2);
156
157 ASSERT_EQ(0, hp->h_addr[0]);
158}
159
160TEST(netdb, gethostbyname2_r) {
161 hostent hent;
162 hostent *hp;
163 char buf[512];
164 int err;
165 int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
166 ASSERT_EQ(0, result);
167 VerifyLocalhost(hp);
168
169 // Change hp->h_addr to test reentrancy.
170 hp->h_addr[0] = 0;
171
172 hostent hent2;
173 hostent *hp2;
174 char buf2[512];
175 result = gethostbyname2_r("localhost", AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
176 ASSERT_EQ(0, result);
177 VerifyLocalhost(hp2);
178
179 ASSERT_EQ(0, hp->h_addr[0]);
180}
181
182TEST(netdb, gethostbyaddr) {
183 char addr[4];
184 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr));
185 hostent *hp = gethostbyaddr(addr, sizeof(addr), AF_INET);
186 VerifyLocalhost(hp);
187}
188
189TEST(netdb, gethostbyaddr_r) {
190 char addr[4];
191 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr));
192
193 hostent hent;
194 hostent *hp;
195 char buf[512];
196 int err;
197 int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
198 ASSERT_EQ(0, result);
199 VerifyLocalhost(hp);
200
201 // Change hp->h_addr to test reentrancy.
202 hp->h_addr[0] = 0;
203
204 hostent hent2;
205 hostent *hp2;
206 char buf2[512];
207 result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
208 ASSERT_EQ(0, result);
209 VerifyLocalhost(hp2);
210
211 ASSERT_EQ(0, hp->h_addr[0]);
212}
213
214TEST(netdb, gethostbyname_r_ERANGE) {
215 hostent hent;
216 hostent *hp;
217 char buf[4]; // Use too small buffer.
218 int err;
219 int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
220 ASSERT_EQ(ERANGE, result);
221 ASSERT_EQ(NULL, hp);
222}
223
224TEST(netdb, gethostbyname2_r_ERANGE) {
225 hostent hent;
226 hostent *hp;
227 char buf[4]; // Use too small buffer.
228 int err;
229 int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
230 ASSERT_EQ(ERANGE, result);
231 ASSERT_EQ(NULL, hp);
232}
233
234TEST(netdb, gethostbyaddr_r_ERANGE) {
235 char addr[4];
236 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr));
237
238 hostent hent;
239 hostent *hp;
240 char buf[4]; // Use too small buffer.
241 int err;
242 int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
243 ASSERT_EQ(ERANGE, result);
244 ASSERT_EQ(NULL, hp);
245}
246
127TEST(netdb, getservbyname) { 247TEST(netdb, getservbyname) {
128 // smtp is TCP-only, so we know we'll get 25/tcp back. 248 // smtp is TCP-only, so we know we'll get 25/tcp back.
129 servent* s = getservbyname("smtp", NULL); 249 servent* s = getservbyname("smtp", NULL);