Sat Nov 5 07:45:41 2011 UTC ()
Add 21 unit tests for the XSI message queues.


(jruoho)
diff -r1.414 -r1.415 src/distrib/sets/lists/tests/mi
diff -r1.17 -r1.18 src/tests/lib/libc/sys/Makefile
diff -r0 -r1.1 src/tests/lib/libc/sys/t_msgctl.c
diff -r0 -r1.1 src/tests/lib/libc/sys/t_msgget.c
diff -r0 -r1.1 src/tests/lib/libc/sys/t_msgrcv.c
diff -r0 -r1.1 src/tests/lib/libc/sys/t_msgsnd.c

cvs diff -r1.414 -r1.415 src/distrib/sets/lists/tests/mi (expand / switch to unified diff)

--- src/distrib/sets/lists/tests/mi 2011/11/04 02:25:31 1.414
+++ src/distrib/sets/lists/tests/mi 2011/11/05 07:45:40 1.415
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: mi,v 1.414 2011/11/04 02:25:31 christos Exp $ 1# $NetBSD: mi,v 1.415 2011/11/05 07:45:40 jruoho Exp $
2# 2#
3# Note: don't delete entries from here - mark them as "obsolete" instead. 3# Note: don't delete entries from here - mark them as "obsolete" instead.
4# 4#
5./etc/mtree/set.tests tests-sys-root 5./etc/mtree/set.tests tests-sys-root
6./usr/libdata/debug/usr/tests tests-base-debug 6./usr/libdata/debug/usr/tests tests-base-debug
7./usr/libdata/debug/usr/tests/atf tests-atf-debug 7./usr/libdata/debug/usr/tests/atf tests-atf-debug
8./usr/libdata/debug/usr/tests/atf/atf-c tests-atf-debug 8./usr/libdata/debug/usr/tests/atf/atf-c tests-atf-debug
9./usr/libdata/debug/usr/tests/atf/atf-c/atf_c_test.debug tests-atf-debug debug,atf 9./usr/libdata/debug/usr/tests/atf/atf-c/atf_c_test.debug tests-atf-debug debug,atf
10./usr/libdata/debug/usr/tests/atf/atf-c/build_test.debug tests-atf-debug debug,atf 10./usr/libdata/debug/usr/tests/atf/atf-c/build_test.debug tests-atf-debug debug,atf
11./usr/libdata/debug/usr/tests/atf/atf-c/check_test.debug tests-atf-debug debug,atf 11./usr/libdata/debug/usr/tests/atf/atf-c/check_test.debug tests-atf-debug debug,atf
12./usr/libdata/debug/usr/tests/atf/atf-c/config_test.debug tests-atf-debug debug,atf 12./usr/libdata/debug/usr/tests/atf/atf-c/config_test.debug tests-atf-debug debug,atf
13./usr/libdata/debug/usr/tests/atf/atf-c/detail tests-atf-debug 13./usr/libdata/debug/usr/tests/atf/atf-c/detail tests-atf-debug
14./usr/libdata/debug/usr/tests/atf/atf-c/detail/dynstr_test.debug tests-atf-debug debug,atf 14./usr/libdata/debug/usr/tests/atf/atf-c/detail/dynstr_test.debug tests-atf-debug debug,atf
@@ -501,26 +501,30 @@ @@ -501,26 +501,30 @@
501./usr/libdata/debug/usr/tests/lib/libc/sys/t_getpid.debug tests-lib-debug debug,atf 501./usr/libdata/debug/usr/tests/lib/libc/sys/t_getpid.debug tests-lib-debug debug,atf
502./usr/libdata/debug/usr/tests/lib/libc/sys/t_getrusage.debug tests-lib-debug debug,atf 502./usr/libdata/debug/usr/tests/lib/libc/sys/t_getrusage.debug tests-lib-debug debug,atf
503./usr/libdata/debug/usr/tests/lib/libc/sys/t_getsid.debug tests-lib-debug debug,atf 503./usr/libdata/debug/usr/tests/lib/libc/sys/t_getsid.debug tests-lib-debug debug,atf
504./usr/libdata/debug/usr/tests/lib/libc/sys/t_gettimeofday.debug tests-lib-debug debug,atf 504./usr/libdata/debug/usr/tests/lib/libc/sys/t_gettimeofday.debug tests-lib-debug debug,atf
505./usr/libdata/debug/usr/tests/lib/libc/sys/t_issetugid.debug tests-lib-debug debug,atf 505./usr/libdata/debug/usr/tests/lib/libc/sys/t_issetugid.debug tests-lib-debug debug,atf
506./usr/libdata/debug/usr/tests/lib/libc/sys/t_kill.debug tests-lib-debug debug,atf 506./usr/libdata/debug/usr/tests/lib/libc/sys/t_kill.debug tests-lib-debug debug,atf
507./usr/libdata/debug/usr/tests/lib/libc/sys/t_link.debug tests-lib-debug debug,atf 507./usr/libdata/debug/usr/tests/lib/libc/sys/t_link.debug tests-lib-debug debug,atf
508./usr/libdata/debug/usr/tests/lib/libc/sys/t_mincore.debug tests-lib-debug debug,atf 508./usr/libdata/debug/usr/tests/lib/libc/sys/t_mincore.debug tests-lib-debug debug,atf
509./usr/libdata/debug/usr/tests/lib/libc/sys/t_mkdir.debug tests-lib-debug debug,atf 509./usr/libdata/debug/usr/tests/lib/libc/sys/t_mkdir.debug tests-lib-debug debug,atf
510./usr/libdata/debug/usr/tests/lib/libc/sys/t_mkfifo.debug tests-lib-debug debug,atf 510./usr/libdata/debug/usr/tests/lib/libc/sys/t_mkfifo.debug tests-lib-debug debug,atf
511./usr/libdata/debug/usr/tests/lib/libc/sys/t_mknod.debug tests-lib-debug debug,atf 511./usr/libdata/debug/usr/tests/lib/libc/sys/t_mknod.debug tests-lib-debug debug,atf
512./usr/libdata/debug/usr/tests/lib/libc/sys/t_mmap.debug tests-lib-debug debug,atf 512./usr/libdata/debug/usr/tests/lib/libc/sys/t_mmap.debug tests-lib-debug debug,atf
513./usr/libdata/debug/usr/tests/lib/libc/sys/t_mprotect.debug tests-lib-debug debug,atf 513./usr/libdata/debug/usr/tests/lib/libc/sys/t_mprotect.debug tests-lib-debug debug,atf
 514./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgctl.debug tests-lib-debug debug,atf
 515./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgget.debug tests-lib-debug debug,atf
 516./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgrcv.debug tests-lib-debug debug,atf
 517./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgsnd.debug tests-lib-debug debug,atf
