Ejemplo n.º 1
0
ATF_TC_BODY(map_alias_name2sym, tc)
{
	GElf_Sym sym1, sym2;
	prsyminfo_t si1, si2;
	struct proc_handle *phdl;
	int error;

	phdl = start_prog(tc, false);

	/* Initialize the rtld_db handle. */
	(void)proc_rdagent(phdl);

	/*
	 * Make sure that "target_prog:main" and "a.out:main" return the same
	 * symbol.
	 */
	error = proc_name2sym(phdl, target_prog_file, "main", &sym1, &si1);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
	    target_prog_file);
	error = proc_name2sym(phdl, aout_object, "main", &sym2, &si2);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
	    aout_object);

	ATF_CHECK_EQ(memcmp(&sym1, &sym2, sizeof(sym1)), 0);
	ATF_CHECK_EQ(si1.prs_id, si2.prs_id);

	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");

	proc_free(phdl);
}
Ejemplo n.º 2
0
static void
h_require(const char *name, u_long start,
	u_long end, int flags, const char *exp)
{
	char buf[4096];
	struct extent_region *rp;
	int n = 0;

	ATF_REQUIRE_STREQ_MSG(ex->ex_name, name,
	    "expected: \"%s\", got: \"%s\"", name, ex->ex_name);
	ATF_REQUIRE_EQ_MSG(ex->ex_start, start,
	    "expected: %#lx, got: %#lx", start, ex->ex_start);
	ATF_REQUIRE_EQ_MSG(ex->ex_end, end,
	    "expected: %#lx, got: %#lx", end, ex->ex_end);
	ATF_REQUIRE_EQ_MSG(ex->ex_flags, flags,
	    "expected: %#x, got: %#x", flags, ex->ex_flags);

	(void)memset(buf, 0, sizeof(buf));
	LIST_FOREACH(rp, &ex->ex_regions, er_link)
		n += snprintf(buf + n, sizeof(buf) - n,
		    "0x%lx - 0x%lx\n", rp->er_start, rp->er_end);

	if (strcmp(buf, exp) == 0)
		return;

	printf("Incorrect extent map\n");
	printf("Expected:\n%s\n", exp);
	printf("Got:\n%s\n", buf);
	atf_tc_fail("incorrect extent map");
}
Ejemplo n.º 3
0
ATF_TC_BODY(signal_forward, tc)
{
	struct proc_handle *phdl;
	int state, status;

	phdl = start_prog(tc, true);
	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");

	/* The process should have been interrupted by a signal. */
	state = proc_wstatus(phdl);
	ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has unexpected state %d",
	    state);

	/* Continue execution and allow the signal to be delivered. */
	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");

	/*
	 * Make sure the process exited with status 0. If it didn't receive the
	 * SIGUSR1 that it sent to itself, it'll exit with a non-zero exit
	 * status, causing the test to fail.
	 */
	state = proc_wstatus(phdl);
	ATF_REQUIRE_EQ_MSG(state, PS_UNDEAD, "process has unexpected state %d",
	    state);

	status = proc_getwstat(phdl);
	ATF_REQUIRE(status >= 0);
	ATF_REQUIRE(WIFEXITED(status));
	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);

	proc_free(phdl);
}
Ejemplo n.º 4
0
ATF_TC_BODY(symbol_lookup, tc)
{
	GElf_Sym main_sym, r_debug_state_sym;
	struct proc_handle *phdl;
	proc_breakpoint_t saved;
	int error;

	phdl = start_prog(tc, false);

	error = proc_name2sym(phdl, target_prog_file, "main", &main_sym, NULL);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main'");

	error = proc_name2sym(phdl, ldelf_object, r_debug_state,
	    &r_debug_state_sym, NULL);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up '%s'", r_debug_state);

	set_bkpt(phdl, (uintptr_t)r_debug_state_sym.st_value, &saved);
	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
	verify_bkpt(phdl, &r_debug_state_sym, r_debug_state, ldelf_object);
	remove_bkpt(phdl, (uintptr_t)r_debug_state_sym.st_value, &saved);

	set_bkpt(phdl, (uintptr_t)main_sym.st_value, &saved);
	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
	verify_bkpt(phdl, &main_sym, "main", target_prog_file);
	remove_bkpt(phdl, (uintptr_t)main_sym.st_value, &saved);

	ATF_CHECK_EQ_MSG(proc_detach(phdl, PRELEASE_HANG), 0, "failed to detach");

	proc_free(phdl);
}
Ejemplo n.º 5
0
ATF_TC_BODY(posix_memalign_basic, tc)
{
	static const size_t size[] = {
		1, 2, 3, 4, 10, 100, 16384, 32768, 65536
	};
	static const size_t align[] = {
		512, 1024, 16, 32, 64, 4, 2048, 16, 2
	};

	size_t i;
	void *p;

	for (i = 0; i < __arraycount(size); i++) {
		int ret;
		p = (void*)0x1;

		(void)printf("Checking posix_memalign(&p, %zu, %zu)...\n",
			align[i], size[i]);
		ret = posix_memalign(&p, align[i], size[i]);

		if ( align[i] < sizeof(void *))
			ATF_REQUIRE_EQ_MSG(ret, EINVAL,
			    "posix_memalign: %s", strerror(ret));
		else {
			ATF_REQUIRE_EQ_MSG(ret, 0,
			    "posix_memalign: %s", strerror(ret));
			ATF_REQUIRE_EQ_MSG(((vaddr_t)p) & (align[i] - 1), 0,
			    "p = %p", p);
			free(p);
		}
	}
}
Ejemplo n.º 6
0
ATF_TC_BODY(symbol_lookup, tc)
{
	GElf_Sym main_sym, r_debug_state_sym;
	struct proc_handle *phdl;
	u_long saved;
	int error;

	phdl = start_prog(tc, false);

	error = proc_name2sym(phdl, target_prog_file, "main", &main_sym, NULL);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main'");

	error = proc_name2sym(phdl, ldelf_object, "r_debug_state",
	    &r_debug_state_sym, NULL);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'r_debug_state'");

	set_bkpt(phdl, r_debug_state_sym.st_value, &saved);
	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
	verify_bkpt(phdl, &r_debug_state_sym, "r_debug_state", ldelf_object);
	remove_bkpt(phdl, r_debug_state_sym.st_value, saved);

	set_bkpt(phdl, main_sym.st_value, &saved);
	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
	verify_bkpt(phdl, &main_sym, "main", target_prog_file);
	remove_bkpt(phdl, main_sym.st_value, saved);

	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");

	proc_free(phdl);
}
Ejemplo n.º 7
0
ATF_TC_BODY(writev_iovmax, tc)
{
	ssize_t retval;

	(void)printf("Calling writev(2, NULL, IOV_MAX + 1)...\n");

	errno = 0;
	retval = writev(2, NULL, IOV_MAX + 1);

	ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
	ATF_REQUIRE_EQ_MSG(errno, EINVAL, "got: %s", strerror(errno));
}
Ejemplo n.º 8
0
static void
remove_bkpt(struct proc_handle *phdl, uintptr_t addr, u_long val)
{
	int error;

	error = proc_bkptdel(phdl, addr, val);
	ATF_REQUIRE_EQ_MSG(error, 0,
	    "failed to delete breakpoint at 0x%jx", (uintmax_t)addr);

	error = proc_regset(phdl, REG_PC, addr);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to reset program counter");
}
Ejemplo n.º 9
0
/*
 * Wait for the specified process to hit a breakpoint at the specified symbol.
 */
