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_timedreceive.c
More file actions
136 lines (120 loc) · 3.62 KB
/
test_mq_timedreceive.c
File metadata and controls
136 lines (120 loc) · 3.62 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
#include <mqueue.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "tests.h"
static void test_mq_timedreceive_onqueue(mqd_t q)
{
char msg[8] = "ABCD1234";
unsigned int prio;
test_begin("mq_timedreceive() with a time-out in the past before Y2038");
struct timespec ts;
int 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_timedreceive(q, msg, sizeof(msg), &prio, &ts);
if (result != -1)
test_failure(1, "mq_timedreceive returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedreceive 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_timedreceive(q, msg, sizeof(msg), &prio, &ts);
if (result != -1)
test_failure(1, "mq_timedreceive returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedreceive 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_BITS_64) && __USE_TIME_BITS_64 == 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");
time_t t = 0x80000001;
result = stime(&t);
if (result)
{
test_failure(1, "stime returned %d", result);
return;
}
test_success();
test_begin("mq_timedreceive() 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_timedreceive(q, msg, sizeof(msg), &prio, &ts);
if (result != -1)
test_failure(1, "mq_timedreceive returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedreceive 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_timedreceive(q, msg, sizeof(msg), &prio, &ts);
if (result != -1)
test_failure(1, "mq_timedreceive returned %d", result);
else if (errno != ETIMEDOUT)
test_failure(1, "mq_timedreceive did not time out");
else
test_success();
}
test_begin("Restore current time");
result = stime(&t0);
if (result) test_failure(1, "stime returned %d", result); else test_success();
#endif
}
void test_mq_timedreceive(void)
{
test_begin("Create the message queue");
struct mq_attr mq_attr = { .mq_maxmsg = 1, .mq_msgsize = 8 };
mqd_t q = mq_open("/y2038r", O_RDWR | O_CREAT, 0x777, &mq_attr);
if (q == (mqd_t) -1)
{
test_failure(1, "mq_open returned -1");
return;
}
test_success();
test_mq_timedreceive_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("/y2038r");
if (uq)
test_failure(1, "mq_unlink returned %d", uq);
else
test_success();
}