remove COPYING* from .gitignore files
[platform/upstream/glibc.git] / sunrpc / tst-xdrmem2.c
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.
4
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.
9
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.
14
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.  */
19
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>
26
27 static int
28 do_test (void)
29 {
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;
36
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)
41     {
42       puts ("Couldn't mmap 2 pages in the middle of address space");
43       return 0;
44     }
45
46   xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_ENCODE);
47
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;                         \
55     }
56
57   T(int, 127)
58
59   u_int pos = xdr_getpos (&xdrs);
60
61   T(u_short, 31)
62
63   if (! xdr_setpos (&xdrs, pos))
64     {
65       puts ("xdr_setpos during encoding failed");
66       return 1;
67     }
68
69   T(u_short, 36)
70
71 #undef T
72
73   xdr_destroy (&xdrs);
74
75   xdrmem_create (&xdrs, (char *) buf, 2 * ps, XDR_DECODE);
76
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;                         \
90     }
91
92   T(int, 127)
93
94   pos = xdr_getpos (&xdrs);
95
96   T(u_short, 36)
97
98   if (! xdr_setpos (&xdrs, pos))
99     {
100       puts ("xdr_setpos during encoding failed");
101       return 1;
102     }
103
104   T(u_short, 36)
105
106 #undef T
107
108   xdr_destroy (&xdrs);
109
110   return 0;
111 }
112
113 #define TEST_FUNCTION do_test ()
114 #include "../test-skeleton.c"