query_module02.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
1/*
2 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 */
17/**********************************************************
18 *
19 *    TEST IDENTIFIER   : query_module02
20 *
21 *    EXECUTED BY       : anyone
22 *
23 *    TEST TITLE        : Checking error conditions for query_module(2)
24 *
25 *    TEST CASE TOTAL   : 5
26 *
27 *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
28 *
29 *    SIGNALS
30 *	Uses SIGUSR1 to pause before test if option set.
31 *	(See the parse_opts(3) man page).
32 *
33 *    DESCRIPTION
34 *	Verify that,
35 *	1. query_module(2) returns -1 and sets errno to ENOENT for non-existing
36 *	   module.
37 *	2. query_module(2) returns -1 and sets errno to EINVAL for invalid
38 *	   which argument.
39 *	3. query_module(2) returns -1 and sets errno to EINVAL for NULL
40 *	   module name and valid which argument.
41 *      4. query_module(2) returns -1 and sets errno to EINVAL, if module
42 *         name parameter is null terminated (zero length) string.
43 *	5. query_module(2) returns -1 and sets errno to ENAMETOOLONG for long
44 *	   module name.
45 *
46 *	Setup:
47 *	  Setup signal handling.
48 *	  Initialize  long module name
49 *	  Set expected errnos for logging
50 *	  Pause for SIGUSR1 if option specified.
51 *
52 *	Test:
53 *	 Loop if the proper options are given.
54 *	  Execute system call
55 *	  Check return code and error number, if matching,
56 *		Issue PASS message
57 *	  Otherwise,
58 *		Issue FAIL message
59 *
60 *	Cleanup:
61 *	  Print errno log and/or timing stats if options given
62 *
63 * USAGE:  <for command-line>
64 *  query_module02 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
65 *		where,  -c n : Run n copies concurrently.
66 *			-e   : Turn on errno logging.
67 *			-f   : Turn off functional testing
68 *			-h   : Show help screen
69 *			-i n : Execute test n times.
70 *			-I x : Execute test for x seconds.
71 *			-p   : Pause for SIGUSR1 before starting
72 *			-P x : Pause for x seconds between iterations.
73 *			-t   : Turn on syscall timing.
74 *
75 ****************************************************************/
76
77#include <errno.h>
78#include <pwd.h>
79#include <sys/types.h>
80#include <unistd.h>
81#include <limits.h>
82#include <asm/atomic.h>
83#include <linux/module.h>
84#include "test.h"
85#include "usctest.h"
86
87#ifndef PAGE_SIZE
88#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
89#endif
90
91#define NULLMODNAME	""
92#define LONGMODNAMECHAR	'm'	/* Arbitrarily selected */
93#define MODNAMEMAX	(PAGE_SIZE + 1)
94#define EXP_RET_VAL	-1
95#define QM_INVALID	(QM_INFO + 100)
96
97struct test_case_t {		/* test case structure */
98	char *modname;
99	int which;
100	void *buf;
101	size_t bufsize;
102	int experrno;		/* expected errno */
103	char *desc;
104};
105
106char *TCID = "query_module02";
107static int exp_enos[] = { ENOENT, EINVAL, ENAMETOOLONG, 0 };
108
109static char longmodname[MODNAMEMAX];
110static int testno;
111static char out_buf[PAGE_SIZE];
112static size_t ret_size;
113
114static void setup(void);
115static void cleanup(void);
116
117static struct test_case_t tdat[] = {
118
119	{"dummy_mod", QM_REFS, (void *)out_buf, sizeof(out_buf), ENOENT,
120	 "results for non-existing module"}
121	,
122
123	{NULL, QM_INVALID, (void *)out_buf, sizeof(out_buf), EINVAL,
124	 "results for invalid which argument"}
125	,
126
127	{NULL, QM_REFS, (void *)out_buf, sizeof(out_buf), EINVAL,
128	 "results for NULL module name and valid which argument"}
129	,
130
131	{NULLMODNAME, QM_REFS, (void *)out_buf, sizeof(out_buf), EINVAL,
132	 "results for null terminated (zero lenght) module name"}
133	,
134
135	{longmodname, QM_REFS, (void *)out_buf, sizeof(out_buf), ENAMETOOLONG,
136	 "results for long module name"}
137	,
138};
139
140int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
141
142int main(int argc, char **argv)
143{
144	int lc;
145	char *msg;
146
147	if ((msg = parse_opts(argc, argv, NULL, NULL)) != (char *)NULL) {
148		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
149	}
150
151	setup();
152
153	for (lc = 0; TEST_LOOPING(lc); lc++) {
154		/* reset Tst_count in case we are looping */
155		Tst_count = 0;
156
157		for (testno = 0; testno < TST_TOTAL; ++testno) {
158
159			TEST(query_module(tdat[testno].modname,
160					  tdat[testno].which, tdat[testno].buf,
161					  tdat[testno].bufsize, &ret_size));
162			TEST_ERROR_LOG(TEST_ERRNO);
163			if ((TEST_RETURN == EXP_RET_VAL) &&
164			    (TEST_ERRNO == tdat[testno].experrno)) {
165				tst_resm(TPASS, "Expected %s, errno: %d",
166					 tdat[testno].desc, TEST_ERRNO);
167			} else {
168				tst_resm(TFAIL, "Unexpected %s ; returned"
169					 " %d (expected %d), errno %d (expected"
170					 " %d)", tdat[testno].desc,
171					 TEST_RETURN, EXP_RET_VAL,
172					 TEST_ERRNO, tdat[testno].experrno);
173			}
174		}
175	}
176	cleanup();
177
178	tst_exit();
179}
180
181/*
182 * setup()
183 *	performs all ONE TIME setup for this test
184 */
185void setup(void)
186{
187
188	tst_sig(FORK, DEF_HANDLER, cleanup);
189
190	if (tst_kvercmp(2, 5, 48) >= 0)
191		tst_brkm(TCONF, NULL, "This test will not work on "
192			 "kernels after 2.5.48");
193
194	/* Initialize longmodname to LONGMODNAMECHAR character */
195	memset(longmodname, LONGMODNAMECHAR, MODNAMEMAX - 1);
196
197	/* set the expected errnos... */
198	TEST_EXP_ENOS(exp_enos);
199
200	/* Pause if that option was specified
201	 * TEST_PAUSE contains the code to fork the test with the -c option.
202	 */
203	TEST_PAUSE;
204}
205
206/*
207 * cleanup()
208 *	performs all ONE TIME cleanup for this test at
209 *	completion or premature exit
210 */
211void cleanup(void)
212{
213	/*
214	 * print timing stats if that option was specified.
215	 * print errno log if that option was specified.
216	 */
217	TEST_CLEANUP;
218
219}
220