static void
verify_bkpt(struct proc_handle *phdl, GElf_Sym *sym, const char *symname,
    const char *mapname)
{
	char mapbname[MAXPATHLEN], *name;
	GElf_Sym tsym;
	prmap_t *map;
	size_t namesz;
	u_long addr;
	int error, state;

	state = proc_wstatus(phdl);
	ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has state %d", state);

	/* Get the program counter and decrement it. */
	error = proc_regget(phdl, REG_PC, &addr);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to obtain PC for '%s'",
	    target_prog_file);
	proc_bkptregadj(&addr);

	/*
	 * Make sure the PC matches the expected value obtained from the symbol
	 * definition we looked up earlier.
	 */
	ATF_CHECK_EQ_MSG(addr, sym->st_value,
	    "program counter 0x%lx doesn't match expected value 0x%jx",
	    addr, (uintmax_t)sym->st_value);

	/*
	 * Ensure we can look up the r_debug_state symbol using its starting
	 * address and that the resulting symbol matches the one we found using
	 * a name lookup.
	 */
	namesz = strlen(symname) + 1;
	name = malloc(namesz);
	ATF_REQUIRE(name != NULL);

	error = proc_addr2sym(phdl, addr, name, namesz, &tsym);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up symbol at 0x%lx", addr);
	ATF_REQUIRE_EQ(memcmp(sym, &tsym, sizeof(*sym)), 0);
	ATF_REQUIRE_EQ_MSG(strcmp(symname, name), 0,
	    "expected symbol name '%s' doesn't match '%s'", symname, name);
	free(name);

	map = proc_addr2map(phdl, addr);
	ATF_REQUIRE_MSG(map != NULL, "failed to look up map for address 0x%lx",
	    addr);
	basename_r(map->pr_mapname, mapbname);
	ATF_REQUIRE_EQ_MSG(strcmp(mapname, mapbname), 0,
	    "expected map name '%s' doesn't match '%s'", mapname, mapbname);
}
Ejemplo n.º 10
0
ATF_TC_BODY(pipe1, tc)
{
	struct kevent event[1];
	int fds[2];
	int kq, n;

	RL(pipe(fds));
	RL(kq = kqueue());
	RL(close(fds[0]));

	EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0);
	ATF_REQUIRE_EQ_MSG((n = kevent(kq, event, 1, NULL, 0, NULL)),
	    -1, "got: %d", n);
	ATF_REQUIRE_EQ_MSG(errno, EBADF, "got: %s", strerror(errno));
}
Ejemplo n.º 11
0
ATF_TC_BODY(fork, tc)
{
	pthread_t p;
	pid_t fork_pid;

	parent = getpid();

	PTHREAD_REQUIRE(pthread_create(&p, NULL, print_pid, NULL));

	fork_pid = fork();
	ATF_REQUIRE(fork_pid != -1);

	if (fork_pid) {
		int status;

		PTHREAD_REQUIRE(pthread_join(p, NULL));
		ATF_REQUIRE_MSG(thread_survived, "thread did not survive in parent");

		waitpid(fork_pid, &status, 0);
		ATF_REQUIRE_MSG(WIFEXITED(status), "child died wrongly");
		ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), 0, "thread survived in child");
	} else {
		sleep(5);
#ifdef __FreeBSD__
		_exit(thread_survived ? 1 : 0);
#else
		exit(thread_survived ? 1 : 0);
#endif
	}
}
Ejemplo n.º 12
0
ATF_TC_BODY(t_exect_null, tc)
{
	struct sigaction act;

	/*
	 * Currently exect(3) is misdesigned -- see PR port-amd64/51700 and it
	 * needs to be redone from scratch.
	 *
	 * This test affects amd64 releng machines causing tests to hang or
	 * fail. As there is little point to test interface that is still not,
	 * designed and implemented and is breaking tests - skip it
	 * unconditionally for all ports.
	 */
	/* Prevent static analysis from requiring t_exec_null to be __dead. */
	if (!caught) 
		atf_tc_skip("exect(3) misdesigned and hangs - PR port-amd64/51700");

	ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0);
	act.sa_sigaction = sigtrap_handler;
	act.sa_flags = SA_SIGINFO;

	ATF_REQUIRE(sigaction(SIGTRAP, &act, 0) == 0);

	ATF_REQUIRE_ERRNO(EFAULT, exect(NULL, NULL, NULL) == -1);

	ATF_REQUIRE_EQ_MSG(caught, 1, "expected caught (1) != received (%d)",
	    (int)caught);
}
Ejemplo n.º 13
0
ATF_TC_BODY(mmap__dev_zero_shared, tc)
{
	char *p1, *p2, *p3;
	size_t i;
	int fd;

	ATF_REQUIRE((fd = open("/dev/zero", O_RDWR)) >= 0);

	p1 = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
	    0);
	ATF_REQUIRE(p1 != MAP_FAILED);

	p2 = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
	    0);
	ATF_REQUIRE(p2 != MAP_FAILED);

	for (i = 0; i < getpagesize(); i++)
		ATF_REQUIRE_EQ_MSG(0, p1[i], "byte at p1[%zu] is %x", i, p1[i]);

	ATF_REQUIRE(memcmp(p1, p2, getpagesize()) == 0);

	p1[0] = 1;

	ATF_REQUIRE(p2[0] == 0);

	p2[0] = 2;

	ATF_REQUIRE(p1[0] == 1);

	p3 = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
	    0);
	ATF_REQUIRE(p3 != MAP_FAILED);

	ATF_REQUIRE(p3[0] == 0);
}
Ejemplo n.º 14
0
ATF_TC_BODY(isc_net_ntop, tc) {
	char buf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
	int r;
	size_t i;
	unsigned char abuf[16];
	struct {
		int		family;
		const char *	address;
	} testdata[] = {
		{ AF_INET, "0.0.0.0" },
		{ AF_INET, "0.1.0.0" },
		{ AF_INET, "0.0.2.0" },
		{ AF_INET, "0.0.0.3" },
		{ AF_INET, "255.255.255.255" },
		{ AF_INET6, "::" },
		{ AF_INET6, "::1.2.3.4" },
		{ AF_INET6, "::ffff:1.2.3.4" },
		{ AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" }
	};

	for (i = 0; i < sizeof(testdata)/sizeof(testdata[0]); i++) {
		r = inet_pton(testdata[i].family, testdata[i].address, abuf);
		ATF_REQUIRE_EQ_MSG(r, 1, "%s", testdata[i].address);
		isc_net_ntop(testdata[i].family, abuf, buf, sizeof(buf));
		ATF_CHECK_STREQ(buf, testdata[i].address);
	}
}
Ejemplo n.º 15
0
ATF_TC_BODY(simple, tc)
{
	int i;
	pthread_t self;

	mainthread = pthread_self();

	ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR);

	for (i = 0; i < NTHREAD; i++) {
		PTHREAD_REQUIRE(pthread_create(&th[i].id, NULL, f, &th[i]));
	}

	sched_yield();

	self = pthread_self();
	ATF_REQUIRE_EQ_MSG(self, mainthread, "thread id changed");

	for (i = 0; i < NTHREAD; i++) {
		PTHREAD_REQUIRE(pthread_kill(th[i].id, SIGUSR1));
	}

	for (i = 0; i < NTHREAD; i++) {
		PTHREAD_REQUIRE(pthread_join(th[i].id, NULL));
	}
}
Ejemplo n.º 16
0
/*
 * Run the test program. If the sig parameter is set to true, the test program
 * will deliver SIGUSR1 to itself during execution.
 */
