Add basic tests for the rest of the mktemp(3) family of functions, including
a case for PR lib/55441.
This commit is contained in:
parent
1765a45b05
commit
7fed543511
|
@ -1,11 +1,11 @@
|
|||
/* $NetBSD: t_mktemp.c,v 1.1 2020/06/27 09:45:57 jruoho Exp $ */
|
||||
/* $NetBSD: t_mktemp.c,v 1.2 2020/07/01 05:37:25 jruoho Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2013 The NetBSD Foundation, Inc.
|
||||
* Copyright (c) 2013, 2020 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
* by Christos Zoulas and Jukka Ruohonen.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -29,16 +29,38 @@
|
|||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: t_mktemp.c,v 1.1 2020/06/27 09:45:57 jruoho Exp $");
|
||||
__RCSID("$NetBSD: t_mktemp.c,v 1.2 2020/07/01 05:37:25 jruoho Exp $");
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <atf-c.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int
|
||||
check_mode(struct stat sa, const int mode, const int dir)
|
||||
{
|
||||
|
||||
if (dir == 0 && S_ISREG(sa.st_mode) == 0)
|
||||
return -1;
|
||||
|
||||
if (dir != 0 && S_ISDIR(sa.st_mode) == 0)
|
||||
return -1;
|
||||
|
||||
if ((sa.st_mode & mode) == 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ATF_TC(mktemp_not_exist);
|
||||
ATF_TC_HEAD(mktemp_not_exist, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Test that mktemp does not fail on"
|
||||
" a path that does not exist");
|
||||
atf_tc_set_md_var(tc, "descr", "Test that mktemp(3)"
|
||||
" does not fail on a path that does not exist");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mktemp_not_exist, tc)
|
||||
|
@ -47,8 +69,198 @@ ATF_TC_BODY(mktemp_not_exist, tc)
|
|||
ATF_REQUIRE(mktemp(template) != NULL);
|
||||
}
|
||||
|
||||
ATF_TC(mktemp_large_template);
|
||||
ATF_TC_HEAD(mktemp_large_template, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "Test that mktemp "
|
||||
"accepts arbitrarily large templates (PR lib/55441)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mktemp_large_template, tc)
|
||||
{
|
||||
const char *path = "/tmp/mktemp.XXXXXX";
|
||||
char template[PATH_MAX] = { 0 };
|
||||
size_t i;
|
||||
|
||||
atf_tc_expect_fail("PR lib/55441");
|
||||
|
||||
ATF_REQUIRE(strcpy(template, path) != NULL);
|
||||
ATF_REQUIRE(mktemp(template) != NULL);
|
||||
ATF_REQUIRE(strlen(template) == strlen(path));
|
||||
ATF_REQUIRE(strcmp(template, path) != 0);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
|
||||
(void)memset(template, '\0', sizeof(template));
|
||||
(void)strcpy(template, "/tmp/mktemp.");
|
||||
|
||||
for (i = 12; i < sizeof(template) - 1; i++)
|
||||
template[i] = 'X';
|
||||
|
||||
ATF_REQUIRE(mktemp(template) != NULL);
|
||||
ATF_REQUIRE(strlen(template) == sizeof(template) - 1);
|
||||
ATF_REQUIRE(strcmp(template, path) != 0);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
}
|
||||
|
||||
ATF_TC(mkstemp_basic);
|
||||
ATF_TC_HEAD(mkstemp_basic, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "A basic test of mkstemp(3)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mkstemp_basic, tc)
|
||||
{
|
||||
char template[] = "/tmp/mktemp.XXXXXX";
|
||||
struct stat sa;
|
||||
int fd;
|
||||
|
||||
(void)memset(&sa, 0, sizeof(struct stat));
|
||||
|
||||
fd = mkstemp(template);
|
||||
|
||||
ATF_REQUIRE(fd != -1);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
ATF_REQUIRE(write(fd, "X", 1) == 1);
|
||||
ATF_REQUIRE(fstat(fd, &sa) == 0);
|
||||
ATF_REQUIRE(check_mode(sa, 0600, 0) == 0);
|
||||
ATF_REQUIRE(close(fd) == 0);
|
||||
ATF_REQUIRE(unlink(template) == 0);
|
||||
}
|
||||
|
||||
ATF_TC(mkstemps_basic);
|
||||
ATF_TC_HEAD(mkstemps_basic, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "A basic test of mkstemps(3)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mkstemps_basic, tc)
|
||||
{
|
||||
char template[] = "/tmp/mktemp.XXXyyy";
|
||||
struct stat sa;
|
||||
int fd;
|
||||
|
||||
(void)memset(&sa, 0, sizeof(struct stat));
|
||||
|
||||
fd = mkstemps(template, 3);
|
||||
|
||||
ATF_REQUIRE(fd != -1);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
|
||||
char *str = strchr(template, 'y');
|
||||
|
||||
ATF_REQUIRE(strcmp(str, "yyy") == 0);
|
||||
ATF_REQUIRE(write(fd, "X", 1) == 1);
|
||||
ATF_REQUIRE(fstat(fd, &sa) == 0);
|
||||
ATF_REQUIRE(check_mode(sa, 0600, 0) == 0);
|
||||
ATF_REQUIRE(close(fd) == 0);
|
||||
ATF_REQUIRE(unlink(template) == 0);
|
||||
}
|
||||
|
||||
ATF_TC(mkdtemp_basic);
|
||||
ATF_TC_HEAD(mkdtemp_basic, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "A basic test of mkdtemp(3)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mkdtemp_basic, tc)
|
||||
{
|
||||
char template[] = "/tmp/mktemp.XXXXXX";
|
||||
char *path = mkdtemp(template);
|
||||
struct stat sa;
|
||||
|
||||
(void)memset(&sa, 0, sizeof(struct stat));
|
||||
|
||||
ATF_REQUIRE(path != NULL);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
ATF_REQUIRE(stat(path, &sa) == 0);
|
||||
ATF_REQUIRE(check_mode(sa, 0700, 1) == 0);
|
||||
ATF_REQUIRE(rmdir(path) == 0);
|
||||
}
|
||||
|
||||
ATF_TC(mkostemp_basic);
|
||||
ATF_TC_HEAD(mkostemp_basic, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "A basic test of mkostemp(3)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mkostemp_basic, tc)
|
||||
{
|
||||
static const int flags[] = {
|
||||
O_APPEND, O_DIRECT,
|
||||
O_SHLOCK, O_EXLOCK,
|
||||
O_SYNC, O_CLOEXEC
|
||||
};
|
||||
|
||||
char template[] = "/tmp/mktemp.XXXXXX";
|
||||
struct stat sa;
|
||||
size_t i;
|
||||
int fd;
|
||||
|
||||
for (i = 0; i < __arraycount(flags); i++) {
|
||||
|
||||
(void)memset(&sa, 0, sizeof(struct stat));
|
||||
|
||||
fd = mkostemp(template, flags[i]);
|
||||
|
||||
ATF_REQUIRE(fd != -1);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
ATF_REQUIRE(write(fd, "X", 1) == 1);
|
||||
ATF_REQUIRE(fstat(fd, &sa) == 0);
|
||||
ATF_REQUIRE(check_mode(sa, 0600 | flags[i], 0) == 0);
|
||||
ATF_REQUIRE(close(fd) == 0);
|
||||
ATF_REQUIRE(unlink(template) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
ATF_TC(mkostemps_basic);
|
||||
ATF_TC_HEAD(mkostemps_basic, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr", "A basic test of mkostemps(3)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(mkostemps_basic, tc)
|
||||
{
|
||||
static const int flags[] = {
|
||||
O_APPEND, O_DIRECT,
|
||||
O_SHLOCK, O_EXLOCK,
|
||||
O_SYNC, O_CLOEXEC
|
||||
};
|
||||
|
||||
char template[] = "/tmp/mktemp.XXXyyy";
|
||||
struct stat sa;
|
||||
size_t i;
|
||||
int fd;
|
||||
|
||||
for (i = 0; i < __arraycount(flags); i++) {
|
||||
|
||||
(void)memset(&sa, 0, sizeof(struct stat));
|
||||
|
||||
fd = mkostemps(template, 3, flags[i]);
|
||||
|
||||
ATF_REQUIRE(fd != -1);
|
||||
ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
|
||||
|
||||
char *str = strchr(template, 'y');
|
||||
|
||||
ATF_REQUIRE(strcmp(str, "yyy") == 0);
|
||||
ATF_REQUIRE(write(fd, "X", 1) == 1);
|
||||
ATF_REQUIRE(fstat(fd, &sa) == 0);
|
||||
ATF_REQUIRE(check_mode(sa, 0600 | flags[i], 0) == 0);
|
||||
ATF_REQUIRE(close(fd) == 0);
|
||||
ATF_REQUIRE(unlink(template) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
ATF_TP_ADD_TCS(tp)
|
||||
{
|
||||
|
||||
ATF_TP_ADD_TC(tp, mktemp_not_exist);
|
||||
ATF_TP_ADD_TC(tp, mktemp_large_template);
|
||||
ATF_TP_ADD_TC(tp, mkstemp_basic);
|
||||
ATF_TP_ADD_TC(tp, mkstemps_basic);
|
||||
ATF_TP_ADD_TC(tp, mkdtemp_basic);
|
||||
ATF_TP_ADD_TC(tp, mkostemp_basic);
|
||||
ATF_TP_ADD_TC(tp, mkostemps_basic);
|
||||
|
||||
return atf_no_error();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue