/*	$NetBSD: t_fopen.c,v 1.7 2019/07/16 17:29:18 martin Exp $ */

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * 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>
__RCSID("$NetBSD: t_fopen.c,v 1.7 2019/07/16 17:29:18 martin Exp $");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/module.h>
#include <atf-c.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static const char *path = "fopen";

ATF_TC_WITH_CLEANUP(fdopen_close);
ATF_TC_HEAD(fdopen_close, tc)
{
	atf_tc_set_md_var(tc, "descr", "See that descriptors are closed");
}

ATF_TC_BODY(fdopen_close, tc)
{
	FILE *f;
	int fd;

	/*
	 * Check that the file descriptor
	 * used to fdopen(3) a stream is
	 * closed once the stream is closed.
	 */
	fd = open(path, O_RDWR | O_CREAT, 0600);

	ATF_REQUIRE(fd >= 0);

	f = fdopen(fd, "w+");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(fclose(f) == 0);
	ATF_REQUIRE(close(fd) == -1);
	ATF_REQUIRE(unlink(path) == 0);
}

ATF_TC_CLEANUP(fdopen_close, tc)
{
	(void)unlink(path);
}

ATF_TC_WITH_CLEANUP(fdopen_err);
ATF_TC_HEAD(fdopen_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from fdopen(3)");
}

ATF_TC_BODY(fdopen_err, tc)
{
	int fd;

	fd = open(path, O_RDONLY | O_CREAT, 0600);
	ATF_REQUIRE(fd >= 0);

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "w") == NULL);

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "a") == NULL);

	ATF_REQUIRE(close(fd) == 0);

	errno = 0;
	ATF_REQUIRE_ERRNO(EBADF, fdopen(fd, "r") == NULL);

	errno = 0;
	ATF_REQUIRE_ERRNO(EBADF, fdopen(-1, "w+") == NULL);

	(void)unlink(path);
}

ATF_TC_CLEANUP(fdopen_err, tc)
{
	(void)unlink(path);
}

ATF_TC_WITH_CLEANUP(fdopen_seek);
ATF_TC_HEAD(fdopen_seek, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test stream position with fdopen(3)");
}

ATF_TC_BODY(fdopen_seek, tc)
{
	FILE *f;
	int fd;

	/*
	 * Verify that the file position associated
	 * with the stream corresponds with the offset
	 * set earlier for the file descriptor.
	 */
	fd = open(path, O_RDWR | O_CREAT, 0600);

	ATF_REQUIRE(fd >= 0);
	ATF_REQUIRE(write(fd, "garbage", 7) == 7);
	ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3);

	f = fdopen(fd, "r+");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(ftell(f) == 3);
	ATF_REQUIRE(fclose(f) == 0);
	ATF_REQUIRE(unlink(path) == 0);
}

ATF_TC_CLEANUP(fdopen_seek, tc)
{
	(void)unlink(path);
}

ATF_TC_WITH_CLEANUP(fopen_err);
ATF_TC_HEAD(fopen_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from fopen(3)");
}

ATF_TC_BODY(fopen_err, tc)
{
	static const char *mode[] = {
		"x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" };

	char buf[PATH_MAX + 1];
	size_t i;
	FILE *f;

	f = fopen(path, "w+");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(fclose(f) == 0);

	/*
	 * Note that also "invalid" characters
	 * may follow the mode-string whenever
	 * the first character is valid.
	 */
	for (i = 0; i < __arraycount(mode); i++) {

		errno = 0;
		f = fopen(path, mode[i]);

		if (f == NULL && errno == EINVAL)
			continue;

		if (f != NULL)
			(void)fclose(f);

		atf_tc_fail_nonfatal("opened file as '%s'",  mode[i]);
	}

	(void)unlink(path);
	(void)memset(buf, 'x', sizeof(buf));

	errno = 0;
	ATF_REQUIRE_ERRNO(EISDIR, fopen("/usr/bin", "w") == NULL);

	errno = 0;
	ATF_REQUIRE_ERRNO(ENOENT, fopen("/a/b/c/d/e/f", "r") == NULL);

	errno = 0;
	ATF_REQUIRE_ERRNO(ENAMETOOLONG, fopen(buf, "r+") == NULL);
}

ATF_TC_CLEANUP(fopen_err, tc)
{
	(void)unlink(path);
}

ATF_TC_WITH_CLEANUP(fopen_append);
ATF_TC_HEAD(fopen_append, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test that append-mode works");
}

ATF_TC_BODY(fopen_append, tc)
{
	char buf[15];
	FILE *f;

	(void)memset(buf, 'x', sizeof(buf));

	f = fopen(path, "w+");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
	ATF_REQUIRE(fclose(f) == 0);

	f = fopen(path, "a");

	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
	ATF_REQUIRE(fclose(f) == 0);

	f = fopen(path, "r");

	ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
	ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);

	ATF_REQUIRE(fclose(f) == 0);
	ATF_REQUIRE(unlink(path) == 0);
}

