Back to index

glibc  2.9
tst-xdrmem2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
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 #include <sys/mman.h>
00025 #include <unistd.h>
00026 
00027 static int
00028 do_test (void)
00029 {
00030   XDR xdrs;
00031   void *buf;
00032   size_t ps = sysconf (_SC_PAGESIZE);
00033   uintptr_t half = -1;
00034   int v_int;
00035   u_short v_u_short;
00036 
00037   half = (half >> 1) & ~(uintptr_t) (ps - 1);
00038   buf = mmap ((void *) half, 2 * ps, PROT_READ | PROT_WRITE,
00039              MAP_PRIVATE | MAP_ANON, -1, 0);
00040   if (buf == MAP_FAILED || buf != (void *) half)
00041     {
00042       puts ("Couldn't mmap 2 pages in the middle of address space");
00043       return 0;
00044     }
00045 
00046   xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_ENCODE);
00047 
00048 #define T(type, val) \
00049   v_##type = val;                  \
00050   if (! xdr_##type (&xdrs, &v_##type))    \
00051     {                              \
00052       puts ("encoding of " #type   \
00053            " " #val " failed");    \
00054       return 1;                           \
00055     }
00056 
00057   T(int, 127)
00058 
00059   u_int pos = xdr_getpos (&xdrs);
00060 
00061   T(u_short, 31)
00062 
00063   if (! xdr_setpos (&xdrs, pos))
00064     {
00065       puts ("xdr_setpos during encoding failed");
00066       return 1;
00067     }
00068 
00069   T(u_short, 36)
00070 
00071 #undef T
00072 
00073   xdr_destroy (&xdrs);
00074 
00075   xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_DECODE);
00076 
00077 #define T(type, val) \
00078   v_##type = 0x15;                 \
00079   if (! xdr_##type (&xdrs, &v_##type))    \
00080     {                              \
00081       puts ("decoding of " #type   \
00082            " " #val " failed");    \
00083       return 1;                           \
00084     }                              \
00085   if (v_##type != val)                    \
00086     {                              \
00087       puts ("decoded value differs, "     \
00088            "type " #type " " #val);       \
00089       return 1;                           \
00090     }
00091 
00092   T(int, 127)
00093 
00094   pos = xdr_getpos (&xdrs);
00095 
00096   T(u_short, 36)
00097 
00098   if (! xdr_setpos (&xdrs, pos))
00099     {
00100       puts ("xdr_setpos during encoding failed");
00101       return 1;
00102     }
00103 
00104   T(u_short, 36)
00105 
00106 #undef T
00107 
00108   xdr_destroy (&xdrs);
00109 
00110   return 0;
00111 }
00112 
00113 #define TEST_FUNCTION do_test ()
00114 #include "../test-skeleton.c"