Back to index

glibc  2.9
tst-xdrmem.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <limits.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <rpc/rpc.h>
00024 
00025 static int
00026 do_test (void)
00027 {
00028   XDR xdrs;
00029   unsigned char buf[8192];
00030   int v_int;
00031   u_int v_u_int;
00032   long v_long;
00033   u_long v_u_long;
00034   quad_t v_hyper;
00035   u_quad_t v_u_hyper;
00036   quad_t v_longlong_t;
00037   u_quad_t v_u_longlong_t;
00038   short v_short;
00039   u_short v_u_short;
00040   char v_char;
00041   u_char v_u_char;
00042   bool_t v_bool;
00043   enum_t v_enum;
00044   char *v_wrapstring;
00045 
00046   xdrmem_create (&xdrs, (char *) buf, sizeof (buf), XDR_ENCODE);
00047 
00048 #define TESTS \
00049   T(int, 0)                        \
00050   T(int, CHAR_MAX)                 \
00051   T(int, CHAR_MIN)                 \
00052   T(int, SHRT_MAX)                 \
00053   T(int, SHRT_MIN)                 \
00054   T(int, INT_MAX)                  \
00055   T(int, INT_MIN)                  \
00056   T(int, 0x123)                           \
00057   T(u_int, 0)                      \
00058   T(u_int, UCHAR_MAX)                     \
00059   T(u_int, USHRT_MAX)                     \
00060   T(u_int, UINT_MAX)               \
00061   T(u_int, 0xdeadbeef)                    \
00062   T(u_int, 0x12345678)                    \
00063   T(long, 0)                       \
00064   T(long, 2147483647L)                    \
00065   T(long, -2147483648L)                   \
00066   T(long, -305419896L)                    \
00067   T(long, -305419896L)                    \
00068   T(u_long, 0)                            \
00069   T(u_long, 0xffffffffUL)          \
00070   T(u_long, 0xdeadbeefUL)          \
00071   T(u_long, 0x12345678UL)          \
00072   T(hyper, 0)                      \
00073   T(hyper, CHAR_MAX)               \
00074   T(hyper, CHAR_MIN)               \
00075   T(hyper, SHRT_MAX)               \
00076   T(hyper, SHRT_MIN)               \
00077   T(hyper, INT_MAX)                \
00078   T(hyper, INT_MIN)                \
00079   T(hyper, LONG_MAX)               \
00080   T(hyper, LONG_MIN)               \
00081   T(hyper, LONG_LONG_MAX)          \
00082   T(hyper, LONG_LONG_MIN)          \
00083   T(hyper, 0x12312345678LL)        \
00084   T(hyper, 0x12387654321LL)        \
00085   T(u_hyper, 0)                           \
00086   T(u_hyper, UCHAR_MAX)                   \
00087   T(u_hyper, USHRT_MAX)                   \
00088   T(u_hyper, UINT_MAX)                    \
00089   T(u_hyper, ULONG_MAX)                   \
00090   T(u_hyper, ULONG_LONG_MAX)              \
00091   T(u_hyper, 0xdeadbeefdeadbeefULL)       \
00092   T(u_hyper, 0x12312345678ULL)            \
00093   T(u_hyper, 0x12387654321ULL)            \
00094   T(longlong_t, 0)                 \
00095   T(longlong_t, CHAR_MAX)          \
00096   T(longlong_t, CHAR_MIN)          \
00097   T(longlong_t, SHRT_MAX)          \
00098   T(longlong_t, SHRT_MIN)          \
00099   T(longlong_t, INT_MAX)           \
00100   T(longlong_t, INT_MIN)           \
00101   T(longlong_t, LONG_MAX)          \
00102   T(longlong_t, LONG_MIN)          \
00103   T(longlong_t, LONG_LONG_MAX)            \
00104   T(longlong_t, LONG_LONG_MIN)            \
00105   T(longlong_t, 0x12312345678LL)   \
00106   T(longlong_t, 0x12387654321LL)   \
00107   T(u_longlong_t, 0)               \
00108   T(u_longlong_t, UCHAR_MAX)              \
00109   T(u_longlong_t, USHRT_MAX)              \
00110   T(u_longlong_t, UINT_MAX)        \
00111   T(u_longlong_t, ULONG_MAX)              \
00112   T(u_longlong_t, ULONG_LONG_MAX)  \
00113   T(u_longlong_t, 0xdeadbeefdeadbeefULL)\
00114   T(u_longlong_t, 0x12312345678ULL)       \
00115   T(u_longlong_t, 0x12387654321ULL)       \
00116   T(short, CHAR_MAX)               \
00117   T(short, CHAR_MIN)               \
00118   T(short, SHRT_MAX)               \
00119   T(short, SHRT_MIN)               \
00120   T(short, 0x123)                  \
00121   T(u_short, 0)                           \
00122   T(u_short, UCHAR_MAX)                   \
00123   T(u_short, USHRT_MAX)                   \
00124   T(u_short, 0xbeef)               \
00125   T(u_short, 0x5678)               \
00126   T(char, CHAR_MAX)                \
00127   T(char, CHAR_MIN)                \
00128   T(char, 0x23)                           \
00129   T(u_char, 0)                            \
00130   T(u_char, UCHAR_MAX)                    \
00131   T(u_char, 0xef)                  \
00132   T(u_char, 0x78)                  \
00133   T(bool, 0)                       \
00134   T(bool, 1)                       \
00135   T(enum, 0)                       \
00136   T(enum, CHAR_MAX)                \
00137   T(enum, CHAR_MIN)                \
00138   T(enum, SHRT_MAX)                \
00139   T(enum, SHRT_MIN)                \
00140   T(enum, INT_MAX)                 \
00141   T(enum, INT_MIN)                 \
00142   T(enum, 0x123)                   \
00143   S(wrapstring, (char *) "")              \
00144   S(wrapstring, (char *) "hello, world")
00145 
00146 #define T(type, val) \
00147   v_##type = val;                  \
00148   if (! xdr_##type (&xdrs, &v_##type))    \
00149     {                              \
00150       puts ("encoding of " #type   \
00151            " " #val " failed");    \
00152       return 1;                           \
00153     }
00154 #define S(type, val) T(type, val)
00155 
00156   TESTS
00157 #undef T
00158 #undef S
00159 
00160   xdr_destroy (&xdrs);
00161 
00162   xdrmem_create (&xdrs, (char *) buf, sizeof (buf), XDR_DECODE);
00163 
00164 #define T(type, val) \
00165   v_##type = 0x15;                 \
00166   if (! xdr_##type (&xdrs, &v_##type))    \
00167     {                              \
00168       puts ("decoding of " #type   \
00169            " " #val " failed");    \
00170       return 1;                           \
00171     }                              \
00172   if (v_##type != val)                    \
00173     {                              \
00174       puts ("decoded value differs, "     \
00175            "type " #type " " #val);       \
00176       return 1;                           \
00177     }
00178 #define S(type, val) \
00179   v_##type = NULL;                 \
00180   if (! xdr_##type (&xdrs, &v_##type))    \
00181     {                              \
00182       puts ("decoding of " #type   \
00183            " " #val " failed");    \
00184       return 1;                           \
00185     }                              \
00186   if (strcmp (v_##type, val))             \
00187     {                              \
00188       puts ("decoded value differs, "     \
00189            "type " #type " " #val);       \
00190       return 1;                           \
00191     }                              \
00192   free (v_##type);                 \
00193   v_##type = NULL;
00194 
00195   TESTS
00196 #undef T
00197 #undef S
00198 
00199   xdr_destroy (&xdrs);
00200 
00201   return 0;
00202 }
00203 
00204 #define TEST_FUNCTION do_test ()
00205 #include "../test-skeleton.c"