blob: 35e0657bb0dc703b0f943f3a6bcae765cebd3905 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * msgsnd03.c
23 *
24 * DESCRIPTION
25 * msgsnd03 - test for EINVAL error
26 *
27 * ALGORITHM
28 * create a message queue with read/write permissions
29 * create a trivial message buffer
30 * loop if that option was specified
31 * call msgsnd() using four different invalid cases
32 * check the errno value
33 * issue a PASS message if we get EINVAL
34 * otherwise, the tests fails
35 * issue a FAIL message
36 * call cleanup
37 *
38 * USAGE: <for command-line>
39 * msgsnd03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
40 * where, -c n : Run n copies concurrently.
41 * -e : Turn on errno logging.
42 * -i n : Execute test n times.
43 * -I x : Execute test for x seconds.
44 * -P x : Pause for x seconds between iterations.
45 * -t : Turn on syscall timing.
46 *
47 * HISTORY
48 * 03/2001 - Written by Wayne Boyer
49 *
50 * RESTRICTIONS
51 * none
52 */
53
54#include "test.h"
plars865695b2001-08-27 22:15:12 +000055
robbiew23499f02002-11-18 19:54:58 +000056#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000057
58void cleanup(void);
59void setup(void);
60
61char *TCID = "msgsnd03";
62int TST_TOTAL = 4;
plars865695b2001-08-27 22:15:12 +000063
plars865695b2001-08-27 22:15:12 +000064int msg_q_1 = -1; /* The message queue id created in setup */
65MSGBUF msg_buf; /* a buffer for the message to queue */
66int bad_q = -1; /* a value to use as a bad queue ID */
67
68struct test_case_t {
69 int *queue_id;
70 MSGBUF *buffer;
71 long mtype;
72 int msg_size;
73 int error;
74} TC[] = {
75 /* EINVAL - the queue ID is invalid */
subrata_modak56207ce2009-03-23 13:35:39 +000076 {
77 &bad_q, &msg_buf, 1, 1, EINVAL},
78 /* EINVAL - the message type is not positive (0) */
79 {
80 &msg_q_1, &msg_buf, 0, 1, EINVAL},
81 /* EINVAL - the message type is not positive (>0) */
82 {
83 &msg_q_1, &msg_buf, -1, 1, EINVAL},
84 /* EINVAL - the message size is less than zero */
85 {
86 &msg_q_1, &msg_buf, 1, -1, EINVAL}
plars865695b2001-08-27 22:15:12 +000087};
88
robbiew23499f02002-11-18 19:54:58 +000089int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000090{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020091 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020092 const char *msg;
plars865695b2001-08-27 22:15:12 +000093 int i;
94
Cyril Hrubis74225622014-06-02 17:54:38 +020095 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080096 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000097
subrata_modak56207ce2009-03-23 13:35:39 +000098 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000099
100 /* The following loop checks looping state if -i option given */
101
102 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800103 /* reset tst_count in case we are looping */
104 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000105
106 /*
107 * loop through the test cases
108 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000111
112 /* set the message type */
113 msg_buf.mtype = TC[i].mtype;
114
115 /* make the call with the TEST macro */
116 TEST(msgsnd(*(TC[i].queue_id), TC[i].buffer,
subrata_modak56207ce2009-03-23 13:35:39 +0000117 TC[i].msg_size, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000118
plars865695b2001-08-27 22:15:12 +0000119 if (TEST_RETURN != -1) {
120 tst_resm(TFAIL, "call succeeded unexpectedly");
121 continue;
122 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000123
plars865695b2001-08-27 22:15:12 +0000124 if (TEST_ERRNO == TC[i].error) {
125 tst_resm(TPASS, "expected failure - "
126 "errno = %d : %s", TEST_ERRNO,
127 strerror(TEST_ERRNO));
128 } else {
129 tst_resm(TFAIL, "unexpected error - %d : %s",
130 TEST_ERRNO, strerror(TEST_ERRNO));
131 }
132 }
133 }
134
135 cleanup();
136
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800137 tst_exit();
plars865695b2001-08-27 22:15:12 +0000138}
139
140/*
141 * setup() - performs all the ONE TIME setup for this test.
142 */
subrata_modak56207ce2009-03-23 13:35:39 +0000143void setup(void)
plars865695b2001-08-27 22:15:12 +0000144{
Garrett Cooper2c282152010-12-16 00:55:50 -0800145
plars865695b2001-08-27 22:15:12 +0000146 tst_sig(NOFORK, DEF_HANDLER, cleanup);
147
plars865695b2001-08-27 22:15:12 +0000148 TEST_PAUSE;
149
150 /*
151 * Create a temporary directory and cd into it.
152 * This helps to ensure that a unique msgkey is created.
153 * See ../lib/libipc.c for more information.
154 */
155 tst_tmpdir();
156
157 msgkey = getipckey();
158
159 /* create a message queue with read/write permission */
160 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
161 tst_brkm(TBROK, cleanup, "Can't create message queue");
162 }
163
164 /* initialize the message buffer with something trivial */
165 msg_buf.mtype = MSGTYPE;
166 msg_buf.mtext[0] = 'a';
167}
168
169/*
170 * cleanup() - performs all the ONE TIME cleanup for this test at completion
171 * or premature exit.
172 */
subrata_modak56207ce2009-03-23 13:35:39 +0000173void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000174{
175 /* if it exists, remove the message queue that was created */
176 rm_queue(msg_q_1);
177
plars865695b2001-08-27 22:15:12 +0000178 tst_rmdir();
179
Chris Dearmanec6edca2012-10-17 19:54:01 -0700180}