aboutsummaryrefslogtreecommitdiff
blob: 5077f311fcb57d61928d4e36291ec778a61a1c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <mcheck.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>


static int
do_test (void)
{
  mtrace ();

  int result = 0;
  struct addrinfo hints;
  struct addrinfo *ai_res;
  int s;

#define T(no, fail, addr, fam, coraddr)					      \
  s = getaddrinfo (addr, NULL, &hints, &ai_res);			      \
  if (s != 0)								      \
    {									      \
      if (s != fail)							      \
	{								      \
	  printf ("getaddrinfo test %d failed: %s\n", no, gai_strerror (s));  \
	  result = 1;							      \
	}								      \
      ai_res = NULL;							      \
    }									      \
  else if (fail)							      \
    {									      \
      printf ("getaddrinfo test %d should have failed but did not\n", no);    \
      result = 1;							      \
    }									      \
  else if (ai_res->ai_family != fam)					      \
    {									      \
      printf ("\
getaddrinfo test %d return address of family %d, expected %d\n",	      \
	      no, ai_res->ai_family, fam);				      \
      result = 1;							      \
    }									      \
  else if (fam == AF_INET)						      \
    {									      \
      if (ai_res->ai_addrlen != sizeof (struct sockaddr_in))		      \
	{								      \
	  printf ("getaddrinfo test %d: address size %zu, expected %zu\n",    \
		  no, (size_t) ai_res->ai_addrlen,			      \
		  sizeof (struct sockaddr_in));				      \
	  result = 1;							      \
	}								      \
      else if (strcmp (coraddr, \
		       inet_ntoa (((struct sockaddr_in *) ai_res->ai_addr)->sin_addr))\
	       != 0)							      \
	{								      \
	  printf ("getaddrinfo test %d: got value %s, expected %s\n",	      \
		  no,							      \
		  inet_ntoa (((struct sockaddr_in *) ai_res->ai_addr)->sin_addr), \
		  coraddr);						      \
	  result = 1;							      \
	}								      \
    }									      \
  else									      \
    {									      \
      char buf[100];							      \
									      \
      if (ai_res->ai_addrlen != sizeof (struct sockaddr_in6))		      \
	{								      \
	  printf ("getaddrinfo test %d: address size %zu, expected %zu\n",    \
		  no, (size_t) ai_res->ai_addrlen,			      \
		  sizeof (struct sockaddr_in6));			      \
	  result = 1;							      \
	}								      \
      else if (strcmp (coraddr, \
		       inet_ntop (AF_INET6,				      \
				  &((struct sockaddr_in6 *) ai_res->ai_addr)->sin6_addr,\
				  buf, sizeof (buf)))			      \
	       != 0)							      \
	{								      \
	  printf ("getaddrinfo test %d: got value %s, expected %s\n",	      \
		  no,							      \
		  inet_ntop (AF_INET6,					      \
			     & ((struct sockaddr_in6 *) ai_res->ai_addr)->sin6_addr, \
			     buf, sizeof (buf)),			      \
		  coraddr);						      \
	  result = 1;							      \
	}								      \
    }									      \
  if (ai_res != NULL && ai_res->ai_next != NULL)			      \
    {									      \
      puts ("expected only one result");				      \
      result = 1;							      \
    }									      \
  freeaddrinfo (ai_res)


  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  T (1, 0, "127.0.0.1", AF_INET, "127.0.0.1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  T (2, 0, "127.0.0.1", AF_INET, "127.0.0.1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_V4MAPPED;
  T (3, 0, "127.0.0.1", AF_INET6, "::ffff:127.0.0.1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  T (4, EAI_ADDRFAMILY, "127.0.0.1", AF_INET6, "");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  T (5, 0, "::1", AF_INET6, "::1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  T (6, EAI_ADDRFAMILY, "::1", AF_INET6, "");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  T (7, 0, "::1", AF_INET6, "::1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  T (8, 0, "::ffff:127.0.0.1", AF_INET6, "::ffff:127.0.0.1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  T (9, 0, "::ffff:127.0.0.1", AF_INET, "127.0.0.1");

  memset (&hints, '\0', sizeof (hints));
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  T (10, 0, "::ffff:127.0.0.1", AF_INET6, "::ffff:127.0.0.1");

  return result;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"