2.9
[glibc/nacl-glibc.git] / sunrpc / tst-xdrmem2.c
blobeed8ccc0726249a7e622cb63dc846c03565cab2e
1 /* Copyright (C) 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <limits.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <rpc/rpc.h>
24 #include <sys/mman.h>
25 #include <unistd.h>
27 static int
28 do_test (void)
30 XDR xdrs;
31 void *buf;
32 size_t ps = sysconf (_SC_PAGESIZE);
33 uintptr_t half = -1;
34 int v_int;
35 u_short v_u_short;
37 half = (half >> 1) & ~(uintptr_t) (ps - 1);
38 buf = mmap ((void *) half, 2 * ps, PROT_READ | PROT_WRITE,
39 MAP_PRIVATE | MAP_ANON, -1, 0);
40 if (buf == MAP_FAILED || buf != (void *) half)
42 puts ("Couldn't mmap 2 pages in the middle of address space");
43 return 0;
46 xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_ENCODE);
48 #define T(type, val) \
49 v_##type = val; \
50 if (! xdr_##type (&xdrs, &v_##type)) \
51 { \
52 puts ("encoding of " #type \
53 " " #val " failed"); \
54 return 1; \
57 T(int, 127)
59 u_int pos = xdr_getpos (&xdrs);
61 T(u_short, 31)
63 if (! xdr_setpos (&xdrs, pos))
65 puts ("xdr_setpos during encoding failed");
66 return 1;
69 T(u_short, 36)
71 #undef T
73 xdr_destroy (&xdrs);
75 xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_DECODE);
77 #define T(type, val) \
78 v_##type = 0x15; \
79 if (! xdr_##type (&xdrs, &v_##type)) \
80 { \
81 puts ("decoding of " #type \
82 " " #val " failed"); \
83 return 1; \
84 } \
85 if (v_##type != val) \
86 { \
87 puts ("decoded value differs, " \
88 "type " #type " " #val); \
89 return 1; \
92 T(int, 127)
94 pos = xdr_getpos (&xdrs);
96 T(u_short, 36)
98 if (! xdr_setpos (&xdrs, pos))
100 puts ("xdr_setpos during encoding failed");
101 return 1;
104 T(u_short, 36)
106 #undef T
108 xdr_destroy (&xdrs);
110 return 0;
113 #define TEST_FUNCTION do_test ()
114 #include "../test-skeleton.c"