blob: 93dbfed951888eb3d472178aace4679ba8208c5b [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 * msgrcv03.c
23 *
24 * DESCRIPTION
25 * msgrcv03 - test for EINVAL error
26 *
27 * ALGORITHM
28 * create a message queue with read/write permissions
29 * loop if that option was specified
30 * call msgrcv() using two different invalid cases
31 * check the errno value
32 * issue a PASS message if we get EINVAL
33 * otherwise, the tests fails
34 * issue a FAIL message
35 * call cleanup
36 *
37 * USAGE: <for command-line>
38 * msgrcv03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 * where, -c n : Run n copies concurrently.
40 * -e : Turn on errno logging.
41 * -i n : Execute test n times.
42 * -I x : Execute test for x seconds.
43 * -P x : Pause for x seconds between iterations.
44 * -t : Turn on syscall timing.
45 *
46 * HISTORY
47 * 03/2001 - Written by Wayne Boyer
48 *
49 * RESTRICTIONS
50 * none
51 */
52
53#include "test.h"
plars865695b2001-08-27 22:15:12 +000054
robbiew23499f02002-11-18 19:54:58 +000055#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000056
57void cleanup(void);
58void setup(void);
59
60char *TCID = "msgrcv03";
61int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000062
plars865695b2001-08-27 22:15:12 +000063int msg_q_1 = -1; /* The message queue id created in setup */
64int bad_q = -1; /* a value to use as a bad queue ID */
65MSGBUF rcv_buf;
66
67struct test_case_t {
68 int *queue_id;
69 int msize;
70 int error;
71} TC[] = {
72 /* EINVAL - the queue ID is invalid */
subrata_modak56207ce2009-03-23 13:35:39 +000073 {
74 &bad_q, MSGSIZE, EINVAL},
75 /* EINVAL - the message size is less than 0 */
76 {
77 &msg_q_1, -1, EINVAL}
plars865695b2001-08-27 22:15:12 +000078};
79
robbiew23499f02002-11-18 19:54:58 +000080int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000081{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020082 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020083 const char *msg;
plars865695b2001-08-27 22:15:12 +000084 int i;
85
Cyril Hrubis74225622014-06-02 17:54:38 +020086 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080087 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000088
subrata_modak56207ce2009-03-23 13:35:39 +000089 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000090
91 /* The following loop checks looping state if -i option given */
92
93 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 /* reset tst_count in case we are looping */
95 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +000098
99 /*
100 * Use the TEST macro to make the call
101 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000102
plars865695b2001-08-27 22:15:12 +0000103 TEST(msgrcv(*(TC[i].queue_id), &rcv_buf, TC[i].msize,
subrata_modak56207ce2009-03-23 13:35:39 +0000104 1, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000105
plars865695b2001-08-27 22:15:12 +0000106 if (TEST_RETURN != -1) {
107 tst_resm(TFAIL, "call succeeded unexpectedly");
108 continue;
109 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000110
plars865695b2001-08-27 22:15:12 +0000111 if (TEST_ERRNO == TC[i].error) {
112 tst_resm(TPASS, "expected failure - errno = "
113 "%d : %s", TEST_ERRNO,
114 strerror(TEST_ERRNO));
115 } else {
116 tst_resm(TFAIL, "call failed with an "
117 "unexpected error - %d : %s",
118 TEST_ERRNO, strerror(TEST_ERRNO));
subrata_modak56207ce2009-03-23 13:35:39 +0000119 }
plars865695b2001-08-27 22:15:12 +0000120 }
121 }
122
123 cleanup();
124
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800125 tst_exit();
plars865695b2001-08-27 22:15:12 +0000126}
127
128/*
129 * setup() - performs all the ONE TIME setup for this test.
130 */
subrata_modak56207ce2009-03-23 13:35:39 +0000131void setup(void)
plars865695b2001-08-27 22:15:12 +0000132{
Garrett Cooper2c282152010-12-16 00:55:50 -0800133
plars865695b2001-08-27 22:15:12 +0000134 tst_sig(NOFORK, DEF_HANDLER, cleanup);
135
plars865695b2001-08-27 22:15:12 +0000136 TEST_PAUSE;
137
138 /*
139 * Create a temporary directory and cd into it.
140 * This helps to ensure that a unique msgkey is created.
141 * See ../lib/libipc.c for more information.
142 */
143 tst_tmpdir();
144
145 msgkey = getipckey();
146
147 /* create a message queue with read/write permission */
148 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
149 tst_brkm(TBROK, cleanup, "Can't create message queue");
150 }
151}
152
153/*
154 * cleanup() - performs all the ONE TIME cleanup for this test at completion
155 * or premature exit.
156 */
subrata_modak56207ce2009-03-23 13:35:39 +0000157void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000158{
159 /* if it exists, remove the message queue that was created */
160 rm_queue(msg_q_1);
161
plars865695b2001-08-27 22:15:12 +0000162 tst_rmdir();
163
Chris Dearmanec6edca2012-10-17 19:54:01 -0700164}