1 /*
2 * Copyright 2014, Haiku, inc.
3 * Distributed under the terms of the MIT licence
4 */
5
6
7 #include "GetAddrInfo.h"
8
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netdb.h>
12
13 #include <cppunit/TestCaller.h>
14 #include <cppunit/TestSuite.h>
15
16
GetAddrInfoTest()17 GetAddrInfoTest::GetAddrInfoTest()
18 {
19 }
20
21
~GetAddrInfoTest()22 GetAddrInfoTest::~GetAddrInfoTest()
23 {
24 }
25
26
27 void
EmptyTest()28 GetAddrInfoTest::EmptyTest()
29 {
30 struct addrinfo* info;
31 addrinfo hint = {0};
32 hint.ai_family = AF_INET;
33 hint.ai_flags = AI_PASSIVE;
34
35 // Trying to resolve an empty host...
36 int result = getaddrinfo("", NULL, &hint, &info);
37
38 if (info)
39 freeaddrinfo(info);
40
41 // getaddrinfo shouldn't suggest that this may work better by trying again.
42 CPPUNIT_ASSERT(result != EAI_AGAIN);
43 }
44
45
46 void
AddrConfigTest()47 GetAddrInfoTest::AddrConfigTest()
48 {
49 struct addrinfo *info = NULL, hints = {0};
50 hints.ai_flags = AI_ADDRCONFIG;
51
52 // localhost is guaranteed to have an IPv6 address.
53 int result = getaddrinfo("localhost", NULL, &hints, &info);
54 CPPUNIT_ASSERT(result == 0);
55 CPPUNIT_ASSERT(info != NULL);
56
57 bool check;
58 for (struct addrinfo* iter = info; iter != NULL; iter = iter->ai_next) {
59 // only IPv4 addresses should be returned as we don't have IPv6 routing.
60 check = iter->ai_family == AF_INET;
61 if (!check) break;
62 }
63
64 freeaddrinfo(info);
65 CPPUNIT_ASSERT(check);
66 }
67
68
69 /* static */ void
AddTests(BTestSuite & parent)70 GetAddrInfoTest::AddTests(BTestSuite& parent)
71 {
72 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("GetAddrInfoTest");
73
74 suite.addTest(new CppUnit::TestCaller<GetAddrInfoTest>(
75 "GetAddrInfoTest::EmptyTest", &GetAddrInfoTest::EmptyTest));
76 suite.addTest(new CppUnit::TestCaller<GetAddrInfoTest>(
77 "GetAddrInfoTest::AddrConfigTest", &GetAddrInfoTest::AddrConfigTest));
78
79 parent.addTest("GetAddrInfoTest", &suite);
80 }
81