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