summary refs log tree commit diff
path: root/inet/tst-inet6_rth.c
blob: 4c5c90ac013160b4f98b2924e0275a49cc190bfa (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/ip6.h>

static int
do_test (void)
{
  int res = 0;
  char buf[1000];
  void *p = inet6_rth_init (buf, 24, IPV6_RTHDR_TYPE_0, 0);
  if (p == NULL)
    {
      puts ("first inet6_rth_init failed");
      res = 1;
    }
  else if (inet6_rth_add (p, &in6addr_any) == 0)
    {
      puts ("first inet6_rth_add succeeded");
      res = 1;
    }

  p = inet6_rth_init (buf, 24, IPV6_RTHDR_TYPE_0, 1);
  if (p == NULL)
    {
      puts ("second inet6_rth_init failed");
      res = 1;
    }
  else if (inet6_rth_add (p, &in6addr_any) != 0)
    {
      puts ("second inet6_rth_add failed");
      res = 1;
    }

  for (int nseg = 4; nseg < 6; ++nseg)
    {
      printf ("nseg = %d\n", nseg);

      p = inet6_rth_init (buf, sizeof (buf), IPV6_RTHDR_TYPE_0, nseg);
      if (p == NULL)
	{
	  puts ("third inet6_rth_init failed");
	  res = 1;
	}
      else
	{
	  struct in6_addr tmp;
	  memset (&tmp, '\0', sizeof (tmp));

	  for (int i = 0; i < nseg; ++i)
	    {
	      tmp.s6_addr[0] = i;
	      if (inet6_rth_add (p, &tmp) != 0)
		{
		  printf ("call %d of third inet6_rth_add failed\n", i + 1);
		  res = 1;
		  goto out;
		}
	    }
	  ((struct ip6_rthdr0 *) p)->ip6r0_segleft = 0;
	  if (inet6_rth_segments (p) != nseg)
	    {
	      puts ("\
inet6_rth_segments returned wrong value after loop with third inet6_rth_add");
	      res = 1;
	      goto out;
	    }

	  char buf2[1000];
	  if (inet6_rth_reverse (p, buf2) != 0)
	    {
	      puts ("first inet6_rth_reverse call failed");
	      res = 1;
	      goto out;
	    }
	  if (((struct ip6_rthdr0 *) buf2)->ip6r0_segleft != nseg)
	    {
	      puts ("segleft after first inet6_rth_reverse wrong");
	      res = 1;
	    }

	  if (inet6_rth_segments (p) != inet6_rth_segments (buf2))
	    {
	      puts ("number of seconds after first inet6_rth_reverse differs");
	      res = 1;
	      goto out;
	    }

	  for (int i = 0; i < nseg; ++i)
	    {
	      struct in6_addr *addr = inet6_rth_getaddr (buf2, i);
	      if (addr == NULL)
		{
		  printf ("call %d of first inet6_rth_getaddr failed\n",
			  i + 1);
		  res = 1;
		}
	      else if (addr->s6_addr[0] != nseg - 1 - i
		       || memcmp (&addr->s6_addr[1], &in6addr_any.s6_addr[1],
				  sizeof (in6addr_any)
				  - sizeof (in6addr_any.s6_addr[0])) != 0)
		{
		  char addrbuf[100];
		  inet_ntop (AF_INET6, addr, addrbuf, sizeof (addrbuf));
		  printf ("\
address %d after first inet6_rth_reverse wrong (%s)\n",
			  i + 1, addrbuf);
		  res = 1;
		}
	    }
	out:
	  ;
	}

      p = inet6_rth_init (buf, sizeof (buf), IPV6_RTHDR_TYPE_0, nseg);
      if (p == NULL)
	{
	  puts ("fourth inet6_rth_init failed");
	  res = 1;
	}
      else
	{
	  struct in6_addr tmp;
	  memset (&tmp, '\0', sizeof (tmp));

	  for (int i = 0; i < nseg; ++i)
	    {
	      tmp.s6_addr[0] = i;
	      if (inet6_rth_add (p, &tmp) != 0)
		{
		  printf ("call %d of fourth inet6_rth_add failed\n", i + 1);
		  res = 1;
		  goto out2;
		}
	    }
	  ((struct ip6_rthdr0 *) p)->ip6r0_segleft = 0;
	  if (inet6_rth_segments (p) != nseg)
	    {
	      puts ("\
inet6_rth_segments returned wrong value after loop with fourth inet6_rth_add");
	      res = 1;
	      goto out2;
	    }

	  if (inet6_rth_reverse (p, p) != 0)
	    {
	      puts ("second inet6_rth_reverse call failed");
	      res = 1;
	      goto out2;
	    }
	  if (((struct ip6_rthdr0 *) p)->ip6r0_segleft != nseg)
	    {
	      puts ("segleft after second inet6_rth_reverse wrong");
	      res = 1;
	    }

	  for (int i = 0; i < nseg; ++i)
	    {
	      struct in6_addr *addr = inet6_rth_getaddr (p, i);
	      if (addr == NULL)
		{
		  printf ("call %d of second inet6_rth_getaddr failed\n",
			  i + 1);
		  res = 1;
		}
	      else if (addr->s6_addr[0] != nseg - 1 - i
		       || memcmp (&addr->s6_addr[1], &in6addr_any.s6_addr[1],
				  sizeof (in6addr_any)
				  - sizeof (in6addr_any.s6_addr[0])) != 0)
		{
		  char addrbuf[100];
		  inet_ntop (AF_INET6, addr, addrbuf, sizeof (addrbuf));
		  printf ("\
address %d after second inet6_rth_reverse wrong (%s)\n",
			  i + 1, addrbuf);
		  res = 1;
		}
	    }
	out2:
	  ;
	}
    }

  return res;
}

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