514./usr/libdata/debug/usr/tests/lib/libc/sys/t_msync.debug tests-lib-debug debug,atf 518./usr/libdata/debug/usr/tests/lib/libc/sys/t_msync.debug tests-lib-debug debug,atf
515./usr/libdata/debug/usr/tests/lib/libc/sys/t_nanosleep.debug tests-lib-debug debug,atf 519./usr/libdata/debug/usr/tests/lib/libc/sys/t_nanosleep.debug tests-lib-debug debug,atf
516./usr/libdata/debug/usr/tests/lib/libc/sys/t_pipe.debug tests-lib-debug debug,atf 520./usr/libdata/debug/usr/tests/lib/libc/sys/t_pipe.debug tests-lib-debug debug,atf
517./usr/libdata/debug/usr/tests/lib/libc/sys/t_pipe2.debug tests-lib-debug debug,atf 521./usr/libdata/debug/usr/tests/lib/libc/sys/t_pipe2.debug tests-lib-debug debug,atf
518./usr/libdata/debug/usr/tests/lib/libc/sys/t_poll.debug tests-lib-debug debug,atf 522./usr/libdata/debug/usr/tests/lib/libc/sys/t_poll.debug tests-lib-debug debug,atf
519./usr/libdata/debug/usr/tests/lib/libc/sys/t_posix_fadvise.debug tests-lib-debug debug,atf 523./usr/libdata/debug/usr/tests/lib/libc/sys/t_posix_fadvise.debug tests-lib-debug debug,atf
520./usr/libdata/debug/usr/tests/lib/libc/sys/t_revoke.debug tests-lib-debug debug,atf 524./usr/libdata/debug/usr/tests/lib/libc/sys/t_revoke.debug tests-lib-debug debug,atf
521./usr/libdata/debug/usr/tests/lib/libc/sys/t_select.debug tests-lib-debug debug,atf 525./usr/libdata/debug/usr/tests/lib/libc/sys/t_select.debug tests-lib-debug debug,atf
522./usr/libdata/debug/usr/tests/lib/libc/sys/t_setrlimit.debug tests-lib-debug debug,atf 526./usr/libdata/debug/usr/tests/lib/libc/sys/t_setrlimit.debug tests-lib-debug debug,atf
523./usr/libdata/debug/usr/tests/lib/libc/sys/t_setuid.debug tests-lib-debug debug,atf 527./usr/libdata/debug/usr/tests/lib/libc/sys/t_setuid.debug tests-lib-debug debug,atf
524./usr/libdata/debug/usr/tests/lib/libc/sys/t_sigaction.debug tests-lib-debug debug,atf 528./usr/libdata/debug/usr/tests/lib/libc/sys/t_sigaction.debug tests-lib-debug debug,atf
525./usr/libdata/debug/usr/tests/lib/libc/sys/t_sigqueue.debug tests-lib-debug debug,atf 529./usr/libdata/debug/usr/tests/lib/libc/sys/t_sigqueue.debug tests-lib-debug debug,atf
526./usr/libdata/debug/usr/tests/lib/libc/sys/t_stat.debug tests-lib-debug debug,atf 530./usr/libdata/debug/usr/tests/lib/libc/sys/t_stat.debug tests-lib-debug debug,atf
@@ -2127,26 +2131,30 @@ @@ -2127,26 +2131,30 @@
2127./usr/tests/lib/libc/sys/t_getpid tests-lib-tests atf 2131./usr/tests/lib/libc/sys/t_getpid tests-lib-tests atf
2128./usr/tests/lib/libc/sys/t_getrusage tests-lib-tests atf 2132./usr/tests/lib/libc/sys/t_getrusage tests-lib-tests atf
2129./usr/tests/lib/libc/sys/t_getsid tests-lib-tests atf 2133./usr/tests/lib/libc/sys/t_getsid tests-lib-tests atf
2130./usr/tests/lib/libc/sys/t_gettimeofday tests-lib-tests atf 2134./usr/tests/lib/libc/sys/t_gettimeofday tests-lib-tests atf
2131./usr/tests/lib/libc/sys/t_issetugid tests-lib-tests atf 2135./usr/tests/lib/libc/sys/t_issetugid tests-lib-tests atf
2132./usr/tests/lib/libc/sys/t_kill tests-lib-tests atf 2136./usr/tests/lib/libc/sys/t_kill tests-lib-tests atf
2133./usr/tests/lib/libc/sys/t_link tests-lib-tests atf 2137./usr/tests/lib/libc/sys/t_link tests-lib-tests atf
2134./usr/tests/lib/libc/sys/t_mincore tests-lib-tests atf 2138./usr/tests/lib/libc/sys/t_mincore tests-lib-tests atf
2135./usr/tests/lib/libc/sys/t_mkdir tests-lib-tests atf 2139./usr/tests/lib/libc/sys/t_mkdir tests-lib-tests atf
2136./usr/tests/lib/libc/sys/t_mkfifo tests-lib-tests atf 2140./usr/tests/lib/libc/sys/t_mkfifo tests-lib-tests atf
2137./usr/tests/lib/libc/sys/t_mknod tests-lib-tests atf 2141./usr/tests/lib/libc/sys/t_mknod tests-lib-tests atf
2138./usr/tests/lib/libc/sys/t_mmap tests-lib-tests atf 2142./usr/tests/lib/libc/sys/t_mmap tests-lib-tests atf
2139./usr/tests/lib/libc/sys/t_mprotect tests-lib-tests atf 2143./usr/tests/lib/libc/sys/t_mprotect tests-lib-tests atf
 2144./usr/tests/lib/libc/sys/t_msgctl tests-lib-tests atf
 2145./usr/tests/lib/libc/sys/t_msgget tests-lib-tests atf
 2146./usr/tests/lib/libc/sys/t_msgrcv tests-lib-tests atf
 2147./usr/tests/lib/libc/sys/t_msgsnd tests-lib-tests atf
