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_mq_timedsend.c
More file actions
144 lines (128 loc) · 3.84 KB
/
test_mq_timedsend.c
File metadata and controls
144 lines (128 loc) · 3.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <mqueue.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "tests.h"
void test_mq_timedsend_onqueue(mqd_t q)
{
char msg[8] = "ABCD1234";
test_begin("mq_send() to fill the queue up");
int result = mq_send(q, msg, sizeof(msg), 1);
if (result) test_failure(1, "mq_send returned %d", q); else test_success();
test_begin("mq_timedsend() with a time-out in the past before Y2038");
struct timespec ts;
result = timespec_get(&ts, TIME_UTC);
if (result != TIME_UTC)
test_failure(0, "timespec_get returned %d instead of %d (TIME_UTC)", result, TIME_UTC);
else
{
result = mq_timedsend(q, msg, sizeof(msg), 1, &ts);
if (result != -1)
test_failure(1, "mq_timedsend returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedsend did not time out");
else
test_success();
}
test_begin("mq_timedreceive() with a time-out in the future before Y2038");
result = timespec_get(&ts, TIME_UTC);
if (result != TIME_UTC)
test_failure(1, "timespec_get returned %d instead of %d (TIME_UTC)", result, TIME_UTC);
else
{
ts.tv_sec++;
result = mq_timedsend(q, msg, sizeof(msg), 1, &ts);
if (result != -1)
test_failure(1, "mq_timedsend returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedsend did not time out");
else
test_success();
}
// Only test this part if we can set the time beyond Y2038
// i.e. if our time_t is 64-bit
#if defined(__USE_TIME_BITS64) && __USE_TIME_BITS64 == 1
test_begin("Get current time");
time_t t0 = time(NULL);
if (t0==(time_t)-1)
{
test_failure(1, "time returned -1");
return;
}
test_success();
test_begin("Set current time at Y2038+1s");
struct timespec t = { 0x80000001, 0};
result = clock_settime(CLOCK_REALTIME, &t);
if (result)
{
test_failure(1, "clock_settime returned %d", result);
return;
}
test_success();
test_begin("mq_timedsend() with a time-out in the past after Y2038");
result = timespec_get(&ts, TIME_UTC);
if (result != TIME_UTC)
test_failure(1, "timespec_get returned %d instead of %d (TIME_UTC)", result, TIME_UTC);
else
{
result = mq_timedsend(q, msg, sizeof(msg), 1, &ts);
if (result != -1)
test_failure(1, "mq_timedsend returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedsend did not time out");
else
test_success();
}
test_begin("mq_timedreceive() with a time-out in the future after Y2038");
result = timespec_get(&ts, TIME_UTC);
if (result != TIME_UTC)
test_failure(1, "timespec_get returned %d instead of %d (TIME_UTC)", result, TIME_UTC);
else
{
ts.tv_sec++;
result = mq_timedsend(q, msg, sizeof(msg), 1, &ts);
if (result != -1)
test_failure(1, "mq_timedsend returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedsend did not time out");
else
test_success();
}
test_begin("Restore current time");
struct timespec ts0;
ts0.tv_sec = t0;
ts0.tv_nsec = 0;
result = clock_settime(CLOCK_REALTIME, &ts0);
if (result)
test_failure(1, "clock_settime returned %d", result);
else
test_success();
#endif
}
void test_mq_timedsend(void)
{
test_begin("Create the message queue");
struct mq_attr mq_attr = { .mq_maxmsg = 1, .mq_msgsize = 8 };
mqd_t q = mq_open("/y2038s", O_RDWR | O_CREAT, 0x777, &mq_attr);
if (q == (mqd_t) -1)
{
test_failure(1, "mq_open returned -1");
return;
}
test_success();
test_mq_timedsend_onqueue(q);
test_begin("Close the message queue");
int cq = mq_close(q);
if (cq)
test_failure(1, "mq_close returned %d", cq);
else
test_success();
test_begin("Remove the message queue");
int uq = mq_unlink("/y2038s");
if (uq)
test_failure(1, "mq_unlink returned %d", uq);
else
test_success();
}