MFC r320737, r320914

r320737:
Fix cleanup in lib/libc/gen/setdomainname_test

ATF cleanup routines run in separate processes from the tests themselves, so
they can't share global variables.

Also, setdomainname_test needs to be is_exclusive because the test cases
access a global resource.

PR:		219967
Reviewed by:	ngie
Sponsored by:	Spectra Logic Corp
Differential Revision:	https://reviews.freebsd.org/D11188

r320914:
Remove an extraneous strlen from t_setdomainname.c

Reported by:	Coverity
CID:		1377568
X-MFC-With:	320737
Sponsored by:	Spectra Logic Corp
This commit is contained in:
Alan Somers 2017-08-01 16:00:21 +00:00
parent cb4353e192
commit 419eee7a3b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/stable/10/; revision=321878
2 changed files with 51 additions and 16 deletions

View File

@ -34,11 +34,13 @@ __RCSID("$NetBSD: t_setdomainname.c,v 1.2 2012/03/25 08:17:54 joerg Exp $");
#include <sys/param.h>
#include <atf-c.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
static char domain[MAXHOSTNAMELEN];
#define DOMAIN_BACKUP_FILE "domain.bak"
static const char domains[][MAXHOSTNAMELEN] = {
"1234567890",
@ -47,6 +49,45 @@ static const char domains[][MAXHOSTNAMELEN] = {
"--------------------------------------------------------------------"
};
static void
backup_domain(void)
{
char domain[MAXHOSTNAMELEN];
int fd;
size_t l;
ssize_t r,n = 0;
memset(domain, 0, sizeof(domain));
ATF_REQUIRE_EQ(0, getdomainname(domain, sizeof(domain)));
l = strnlen(domain, MAXHOSTNAMELEN);
fd = open(DOMAIN_BACKUP_FILE, O_WRONLY | O_CREAT | O_EXCL, 0644);
ATF_REQUIRE(fd >= 0);
while ((r = write(fd, domain + n, l - n)) > 0)
n += r;
ATF_REQUIRE_EQ(0, r);
close(fd);
}
static void
restore_domain(void)
{
char domain[MAXHOSTNAMELEN];
int fd;
ssize_t r, n = 0;
memset(domain, 0, sizeof(domain));
if ((fd = open(DOMAIN_BACKUP_FILE, O_RDONLY)) < 0)
err(1, "open");
while ((r = read(fd, domain + n, sizeof(domain) - n)) > 0)
n += r;
if (r < 0)
err(1, "read");
if (setdomainname(domain, n) != 0)
err(1, "setdomainname");
close(fd);
}
ATF_TC_WITH_CLEANUP(setdomainname_basic);
ATF_TC_HEAD(setdomainname_basic, tc)
{
@ -59,6 +100,7 @@ ATF_TC_BODY(setdomainname_basic, tc)
char name[MAXHOSTNAMELEN];
size_t i;
backup_domain();
for (i = 0; i < __arraycount(domains); i++) {
(void)memset(name, 0, sizeof(name));
@ -80,12 +122,11 @@ ATF_TC_BODY(setdomainname_basic, tc)
ATF_REQUIRE(strcmp(domains[i], name) == 0);
}
(void)setdomainname(domain, sizeof(domain));
}
ATF_TC_CLEANUP(setdomainname_basic, tc)
{
(void)setdomainname(domain, sizeof(domain));
restore_domain();
}
ATF_TC_WITH_CLEANUP(setdomainname_limit);
@ -100,6 +141,7 @@ ATF_TC_BODY(setdomainname_limit, tc)
char name[MAXHOSTNAMELEN + 1];
(void)memset(name, 0, sizeof(name));
backup_domain();
#ifdef __FreeBSD__
ATF_REQUIRE(setdomainname(name, MAXHOSTNAMELEN - 1 ) == 0);
@ -110,10 +152,10 @@ ATF_TC_BODY(setdomainname_limit, tc)
ATF_TC_CLEANUP(setdomainname_limit, tc)
{
(void)setdomainname(domain, sizeof(domain));
restore_domain();
}
ATF_TC_WITH_CLEANUP(setdomainname_perm);
ATF_TC(setdomainname_perm);
ATF_TC_HEAD(setdomainname_perm, tc)
{
atf_tc_set_md_var(tc, "descr", "Can normal user set the domain name?");
@ -122,24 +164,16 @@ ATF_TC_HEAD(setdomainname_perm, tc)
ATF_TC_BODY(setdomainname_perm, tc)
{
char domain[MAXHOSTNAMELEN];
memset(domain, 0, sizeof(domain));
errno = 0;
ATF_REQUIRE_ERRNO(EPERM, setdomainname(domain, sizeof(domain)) == -1);
}
ATF_TC_CLEANUP(setdomainname_perm, tc)
{
(void)setdomainname(domain, sizeof(domain));
}
ATF_TP_ADD_TCS(tp)
{
(void)memset(domain, 0, sizeof(domain));
ATF_REQUIRE(getdomainname(domain, sizeof(domain)) == 0);
ATF_TP_ADD_TC(tp, setdomainname_basic);
ATF_TP_ADD_TC(tp, setdomainname_limit);
ATF_TP_ADD_TC(tp, setdomainname_perm);

View File

@ -76,6 +76,7 @@ CFLAGS+= -I${.CURDIR}
SRCS.fmtcheck2_test= fmtcheck_test.c
SRCS.fnmatch2_test= fnmatch_test.c
TEST_METADATA.setdomainname_test+= is_exclusive=true
TESTS_SUBDIRS= execve
TESTS_SUBDIRS+= posix_spawn