2140./usr/tests/lib/libc/sys/t_msync tests-lib-tests atf 2148./usr/tests/lib/libc/sys/t_msync tests-lib-tests atf
2141./usr/tests/lib/libc/sys/t_nanosleep tests-lib-tests atf 2149./usr/tests/lib/libc/sys/t_nanosleep tests-lib-tests atf
2142./usr/tests/lib/libc/sys/t_pipe tests-lib-tests atf 2150./usr/tests/lib/libc/sys/t_pipe tests-lib-tests atf
2143./usr/tests/lib/libc/sys/t_pipe2 tests-lib-tests atf 2151./usr/tests/lib/libc/sys/t_pipe2 tests-lib-tests atf
2144./usr/tests/lib/libc/sys/t_poll tests-lib-tests atf 2152./usr/tests/lib/libc/sys/t_poll tests-lib-tests atf
2145./usr/tests/lib/libc/sys/t_posix_fadvise tests-lib-tests atf 2153./usr/tests/lib/libc/sys/t_posix_fadvise tests-lib-tests atf
2146./usr/tests/lib/libc/sys/t_revoke tests-lib-tests atf 2154./usr/tests/lib/libc/sys/t_revoke tests-lib-tests atf
2147./usr/tests/lib/libc/sys/t_select tests-lib-tests atf 2155./usr/tests/lib/libc/sys/t_select tests-lib-tests atf
2148./usr/tests/lib/libc/sys/t_setrlimit tests-lib-tests atf 2156./usr/tests/lib/libc/sys/t_setrlimit tests-lib-tests atf
2149./usr/tests/lib/libc/sys/t_setuid tests-lib-tests atf 2157./usr/tests/lib/libc/sys/t_setuid tests-lib-tests atf
2150./usr/tests/lib/libc/sys/t_sigaction tests-lib-tests atf 2158./usr/tests/lib/libc/sys/t_sigaction tests-lib-tests atf
2151./usr/tests/lib/libc/sys/t_sigqueue tests-lib-tests atf 2159./usr/tests/lib/libc/sys/t_sigqueue tests-lib-tests atf
2152./usr/tests/lib/libc/sys/t_stat tests-lib-tests atf 2160./usr/tests/lib/libc/sys/t_stat tests-lib-tests atf

cvs diff -r1.17 -r1.18 src/tests/lib/libc/sys/Makefile (expand / switch to unified diff)

