tools/llvm: Do not build with symbols
[minix3.git] / tests / kernel / t_mqueue.c
blob1047eb736f2b520380d99ca388824f2b0d8385bc
1 /* $NetBSD: t_mqueue.c,v 1.3 2012/11/06 19:35:38 pgoyette Exp $ */
3 /*
4 * Test for POSIX message queue priority handling.
6 * This file is in the Public Domain.
7 */
9 #include <atf-c.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <unistd.h>
17 #include <mqueue.h>
19 char *tmpdir;
21 #define MQ_PRIO_BASE 24
23 static void
24 send_msgs(mqd_t mqfd)
26 char msg[2];
28 msg[1] = '\0';
30 msg[0] = 'a';
31 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
32 "mq_send 1 failed: %d", errno);
34 msg[0] = 'b';
35 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
36 "mq_send 2 failed: %d", errno);
38 msg[0] = 'c';
39 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
40 "mq_send 3 failed: %d", errno);
42 msg[0] = 'd';
43 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE - 1) != -1,
44 "mq_send 4 failed: %d", errno);
46 msg[0] = 'e';
47 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), 0) != -1,
48 "mq_send 5 failed: %d", errno);
50 msg[0] = 'f';
51 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
52 "mq_send 6 failed: %d", errno);
55 static void
56 receive_msgs(mqd_t mqfd)
58 struct mq_attr mqa;
59 char *m;
60 unsigned p;
61 int len;
63 ATF_REQUIRE_MSG(mq_getattr(mqfd, &mqa) != -1, "mq_getattr failed %d",
64 errno);
66 len = mqa.mq_msgsize;
67 m = calloc(1, len);
68 ATF_REQUIRE_MSG(m != NULL, "calloc failed");
70 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
71 "mq_receive 1 failed: %d", errno);
72 ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'b',
73 "mq_receive 1 prio/data mismatch");
75 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
76 "mq_receive 2 failed: %d", errno);
77 ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'f',
78 "mq_receive 2 prio/data mismatch");
80 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
81 "mq_receive 3 failed: %d", errno);
82 ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'a',
83 "mq_receive 3 prio/data mismatch");
85 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
86 "mq_receive 4 failed: %d", errno);
87 ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'c',
88 "mq_receive 4 prio/data mismatch");
90 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
91 "mq_receive 5 failed: %d", errno);
92 ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE - 1) && m[0] == 'd',
93 "mq_receive 5 prio/data mismatch");
95 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
96 "mq_receive 6 failed: %d", errno);
97 ATF_REQUIRE_MSG(p == 0 && m[0] == 'e',
98 "mq_receive 6 prio/data mismatch");
101 ATF_TC_WITH_CLEANUP(mqueue);
102 ATF_TC_HEAD(mqueue, tc)
105 atf_tc_set_md_var(tc, "timeout", "3");
106 atf_tc_set_md_var(tc, "descr", "Checks mqueue send/receive");
109 ATF_TC_BODY(mqueue, tc)
111 int status;
112 char template[32];
113 char mq_name[64];
115 strlcpy(template, "./t_mqueue.XXXXXX", sizeof(template));
116 tmpdir = mkdtemp(template);
117 ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed: %d", errno);
118 snprintf(mq_name, sizeof(mq_name), "%s/mq", tmpdir);
120 mqd_t mqfd;
122 mqfd = mq_open(mq_name, O_RDWR | O_CREAT,
123 S_IRUSR | S_IRWXG | S_IROTH, NULL);
124 ATF_REQUIRE_MSG(mqfd != -1, "mq_open failed: %d", errno);
126 send_msgs(mqfd);
127 receive_msgs(mqfd);
129 status = mq_close(mqfd);
130 ATF_REQUIRE_MSG(status == 0, "mq_close failed: %d", errno);
133 ATF_TC_CLEANUP(mqueue, tc)
135 int status;
137 if (tmpdir != NULL) {
138 status = rmdir(tmpdir);
139 ATF_REQUIRE_MSG(status == 0, "rmdir failed: %d", errno);
143 ATF_TP_ADD_TCS(tp)
145 ATF_TP_ADD_TC(tp, mqueue);
147 return atf_no_error();