[BACK]Return to t_sockaddr_snprintf.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / tests / lib / libutil

File: [cvs.NetBSD.org] / src / tests / lib / libutil / t_sockaddr_snprintf.c (download)

Revision 1.1, Fri Jul 16 13:56:32 2010 UTC (10 years, 9 months ago) by jmmv
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-mips64-premerge-20101231, localcount-20160914, is-mlppp-base, is-mlppp, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, HEAD

Convert the libposix, libprop, librt, libskey and libutil tests to ATF.
Initial work from the GSoC 2008 project by Lukasz Strzygowski.

/* $NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $ */

/*
 * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code was contributed to The NetBSD Foundation by Christos Zoulas.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2008, 2010\
 The NetBSD Foundation, inc. All rights reserved.");
__RCSID("$NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $");

#include <sys/socket.h>		/* AF_ */
#include <sys/un.h>			/* sun */

#include <net/if_dl.h>		/* sdl */
#include <netatalk/at.h>	/* sat */
#include <netinet/in.h>		/* sin/sin6 */

#include <string.h>
#include <util.h>

#include <atf-c.h>

ATF_TC(sockaddr_snprintf_in);
ATF_TC_HEAD(sockaddr_snprintf_in, tc)
{
	atf_tc_set_md_var(tc, "descr",
		"Checks sockaddr_snprintf(3) with sockaddr_in");
}
ATF_TC_BODY(sockaddr_snprintf_in, tc)
{
	char buf[1024];
	struct sockaddr_in sin4;
	int i;

	memset(&sin4, 0, sizeof(sin4));
	sin4.sin_len = sizeof(sin4);
	sin4.sin_family = AF_INET;
	sin4.sin_port = ntohs(80);
	sin4.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %p %a",
		(struct sockaddr *)&sin4);

	ATF_REQUIRE_EQ_MSG(i, 17, "bad length for sin4");
	ATF_REQUIRE_STREQ(buf, "2 16 80 127.0.0.1");
}

ATF_TC(sockaddr_snprintf_in6);
ATF_TC_HEAD(sockaddr_snprintf_in6, tc)
{
	atf_tc_set_md_var(tc, "descr",
		"Checks sockaddr_snprintf(3) with sockaddr_in6");
}
ATF_TC_BODY(sockaddr_snprintf_in6, tc)
{
#ifdef INET6
	char buf[1024];
	struct sockaddr_in6 sin6;
	int i;

	memset(&sin6, 0, sizeof(sin6));
	sin6.sin6_len = sizeof(sin6);
	sin6.sin6_family = AF_INET6;
	sin6.sin6_port = ntohs(80);
	sin6.sin6_addr = in6addr_nodelocal_allnodes;
	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %p %a",
		(struct sockaddr *)&sin6);

	ATF_REQUIRE_EQ_MSG(i, 16, "bad length for sin6");
	ATF_REQUIRE_STREQ(buf, "24 28 80 ff01::1");
#else
	atf_tc_skip("Tests built with USE_INET6=no");
#endif /* INET6 */
}

ATF_TC(sockaddr_snprintf_un);
ATF_TC_HEAD(sockaddr_snprintf_un, tc)
{
	atf_tc_set_md_var(tc, "descr",
		"Checks sockaddr_snprintf(3) with sockaddr_un");
}
ATF_TC_BODY(sockaddr_snprintf_un, tc)
{
	char buf[1024];
	struct sockaddr_un sun;
	int i;

	memset(&sun, 0, sizeof(sun));
	sun.sun_len = sizeof(sun);
	sun.sun_family = AF_UNIX;
	strncpy(sun.sun_path, "/tmp/sock", sizeof(sun.sun_path));
	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
		(struct sockaddr *)&sun);

	ATF_REQUIRE_EQ_MSG(i, 15, "bad length for sun");
	ATF_REQUIRE_STREQ(buf, "1 106 /tmp/sock");
}

ATF_TC(sockaddr_snprintf_at);
ATF_TC_HEAD(sockaddr_snprintf_at, tc)
{
	atf_tc_set_md_var(tc, "descr",
		"Checks sockaddr_snprintf(3) with sockaddr_at");
}
ATF_TC_BODY(sockaddr_snprintf_at, tc)
{
	char buf[1024];
	struct sockaddr_at sat;
	int i;

	memset(&sat, 0, sizeof(sat));
	sat.sat_len = sizeof(sat);
	sat.sat_family = AF_APPLETALK;
	sat.sat_addr.s_net = ntohs(101);
	sat.sat_addr.s_node = 3;
	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
		(struct sockaddr *)&sat);

	ATF_REQUIRE_EQ_MSG(i, 11, "bad length for sat");
	ATF_REQUIRE_STREQ(buf, "16 16 101.3");
}

ATF_TC(sockaddr_snprintf_dl);
ATF_TC_HEAD(sockaddr_snprintf_dl, tc)
{
	atf_tc_set_md_var(tc, "descr",
		"Checks sockaddr_snprintf(3) with sockaddr_dl");
}
ATF_TC_BODY(sockaddr_snprintf_dl, tc)
{
	char buf[1024];
	struct sockaddr_dl sdl;
	int i;

	memset(&sdl, 0, sizeof(sdl));
	sdl.sdl_len = sizeof(sdl);
	sdl.sdl_family = AF_LINK;
	sdl.sdl_index = 0;
	sdl.sdl_type = 0;
	sdl.sdl_nlen = 0;
	sdl.sdl_alen = 6;
	sdl.sdl_slen = 0;
	memcpy(sdl.sdl_data, "\01\02\03\04\05\06", 6);
	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
		(struct sockaddr *)&sdl);

	ATF_REQUIRE_EQ_MSG(i, 17, "bad length for sdl");
	ATF_REQUIRE_STREQ(buf, "18 20 1.2.3.4.5.6");
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, sockaddr_snprintf_in);
	ATF_TP_ADD_TC(tp, sockaddr_snprintf_in6);
	ATF_TP_ADD_TC(tp, sockaddr_snprintf_un);
	ATF_TP_ADD_TC(tp, sockaddr_snprintf_at);
	ATF_TP_ADD_TC(tp, sockaddr_snprintf_dl);

	return atf_no_error();
}