1 /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <limits.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <rpc/rpc.h>
22 
23 static int
do_test(void)24 do_test (void)
25 {
26   XDR xdrs;
27   unsigned char buf[8192];
28   int v_int;
29   u_int v_u_int;
30   long v_long;
31   u_long v_u_long;
32   quad_t v_hyper;
33   u_quad_t v_u_hyper;
34   quad_t v_longlong_t;
35   u_quad_t v_u_longlong_t;
36   short v_short;
37   u_short v_u_short;
38   char v_char;
39   u_char v_u_char;
40   bool_t v_bool;
41   enum_t v_enum;
42   char *v_wrapstring;
43 
44   xdrmem_create (&xdrs, (char *) buf, sizeof (buf), XDR_ENCODE);
45 
46 #define TESTS \
47   T(int, 0)				\
48   T(int, CHAR_MAX)			\
49   T(int, CHAR_MIN)			\
50   T(int, SHRT_MAX)			\
51   T(int, SHRT_MIN)			\
52   T(int, INT_MAX)			\
53   T(int, INT_MIN)			\
54   T(int, 0x123)				\
55   T(u_int, 0)				\
56   T(u_int, UCHAR_MAX)			\
57   T(u_int, USHRT_MAX)			\
58   T(u_int, UINT_MAX)			\
59   T(u_int, 0xdeadbeef)			\
60   T(u_int, 0x12345678)			\
61   T(long, 0)				\
62   T(long, 2147483647L)			\
63   T(long, -2147483648L)			\
64   T(long, -305419896L)			\
65   T(long, -305419896L)			\
66   T(u_long, 0)				\
67   T(u_long, 0xffffffffUL)		\
68   T(u_long, 0xdeadbeefUL)		\
69   T(u_long, 0x12345678UL)		\
70   T(hyper, 0)				\
71   T(hyper, CHAR_MAX)			\
72   T(hyper, CHAR_MIN)			\
73   T(hyper, SHRT_MAX)			\
74   T(hyper, SHRT_MIN)			\
75   T(hyper, INT_MAX)			\
76   T(hyper, INT_MIN)			\
77   T(hyper, LONG_MAX)			\
78   T(hyper, LONG_MIN)			\
79   T(hyper, LONG_LONG_MAX)		\
80   T(hyper, LONG_LONG_MIN)		\
81   T(hyper, 0x12312345678LL)		\
82   T(hyper, 0x12387654321LL)		\
83   T(u_hyper, 0)				\
84   T(u_hyper, UCHAR_MAX)			\
85   T(u_hyper, USHRT_MAX)			\
86   T(u_hyper, UINT_MAX)			\
87   T(u_hyper, ULONG_MAX)			\
88   T(u_hyper, ULONG_LONG_MAX)		\
89   T(u_hyper, 0xdeadbeefdeadbeefULL)	\
90   T(u_hyper, 0x12312345678ULL)		\
91   T(u_hyper, 0x12387654321ULL)		\
92   T(longlong_t, 0)			\
93   T(longlong_t, CHAR_MAX)		\
94   T(longlong_t, CHAR_MIN)		\
95   T(longlong_t, SHRT_MAX)		\
96   T(longlong_t, SHRT_MIN)		\
97   T(longlong_t, INT_MAX)		\
98   T(longlong_t, INT_MIN)		\
99   T(longlong_t, LONG_MAX)		\
100   T(longlong_t, LONG_MIN)		\
101   T(longlong_t, LONG_LONG_MAX)		\
102   T(longlong_t, LONG_LONG_MIN)		\
103   T(longlong_t, 0x12312345678LL)	\
104   T(longlong_t, 0x12387654321LL)	\
105   T(u_longlong_t, 0)			\
106   T(u_longlong_t, UCHAR_MAX)		\
107   T(u_longlong_t, USHRT_MAX)		\
108   T(u_longlong_t, UINT_MAX)		\
109   T(u_longlong_t, ULONG_MAX)		\
110   T(u_longlong_t, ULONG_LONG_MAX)	\
111   T(u_longlong_t, 0xdeadbeefdeadbeefULL)\
112   T(u_longlong_t, 0x12312345678ULL)	\
113   T(u_longlong_t, 0x12387654321ULL)	\
114   T(short, CHAR_MAX)			\
115   T(short, CHAR_MIN)			\
116   T(short, SHRT_MAX)			\
117   T(short, SHRT_MIN)			\
118   T(short, 0x123)			\
119   T(u_short, 0)				\
120   T(u_short, UCHAR_MAX)			\
121   T(u_short, USHRT_MAX)			\
122   T(u_short, 0xbeef)			\
123   T(u_short, 0x5678)			\
124   T(char, CHAR_MAX)			\
125   T(char, CHAR_MIN)			\
126   T(char, 0x23)				\
127   T(u_char, 0)				\
128   T(u_char, UCHAR_MAX)			\
129   T(u_char, 0xef)			\
130   T(u_char, 0x78)			\
131   T(bool, 0)				\
132   T(bool, 1)				\
133   T(enum, 0)				\
134   T(enum, CHAR_MAX)			\
135   T(enum, CHAR_MIN)			\
136   T(enum, SHRT_MAX)			\
137   T(enum, SHRT_MIN)			\
138   T(enum, INT_MAX)			\
139   T(enum, INT_MIN)			\
140   T(enum, 0x123)			\
141   S(wrapstring, (char *) "")		\
142   S(wrapstring, (char *) "hello, world")
143 
144 #define T(type, val) \
145   v_##type = val;			\
146   if (! xdr_##type (&xdrs, &v_##type))	\
147     {					\
148       puts ("encoding of " #type	\
149 	    " " #val " failed");	\
150       return 1;				\
151     }
152 #define S(type, val) T(type, val)
153 
154   TESTS
155 #undef T
156 #undef S
157 
158   xdr_destroy (&xdrs);
159 
160   xdrmem_create (&xdrs, (char *) buf, sizeof (buf), XDR_DECODE);
161 
162 #define T(type, val) \
163   v_##type = 0x15;			\
164   if (! xdr_##type (&xdrs, &v_##type))	\
165     {					\
166       puts ("decoding of " #type	\
167 	    " " #val " failed");	\
168       return 1;				\
169     }					\
170   if (v_##type != val)			\
171     {					\
172       puts ("decoded value differs, "	\
173 	    "type " #type " " #val);	\
174       return 1;				\
175     }
176 #define S(type, val) \
177   v_##type = NULL;			\
178   if (! xdr_##type (&xdrs, &v_##type))	\
179     {					\
180       puts ("decoding of " #type	\
181 	    " " #val " failed");	\
182       return 1;				\
183     }					\
184   if (strcmp (v_##type, val))		\
185     {					\
186       puts ("decoded value differs, "	\
187 	    "type " #type " " #val);	\
188       return 1;				\
189     }					\
190   free (v_##type);			\
191   v_##type = NULL;
192 
193   TESTS
194 #undef T
195 #undef S
196 
197   xdr_destroy (&xdrs);
198 
199   return 0;
200 }
201 
202 #define TEST_FUNCTION do_test ()
203 #include "../test-skeleton.c"
204