1 /*
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
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"
55 
56 #include "ipcmsg.h"
57 
58 void cleanup(void);
59 void setup(void);
60 
61 char *TCID = "msgsnd03";
62 int TST_TOTAL = 4;
63 
64 int msg_q_1 = -1;		/* The message queue id created in setup */
65 MSGBUF msg_buf;			/* a buffer for the message to queue */
66 int bad_q = -1;			/* a value to use as a bad queue ID */
67 
68 struct 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 */
76 	{
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}
87 };
88 
main(int ac,char ** av)89 int main(int ac, char **av)
90 {
91 	int lc;
92 	int i;
93 
94 	tst_parse_opts(ac, av, NULL, NULL);
95 
96 	setup();		/* global setup */
97 
98 	/* The following loop checks looping state if -i option given */
99 
100 	for (lc = 0; TEST_LOOPING(lc); lc++) {
101 		/* reset tst_count in case we are looping */
102 		tst_count = 0;
103 
104 		/*
105 		 * loop through the test cases
106 		 */
107 
108 		for (i = 0; i < TST_TOTAL; i++) {
109 
110 			/* set the message type */
111 			msg_buf.mtype = TC[i].mtype;
112 
113 			/* make the call with the TEST macro */
114 			TEST(msgsnd(*(TC[i].queue_id), TC[i].buffer,
115 				    TC[i].msg_size, 0));
116 
117 			if (TEST_RETURN != -1) {
118 				tst_resm(TFAIL, "call succeeded unexpectedly");
119 				continue;
120 			}
121 
122 			if (TEST_ERRNO == TC[i].error) {
123 				tst_resm(TPASS, "expected failure - "
124 					 "errno = %d : %s", TEST_ERRNO,
125 					 strerror(TEST_ERRNO));
126 			} else {
127 				tst_resm(TFAIL, "unexpected error - %d : %s",
128 					 TEST_ERRNO, strerror(TEST_ERRNO));
129 			}
130 		}
131 	}
132 
133 	cleanup();
134 
135 	tst_exit();
136 }
137 
138 /*
139  * setup() - performs all the ONE TIME setup for this test.
140  */
setup(void)141 void setup(void)
142 {
143 
144 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
145 
146 	TEST_PAUSE;
147 
148 	/*
149 	 * Create a temporary directory and cd into it.
150 	 * This helps to ensure that a unique msgkey is created.
151 	 * See ../lib/libipc.c for more information.
152 	 */
153 	tst_tmpdir();
154 
155 	msgkey = getipckey();
156 
157 	/* create a message queue with read/write permission */
158 	if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
159 		tst_brkm(TBROK, cleanup, "Can't create message queue");
160 	}
161 
162 	/* initialize the message buffer with something trivial */
163 	msg_buf.mtype = MSGTYPE;
164 	msg_buf.mtext[0] = 'a';
165 }
166 
167 /*
168  * cleanup() - performs all the ONE TIME cleanup for this test at completion
169  * 	       or premature exit.
170  */
cleanup(void)171 void cleanup(void)
172 {
173 	/* if it exists, remove the message queue that was created */
174 	rm_queue(msg_q_1);
175 
176 	tst_rmdir();
177 
178 }
179