static struct proc_handle *
start_prog(const struct atf_tc *tc, bool sig)
{
	char *argv[3];
	struct proc_handle *phdl;
	int error;

	asprintf(&argv[0], "%s/%s", atf_tc_get_config_var(tc, "srcdir"),
	    target_prog_file);
	ATF_REQUIRE(argv[0] != NULL);

	if (sig) {
		argv[1] = strdup("-s");
		argv[2] = NULL;
	} else {
		argv[1] = NULL;
	}

	error = proc_create(argv[0], argv, NULL, NULL, &phdl);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to run '%s'", target_prog_file);
	ATF_REQUIRE(phdl != NULL);

	free(argv[0]);
	free(argv[1]);

	return (phdl);
}
Ejemplo n.º 17
0
ATF_TC_BODY(ptm, tc)
{
	struct stat stm, sts;
	struct ptmget ptm;
	int fdm;
	struct group *gp;

	if ((fdm = open("/dev/ptm", O_RDWR)) == -1) {
		if (errno == ENOENT || errno == ENODEV)
			atf_tc_skip("/dev/ptm: %s", strerror(errno));
		atf_tc_fail("/dev/ptm: %s", strerror(errno));
	}

	REQUIRE_ERRNO(fstat(fdm, &stm), -1);
	ATF_REQUIRE_EQ(major(stm.st_rdev), 165);
	REQUIRE_ERRNO(ioctl(fdm, TIOCPTMGET, &ptm), -1);

	ATF_REQUIRE_MSG(strncmp(ptm.cn, "/dev/pty", 8) == 0
		|| strncmp(ptm.cn, "/dev/null", 9) == 0,
		"bad master name: %s", ptm.cn);

	ATF_REQUIRE_MSG(strncmp(ptm.sn, "/dev/tty", 8) == 0
		|| strncmp(ptm.sn, "/dev/pts/", 9) == 0,
		"bad slave name: %s", ptm.sn);

	if (strncmp(ptm.cn, "/dev/null", 9) != 0) {
		REQUIRE_ERRNO(fstat(ptm.cfd, &stm), -1);
		REQUIRE_ERRNO(stat(ptm.cn, &sts), -1);
		ATF_REQUIRE_EQ(stm.st_rdev, sts.st_rdev);
	}

	REQUIRE_ERRNO(fstat(ptm.sfd, &stm), -1);
	REQUIRE_ERRNO(stat(ptm.sn, &sts), -1);
	ATF_REQUIRE_EQ(stm.st_rdev, sts.st_rdev);

	ATF_REQUIRE_EQ_MSG(sts.st_uid, getuid(), "bad slave uid");

	ATF_REQUIRE_MSG((gp = getgrnam("tty")) != NULL,
	    "cannot find `tty' group");
	ATF_REQUIRE_EQ_MSG(sts.st_gid, gp->gr_gid, "bad slave grid");

	(void)close(ptm.sfd);
	(void)close(ptm.cfd);
	(void)close(fdm);
}
Ejemplo n.º 18
0
static void
set_bkpt(struct proc_handle *phdl, uintptr_t addr, u_long *saved)
{
	int error;

	error = proc_bkptset(phdl, addr, saved);
	ATF_REQUIRE_EQ_MSG(error, 0, "failed to set breakpoint at 0x%jx",
	    (uintmax_t)addr);
}
Ejemplo n.º 19
0
ATF_TC_BODY(ptmx, tc)
{
	struct stat stm, sts;
	char *pty;
	int fdm, fds;
	struct group *gp;

	if ((fdm = posix_openpt(O_RDWR|O_NOCTTY)) == -1) {
		if (errno == ENOENT || errno == ENODEV)
			atf_tc_skip("/dev/ptmx: %s", strerror(errno));

		atf_tc_fail("/dev/ptmx: %s", strerror(errno));
	}

	REQUIRE_ERRNO(fstat(fdm, &stm), -1);

#ifdef PTY_DEVNO_CHECK
	REQUIRE_ERRNO(stat("/dev/ptyp0", &sts), -1);

	ATF_REQUIRE_EQ_MSG(major(stm.st_rdev), major(sts.st_rdev),
		"bad master major number");
#endif

	REQUIRE_ERRNO(grantpt(fdm), -1);
	REQUIRE_ERRNO(unlockpt(fdm), -1);
	REQUIRE_ERRNO((pty = ptsname(fdm)), NULL);

	REQUIRE_ERRNO((fds = open(pty, O_RDWR|O_NOCTTY)), -1);
	REQUIRE_ERRNO(fstat(fds, &sts), -1);

#ifdef PTY_DEVNO_CHECK
	ATF_REQUIRE_EQ_MSG(minor(stm.st_rdev), minor(sts.st_rdev),
		"bad slave minor number");
#endif

	ATF_REQUIRE_EQ_MSG(sts.st_uid, getuid(), "bad slave uid");
	ATF_REQUIRE_MSG((gp = getgrnam("tty")) != NULL,
	    "cannot find `tty' group");

	ATF_REQUIRE_EQ_MSG(sts.st_gid, gp->gr_gid, "bad slave gid");
}
Ejemplo n.º 20
0
static void
h_wctomb(const struct test *t, char tc)
{
	wchar_t wcs[16 + 2];
	char buf[128];
	char cs[MB_LEN_MAX];
	const char *pcs;
	char *str;
	mbstate_t st;
	mbstate_t *stp = NULL;
	size_t sz, ret, i;

	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);

	(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
	(void)printf("Checking sequence: \"%s\"\n", buf);

	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
	(void)printf("Using locale: %s\n", str);

	if (tc == TC_WCRTOMB_ST) {
		(void)memset(&st, 0, sizeof(st));
		stp = &st;
	}

	wcs[t->wclen] = L'X'; /* poison */
	pcs = t->data;
	sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL);
	ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: "
		"%zu, expected: %zu", sz, t->wclen);
	ATF_REQUIRE_EQ(wcs[t->wclen], 0);

	for (i = 0; i < t->wclen + 1; i++) {
		if (tc == TC_WCTOMB)
			ret = wctomb(cs, wcs[i]);
		else
			ret = wcrtomb(cs, wcs[i], stp);

		if (ret == t->mblen[i])
			continue;

		(void)printf("At position %zd:\n", i);
		(void)printf("  expected: %zd\n", t->mblen[i]);
		(void)printf("  got     : %zd\n", ret);
		atf_tc_fail("Test failed");
		/* NOTREACHED */
	}

	(void)printf("Ok.\n");
}
Ejemplo n.º 21
0
ATF_TC_BODY(aligned_alloc_basic, tc)
{
	static const size_t size[] = {
		1, 2, 3, 4, 10, 100, 16384, 32768, 65536, 10000, 0
	};
	static const size_t align[] = {
		512, 1024, 16, 32, 64, 4, 2048, 16, 2, 2048, 0
	};

	size_t i;
	void *p;

	for (i = 0; i < __arraycount(size); i++) {
		(void)printf("Checking aligned_alloc(%zu, %zu)...\n",
		    align[i], size[i]);
		p = aligned_alloc(align[i], size[i]);
                if (p == NULL) {
			if (align[i] == 0 || ((align[i] - 1) & align[i]) != 0 ||
			    size[i] % align[i] != 0) {
				ATF_REQUIRE_EQ_MSG(errno, EINVAL,
				    "aligned_alloc: %s", strerror(errno));
			}
			else {
				ATF_REQUIRE_EQ_MSG(errno, ENOMEM,
				    "aligned_alloc: %s", strerror(errno));
			}
                }
		else {
			ATF_REQUIRE_EQ_MSG(align[i] == 0, false,
			    "aligned_alloc: success when alignment was not "
			    "a power of 2");
			ATF_REQUIRE_EQ_MSG((align[i] - 1) & align[i], 0,
			    "aligned_alloc: success when alignment was not "
			    "a power of 2");
#ifdef __NetBSD__
			/*
			 * NetBSD-specific invariant
			 *
			 * From aligned_alloc(3) on FreeBSD:
			 *
			 * Behavior is undefined if size is not an integral
			 * multiple of alignment.
			 */
			ATF_REQUIRE_EQ_MSG(size[i] % align[i], 0,
			    "aligned_alloc: success when size was not an "
			    "integer multiple of alignment");
#endif
			ATF_REQUIRE_EQ_MSG(((intptr_t)p) & (intptr_t)(align[i] - 1), 0,
			    "p = %p", p);
			free(p);
		}
	}
}
Ejemplo n.º 22
0
ATF_TC_BODY(getopt, tc)
{
    /* Provide an option that is unknown to the test program driver and
     * one that is, together with an argument that would be swallowed by
     * the test program option if it were recognized. */
    int argc = 4;
    char arg1[] = "progname";
    char arg2[] = "-Z";
    char arg3[] = "-s";
    char arg4[] = "foo";
    char *const argv[] = { arg1, arg2, arg3, arg4, NULL };

    int ch;
    bool zflag;

    /* Given that this obviously is a test program, and that we used the
     * same driver to start, we can test getopt(3) right here without doing
     * any fancy stuff. */
    zflag = false;
    while ((ch = getopt(argc, argv, ":Z")) != -1) {
        switch (ch) {
        case 'Z':
            zflag = true;
            break;

        case '?':
        default:
            if (optopt != 's')
                atf_tc_fail("Unexpected unknown option -%c found", optopt);
        }
    }

    ATF_REQUIRE(zflag);
    ATF_REQUIRE_EQ_MSG(1, argc - optind, "Invalid number of arguments left "
        "after the call to getopt(3)");
    ATF_CHECK_STREQ_MSG("foo", argv[optind], "The non-option argument is "
        "invalid");
}
Ejemplo n.º 23
0
ATF_TC_BODY(mmap__dev_zero_private, tc)
{
	char *p1, *p2, *p3;
	size_t i;
	int fd, pagesize;

	ATF_REQUIRE((pagesize = getpagesize()) > 0);
	ATF_REQUIRE((fd = open("/dev/zero", O_RDONLY)) >= 0);

	p1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	ATF_REQUIRE(p1 != MAP_FAILED);

	p2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	ATF_REQUIRE(p2 != MAP_FAILED);

	for (i = 0; i < pagesize; i++)
		ATF_REQUIRE_EQ_MSG(0, p1[i], "byte at p1[%zu] is %x", i, p1[i]);

	ATF_REQUIRE(memcmp(p1, p2, pagesize) == 0);

	p1[0] = 1;

	ATF_REQUIRE(p2[0] == 0);

	p2[0] = 2;

	ATF_REQUIRE(p1[0] == 1);

	p3 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	ATF_REQUIRE(p3 != MAP_FAILED);

	ATF_REQUIRE(p3[0] == 0);

	munmap(p1, pagesize);
	munmap(p2, pagesize);
	munmap(p3, pagesize);
	close(fd);
}
Ejemplo n.º 24
0
ATF_TC_BODY(kqueue_desc_passing, tc)
{
	pid_t child;
	int s[2], storage, status, kq;
	struct cmsghdr *msg;
	struct iovec iov;
	struct msghdr m;
	struct kevent ev;

	ATF_REQUIRE((kq = kqueue()) != -1);

	// atf_tc_skip("crashes kernel (PR 46463)");

	ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
	msg = malloc(CMSG_SPACE(sizeof(int)));
	m.msg_iov = &iov;
	m.msg_iovlen = 1;
	m.msg_name = NULL;
	m.msg_namelen = 0;
	m.msg_control = msg;
	m.msg_controllen = CMSG_SPACE(sizeof(int));

	child = fork();
	if (child == 0) {
		close(s[0]);

		iov.iov_base = &storage;
		iov.iov_len = sizeof(int);
		m.msg_iov = &iov;
		m.msg_iovlen = 1;

		if (recvmsg(s[1], &m, 0) == -1)
			err(1, "child: could not recvmsg");

#ifdef __FreeBSD__
		bcopy(CMSG_DATA(msg), &kq, sizeof(kq));
		printf("child (pid %d): received kq fd %d\n", getpid(), kq);
		_exit(0);
#else
		kq = *(int *)CMSG_DATA(msg);
		printf("child (pid %d): received kq fd %d\n", getpid(), kq);
		exit(0);
#endif
	}

	close(s[1]);

	iov.iov_base = &storage;
	iov.iov_len = sizeof(int);

	msg->cmsg_level = SOL_SOCKET;
	msg->cmsg_type = SCM_RIGHTS;
	msg->cmsg_len = CMSG_LEN(sizeof(int));

#ifdef __FreeBSD__
	/* 
	 * What is should have been
	 *   bcopy(&s[0], CMSG_DATA(msg), sizeof(kq));
	 */
	bcopy(&kq, CMSG_DATA(msg), sizeof(kq));
#else
	*(int *)CMSG_DATA(msg) = kq;
#endif

	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
	ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);

	printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
	if (sendmsg(s[0], &m, 0) == -1) {
#ifdef __NetBSD__
		ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
		atf_tc_skip("PR kern/46523");
#endif
#ifdef __FreeBSD__
		ATF_REQUIRE_EQ_MSG(errno, EOPNOTSUPP, "errno is %d", errno);
		close(s[0]);
#endif
	}

	close(kq);

	waitpid(child, &status, 0);
	ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
}
Ejemplo n.º 25
0
static void
h_ctype2(const struct test *t, bool use_mbstate)
{
	mbstate_t *stp;
	mbstate_t st;
	char buf[SIZE];
	char *str;
	size_t n;

	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
#if defined(__NetBSD__)
	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
#else
	if (setlocale(LC_CTYPE, t->locale) == NULL) {
		fprintf(stderr, "Locale %s not found.\n", t->locale);
		return;
	}
#endif

	(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
	(void)printf("Checking string: \"%s\"\n", buf);

	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
	(void)printf("Using locale: %s\n", str);

	(void)printf("Using mbstate: %s\n", use_mbstate ? "yes" : "no");

	(void)memset(&st, 0, sizeof(st));
//	mbrtowc(0, 0, 0, &st); /* XXX for ISO2022-JP */
	stp = use_mbstate ? &st : 0;

	for (n = 9; n > 0; n--) {
		const char *src = t->data;
		wchar_t dst;
		size_t nchar = 0;
		int width = 0;

		ATF_REQUIRE(mbsinit(stp) != 0);

		for (;;) {
			size_t rv = mbrtowc(&dst, src, n, stp);

			if (rv == 0)
				break;

			if (rv == (size_t)-2) {
				src += n;
				width += n;

				continue;
			}
			if (rv == (size_t)-1) {
				ATF_REQUIRE_EQ(errno, EILSEQ);
				atf_tc_fail("Invalid sequence");
				/* NOTREACHED */
			}

			width += rv;
			src += rv;

			if (dst != t->wchars[nchar] ||
			    width != t->widths[nchar]) {
				(void)printf("At position %zd:\n", nchar);
				(void)printf("  expected: 0x%04X (%u)\n",
					t->wchars[nchar], t->widths[nchar]);
				(void)printf("  got     : 0x%04X (%u)\n",
					dst, width);
				atf_tc_fail("Test failed");
			}

			nchar++;
			width = 0;
		}

		ATF_REQUIRE_EQ_MSG(dst, 0, "Incorrect terminating character: "
			"0x%04X (expected: 0x00)", dst);

		ATF_REQUIRE_EQ_MSG(nchar, t->length, "Incorrect length: "
			"%zd (expected: %zd)", nchar, t->length);
	}

	{
		wchar_t wbuf[SIZE];
		size_t rv;
		char const *src = t->data;
		int i;

		(void)memset(wbuf, 0xFF, sizeof(wbuf));

		rv = mbsrtowcs(wbuf, &src, SIZE, stp);

		ATF_REQUIRE_EQ_MSG(rv, t->length, "Incorrect length: %zd "
			"(expected: %zd)", rv, t->length);
		ATF_REQUIRE_EQ(src, NULL);

		for (i = 0; wbuf[i] != 0; ++i) {
			if (wbuf[i] == t->wchars[i])
				continue;

			(void)printf("At position %d:\n", i);
			(void)printf("  expected: 0x%04X\n", t->wchars[i]);
			(void)printf("  got     : 0x%04X\n", wbuf[i]);
			atf_tc_fail("Test failed");
		}

		ATF_REQUIRE_EQ_MSG((size_t)i, t->length, "Incorrect length: "
			"%d (expected: %zd)", i, t->length);
	}

	(void)printf("Ok.\n");
}