1 /*
2 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author: Alexey Kodanev <[email protected]>
19 *
20 * Test checks block device kernel API.
21 */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include "test.h"
30 #include "safe_macros.h"
31 #include "old_module.h"
32
33 char *TCID = "block_dev";
34 int TST_TOTAL = 9;
35
36 static const char module_name[] = "ltp_block_dev.ko";
37 static const char dev_result[] = "/sys/devices/ltp_block_dev/result";
38 static const char dev_tcase[] = "/sys/devices/ltp_block_dev/tcase";
39 static int module_loaded;
40
41 static int run_all_testcases;
42 static const option_t options[] = {
43 {"a", &run_all_testcases, NULL},
44 {NULL, NULL, NULL}
45 };
46
cleanup(void)47 static void cleanup(void)
48 {
49 if (module_loaded)
50 tst_module_unload(NULL, module_name);
51 }
52
help(void)53 static void help(void)
54 {
55 printf(" -a Run all test-cases (can crash the kernel)\n");
56 }
57
setup(int argc,char * argv[])58 void setup(int argc, char *argv[])
59 {
60 tst_parse_opts(argc, argv, options, help);
61
62 tst_require_root();
63
64 tst_sig(FORK, DEF_HANDLER, cleanup);
65 }
66
test_run(void)67 static void test_run(void)
68 {
69 int off = 0;
70 /*
71 * test-cases #8 and #9 can crash the kernel.
72 * We have to wait for kernel fix where register_blkdev() &
73 * unregister_blkdev() checks the input device name parameter
74 * against NULL pointer.
75 */
76 if (!run_all_testcases)
77 off = 2;
78
79 tst_module_load(cleanup, module_name, NULL);
80 module_loaded = 1;
81
82 int i, pass = 0;
83 for (i = 0; i < TST_TOTAL - off; ++i) {
84 SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i + 1);
85 SAFE_FILE_SCANF(cleanup, dev_result, "%d", &pass);
86 tst_resm((pass) ? TPASS : TFAIL, "Test-case '%d'", i + 1);
87 }
88 }
89
main(int argc,char * argv[])90 int main(int argc, char *argv[])
91 {
92 setup(argc, argv);
93
94 test_run();
95
96 cleanup();
97
98 tst_exit();
99 }
100