Add POSIX test suite 1.5.2 (http://sourceforge.net/projects/posixtest/).
[dragonfly.git] / test / contrib / posixtestsuite-1.5.2 / conformance / interfaces / mq_timedreceive / speculative / 10-2.c
1 /*
2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
3  * Created by:  crystal.xiong REMOVE-THIS AT intel DOT com
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  */
8
9 /*
10  * mq_timedreceive test plan:
11  * If message can be removed from message queue immediately, the validity
12  * of timout need not be checked.
13  *
14  * This is a speculative test because if abs_timeout _is_ checked for
15  * validity, it is not a failure.
16  */
17
18 #include <stdio.h>
19 #include <mqueue.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <time.h>
26 #include "posixtest.h"
27
28 #define TEST "10-2"
29 #define FUNCTION "mq_timedreceive"
30 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
31
32 #define NAMESIZE 50
33 #define BUFFER 40
34
35 int main()
36 {
37         char mqname[NAMESIZE], msgrv[BUFFER];
38         const char *msgptr = "test message";
39         mqd_t mqdes;
40         unsigned rvprio, sdprio = 1;
41         struct timespec ts;
42         struct mq_attr attr;
43         int unresolved = 0;
44
45         sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());
46
47         attr.mq_msgsize = BUFFER;
48         attr.mq_maxmsg = BUFFER;
49         mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
50         if (mqdes == (mqd_t)-1) {
51                 perror(ERROR_PREFIX "mq_open");
52                 unresolved = 1;
53         }
54
55         if (mq_send(mqdes, msgptr, strlen(msgptr), sdprio) != 0) {
56                 perror(ERROR_PREFIX "mq_send");
57                 unresolved = 1;
58         }
59
60         ts.tv_sec = time(NULL) + 1;
61         ts.tv_nsec = -1;
62         if (mq_timedreceive(mqdes, msgrv, BUFFER, &rvprio, &ts) == -1) {
63                 printf("mq_timedreceive() did fail on invalid abs_time\n");
64         }
65         else {
66                 printf("mq_timedreceive() did not fail on invalid abs_time\n");
67         }
68
69         if (mq_close(mqdes) != 0) {
70                 perror(ERROR_PREFIX "mq_close");
71                 unresolved = 1;
72         }
73
74         if (mq_unlink(mqname) != 0) {
75                 perror(ERROR_PREFIX "mq_unlink");
76                 unresolved = 1;
77         }
78
79         if (unresolved==1) {
80                 printf("Test UNRESOLVED\n");
81                 return PTS_UNRESOLVED;
82         }
83
84         printf("Test PASSED\n");
85         return PTS_PASS;
86 }