ATF_TC_CLEANUP(fopen_append, tc)
{
	(void)unlink(path);
}

ATF_TC_WITH_CLEANUP(fopen_mode);
ATF_TC_HEAD(fopen_mode, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test fopen(3) modes");
}

ATF_TC_BODY(fopen_mode, tc)
{
	size_t i;
	FILE *f;

	static const char *mode[] = {
		"r", "r+", "w", "w+", "a", "a+",
		"rb", "r+b", "wb", "w+b", "ab", "a+b",
		"re", "r+e", "we", "w+e", "ae", "a+e",
		"rf", "r+f", "wf", "w+f", "af", "a+f",
		"rl", "r+l", "wl", "w+l", "al", "a+l"
	};

	f = fopen(path, "w+");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(fclose(f) == 0);

	/*
	 * Verify that various modes work.
	 */
	for (i = 0; i < __arraycount(mode); i++) {

		f = fopen(path, mode[i]);

		if (f != NULL) {
			ATF_REQUIRE(fclose(f) == 0);
			continue;
		}

		atf_tc_fail_nonfatal("failed to open file as %s",  mode[i]);
	}

	(void)unlink(path);
}

ATF_TC_CLEANUP(fopen_mode, tc)
{
	(void)unlink(path);
}

static void
check_kernel_modular(void)
{
	int err;

	err = modctl(MODCTL_EXISTS, 0);
	if (err == 0) return;
	if (errno == ENOSYS)
		atf_tc_skip("Kernel does not have 'options MODULAR'.");
	if (errno == EPERM)
		return; /* Module loading can be administratively forbidden */
	ATF_REQUIRE_EQ_MSG(errno, 0, "unexpected error %d from "
	    "modctl(MODCTL_EXISTS, 0)", errno);
}

static bool
is_module_present(const char *name)
{
	bool found;
	size_t len;
	int count;
	struct iovec iov;
	modstat_t *ms;

	for (len = 8192; ;) {
		iov.iov_base = malloc(len);
		iov.iov_len = len;

		errno = 0;

		if (modctl(MODCTL_STAT, &iov) != 0) {
			fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n",
			    strerror(errno));
			atf_tc_fail("Failed to query module status");
		}
		if (len >= iov.iov_len)
			break;
		free(iov.iov_base);
		len = iov.iov_len;
	}

	found = false;
	count = *(int *)iov.iov_base;
	ms = (modstat_t *)((char *)iov.iov_base + sizeof(int));
	while (count > 0) {
		if (strcmp(ms->ms_name, name) == 0) {
			found = true;
			break;
		}
		ms++;
		count--;
	}

	free(iov.iov_base);

	return found;
}

#define COMPAT10_MODNAME "compat_10"

ATF_TC(fopen_nullptr);
ATF_TC_HEAD(fopen_nullptr, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test fopen(3) with NULL path (without "
	    COMPAT10_MODNAME ")");
}

ATF_TC_BODY(fopen_nullptr, tc)
{
	bool compat10;

	check_kernel_modular();
	compat10 = is_module_present(COMPAT10_MODNAME);

	if (compat10)
		atf_tc_skip("Kernel does have the " COMPAT10_MODNAME
		    " module loaded into the kernel");

	/* NULL shall trigger error */
	ATF_REQUIRE_ERRNO(EFAULT, fopen(NULL, "r") == NULL);
}

ATF_TC(fopen_nullptr_compat10);
ATF_TC_HEAD(fopen_nullptr_compat10, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test fopen(3) with NULL path (with "
	    COMPAT10_MODNAME ")");
}

ATF_TC_BODY(fopen_nullptr_compat10, tc)
{
	FILE *fp;
	bool compat10;

	check_kernel_modular();
	compat10 = is_module_present(COMPAT10_MODNAME);

	if (!compat10)
		atf_tc_skip("Kernel does not have the " COMPAT10_MODNAME
		    " module loaded into the kernel");

	/* NULL is translated to "." and shall success */
	fp = fopen(NULL, "r");

	ATF_REQUIRE(fp != NULL);
	ATF_REQUIRE(fclose(fp) == 0);
}

ATF_TC(fopen_perm);
ATF_TC_HEAD(fopen_perm, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test permissions with fopen(3)");
	atf_tc_set_md_var(tc, "require.user", "unprivileged");
}

ATF_TC_BODY(fopen_perm, tc)
{

	errno = 0;
	ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "a+") == NULL);

	errno = 0;
	ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "w+") == NULL);
}

ATF_TC(fopen_regular);
ATF_TC_HEAD(fopen_regular, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'f' mode");
}

