2 * Copyright (c) 2009, Stathis Kamperis
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <limits.h> /* for LONG_MAX */
35 #include <string.h> /* memset() */
37 #define MQNAME "/t_mq_open"
44 /* ------------------------------------------------------------------ */
45 /* Empty message queue name. */
46 assert(mq_open("", O_CREAT | O_EXCL | O_RDWR, 0700, NULL) == (mqd_t)-1
50 /* ------------------------------------------------------------------ */
51 /* O_CREAT is not set and the named mqueue doesn't exist. */
52 assert(mq_open("/t_mq_definitelydoesntexist", O_RDWR, 0700, NULL)
53 == (mqd_t)-1 && errno == ENOENT);
56 /* ------------------------------------------------------------------ */
57 /* Pathname is too long. XXX Probe host OS to report real PATH_MAX. */
58 char *pathname = malloc(65536);
59 assert(pathname != NULL);
61 /* Make sure we don't terminate prematurely. */
62 memset(pathname, 0xFF, 65536);
64 assert(mq_open(pathname, O_CREAT | O_EXCL | O_RDWR, 0700, NULL)
65 == -1 && errno == ENAMETOOLONG);
69 /* ------------------------------------------------------------------ */
70 /* Message queue already exists and O_CREAT, O_EXCL are both set. */
71 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, NULL);
72 assert(md != (mqd_t)-1);
74 assert(mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, NULL)
75 == (mqd_t)-1 && errno == EEXIST);
77 assert(mq_close(md) != -1);
78 assert(mq_unlink(MQNAME) != -1);
81 /* ------------------------------------------------------------------ */
82 /* Try to open a read only message queue for write. */
83 md = mq_open(MQNAME, O_CREAT | O_EXCL, 0400, NULL);
84 assert(md != (mqd_t)-1);
86 assert(mq_open(MQNAME, O_RDWR) == (mqd_t)-1 && errno == EACCES);
87 assert(mq_open(MQNAME, O_WRONLY) == (mqd_t)-1 && errno == EACCES);
89 assert(mq_close(md) != -1);
90 assert(mq_unlink(MQNAME) != -1);
93 /* ------------------------------------------------------------------ */
94 /* Try to open a write only message queue for read. */
95 md = mq_open(MQNAME, O_CREAT | O_EXCL, 0200, NULL);
96 assert(md != (mqd_t)-1);
98 assert(mq_open(MQNAME, O_RDWR) == (mqd_t)-1 && errno == EACCES);
99 assert(mq_open(MQNAME, O_RDONLY) == (mqd_t)-1 && errno == EACCES);
101 assert(mq_close(md) != -1);
102 assert(mq_unlink(MQNAME) != -1);
105 /* ------------------------------------------------------------------ */
106 /* Try to open a queue with enormous capacity. */
109 memset(&attr, 0, sizeof(attr));
110 attr.mq_maxmsg = LONG_MAX;
111 attr.mq_msgsize = 1024;
113 assert(mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, &attr) == -1);
117 return (EXIT_SUCCESS);