18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci#include <errno.h>
38c2ecf20Sopenharmony_ci#include <fcntl.h>
48c2ecf20Sopenharmony_ci#include <stdio.h>
58c2ecf20Sopenharmony_ci#include <string.h>
68c2ecf20Sopenharmony_ci#include <unistd.h>
78c2ecf20Sopenharmony_ci#include <sys/socket.h>
88c2ecf20Sopenharmony_ci#include <sys/stat.h>
98c2ecf20Sopenharmony_ci#include <sys/sysmacros.h>
108c2ecf20Sopenharmony_ci#include <sys/types.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "../kselftest_harness.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* Remove a file, ignoring the result if it didn't exist. */
158c2ecf20Sopenharmony_civoid rm(struct __test_metadata *_metadata, const char *pathname,
168c2ecf20Sopenharmony_ci	int is_dir)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	int rc;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	if (is_dir)
218c2ecf20Sopenharmony_ci		rc = rmdir(pathname);
228c2ecf20Sopenharmony_ci	else
238c2ecf20Sopenharmony_ci		rc = unlink(pathname);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	if (rc < 0) {
268c2ecf20Sopenharmony_ci		ASSERT_EQ(errno, ENOENT) {
278c2ecf20Sopenharmony_ci			TH_LOG("Not ENOENT: %s", pathname);
288c2ecf20Sopenharmony_ci		}
298c2ecf20Sopenharmony_ci	} else {
308c2ecf20Sopenharmony_ci		ASSERT_EQ(rc, 0) {
318c2ecf20Sopenharmony_ci			TH_LOG("Failed to remove: %s", pathname);
328c2ecf20Sopenharmony_ci		}
338c2ecf20Sopenharmony_ci	}
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciFIXTURE(file) {
378c2ecf20Sopenharmony_ci	char *pathname;
388c2ecf20Sopenharmony_ci	int is_dir;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciFIXTURE_VARIANT(file)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	const char *name;
448c2ecf20Sopenharmony_ci	int expected;
458c2ecf20Sopenharmony_ci	int is_dir;
468c2ecf20Sopenharmony_ci	void (*setup)(struct __test_metadata *_metadata,
478c2ecf20Sopenharmony_ci		      FIXTURE_DATA(file) *self,
488c2ecf20Sopenharmony_ci		      const FIXTURE_VARIANT(file) *variant);
498c2ecf20Sopenharmony_ci	int major, minor, mode; /* for mknod() */
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_civoid setup_link(struct __test_metadata *_metadata,
538c2ecf20Sopenharmony_ci		FIXTURE_DATA(file) *self,
548c2ecf20Sopenharmony_ci		const FIXTURE_VARIANT(file) *variant)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	const char * const paths[] = {
578c2ecf20Sopenharmony_ci		"/bin/true",
588c2ecf20Sopenharmony_ci		"/usr/bin/true",
598c2ecf20Sopenharmony_ci	};
608c2ecf20Sopenharmony_ci	int i;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(paths); i++) {
638c2ecf20Sopenharmony_ci		if (access(paths[i], X_OK) == 0) {
648c2ecf20Sopenharmony_ci			ASSERT_EQ(symlink(paths[i], self->pathname), 0);
658c2ecf20Sopenharmony_ci			return;
668c2ecf20Sopenharmony_ci		}
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	ASSERT_EQ(1, 0) {
698c2ecf20Sopenharmony_ci		TH_LOG("Could not find viable 'true' binary");
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciFIXTURE_VARIANT_ADD(file, S_IFLNK)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	.name = "S_IFLNK",
768c2ecf20Sopenharmony_ci	.expected = ELOOP,
778c2ecf20Sopenharmony_ci	.setup = setup_link,
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_civoid setup_dir(struct __test_metadata *_metadata,
818c2ecf20Sopenharmony_ci	       FIXTURE_DATA(file) *self,
828c2ecf20Sopenharmony_ci	       const FIXTURE_VARIANT(file) *variant)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	ASSERT_EQ(mkdir(self->pathname, 0755), 0);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciFIXTURE_VARIANT_ADD(file, S_IFDIR)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	.name = "S_IFDIR",
908c2ecf20Sopenharmony_ci	.is_dir = 1,
918c2ecf20Sopenharmony_ci	.expected = EACCES,
928c2ecf20Sopenharmony_ci	.setup = setup_dir,
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_civoid setup_node(struct __test_metadata *_metadata,
968c2ecf20Sopenharmony_ci		FIXTURE_DATA(file) *self,
978c2ecf20Sopenharmony_ci		const FIXTURE_VARIANT(file) *variant)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	dev_t dev;
1008c2ecf20Sopenharmony_ci	int rc;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	dev = makedev(variant->major, variant->minor);
1038c2ecf20Sopenharmony_ci	rc = mknod(self->pathname, 0755 | variant->mode, dev);
1048c2ecf20Sopenharmony_ci	ASSERT_EQ(rc, 0) {
1058c2ecf20Sopenharmony_ci		if (errno == EPERM)
1068c2ecf20Sopenharmony_ci			SKIP(return, "Please run as root; cannot mknod(%s)",
1078c2ecf20Sopenharmony_ci				variant->name);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciFIXTURE_VARIANT_ADD(file, S_IFBLK)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	.name = "S_IFBLK",
1148c2ecf20Sopenharmony_ci	.expected = EACCES,
1158c2ecf20Sopenharmony_ci	.setup = setup_node,
1168c2ecf20Sopenharmony_ci	/* /dev/loop0 */
1178c2ecf20Sopenharmony_ci	.major = 7,
1188c2ecf20Sopenharmony_ci	.minor = 0,
1198c2ecf20Sopenharmony_ci	.mode = S_IFBLK,
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciFIXTURE_VARIANT_ADD(file, S_IFCHR)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	.name = "S_IFCHR",
1258c2ecf20Sopenharmony_ci	.expected = EACCES,
1268c2ecf20Sopenharmony_ci	.setup = setup_node,
1278c2ecf20Sopenharmony_ci	/* /dev/zero */
1288c2ecf20Sopenharmony_ci	.major = 1,
1298c2ecf20Sopenharmony_ci	.minor = 5,
1308c2ecf20Sopenharmony_ci	.mode = S_IFCHR,
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_civoid setup_fifo(struct __test_metadata *_metadata,
1348c2ecf20Sopenharmony_ci		FIXTURE_DATA(file) *self,
1358c2ecf20Sopenharmony_ci		const FIXTURE_VARIANT(file) *variant)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	ASSERT_EQ(mkfifo(self->pathname, 0755), 0);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciFIXTURE_VARIANT_ADD(file, S_IFIFO)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	.name = "S_IFIFO",
1438c2ecf20Sopenharmony_ci	.expected = EACCES,
1448c2ecf20Sopenharmony_ci	.setup = setup_fifo,
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciFIXTURE_SETUP(file)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	ASSERT_GT(asprintf(&self->pathname, "%s.test", variant->name), 6);
1508c2ecf20Sopenharmony_ci	self->is_dir = variant->is_dir;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	rm(_metadata, self->pathname, variant->is_dir);
1538c2ecf20Sopenharmony_ci	variant->setup(_metadata, self, variant);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciFIXTURE_TEARDOWN(file)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	rm(_metadata, self->pathname, self->is_dir);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciTEST_F(file, exec_errno)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	char * const argv[2] = { (char * const)self->pathname, NULL };
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	EXPECT_LT(execv(argv[0], argv), 0);
1668c2ecf20Sopenharmony_ci	EXPECT_EQ(errno, variant->expected);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/* S_IFSOCK */
1708c2ecf20Sopenharmony_ciFIXTURE(sock)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	int fd;
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciFIXTURE_SETUP(sock)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	self->fd = socket(AF_INET, SOCK_STREAM, 0);
1788c2ecf20Sopenharmony_ci	ASSERT_GE(self->fd, 0);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciFIXTURE_TEARDOWN(sock)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	if (self->fd >= 0)
1848c2ecf20Sopenharmony_ci		ASSERT_EQ(close(self->fd), 0);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciTEST_F(sock, exec_errno)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	char * const argv[2] = { " magic socket ", NULL };
1908c2ecf20Sopenharmony_ci	char * const envp[1] = { NULL };
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	EXPECT_LT(fexecve(self->fd, argv, envp), 0);
1938c2ecf20Sopenharmony_ci	EXPECT_EQ(errno, EACCES);
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciTEST_HARNESS_MAIN
197