1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <[email protected]>
4 */
5
6 /*\
7 * [Description]
8 *
9 * Failure tests for name_to_handle_at().
10 */
11
12 #define _GNU_SOURCE
13 #include "lapi/name_to_handle_at.h"
14
15 #define TEST_FILE "test_file"
16
17 static struct file_handle fh, high_fh = {.handle_bytes = MAX_HANDLE_SZ + 1};
18 static struct file_handle *valid_fhp = &fh, *invalid_fhp, *high_fhp = &high_fh;
19 static int mount_id, *valid_mount_id = &mount_id, *invalid_mount_id;
20 static const char *valid_path = TEST_FILE, *invalid_path, *empty_path = "";
21
22 static struct tcase {
23 const char *name;
24 int dfd;
25 const char **pathname;
26 int flags;
27 struct file_handle **fhp;
28 int **mount_id;
29 int exp_errno;
30 } tcases[] = {
31 {"invalid-dfd", -1, &valid_path, 0, &valid_fhp, &valid_mount_id, EBADF},
32 {"not a directory", 0, &valid_path, 0, &valid_fhp, &valid_mount_id, ENOTDIR},
33 {"invalid-path", AT_FDCWD, &invalid_path, 0, &valid_fhp, &valid_mount_id, EFAULT},
34 {"invalid-file-handle", AT_FDCWD, &valid_path, 0, &invalid_fhp, &valid_mount_id, EFAULT},
35 {"zero-file-handle-size", AT_FDCWD, &valid_path, 0, &valid_fhp, &valid_mount_id, EOVERFLOW},
36 {"high-file-handle-size", AT_FDCWD, &valid_path, 0, &high_fhp, &valid_mount_id, EINVAL},
37 {"invalid-mount_id", AT_FDCWD, &valid_path, 0, &valid_fhp, &invalid_mount_id, EFAULT},
38 {"invalid-flags", AT_FDCWD, &valid_path, -1, &valid_fhp, &valid_mount_id, EINVAL},
39 {"empty-path", AT_FDCWD, &empty_path, 0, &valid_fhp, &valid_mount_id, ENOENT},
40 };
41
setup(void)42 static void setup(void)
43 {
44 void *faulty_address;
45
46 SAFE_TOUCH(TEST_FILE, 0600, NULL);
47 faulty_address = tst_get_bad_addr(NULL);
48 invalid_fhp = faulty_address;
49 invalid_mount_id = faulty_address;
50 invalid_path = faulty_address;
51 }
52
run(unsigned int n)53 static void run(unsigned int n)
54 {
55 struct tcase *tc = &tcases[n];
56
57 memset(&fh, 0, sizeof(fh));
58
59 TEST(name_to_handle_at(tc->dfd, *tc->pathname, *tc->fhp, *tc->mount_id,
60 tc->flags));
61
62 if (TST_RET != -1) {
63 tst_res(TFAIL, "%s: name_to_handle_at() passed unexpectedly",
64 tc->name);
65 return;
66 }
67
68 if (tc->exp_errno != TST_ERR) {
69 tst_res(TFAIL | TTERRNO,
70 "%s: name_to_handle_at() should fail with %s", tc->name,
71 tst_strerrno(tc->exp_errno));
72 return;
73 }
74
75 tst_res(TPASS | TTERRNO, "%s: name_to_handle_at() failed as expected",
76 tc->name);
77 }
78
79 static struct tst_test test = {
80 .tcnt = ARRAY_SIZE(tcases),
81 .test = run,
82 .setup = setup,
83 .needs_tmpdir = 1,
84 };
85