--- src/tests/lib/libc/sys/Makefile 2011/10/31 15:41:31 1.17
+++ src/tests/lib/libc/sys/Makefile 2011/11/05 07:45:41 1.18
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: Makefile,v 1.17 2011/10/31 15:41:31 christos Exp $ 1# $NetBSD: Makefile,v 1.18 2011/11/05 07:45:41 jruoho Exp $
2 2
3MKMAN= no 3MKMAN= no
4 4
5.include <bsd.own.mk> 5.include <bsd.own.mk>
6 6
7.include "../arch/Makefile.exec_prot" 7.include "../arch/Makefile.exec_prot"
8 8
9TESTSDIR= ${TESTSBASE}/lib/libc/sys 9TESTSDIR= ${TESTSBASE}/lib/libc/sys
10 10
11TESTS_C+= t_access 11TESTS_C+= t_access
12TESTS_C+= t_chroot 12TESTS_C+= t_chroot
13TESTS_C+= t_clock_gettime 13TESTS_C+= t_clock_gettime
14TESTS_C+= t_clone 14TESTS_C+= t_clone
@@ -21,26 +21,30 @@ TESTS_C+= t_getlogin @@ -21,26 +21,30 @@ TESTS_C+= t_getlogin
21TESTS_C+= t_getpid 21TESTS_C+= t_getpid
22TESTS_C+= t_getrusage 22TESTS_C+= t_getrusage
23TESTS_C+= t_getsid 23TESTS_C+= t_getsid
24TESTS_C+= t_gettimeofday 24TESTS_C+= t_gettimeofday
25TESTS_C+= t_issetugid 25TESTS_C+= t_issetugid
26TESTS_C+= t_kill 26TESTS_C+= t_kill
27TESTS_C+= t_link 27TESTS_C+= t_link
28TESTS_C+= t_mincore 28TESTS_C+= t_mincore
29TESTS_C+= t_mkdir 29TESTS_C+= t_mkdir
30TESTS_C+= t_mkfifo 30TESTS_C+= t_mkfifo
31TESTS_C+= t_mknod 31TESTS_C+= t_mknod
32TESTS_C+= t_mmap 32TESTS_C+= t_mmap
33TESTS_C+= t_mprotect 33TESTS_C+= t_mprotect
 34TESTS_C+= t_msgctl
 35TESTS_C+= t_msgget
 36TESTS_C+= t_msgrcv
 37TESTS_C+= t_msgsnd
34TESTS_C+= t_msync 38TESTS_C+= t_msync
35TESTS_C+= t_nanosleep 39TESTS_C+= t_nanosleep
36TESTS_C+= t_pipe 40TESTS_C+= t_pipe
37TESTS_C+= t_pipe2 41TESTS_C+= t_pipe2
38TESTS_C+= t_posix_fadvise 42TESTS_C+= t_posix_fadvise
39TESTS_C+= t_poll 43TESTS_C+= t_poll
40TESTS_C+= t_revoke 44TESTS_C+= t_revoke
41TESTS_C+= t_select 45TESTS_C+= t_select
42TESTS_C+= t_setrlimit 46TESTS_C+= t_setrlimit
43TESTS_C+= t_setuid 47TESTS_C+= t_setuid
44TESTS_C+= t_sigaction 48TESTS_C+= t_sigaction
45TESTS_C+= t_sigqueue 49TESTS_C+= t_sigqueue
46TESTS_C+= t_stat 50TESTS_C+= t_stat

File Added: src/tests/lib/libc/sys/t_msgctl.c
/* $NetBSD: t_msgctl.c,v 1.1 2011/11/05 07:45:41 jruoho 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_msgctl.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");

#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/wait.h>

#include <atf-c.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#define MSG_KEY		12345689
#define MSG_MTYPE_1	0x41

struct msg {
	long		 mtype;
	char		 buf[3];
};

static void		clean(void);

static void
clean(void)
{
	int id;

	if ((id = msgget(MSG_KEY, 0)) != -1)
		(void)msgctl(id, IPC_RMID, 0);
}

ATF_TC_WITH_CLEANUP(msgctl_err);
ATF_TC_HEAD(msgctl_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from msgctl(2)");
}

ATF_TC_BODY(msgctl_err, tc)
{
	const int cmd[] = { IPC_STAT, IPC_SET, IPC_RMID };
	struct msqid_ds msgds;
	size_t i;
	int id;

	(void)memset(&msgds, 0, sizeof(struct msqid_ds));

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, msgctl(id, INT_MAX, &msgds) == -1);

	errno = 0;
	ATF_REQUIRE_ERRNO(EFAULT, msgctl(id, IPC_STAT, (void *)-1) == -1);

	for (i = 0; i < __arraycount(cmd); i++) {
		errno = 0;
		ATF_REQUIRE_ERRNO(EINVAL, msgctl(-1, cmd[i], &msgds) == -1);
	}

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgctl_err, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgctl_perm);
ATF_TC_HEAD(msgctl_perm, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test permissiosn with msgctl(2)");
	atf_tc_set_md_var(tc, "require.user", "root");
}

ATF_TC_BODY(msgctl_perm, tc)
{
	struct msqid_ds msgds;
	struct passwd *pw;
	pid_t pid;
	int sta;
	int id;

	(void)memset(&msgds, 0, sizeof(struct msqid_ds));

	pw = getpwnam("nobody");
	id = msgget(MSG_KEY, IPC_CREAT | 0600);

	ATF_REQUIRE(id != -1);
	ATF_REQUIRE(pw != NULL);
	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		if (setuid(pw->pw_uid) != 0)
			_exit(EX_OSERR);

		msgds.msg_perm.uid = getuid();
		msgds.msg_perm.gid = getgid();

		errno = 0;

		if (msgctl(id, IPC_SET, &msgds) == 0)
			_exit(EXIT_FAILURE);

		if (errno != EPERM)
			_exit(EXIT_FAILURE);

		(void)memset(&msgds, 0, sizeof(struct msqid_ds));

		if (msgctl(id, IPC_STAT, &msgds) != 0)
			_exit(EX_OSERR);

		msgds.msg_qbytes = 1;

		if (msgctl(id, IPC_SET, &msgds) == 0)
			_exit(EXIT_FAILURE);

		if (errno != EPERM)
			_exit(EXIT_FAILURE);

		_exit(EXIT_SUCCESS);
	}

	(void)wait(&sta);

	if (WIFEXITED(sta) == 0) {

		if (WEXITSTATUS(sta) == EX_OSERR)
			atf_tc_fail("system call failed");

		if (WEXITSTATUS(sta) == EXIT_FAILURE)
			atf_tc_fail("UID %u manipulated root's "
			    "message queue", pw->pw_uid);
	}

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgctl_perm, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgctl_pid);
ATF_TC_HEAD(msgctl_pid, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test that PIDs are updated");
}

ATF_TC_BODY(msgctl_pid, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct msqid_ds msgds;
	int id, sta;
	pid_t pid;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		(void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);

		_exit(EXIT_SUCCESS);
	}

	(void)sleep(1);
	(void)wait(&sta);
	(void)memset(&msgds, 0, sizeof(struct msqid_ds));

	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);

	if (pid != msgds.msg_lspid)
		atf_tc_fail("the PID of last msgsnd(2) was not updated");

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		(void)msgrcv(id, &msg,
		    sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);

		_exit(EXIT_SUCCESS);
	}

	(void)sleep(1);
	(void)wait(&sta);
	(void)memset(&msgds, 0, sizeof(struct msqid_ds));

	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);

	if (pid != msgds.msg_lrpid)
		atf_tc_fail("the PID of last msgrcv(2) was not updated");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgctl_pid, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgctl_set);
ATF_TC_HEAD(msgctl_set, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgctl(2) with IPC_SET");
	atf_tc_set_md_var(tc, "require.user", "root");
}

ATF_TC_BODY(msgctl_set, tc)
{
	struct msqid_ds msgds;
	struct passwd *pw;
	int id;

	(void)memset(&msgds, 0, sizeof(struct msqid_ds));

	pw = getpwnam("nobody");
	id = msgget(MSG_KEY, IPC_CREAT | 0600);

	ATF_REQUIRE(id != -1);
	ATF_REQUIRE(pw != NULL);
	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);

	msgds.msg_perm.uid = pw->pw_uid;

	if (msgctl(id, IPC_SET, &msgds) != 0)
		atf_tc_fail("root failed to change the UID of message queue");

	msgds.msg_perm.uid = getuid();
	msgds.msg_perm.gid = pw->pw_gid;

	if (msgctl(id, IPC_SET, &msgds) != 0)
		atf_tc_fail("root failed to change the GID of message queue");

	/*
	 * Note setting the qbytes to zero fails even as root.
	 */
	msgds.msg_qbytes = 1;
	msgds.msg_perm.gid = getgid();

	if (msgctl(id, IPC_SET, &msgds) != 0)
		atf_tc_fail("root failed to change qbytes of message queue");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgctl_set, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgctl_time);
