1 /*
2 * Copyright (c) 2023 Antmicro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/fs/ext2.h>
10 #include "utils.h"
11
ZTEST(ext2tests,test_mount_only)12 ZTEST(ext2tests, test_mount_only)
13 {
14 struct fs_mount_t *mp = &testfs_mnt;
15 int ret = 0;
16
17 /* Test FS_MOUNT_FLAG_NO_FORMAT flag */
18 mp->flags |= FS_MOUNT_FLAG_NO_FORMAT;
19 ret = fs_mount(mp);
20 TC_PRINT("Mount unformatted with FS_MOUNT_FLAG_NO_FORMAT set\n");
21 zassert_false(ret == 0, "Expected failure (ret=%d)", ret);
22
23 /* Test FS_MOUNT_FLAG_READ_ONLY on non-formatted volume*/
24 mp->flags = FS_MOUNT_FLAG_READ_ONLY;
25 ret = fs_mount(mp);
26 TC_PRINT("Mount unformatted with FS_MOUNT_FLAG_READ_ONLY set\n");
27 zassert_false(ret == 0, "Expected failure (ret=%d)", ret);
28
29 /* Format volume and add some files/dirs to check read-only flag */
30 mp->flags = 0;
31 ret = fs_mount(mp);
32 TC_PRINT("Mount again to format volume\n");
33 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
34 TC_PRINT("Create some file\n");
35
36 ret = fs_unmount(mp);
37 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
38
39 /* Check fs operation on volume mounted with FS_MOUNT_FLAG_READ_ONLY */
40 mp->flags = FS_MOUNT_FLAG_READ_ONLY;
41 TC_PRINT("Mount as read-only\n");
42 ret = fs_mount(mp);
43 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
44
45 ret = fs_unmount(mp);
46 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
47 }
48
ZTEST(ext2tests,test_statvfs)49 ZTEST(ext2tests, test_statvfs)
50 {
51 int ret = 0;
52 struct fs_statvfs sbuf;
53 struct fs_mount_t *mp = &testfs_mnt;
54 size_t partition_size = MIN(0x800000, get_partition_size((uintptr_t)mp->storage_dev));
55
56 mp->flags = 0;
57 ret = fs_mount(mp);
58 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
59
60 ret = fs_statvfs(mp->mnt_point, &sbuf);
61 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
62
63 TC_PRINT("Mounted file system: bsize:%lu frsize:%lu blocks:%lu, bfree:%lu\n",
64 sbuf.f_bsize, sbuf.f_frsize, sbuf.f_blocks, sbuf.f_bfree);
65
66 zassert_equal(sbuf.f_bsize, 1024,
67 "Wrong block size %lu (expected %lu)", sbuf.f_bsize, 1024);
68 zassert_equal(sbuf.f_frsize, 1024,
69 "Wrong frag size %lu (expected %lu)", sbuf.f_frsize, 1024);
70 zassert_equal(sbuf.f_blocks, partition_size / 1024,
71 "Wrong block count %lu (expected %lu)",
72 sbuf.f_blocks, partition_size / 1024);
73
74 ret = fs_unmount(mp);
75 zassert_equal(ret, 0, "Expected success (ret=%d)", ret);
76 }
77
78 /* Tests from common directory */
79
80 void test_fs_mkfs_simple(void);
81
82 /* Global variables expected by tests */
83 struct fs_mount_t *fs_mkfs_mp = &testfs_mnt;
84 const int fs_mkfs_type = FS_EXT2;
85 uintptr_t fs_mkfs_dev_id;
86 int fs_mkfs_flags;
87
ZTEST(ext2tests,test_mkfs_simple)88 ZTEST(ext2tests, test_mkfs_simple)
89 {
90 struct fs_mount_t *mp = &testfs_mnt;
91
92 fs_mkfs_dev_id = (uintptr_t) mp->storage_dev;
93 fs_mkfs_flags = 0;
94 test_fs_mkfs_simple();
95 }
96
mkfs_custom_config(struct ext2_cfg * cfg)97 void mkfs_custom_config(struct ext2_cfg *cfg)
98 {
99 int ret = 0;
100 struct fs_statvfs sbuf;
101 struct fs_mount_t *mp = &testfs_mnt;
102 size_t partition_size = MIN(cfg->fs_size, get_partition_size((uintptr_t)mp->storage_dev));
103
104 ret = fs_mkfs(FS_EXT2, (uintptr_t)mp->storage_dev, cfg, 0);
105 zassert_equal(ret, 0, "Failed to mkfs with 2K blocks");
106
107 mp->flags = FS_MOUNT_FLAG_NO_FORMAT;
108 ret = fs_mount(mp);
109 zassert_equal(ret, 0, "Mount failed (ret=%d)", ret);
110
111 ret = fs_statvfs(mp->mnt_point, &sbuf);
112 zassert_equal(ret, 0, "Statvfs failed (ret=%d)", ret);
113
114 TC_PRINT("Mounted file system: bsize:%lu frsize:%lu blocks:%lu, bfree:%lu\n",
115 sbuf.f_bsize, sbuf.f_frsize, sbuf.f_blocks, sbuf.f_bfree);
116
117 zassert_equal(sbuf.f_bsize, cfg->block_size,
118 "Wrong block size %lu (expected %lu)", sbuf.f_bsize, cfg->block_size);
119 zassert_equal(sbuf.f_frsize, cfg->block_size,
120 "Wrong frag size %lu (expected %lu)", sbuf.f_frsize, cfg->block_size);
121 zassert_equal(sbuf.f_blocks, partition_size / cfg->block_size,
122 "Wrong block count %lu (expected %lu)",
123 sbuf.f_blocks, partition_size / cfg->block_size);
124
125 ret = fs_unmount(mp);
126 zassert_equal(ret, 0, "Unmount failed (ret=%d)", ret);
127 }
128
129 #if defined(CONFIG_APP_TEST_BIG)
ZTEST(ext2tests,test_mkfs_custom_2K)130 ZTEST(ext2tests, test_mkfs_custom_2K)
131 {
132 struct ext2_cfg config = {
133 .block_size = 2048,
134 .fs_size = 0x2000000,
135 .bytes_per_inode = 0,
136 .volume_name[0] = 0,
137 .set_uuid = false,
138 };
139
140 mkfs_custom_config(&config);
141 }
142
ZTEST(ext2tests,test_mkfs_custom_4K)143 ZTEST(ext2tests, test_mkfs_custom_4K)
144 {
145 struct ext2_cfg config = {
146 .block_size = 4096,
147 .fs_size = 0x8000000,
148 .bytes_per_inode = 0,
149 .volume_name[0] = 0,
150 .set_uuid = false,
151 };
152
153 mkfs_custom_config(&config);
154 }
155 #endif
156