ATF_TC_BODY(setenv_basic, tc)
{
	const size_t numvars = 8192;
	size_t i, offset;
	char name[1024];
	char value[1024];

	offset = lrand48();
	for (i = 0; i < numvars; i++) {
		(void)snprintf(name, sizeof(name), "var%zu",
		    (i * 7 + offset) % numvars);
		(void)snprintf(value, sizeof(value), "value%ld", lrand48());
		ATF_CHECK(setenv(name, value, 1) != -1);
		ATF_CHECK(setenv(name, "foo", 0) != -1);
		ATF_CHECK_STREQ(getenv(name), value);
	}

	offset = lrand48();
	for (i = 0; i < numvars; i++) {
		(void)snprintf(name, sizeof(name), "var%zu",
		    (i * 11 + offset) % numvars);
		ATF_CHECK(unsetenv(name) != -1);
		ATF_CHECK(getenv(name) == NULL);
		ATF_CHECK(unsetenv(name) != -1);
	}

	ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
	ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
	ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
	ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);

	ATF_CHECK(setenv("var", "=val", 1) == 0);
	ATF_CHECK_STREQ(getenv("var"), "=val");
}
Exemple #2
0
void
atf_check_errno_semicolons(void)
{
    /* Check that ATF_CHECK_ERRNO does not contain a semicolon that would
     * cause an empty-statement that confuses some compilers. */
    ATF_CHECK_ERRNO(1, 1 == 1);
    ATF_CHECK_ERRNO(2, 2 == 2);
}
Exemple #3
0
ATF_TC_BODY(setrlimit_perm, tc)
{
	struct rlimit res;
	size_t i;

	/*
	 * Try to raise the maximum limits as an user.
	 */
	for (i = 0; i < __arraycount(rlimit); i++) {

		ATF_REQUIRE(getrlimit(rlimit[i], &res) == 0);

#ifdef __FreeBSD__
		if (res.rlim_max == INT64_MAX) /* Overflow. */
#else
		if (res.rlim_max == UINT64_MAX) /* Overflow. */
#endif
			continue;

		errno = 0;
		res.rlim_max = res.rlim_max + 1;

		ATF_CHECK_ERRNO(EPERM, setrlimit(rlimit[i], &res) != 0);
	}
}
Exemple #4
0
ATF_TC_BODY(putenv_basic, tc)
{
	char string[1024];

	snprintf(string, sizeof(string), "crap=true");
	ATF_CHECK(putenv(string) != -1);
	ATF_CHECK_STREQ(getenv("crap"), "true");
	string[1] = 'l';
	ATF_CHECK_STREQ(getenv("clap"), "true");
	ATF_CHECK(getenv("crap") == NULL);
	string[1] = 'r';
	ATF_CHECK(unsetenv("crap") != -1);
	ATF_CHECK(getenv("crap") == NULL);

	ATF_CHECK_ERRNO(EINVAL, putenv(NULL) == -1);
	ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("val")) == -1);
	ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("=val")) == -1);
}
Exemple #5
0
ATF_TC_BODY(setenv_basic, tc)
{
	const size_t numvars = 8192;
	size_t i, offset;
	char name[1024];
	char value[1024];

	offset = lrand48();
	for (i = 0; i < numvars; i++) {
		(void)snprintf(name, sizeof(name), "var%zu",
		    (i * 7 + offset) % numvars);
		(void)snprintf(value, sizeof(value), "value%ld", lrand48());
		ATF_CHECK(setenv(name, value, 1) != -1);
		ATF_CHECK(setenv(name, "foo", 0) != -1);
		ATF_CHECK_STREQ(getenv(name), value);
	}

	offset = lrand48();
	for (i = 0; i < numvars; i++) {
		(void)snprintf(name, sizeof(name), "var%zu",
		    (i * 11 + offset) % numvars);
		ATF_CHECK(unsetenv(name) != -1);
		ATF_CHECK(getenv(name) == NULL);
		ATF_CHECK(unsetenv(name) != -1);
	}

	ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
	ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
	ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
#ifdef __FreeBSD__
	/*
	   Both FreeBSD and OS/X does not validate the second
	   argument to setenv(3)
	 */
	atf_tc_expect_signal(SIGSEGV, "FreeBSD does not validate the second "
	    "argument to setenv(3); see bin/189805");
#endif

	ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);

	ATF_CHECK(setenv("var", "=val", 1) == 0);
	ATF_CHECK_STREQ(getenv("var"), "=val");
}
Exemple #6
0
ATF_TC_BODY(setrlimit_nthr, tc)
{
	struct rlimit res;
	lwpid_t lwpid;
	ucontext_t c;

	/*
	 * Set RLIMIT_NTHR to zero and try to create a thread.
	 */
	res.rlim_cur = 0;
	res.rlim_max = 0;
	ATF_REQUIRE(setrlimit(RLIMIT_NTHR, &res) == 0);
	ATF_REQUIRE(getcontext(&c) == 0);
	c.uc_link = NULL;
	sigemptyset(&c.uc_sigmask);
	c.uc_stack.ss_flags = 0;
	c.uc_stack.ss_size = 4096;
	ATF_REQUIRE((c.uc_stack.ss_sp = malloc(c.uc_stack.ss_size)) != NULL);
	makecontext(&c, func, 1, &lwpid);
	ATF_CHECK_ERRNO(EAGAIN, _lwp_create(&c, 0, &lwpid) == -1);
}
Exemple #7
0
ATF_TC_BODY(bpfwritetrunc, tc)
{
	int bpfd;
	struct ifreq ifr;
	struct iovec *iov;
	size_t iovlen, sz;
	const size_t extra_bytes = 28;
	const size_t total = extra_bytes + UINT_MAX + 1;
	long iov_max, vm_page_size; /* round_page wants vm_page_size variable */

	memset(&ifr, 0, sizeof(ifr));

	iov_max      = sysconf(_SC_IOV_MAX);
	vm_page_size = sysconf(_SC_PAGE_SIZE);
	ATF_REQUIRE(iov_max > 1 && vm_page_size > 1);

	/*
	 * Minimize memory consumption by using many iovecs
	 * all pointing to one memory region.
	 */
	iov = calloc(iov_max, sizeof(struct iovec));
	ATF_REQUIRE(iov != NULL);

	sz = round_page((total + (iov_max - 1)) / iov_max);

	iov[0].iov_len = sz;
	iov[0].iov_base = mmap(NULL, sz, PROT_READ, MAP_ANON, -1, 0);
	ATF_REQUIRE(iov[0].iov_base != MAP_FAILED);

	iovlen = 1;
	while (sz + iov[0].iov_len <= total)
	{
		iov[iovlen].iov_len  = iov[0].iov_len;
		iov[iovlen].iov_base = iov[0].iov_base;
		sz += iov[0].iov_len;
		iovlen++;
	}

	if (sz < total)
	{
		iov[iovlen].iov_len = total - sz;
		iov[iovlen].iov_base = iov[0].iov_base;
		iovlen++;
	}

	/* Sanity checks */
	ATF_REQUIRE(iovlen >= 1 && iovlen <= (size_t)iov_max);
	ATF_REQUIRE_EQ(iov[iovlen-1].iov_len, total % iov[0].iov_len);

	RZ(rump_init());
	netcfg_rump_makeshmif("bpfwritetrunc", ifr.ifr_name);
	netcfg_rump_if(ifr.ifr_name, "10.1.1.1", "255.0.0.0");

	RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
	RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));

	ATF_CHECK_ERRNO(EMSGSIZE, rump_sys_writev(bpfd, iov, iovlen) == -1);

	munmap(iov[0].iov_base, iov[0].iov_len);
	free(iov);
}
Exemple #8
0
static void
rename_reg_nodir(const atf_tc_t *tc, const char *mp)
{
	bool haslinks;
	struct stat sb;
	ino_t f1ino;

	if (FSTYPE_RUMPFS(tc))
		atf_tc_skip("rename not supported by file system");

	if (rump_sys_chdir(mp) == -1)
		atf_tc_fail_errno("chdir mountpoint");

	if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
		haslinks = false;
	else
		haslinks = true;

	if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
		atf_tc_fail_errno("create file");
	if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
		atf_tc_fail_errno("create file");

	if (rump_sys_stat("file1", &sb) == -1)
		atf_tc_fail_errno("stat");
	f1ino = sb.st_ino;

	if (haslinks) {
		if (rump_sys_link("file1", "file_link") == -1)
			atf_tc_fail_errno("link");
		if (rump_sys_stat("file_link", &sb) == -1)
			atf_tc_fail_errno("stat");
		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
		ATF_REQUIRE_EQ(sb.st_nlink, 2);
	}

	if (rump_sys_stat("file2", &sb) == -1)
		atf_tc_fail_errno("stat");

	if (rump_sys_rename("file1", "file3") == -1)
		atf_tc_fail_errno("rename 1");
	if (rump_sys_stat("file3", &sb) == -1)
		atf_tc_fail_errno("stat 1");
	if (haslinks) {
		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
	}
	if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
		atf_tc_fail_errno("source 1");

	if (rump_sys_rename("file3", "file2") == -1)
		atf_tc_fail_errno("rename 2");
	if (rump_sys_stat("file2", &sb) == -1)
		atf_tc_fail_errno("stat 2");
	if (haslinks) {
		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
	}

	if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
		atf_tc_fail_errno("source 2");

	if (haslinks) {
		if (rump_sys_rename("file2", "file_link") == -1)
			atf_tc_fail_errno("rename hardlink");
		if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
			atf_tc_fail_errno("source 3");
		if (rump_sys_stat("file_link", &sb) == -1)
			atf_tc_fail_errno("stat 2");
		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
		ATF_REQUIRE_EQ(sb.st_nlink, 1);
	}

	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename("file2", NULL) == -1);
	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename(NULL, "file2") == -1);

	rump_sys_chdir("/");
}