1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
4 * Email: code@zilogic.com
5 */
6
7 /*
8 * Test statx
9 *
10 * 1) STATX_ATTR_ENCRYPTED - A key is required for the file to be encrypted by
11 * the filesystem.
12 *
13 * e4crypt is used to set the encrypt flag (currently supported only by ext4).
14 *
15 * Two directories are tested.
16 * First directory has all flags set.
17 * Second directory has no flags set.
18 *
19 * Minimum kernel version required is 4.11.
20 */
21
22 #define _GNU_SOURCE
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include "tst_test.h"
28 #include "lapi/fs.h"
29 #include "lapi/stat.h"
30
31 #define MNTPOINT "mnt_point"
32 #define TESTDIR_FLAGGED MNTPOINT"/test_dir1"
33 #define TESTDIR_UNFLAGGED MNTPOINT"/test_dir2"
34
35 static int mount_flag;
36
test_flagged(void)37 static void test_flagged(void)
38 {
39 struct statx buf;
40
41 TEST(statx(AT_FDCWD, TESTDIR_FLAGGED, 0, 0, &buf));
42 if (TST_RET == 0)
43 tst_res(TPASS,
44 "sys_statx(AT_FDCWD, %s, 0, 0, &buf)", TESTDIR_FLAGGED);
45 else
46 tst_brk(TFAIL | TTERRNO,
47 "sys_statx(AT_FDCWD, %s, 0, 0, &buf)", TESTDIR_FLAGGED);
48
49 if (buf.stx_attributes & STATX_ATTR_ENCRYPTED)
50 tst_res(TPASS, "STATX_ATTR_ENCRYPTED flag is set");
51 else
52 tst_res(TFAIL, "STATX_ATTR_ENCRYPTED flag is not set");
53 }
54
test_unflagged(void)55 static void test_unflagged(void)
56 {
57 struct statx buf;
58
59 TEST(statx(AT_FDCWD, TESTDIR_UNFLAGGED, 0, 0, &buf));
60 if (TST_RET == 0)
61 tst_res(TPASS,
62 "sys_statx(AT_FDCWD, %s, 0, 0, &buf)",
63 TESTDIR_UNFLAGGED);
64 else
65 tst_brk(TFAIL | TTERRNO,
66 "sys_statx(AT_FDCWD, %s, 0, 0, &buf)",
67 TESTDIR_UNFLAGGED);
68
69 if ((buf.stx_attributes & STATX_ATTR_ENCRYPTED) == 0)
70 tst_res(TPASS, "STATX_ATTR_ENCRYPTED flag is not set");
71 else
72 tst_res(TFAIL, "STATX_ATTR_ENCRYPTED flag is set");
73 }
74
75 struct test_cases {
76 void (*tfunc)(void);
77 } tcases[] = {
78 {&test_flagged},
79 {&test_unflagged},
80 };
81
run(unsigned int i)82 static void run(unsigned int i)
83 {
84 tcases[i].tfunc();
85 }
86
setup(void)87 static void setup(void)
88 {
89 char opt_bsize[32];
90 const char *const fs_opts[] = {"-O encrypt", opt_bsize, NULL};
91 int ret;
92
93 snprintf(opt_bsize, sizeof(opt_bsize), "-b %i", getpagesize());
94
95 SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
96 SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, 0);
97 mount_flag = 1;
98
99 SAFE_MKDIR(TESTDIR_FLAGGED, 0777);
100 SAFE_MKDIR(TESTDIR_UNFLAGGED, 0777);
101
102 ret = tst_system("echo qwery | e4crypt add_key "TESTDIR_FLAGGED);
103
104 if (WEXITSTATUS(ret) == 127)
105 tst_brk(TCONF, "e4crypt not installed!");
106
107 if (WEXITSTATUS(ret))
108 tst_brk(TCONF, "e4crypt failed (CONFIG_EXT4_ENCRYPTION not set?)");
109 }
110
cleanup(void)111 static void cleanup(void)
112 {
113 if (mount_flag)
114 tst_umount(MNTPOINT);
115 }
116
117 static struct tst_test test = {
118 .test = run,
119 .tcnt = ARRAY_SIZE(tcases),
120 .setup = setup,
121 .cleanup = cleanup,
122 .min_kver = "4.11",
123 .needs_root = 1,
124 .needs_device = 1,
125 .mntpoint = MNTPOINT,
126 .dev_fs_type = "ext4",
127 };
128