ATF_TC_HEAD(msgctl_time, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgctl(2) updates times");
}

ATF_TC_BODY(msgctl_time, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct msqid_ds msgds;
	time_t t;
	int id;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	t = time(NULL);

	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
	(void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
	(void)msgctl(id, IPC_STAT, &msgds);

	if (abs(t - msgds.msg_stime) > 1)
		atf_tc_fail("time of last msgsnd(2) was not updated");

	if (msgds.msg_rtime != 0)
		atf_tc_fail("time of last msgrcv(2) was updated incorrectly");

	t = time(NULL);

	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
	(void)msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
	(void)msgctl(id, IPC_STAT, &msgds);

	if (abs(t - msgds.msg_rtime) > 1)
		atf_tc_fail("time of last msgrcv(2) was not updated");

	/*
	 * Note that this is non-zero even after the memset(3).
	 */
	if (msgds.msg_stime == 0)
		atf_tc_fail("time of last msgsnd(2) was updated incorrectly");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgctl_time, tc)
{
	clean();
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, msgctl_err);
	ATF_TP_ADD_TC(tp, msgctl_perm);
	ATF_TP_ADD_TC(tp, msgctl_pid);
	ATF_TP_ADD_TC(tp, msgctl_set);
	ATF_TP_ADD_TC(tp, msgctl_time);

	return atf_no_error();
}

File Added: src/tests/lib/libc/sys/t_msgget.c
/* $NetBSD: t_msgget.c,v 1.1 2011/11/05 07:45:41 jruoho 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_msgget.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");

#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/wait.h>

#include <atf-c.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#define MSG_KEY		12345689

static void		clean(void);

static void
clean(void)
{
	int id;

	if ((id = msgget(MSG_KEY, 0)) != -1)
		(void)msgctl(id, IPC_RMID, 0);
}

ATF_TC_WITH_CLEANUP(msgget_excl);
ATF_TC_HEAD(msgget_excl, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgget(2) with IPC_EXCL");
}

ATF_TC_BODY(msgget_excl, tc)
{
	int id;

	/*
	 * Create a message queue and re-open it with
	 * O_CREAT and IPC_EXCL set. This should fail.
	 */
	id = msgget(MSG_KEY, IPC_CREAT | 0600);

	if (id < 0)
		atf_tc_fail("failed to create message queue");

	errno = 0;

	if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) != -1)
		atf_tc_fail("msgget(2) failed for IPC_EXCL");

	ATF_REQUIRE(errno == EEXIST);

	/*
	 * However, the same call should succeed
	 * when IPC_EXCL is not set in the flags.
	 */
	id = msgget(MSG_KEY, IPC_CREAT | 0600);

	if (id < 0)
		atf_tc_fail("msgget(2) failed to re-open");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgget_excl, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgget_exit);
ATF_TC_HEAD(msgget_exit, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test that XSI message queues are "
	    "not removed when the process exits");
}

