etc/services - sync with NetBSD-8
[minix.git] / tests / kernel / t_mqueue.c
blob2a0310116207e5957b4c69d68d48cb956c5c070a
1 /* $NetBSD: t_mqueue.c,v 1.4 2014/03/02 19:56:48 jmmv 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 #define MQ_PRIO_BASE 24
21 static void
22 send_msgs(mqd_t mqfd)
24 char msg[2];
26 msg[1] = '\0';
28 msg[0] = 'a';
29 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
30 "mq_send 1 failed: %d", errno);
32 msg[0] = 'b';
33 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
34 "mq_send 2 failed: %d", errno);
36 msg[0] = 'c';
37 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
38 "mq_send 3 failed: %d", errno);
40 msg[0] = 'd';
41 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE - 1) != -1,
42 "mq_send 4 failed: %d", errno);
44 msg[0] = 'e';
45 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), 0) != -1,
46 "mq_send 5 failed: %d", errno);
48 msg[0] = 'f';
49 ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
50 "mq_send 6 failed: %d", errno);
53 static void
54 receive_msgs(mqd_t mqfd)
56 struct mq_attr mqa;
57 char *m;
58 unsigned p;
59 int len;
61 ATF_REQUIRE_MSG(mq_getattr(mqfd, &mqa) != -1, "mq_getattr failed %d",
62 errno);
64 len = mqa.mq_msgsize;
65 m = calloc(1, len);
66 ATF_REQUIRE_MSG(m != NULL, "calloc failed");
68 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
69 "mq_receive 1 failed: %d", errno);
70 ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'b',
71 "mq_receive 1 prio/data mismatch");
73 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
74 "mq_receive 2 failed: %d", errno);
75 ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'f',
76 "mq_receive 2 prio/data mismatch");
78 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
79 "mq_receive 3 failed: %d", errno);
80 ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'a',
81 "mq_receive 3 prio/data mismatch");
83 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
84 "mq_receive 4 failed: %d", errno);
85 ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'c',
86 "mq_receive 4 prio/data mismatch");
88 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
89 "mq_receive 5 failed: %d", errno);
90 ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE - 1) && m[0] == 'd',
91 "mq_receive 5 prio/data mismatch");
93 ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
94 "mq_receive 6 failed: %d", errno);
95 ATF_REQUIRE_MSG(p == 0 && m[0] == 'e',
96 "mq_receive 6 prio/data mismatch");
99 ATF_TC(mqueue);
100 ATF_TC_HEAD(mqueue, tc)
103 atf_tc_set_md_var(tc, "timeout", "3");
104 atf_tc_set_md_var(tc, "descr", "Checks mqueue send/receive");
107 ATF_TC_BODY(mqueue, tc)
109 int status;
110 char *tmpdir;
111 char template[32];
112 char mq_name[64];
114 strlcpy(template, "./t_mqueue.XXXXXX", sizeof(template));
115 tmpdir = mkdtemp(template);
116 ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed: %d", errno);
117 snprintf(mq_name, sizeof(mq_name), "%s/mq", tmpdir);
119 mqd_t mqfd;
121 mqfd = mq_open(mq_name, O_RDWR | O_CREAT,
122 S_IRUSR | S_IRWXG | S_IROTH, NULL);
123 ATF_REQUIRE_MSG(mqfd != -1, "mq_open failed: %d", errno);
125 send_msgs(mqfd);
126 receive_msgs(mqfd);
128 status = mq_close(mqfd);
129 ATF_REQUIRE_MSG(status == 0, "mq_close failed: %d", errno);
132 ATF_TP_ADD_TCS(tp)
134 ATF_TP_ADD_TC(tp, mqueue);
136 return atf_no_error();