This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_msgctl.c
More file actions
99 lines (90 loc) · 2.84 KB
/
test_msgctl.c
File metadata and controls
99 lines (90 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "tests.h"
static void test_msgctl_onqueue(int q)
{
typedef struct
{
long mtype;
char mtext[1];
} msg_t;
msg_t msgs = { .mtype = 42, .mtext = { 'X' } };
msg_t msgr = { .mtype = -1, .mtext = { 'Z' } };
test_begin("call msgctl() with a never-used queue");
struct msqid_ds stat;
int result = msgctl(q, IPC_STAT, &stat);
if (result)
test_failure(1, "msgctl returned %d", result);
else if (stat.msg_stime)
test_failure(0, "msgctl: stat.msg_stime = %lld, expected 0", (long long) stat.msg_stime);
else if (stat.msg_rtime)
test_failure(0, "msgctl: stat.msg_rtime = %lld, expected 0", (long long) stat.msg_rtime);
else
test_success();
test_begin("call msgctl() after a single queue write");
result = msgsnd(q, &msgs, sizeof(msgs.mtext), IPC_NOWAIT);
if (result)
test_failure(1, "msgsnd returned %d", result);
else
{
result = msgctl(q, IPC_STAT, &stat);
if (result)
test_failure(1, "msgctl returned %d", result);
else if (stat.msg_stime == 0)
test_failure(0, "msgctl: stat.msg_stime = 0, expected nonzero");
else if (stat.msg_rtime)
test_failure(0, "msgctl: stat.msg_rtime = %lld, expected 0", (long long) stat.msg_rtime);
else
test_success();
}
test_begin("call msgctl() after a queue write and read");
result = msgsnd(q, &msgs, sizeof(msgs.mtext), IPC_NOWAIT);
if (result)
test_failure(1, "msgsnd returned %d", result);
else
{
result = msgrcv(q, &msgr, sizeof(msgs.mtext), 0, IPC_NOWAIT);
if (result < 0)
test_failure(1, "msgrcv returned %d", result);
else if (result != sizeof(msgs.mtext))
test_failure(0, "msgrcv returned %d, expected %d", result, sizeof(msgs.mtext));
else
{
result = msgctl(q, IPC_STAT, &stat);
if (result)
test_failure(1, "msgctl returned %d", result);
else if (stat.msg_stime == 0)
test_failure(0, "msgctl: stat.msg_stime = 0, expected nonzero");
else if (stat.msg_rtime == 0)
test_failure(0, "msgctl: stat.msg_rtime = 0, expected nonzero");
else if (msgr.mtype != 42)
test_failure(0, "msgctl: msgr.mtype = %ld, expected 42", msgr.mtype);
else if (msgr.mtext[0] != 'X')
test_failure(0, "msgctl: msgr.mttext = (char)%d, expected 'X'", msgr.mtext[0]);
else
test_success();
}
}
}
void test_msgctl(void)
{
test_begin("Create the SysV message queue for msgctl tests");
int q = msgget(IPC_PRIVATE, 0x777);
if (q == -1)
{
test_failure(1, "msgget returned -1");
return;
}
test_success();
test_msgctl_onqueue(q);
test_begin("Close the msgctl test SysV message queue");
int r = msgctl(q, IPC_RMID, NULL);
if (r)
test_failure(1, "msgctl returned %d", r);
else
test_success();
}