ATF_TC_BODY(msgget_exit, tc)
{
	int id, sta;
	pid_t pid;

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) == -1)
			_exit(EXIT_FAILURE);

		_exit(EXIT_SUCCESS);
	}

	(void)wait(&sta);

	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
		atf_tc_fail("failed to create message queue");

	id = msgget(MSG_KEY, 0);

	if (id == -1)
		atf_tc_fail("message queue was removed on process exit");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgget_exit, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgget_init);
ATF_TC_HEAD(msgget_init, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test that msgget(2) initializes data structures properly");
}

ATF_TC_BODY(msgget_init, tc)
{
	const uid_t uid = geteuid();
	const gid_t gid = getegid();
	struct msqid_ds msgds;
	time_t t;
	int id;

	(void)memset(&msgds, 0x9, sizeof(struct msqid_ds));

	t = time(NULL);
	id = msgget(MSG_KEY, IPC_CREAT | 0600);

	ATF_REQUIRE(id !=-1);
	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);

	ATF_CHECK(msgds.msg_qnum == 0);
	ATF_CHECK(msgds.msg_lspid == 0);
	ATF_CHECK(msgds.msg_lrpid == 0);
	ATF_CHECK(msgds.msg_rtime == 0);
	ATF_CHECK(msgds.msg_stime == 0);
	ATF_CHECK(msgds.msg_perm.uid == uid);
	ATF_CHECK(msgds.msg_perm.gid == gid);
	ATF_CHECK(msgds.msg_perm.cuid == uid);
	ATF_CHECK(msgds.msg_perm.cgid == gid);
	ATF_CHECK(msgds.msg_perm.mode == 0600);

	if (abs(t - msgds.msg_ctime) > 5)
		atf_tc_fail("msgget(2) initialized current time incorrectly");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgget_init, tc)
{
	clean();
}

ATF_TC(msgget_limit);
ATF_TC_HEAD(msgget_limit, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgget(2) against system limits");
}

ATF_TC_BODY(msgget_limit, tc)
{
	size_t len = sizeof(int);
	bool fail = false;
	int i, lim = 0;
	int *buf;

	if (sysctlbyname("kern.ipc.msgmni", &lim, &len, NULL, 0) != 0)
		atf_tc_skip("failed to read kern.ipc.msgmni sysctl");

	buf = calloc(lim + 1, sizeof(*buf));
	ATF_REQUIRE(buf != NULL);

	for (i = 0; i < lim; i++) {

		buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);

		(void)fprintf(stderr, "key[%d] = %d\n", i, buf[i]);

		/*
		 * This test only works when there are zero existing
		 * message queues. Thus, bypass the unit test when
		 * this precondition is not met, for reason or another.
		 */
		if (buf[i] == -1)
			goto out;
	}

	i++;
	errno = 0;

	buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);

	if (buf[i] != -1 || errno != ENOSPC)
		fail = true;

out:	/* Remember to clean-up. */
	for (i = 0; i < lim; i++)
		(void)msgctl(buf[i], IPC_RMID, 0);

	free(buf);

	if (fail != false)
		atf_tc_fail("msgget(2) opened more than %d queues", lim);
}

ATF_TC_WITH_CLEANUP(msgget_mode);
ATF_TC_HEAD(msgget_mode, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test different modes with msgget(2)");
	atf_tc_set_md_var(tc, "require.user", "root");
}

ATF_TC_BODY(msgget_mode, tc)
{
	static const mode_t mode[] = {
		S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP,
		S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH
	};

	struct msqid_ds msgds;
	size_t i;
	int id;

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

		(void)fprintf(stderr, "testing mode %d\n", mode[i]);
		(void)memset(&msgds, 0, sizeof(struct msqid_ds));

		id = msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | (int)mode[i]);

		ATF_REQUIRE(id != -1);
		ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
		ATF_REQUIRE(msgds.msg_perm.mode == mode[i]);
		ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
	}
}

ATF_TC_CLEANUP(msgget_mode, tc)
{
	clean();
}


ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, msgget_excl);
	ATF_TP_ADD_TC(tp, msgget_exit);
	ATF_TP_ADD_TC(tp, msgget_init);
	ATF_TP_ADD_TC(tp, msgget_limit);
	ATF_TP_ADD_TC(tp, msgget_mode);

	return atf_no_error();
}

File Added: src/tests/lib/libc/sys/t_msgrcv.c
/* $NetBSD: t_msgrcv.c,v 1.1 2011/11/05 07:45:41 jruoho 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_msgrcv.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");

#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/wait.h>

#include <atf-c.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#define MSG_KEY		1234
#define MSG_MTYPE_1	0x41
#define	MSG_MTYPE_2	0x42
#define MSG_MTYPE_3	0x43

struct msg {
	long		 mtype;
	char		 buf[3];
};

static void		clean(void);

static void
clean(void)
{
	int id;

	if ((id = msgget(MSG_KEY, 0)) != -1)
		(void)msgctl(id, IPC_RMID, 0);
}

ATF_TC_WITH_CLEANUP(msgrcv_basic);
ATF_TC_HEAD(msgrcv_basic, tc)
{
	atf_tc_set_md_var(tc, "descr", "A basic test of msgrcv(2)");
}

ATF_TC_BODY(msgrcv_basic, tc)
{
	struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct msg msg2 = { MSG_MTYPE_1, { 'x', 'y', 'z' } };
	int id;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	(void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT);
	(void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);

	ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
	ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
	ATF_CHECK(msg1.buf[2] == msg2.buf[2]);

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgrcv_basic, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgrcv_block);
ATF_TC_HEAD(msgrcv_block, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test that msgrcv(2) blocks");
}

ATF_TC_BODY(msgrcv_block, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	int id, sta;
	pid_t pid;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		if (msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, 0) < 0)
			_exit(EXIT_FAILURE);

		_exit(EXIT_SUCCESS);
	}

	/*
	 * Below msgsnd(2) should unblock the child,
	 * and hence kill(2) should fail with ESRCH.
	  */
	(void)sleep(1);
	(void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
	(void)sleep(1);
	(void)kill(pid, SIGKILL);
	(void)wait(&sta);

	if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
		atf_tc_fail("msgrcv(2) did not block");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgrcv_block, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgrcv_err);
