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 queue is empty and O_NONBLOCK is set in the message queue,
12 * no message will be removed from the queue, mq_timedreceive will return an
13 * error.
14 */
15
16#include <stdio.h>
17#include <mqueue.h>
18#include <fcntl.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22#include <string.h>
23#include <stdlib.h>
24#include <time.h>
25#include "posixtest.h"
26
27#define TEST "7-1"
28#define FUNCTION "mq_timedreceive"
29#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
30
31#define NAMESIZE 50
32#define BUFFER	40
33
34int main()
35{
36        char mqname[NAMESIZE], msgrv[BUFFER];
37        mqd_t mqdes;
38	struct timespec ts;
39	struct mq_attr attr;
40	int unresolved = 0, failure = 0;
41
42	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());
43
44	attr.mq_msgsize = BUFFER;
45	attr.mq_maxmsg = BUFFER;
46	mqdes = mq_open(mqname, O_CREAT | O_NONBLOCK | O_RDWR, S_IRUSR | S_IWUSR, &attr);
47        if (mqdes == (mqd_t)-1) {
48                perror(ERROR_PREFIX "mq_open");
49		unresolved = 1;
50        }
51
52	ts.tv_sec = time(NULL) + 1;
53	ts.tv_nsec = 0;
54       	if (mq_timedreceive(mqdes, msgrv, BUFFER, NULL, &ts) != -1) {
55		printf("mq_timedreceive succeed unexpectly \n");
56		failure = 1;
57	}
58        if (mq_close(mqdes) != 0) {
59		perror(ERROR_PREFIX "mq_close");
60		unresolved = 1;
61        }
62
63        if (mq_unlink(mqname) != 0) {
64		perror(ERROR_PREFIX "mq_unlink");
65		unresolved = 1;
66        }
67
68	if (failure==1) {
69                printf("Test FAILED\n");
70                return PTS_FAIL;
71        }
72
73        if (unresolved==1) {
74                printf("Test UNRESOLVED\n");
75                return PTS_UNRESOLVED;
76        }
77	printf("Test PASSED\n");
78	return PTS_PASS;
79}
80
81