1*b607edaeSpgoyette /* $NetBSD: t_xdr.c,v 1.1 2011/01/08 06:59:37 pgoyette Exp $ */
2*b607edaeSpgoyette
3*b607edaeSpgoyette /*
4*b607edaeSpgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*b607edaeSpgoyette * All rights reserved.
6*b607edaeSpgoyette *
7*b607edaeSpgoyette * This code is derived from software contributed to The NetBSD Foundation
8*b607edaeSpgoyette * by Ben Harris.
9*b607edaeSpgoyette *
10*b607edaeSpgoyette * Redistribution and use in source and binary forms, with or without
11*b607edaeSpgoyette * modification, are permitted provided that the following conditions
12*b607edaeSpgoyette * are met:
13*b607edaeSpgoyette * 1. Redistributions of source code must retain the above copyright
14*b607edaeSpgoyette * notice, this list of conditions and the following disclaimer.
15*b607edaeSpgoyette * 2. Redistributions in binary form must reproduce the above copyright
16*b607edaeSpgoyette * notice, this list of conditions and the following disclaimer in the
17*b607edaeSpgoyette * documentation and/or other materials provided with the distribution.
18*b607edaeSpgoyette *
19*b607edaeSpgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*b607edaeSpgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*b607edaeSpgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*b607edaeSpgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*b607edaeSpgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*b607edaeSpgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*b607edaeSpgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*b607edaeSpgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*b607edaeSpgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*b607edaeSpgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*b607edaeSpgoyette * POSSIBILITY OF SUCH DAMAGE.
30*b607edaeSpgoyette */
31*b607edaeSpgoyette
32*b607edaeSpgoyette /*-
33*b607edaeSpgoyette * Copyright (c) 2001 Ben Harris
34*b607edaeSpgoyette * All rights reserved.
35*b607edaeSpgoyette *
36*b607edaeSpgoyette * Redistribution and use in source and binary forms, with or without
37*b607edaeSpgoyette * modification, are permitted provided that the following conditions
38*b607edaeSpgoyette * are met:
39*b607edaeSpgoyette * 1. Redistributions of source code must retain the above copyright
40*b607edaeSpgoyette * notice, this list of conditions and the following disclaimer.
41*b607edaeSpgoyette * 2. Redistributions in binary form must reproduce the above copyright
42*b607edaeSpgoyette * notice, this list of conditions and the following disclaimer in the
43*b607edaeSpgoyette * documentation and/or other materials provided with the distribution.
44*b607edaeSpgoyette * 3. The name of the author may not be used to endorse or promote products
45*b607edaeSpgoyette * derived from this software without specific prior written permission.
46*b607edaeSpgoyette *
47*b607edaeSpgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48*b607edaeSpgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49*b607edaeSpgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50*b607edaeSpgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51*b607edaeSpgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52*b607edaeSpgoyette * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53*b607edaeSpgoyette * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54*b607edaeSpgoyette * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55*b607edaeSpgoyette * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56*b607edaeSpgoyette * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57*b607edaeSpgoyette */
58*b607edaeSpgoyette
59*b607edaeSpgoyette #include <sys/cdefs.h>
60*b607edaeSpgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
61*b607edaeSpgoyette The NetBSD Foundation, inc. All rights reserved.");
62*b607edaeSpgoyette __RCSID("$NetBSD: t_xdr.c,v 1.1 2011/01/08 06:59:37 pgoyette Exp $");
63*b607edaeSpgoyette
64*b607edaeSpgoyette #include <rpc/types.h>
65*b607edaeSpgoyette #include <rpc/xdr.h>
66*b607edaeSpgoyette
67*b607edaeSpgoyette #include <string.h>
68*b607edaeSpgoyette
69*b607edaeSpgoyette #include <atf-c.h>
70*b607edaeSpgoyette
71*b607edaeSpgoyette #include "h_testbits.h"
72*b607edaeSpgoyette
73*b607edaeSpgoyette char xdrdata[] = {
74*b607edaeSpgoyette 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* double 1.0 */
75*b607edaeSpgoyette 0x00, 0x00, 0x00, 0x01, /* enum smallenum SE_ONE */
76*b607edaeSpgoyette 0xff, 0xff, 0xfb, 0x2e, /* enum medenum ME_NEG */
77*b607edaeSpgoyette 0x00, 0x12, 0xd6, 0x87, /* enum bigenum BE_LOTS */
78*b607edaeSpgoyette };
79*b607edaeSpgoyette
80*b607edaeSpgoyette ATF_TC(xdr);
ATF_TC_HEAD(xdr,tc)81*b607edaeSpgoyette ATF_TC_HEAD(xdr, tc)
82*b607edaeSpgoyette {
83*b607edaeSpgoyette atf_tc_set_md_var(tc, "descr",
84*b607edaeSpgoyette "Checks encoding/decoding of doubles and enumerations");
85*b607edaeSpgoyette }
ATF_TC_BODY(xdr,tc)86*b607edaeSpgoyette ATF_TC_BODY(xdr, tc)
87*b607edaeSpgoyette {
88*b607edaeSpgoyette XDR x;
89*b607edaeSpgoyette double d;
90*b607edaeSpgoyette smallenum s;
91*b607edaeSpgoyette medenum m;
92*b607edaeSpgoyette bigenum b;
93*b607edaeSpgoyette char newdata[sizeof(xdrdata)];
94*b607edaeSpgoyette
95*b607edaeSpgoyette xdrmem_create(&x, xdrdata, sizeof(xdrdata), XDR_DECODE);
96*b607edaeSpgoyette
97*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_double(&x, &d), "xdr_double DECODE failed");
98*b607edaeSpgoyette ATF_REQUIRE_EQ_MSG(d, 1.0, "double 1.0 decoded as %g", d);
99*b607edaeSpgoyette
100*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_smallenum(&x, &s), "xdr_smallenum DECODE failed");
101*b607edaeSpgoyette ATF_REQUIRE_EQ_MSG(s, SE_ONE, "SE_ONE decoded as %d", s);
102*b607edaeSpgoyette
103*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_medenum(&x, &m), "xdr_medenum DECODE failed");
104*b607edaeSpgoyette ATF_REQUIRE_EQ_MSG(m, ME_NEG, "ME_NEG decoded as %d", m);
105*b607edaeSpgoyette
106*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_bigenum(&x, &b), "xdr_bigenum DECODE failed");
107*b607edaeSpgoyette ATF_REQUIRE_EQ_MSG(b, BE_LOTS, "BE_LOTS decoded as %d", b);
108*b607edaeSpgoyette
109*b607edaeSpgoyette xdr_destroy(&x);
110*b607edaeSpgoyette
111*b607edaeSpgoyette
112*b607edaeSpgoyette xdrmem_create(&x, newdata, sizeof(newdata), XDR_ENCODE);
113*b607edaeSpgoyette
114*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_double(&x, &d), "xdr_double ENCODE failed");
115*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_smallenum(&x, &s), "xdr_smallenum ENCODE failed");
116*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_medenum(&x, &m), "xdr_medenum ENCODE failed");
117*b607edaeSpgoyette ATF_REQUIRE_MSG(xdr_bigenum(&x, &b), "xdr_bigenum ENCODE failed");
118*b607edaeSpgoyette ATF_REQUIRE_MSG(memcmp(newdata, xdrdata, sizeof(xdrdata)) == 0,
119*b607edaeSpgoyette "xdr ENCODE result differs");
120*b607edaeSpgoyette
121*b607edaeSpgoyette xdr_destroy(&x);
122*b607edaeSpgoyette }
123*b607edaeSpgoyette
ATF_TP_ADD_TCS(tp)124*b607edaeSpgoyette ATF_TP_ADD_TCS(tp)
125*b607edaeSpgoyette {
126*b607edaeSpgoyette ATF_TP_ADD_TC(tp, xdr);
127*b607edaeSpgoyette
128*b607edaeSpgoyette return atf_no_error();
129*b607edaeSpgoyette }
130