ATF_TC_BODY(fopen_regular, tc)
{
	static const char *mode[] = { "rf", "r+f", "wf", "w+f", "af", "a+f" };
	static const char *devs[] = { _PATH_DEVNULL };

	size_t i, j;
	FILE *f;

	for (i = 0; i < __arraycount(devs); i++) {

		for (j = 0; j < __arraycount(mode); j++) {

			errno = 0;
			f = fopen(devs[i], mode[j]);

			if (f == NULL && errno == EFTYPE)
				continue;

			if (f != NULL) {
				(void)fclose(f);

				atf_tc_fail_nonfatal("opened %s as %s",
				    devs[i], mode[j]);
			} else {
				atf_tc_fail_nonfatal(
				    "err %d (%s) from open of %s as %s", errno,
				    strerror(errno), devs[i], mode[j]);
			}
		}
	}
}

static char linkpath[] = "symlink";

ATF_TC_WITH_CLEANUP(fopen_symlink);
ATF_TC_HEAD(fopen_symlink, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'l' mode");
}

ATF_TC_BODY(fopen_symlink, tc)
{
	static const char *mode[] = { "rl", "r+l", "wl", "w+l", "al", "a+l" };
	size_t j;
	FILE *f;

	ATF_CHECK(symlink("/dev/null", linkpath) != -1);

	for (j = 0; j < __arraycount(mode); j++) {

		errno = 0;
		f = fopen(linkpath, mode[j]);

		if (f == NULL && errno == EFTYPE)
			continue;

		if (f != NULL) {
			(void)fclose(f);

			atf_tc_fail_nonfatal("opened %s as %s", linkpath,
			    mode[j]);
		} else {
			atf_tc_fail_nonfatal(
			    "err %d (%s) from open of %s as %s", errno,
			    strerror(errno), linkpath, mode[j]);
		}
	}
	ATF_REQUIRE(unlink(linkpath) == 0);
}

ATF_TC_CLEANUP(fopen_symlink, tc)
{
	(void)unlink(linkpath);
}

ATF_TC_WITH_CLEANUP(fopen_seek);
ATF_TC_HEAD(fopen_seek, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test initial stream position");
}

ATF_TC_BODY(fopen_seek, tc)
{
	FILE *f;

	f = fopen(path, "w+");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
	ATF_REQUIRE(fclose(f) == 0);

	/*
	 * The position of the stream should be
	 * at the start, except for append-mode.
	 */
	f = fopen(path, "r");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(ftello(f) == 0);
	ATF_REQUIRE(fclose(f) == 0);

	f = fopen(path, "a");

	ATF_REQUIRE(f != NULL);
	ATF_REQUIRE(ftello(f) == 7);
	ATF_REQUIRE(fclose(f) == 0);
	ATF_REQUIRE(unlink(path) == 0);
}

ATF_TC_CLEANUP(fopen_seek, tc)
{
	(void)unlink(path);
}

ATF_TC_WITH_CLEANUP(freopen_std);
ATF_TC_HEAD(freopen_std, tc)
{
	atf_tc_set_md_var(tc, "descr", "A basic test of freopen(3)");
}

ATF_TC_BODY(freopen_std, tc)
{
	FILE *std[2] = { stdin, stdout };
	char buf[15];
	size_t i;
	FILE *f;

	/*
	 * Associate a standard stream with a custom stream.
	 * Then write to the standard stream and verify that
	 * the result now appears in the custom stream.
	 */
	for (i = 0; i < __arraycount(std); i++) {

		(void)memset(buf, 'x', sizeof(buf));

		f = fopen(path, "w+");
		ATF_REQUIRE(f != NULL);

		f = freopen(path, "w+", std[i]);
		ATF_REQUIRE(f != NULL);

		ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
		ATF_REQUIRE(fprintf(std[i], "garbage") == 7);
		ATF_REQUIRE(fclose(f) == 0);

		f = fopen(path, "r");

		ATF_REQUIRE(f != NULL);
		ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
		ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);

		ATF_REQUIRE(fclose(f) == 0);
	}

	ATF_REQUIRE(unlink(path) == 0);
}

ATF_TC_CLEANUP(freopen_std, tc)
{
	(void)unlink(path);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, fdopen_close);
	ATF_TP_ADD_TC(tp, fdopen_err);
	ATF_TP_ADD_TC(tp, fdopen_seek);
	ATF_TP_ADD_TC(tp, fopen_append);
	ATF_TP_ADD_TC(tp, fopen_err);
	ATF_TP_ADD_TC(tp, fopen_mode);
	ATF_TP_ADD_TC(tp, fopen_nullptr);
	ATF_TP_ADD_TC(tp, fopen_nullptr_compat10);
	ATF_TP_ADD_TC(tp, fopen_perm);
	ATF_TP_ADD_TC(tp, fopen_regular);
	ATF_TP_ADD_TC(tp, fopen_symlink);
	ATF_TP_ADD_TC(tp, fopen_seek);
	ATF_TP_ADD_TC(tp, freopen_std);

	return atf_no_error();
}