ATF_TC_HEAD(msgrcv_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from msgrcv(2)");
}

ATF_TC_BODY(msgrcv_err, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	int id, r = 0;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	errno = 0;

	ATF_REQUIRE_ERRNO(ENOMSG, msgrcv(id, &msg,
		sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1);

	ATF_REQUIRE(msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0);

	errno = 0;

	ATF_REQUIRE_ERRNO(EFAULT, msgrcv(id, (void *)-1,
		sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1);

	errno = 0;

	ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
		sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1);

	errno = 0;

	ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
		SSIZE_MAX, MSG_MTYPE_1, IPC_NOWAIT) == -1);

	ATF_REQUIRE(msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0);

	errno = 0;

	ATF_REQUIRE_ERRNO(E2BIG, msgrcv(id, &r,
		sizeof(int), MSG_MTYPE_1, IPC_NOWAIT) == -1);

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgrcv_err, tc)
{
	clean();
}


ATF_TC_WITH_CLEANUP(msgrcv_mtype);
ATF_TC_HEAD(msgrcv_mtype, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test message types with msgrcv(2)");
}

ATF_TC_BODY(msgrcv_mtype, tc)
{
	struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct msg msg2 = { MSG_MTYPE_3, { 'x', 'y', 'z' } };
	int id;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	(void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT);
	(void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_2, IPC_NOWAIT);

	ATF_CHECK(msg1.buf[0] != msg2.buf[0]);	/* Different mtype. */
	ATF_CHECK(msg1.buf[1] != msg2.buf[1]);
	ATF_CHECK(msg1.buf[2] != msg2.buf[2]);

	(void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);

	ATF_CHECK(msg1.buf[0] == msg2.buf[0]);	/* Same mtype. */
	ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
	ATF_CHECK(msg1.buf[2] == msg2.buf[2]);

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgrcv_mtype, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgrcv_nonblock);
ATF_TC_HEAD(msgrcv_nonblock, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with IPC_NOWAIT");
	atf_tc_set_md_var(tc, "timeout", "10");
}

ATF_TC_BODY(msgrcv_nonblock, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	const ssize_t n = 10;
	int id, sta;
	ssize_t i;
	pid_t pid;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	for (i = 0; i < n; i++) {

		ATF_REQUIRE(msgsnd(id, &msg,
			sizeof(struct msg), IPC_NOWAIT) == 0);
	}

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		while (i != 0) {

			if (msgrcv(id, &msg, sizeof(struct msg),
				MSG_MTYPE_1, IPC_NOWAIT) == -1)
				_exit(EXIT_FAILURE);

			i--;
		}

		_exit(EXIT_SUCCESS);
	}

	(void)sleep(2);
	(void)kill(pid, SIGKILL);
	(void)wait(&sta);

	if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
		atf_tc_fail("msgrcv(2) blocked with IPC_NOWAIT");

	if (WIFEXITED(sta) == 0 && WEXITSTATUS(sta) != EXIT_SUCCESS)
		atf_tc_fail("msgrcv(2) failed");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgrcv_nonblock, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgrcv_truncate);
ATF_TC_HEAD(msgrcv_truncate, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with MSG_NOERROR");
}

ATF_TC_BODY(msgrcv_truncate, tc)
{
	struct msgsmall {
		long		 mtype;
		char		 buf[2];
	};

	struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct msgsmall msg2 = { MSG_MTYPE_1, { 'x', 'y' } };
	int id;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	(void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT);
	(void)msgrcv(id, &msg2, sizeof(struct msgsmall),
	    MSG_MTYPE_1, IPC_NOWAIT | MSG_NOERROR);

	ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
	ATF_CHECK(msg1.buf[1] == msg2.buf[1]);

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgrcv_truncate, tc)
{
	clean();
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, msgrcv_basic);
	ATF_TP_ADD_TC(tp, msgrcv_block);
	ATF_TP_ADD_TC(tp, msgrcv_err);
	ATF_TP_ADD_TC(tp, msgrcv_mtype);
	ATF_TP_ADD_TC(tp, msgrcv_nonblock);
	ATF_TP_ADD_TC(tp, msgrcv_truncate);

	return atf_no_error();
}

