Blame sunrpc/tst-xdrmem2.c

Packit 6c4009
/* Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  XDR xdrs;
Packit 6c4009
  void *buf;
Packit 6c4009
  size_t ps = sysconf (_SC_PAGESIZE);
Packit 6c4009
  uintptr_t half = -1;
Packit 6c4009
  int v_int;
Packit 6c4009
  u_short v_u_short;
Packit 6c4009
Packit 6c4009
  half = (half >> 1) & ~(uintptr_t) (ps - 1);
Packit 6c4009
  buf = mmap ((void *) half, 2 * ps, PROT_READ | PROT_WRITE,
Packit 6c4009
	      MAP_PRIVATE | MAP_ANON, -1, 0);
Packit 6c4009
  if (buf == MAP_FAILED || buf != (void *) half)
Packit 6c4009
    {
Packit 6c4009
      puts ("Couldn't mmap 2 pages in the middle of address space");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_ENCODE);
Packit 6c4009
Packit 6c4009
#define T(type, val) \
Packit 6c4009
  v_##type = val;			\
Packit 6c4009
  if (! xdr_##type (&xdrs, &v_##type))	\
Packit 6c4009
    {					\
Packit 6c4009
      puts ("encoding of " #type	\
Packit 6c4009
	    " " #val " failed");	\
Packit 6c4009
      return 1;				\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  T(int, 127)
Packit 6c4009
Packit 6c4009
  u_int pos = xdr_getpos (&xdrs);
Packit 6c4009
Packit 6c4009
  T(u_short, 31)
Packit 6c4009
Packit 6c4009
  if (! xdr_setpos (&xdrs, pos))
Packit 6c4009
    {
Packit 6c4009
      puts ("xdr_setpos during encoding failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  T(u_short, 36)
Packit 6c4009
Packit 6c4009
#undef T
Packit 6c4009
Packit 6c4009
  xdr_destroy (&xdrs);
Packit 6c4009
Packit 6c4009
  xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_DECODE);
Packit 6c4009
Packit 6c4009
#define T(type, val) \
Packit 6c4009
  v_##type = 0x15;			\
Packit 6c4009
  if (! xdr_##type (&xdrs, &v_##type))	\
Packit 6c4009
    {					\
Packit 6c4009
      puts ("decoding of " #type	\
Packit 6c4009
	    " " #val " failed");	\
Packit 6c4009
      return 1;				\
Packit 6c4009
    }					\
Packit 6c4009
  if (v_##type != val)			\
Packit 6c4009
    {					\
Packit 6c4009
      puts ("decoded value differs, "	\
Packit 6c4009
	    "type " #type " " #val);	\
Packit 6c4009
      return 1;				\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  T(int, 127)
Packit 6c4009
Packit 6c4009
  pos = xdr_getpos (&xdrs);
Packit 6c4009
Packit 6c4009
  T(u_short, 36)
Packit 6c4009
Packit 6c4009
  if (! xdr_setpos (&xdrs, pos))
Packit 6c4009
    {
Packit 6c4009
      puts ("xdr_setpos during encoding failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  T(u_short, 36)
Packit 6c4009
Packit 6c4009
#undef T
Packit 6c4009
Packit 6c4009
  xdr_destroy (&xdrs);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"