Zeng Linggang | 11018d2 | 2014-05-30 10:57:42 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | /* |
| 18 | * Test Description: |
| 19 | * Verify that, |
| 20 | * 1. path is NULL, EFAULT would return. |
| 21 | * 2. Too many symbolic links were encountered in translating path, |
| 22 | * ELOOP would return. |
| 23 | * 3. path is too long, ENAMETOOLONG would return. |
| 24 | * 4. The file referred to by path does not exist, ENOENT would return. |
| 25 | * 5. A component of the path prefix of path is not a directory, |
| 26 | * ENOENT would return. |
| 27 | */ |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <sys/statvfs.h> |
| 31 | #include <sys/mman.h> |
| 32 | |
| 33 | #include "test.h" |
Zeng Linggang | 11018d2 | 2014-05-30 10:57:42 +0800 | [diff] [blame] | 34 | #include "safe_macros.h" |
| 35 | |
| 36 | #define TEST_SYMLINK "statvfs_symlink" |
| 37 | #define TEST_FILE "statvfs_file" |
| 38 | |
| 39 | char *TCID = "statvfs02"; |
| 40 | |
| 41 | static struct statvfs buf; |
| 42 | static char nametoolong[PATH_MAX+2]; |
| 43 | static void setup(void); |
| 44 | static void cleanup(void); |
| 45 | |
| 46 | static struct test_case_t { |
| 47 | char *path; |
| 48 | struct statvfs *buf; |
| 49 | int exp_errno; |
| 50 | } test_cases[] = { |
| 51 | {NULL, &buf, EFAULT}, |
| 52 | {TEST_SYMLINK, &buf, ELOOP}, |
| 53 | {nametoolong, &buf, ENAMETOOLONG}, |
| 54 | {"filenoexist", &buf, ENOENT}, |
| 55 | {"statvfs_file/test", &buf, ENOTDIR}, |
| 56 | }; |
| 57 | |
| 58 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
Zeng Linggang | 11018d2 | 2014-05-30 10:57:42 +0800 | [diff] [blame] | 59 | static void statvfs_verify(const struct test_case_t *); |
| 60 | |
| 61 | int main(int argc, char **argv) |
| 62 | { |
| 63 | int i, lc; |
| 64 | const char *msg; |
| 65 | |
| 66 | msg = parse_opts(argc, argv, NULL, NULL); |
| 67 | if (msg != NULL) |
| 68 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 69 | |
| 70 | setup(); |
| 71 | |
| 72 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 73 | tst_count = 0; |
| 74 | for (i = 0; i < TST_TOTAL; i++) |
| 75 | statvfs_verify(&test_cases[i]); |
| 76 | } |
| 77 | |
| 78 | cleanup(); |
| 79 | tst_exit(); |
| 80 | } |
| 81 | |
| 82 | static void setup(void) |
| 83 | { |
| 84 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 85 | |
Zeng Linggang | 11018d2 | 2014-05-30 10:57:42 +0800 | [diff] [blame] | 86 | TEST_PAUSE; |
| 87 | |
| 88 | tst_tmpdir(); |
| 89 | |
| 90 | SAFE_SYMLINK(cleanup, TEST_SYMLINK, "statfs_symlink_2"); |
| 91 | SAFE_SYMLINK(cleanup, "statfs_symlink_2", TEST_SYMLINK); |
| 92 | |
| 93 | memset(nametoolong, 'a', PATH_MAX+1); |
| 94 | |
| 95 | SAFE_TOUCH(cleanup, TEST_FILE, 0644, NULL); |
| 96 | |
| 97 | test_cases[0].path = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE, |
| 98 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 99 | } |
| 100 | |
| 101 | static void statvfs_verify(const struct test_case_t *test) |
| 102 | { |
| 103 | TEST(statvfs(test->path, test->buf)); |
| 104 | |
| 105 | if (TEST_RETURN != -1) { |
| 106 | tst_resm(TFAIL, "statvfs() succeeded unexpectedly"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (TEST_ERRNO == test->exp_errno) { |
| 111 | tst_resm(TPASS | TTERRNO, "statvfs() failed as expected"); |
| 112 | } else { |
| 113 | tst_resm(TFAIL | TTERRNO, |
| 114 | "statvfs() failed unexpectedly; expected: %d - %s", |
| 115 | test->exp_errno, strerror(test->exp_errno)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static void cleanup(void) |
| 120 | { |
Zeng Linggang | 11018d2 | 2014-05-30 10:57:42 +0800 | [diff] [blame] | 121 | tst_rmdir(); |
| 122 | } |