File Added: src/tests/lib/libc/sys/t_msgsnd.c
/* $NetBSD: t_msgsnd.c,v 1.1 2011/11/05 07:45:41 jruoho 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_msgsnd.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");

#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/wait.h>

#include <atf-c.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#define MSG_KEY		1234
#define MSG_MTYPE_1	0x41
#define	MSG_MTYPE_2	0x42
#define MSG_MTYPE_3	0x43

struct msg {
	long		 mtype;
	char		 buf[3];
};

static void		clean(void);

static void
clean(void)
{
	int id;

	if ((id = msgget(MSG_KEY, 0)) != -1)
		(void)msgctl(id, IPC_RMID, 0);
}

ATF_TC_WITH_CLEANUP(msgsnd_block);
ATF_TC_HEAD(msgsnd_block, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) blocks");
	atf_tc_set_md_var(tc, "timeout", "10");
}

ATF_TC_BODY(msgsnd_block, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	int id, sta;
	pid_t pid;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		/*
		 * Enqueue messages until some limit (e.g. the maximum
		 * number of messages in the queue or the maximum number
		 * of bytes in the queue) is reached. After this the call
		 * should block when the IPC_NOWAIT is not set.
		 */
		for (;;) {

			if (msgsnd(id, &msg, sizeof(struct msg), 0) < 0)
				_exit(EXIT_FAILURE);
		}
	}

	(void)sleep(2);
	(void)kill(pid, SIGKILL);
	(void)wait(&sta);

	if (WIFEXITED(sta) != 0 || WIFSIGNALED(sta) == 0)
		atf_tc_fail("msgsnd(2) did not block");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgsnd_block, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgsnd_count);
ATF_TC_HEAD(msgsnd_count, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test that msgsnd(2) increments the amount of "
	    "message in the queue, as given by msgctl(2)");
	atf_tc_set_md_var(tc, "timeout", "10");
}

ATF_TC_BODY(msgsnd_count, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct msqid_ds ds;
	size_t i = 0;
	int id, rv;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	for (;;) {

		errno = 0;
		rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);

		if (rv == 0) {
			i++;
			continue;
		}

		if (rv == -1 && errno == EAGAIN)
			break;

		atf_tc_fail("failed to enqueue a message");
	}

	(void)memset(&ds, 0, sizeof(struct msqid_ds));
	(void)msgctl(id, IPC_STAT, &ds);

	if (ds.msg_qnum != i)
		atf_tc_fail("incorrect message count");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgsnd_count, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgsnd_err);
ATF_TC_HEAD(msgsnd_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from msgsnd(2)");
}

ATF_TC_BODY(msgsnd_err, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	int id;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	errno = 0;

	ATF_REQUIRE_ERRNO(EFAULT, msgsnd(id, (void *)-1,
		sizeof(struct msg), IPC_NOWAIT) == -1);

	errno = 0;

	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
		sizeof(struct msg), IPC_NOWAIT) == -1);

	errno = 0;

	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
		SSIZE_MAX, IPC_NOWAIT) == -1);

	errno = 0;
	msg.mtype = 0;

	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(id, &msg,
		sizeof(struct msg), IPC_NOWAIT) == -1);

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgsnd_err, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgsnd_nonblock);
ATF_TC_HEAD(msgsnd_nonblock, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) with IPC_NOWAIT");
	atf_tc_set_md_var(tc, "timeout", "10");
}

ATF_TC_BODY(msgsnd_nonblock, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	int id, rv, sta;
	pid_t pid;

	id = msgget(MSG_KEY, IPC_CREAT | 0600);
	ATF_REQUIRE(id != -1);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		for (;;) {

			errno = 0;
			rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);

			if (rv == -1 && errno == EAGAIN)
				_exit(EXIT_SUCCESS);
		}
	}

	(void)sleep(2);
	(void)kill(pid, SIGKILL);
	(void)wait(&sta);

	if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
		atf_tc_fail("msgsnd(2) blocked with IPC_NOWAIT");

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgsnd_nonblock, tc)
{
	clean();
}

ATF_TC_WITH_CLEANUP(msgsnd_perm);
ATF_TC_HEAD(msgsnd_perm, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test permissions with msgsnd(2)");
	atf_tc_set_md_var(tc, "require.user", "root");
}

ATF_TC_BODY(msgsnd_perm, tc)
{
	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
	struct passwd *pw;
	int id, sta;
	pid_t pid;
	uid_t uid;

	pw = getpwnam("nobody");
	id = msgget(MSG_KEY, IPC_CREAT | 0600);

	ATF_REQUIRE(id != -1);
	ATF_REQUIRE(pw != NULL);

	uid = pw->pw_uid;
	ATF_REQUIRE(uid != 0);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {

		/*
		 * Try to enqueue a message to the queue
		 * created by root as RW for owner only.
		 */
		if (setuid(uid) != 0)
			_exit(EX_OSERR);

		id = msgget(MSG_KEY, 0);

		if (id == -1)
			_exit(EX_OSERR);

		errno = 0;

		if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0)
			_exit(EXIT_FAILURE);

		if (errno != EACCES)
			_exit(EXIT_FAILURE);

		_exit(EXIT_SUCCESS);
	}

	(void)wait(&sta);

	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {

		if (errno == EX_OSERR)
			atf_tc_fail("system call failed");

		atf_tc_fail("UID %u enqueued message to root's queue", uid);
	}

	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}

ATF_TC_CLEANUP(msgsnd_perm, tc)
{
	clean();
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, msgsnd_block);
	ATF_TP_ADD_TC(tp, msgsnd_count);
	ATF_TP_ADD_TC(tp, msgsnd_err);
	ATF_TP_ADD_TC(tp, msgsnd_nonblock);
	ATF_TP_ADD_TC(tp, msgsnd_perm);

	return atf_no_error();
}