1 /*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 * Copyright (c) 2023 Antmicro
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/fs/fs.h>
11
12 /* Mount point for test should be provided by test runner */
13 extern struct fs_mount_t *mount_flags_mp;
14 extern const char *mount_flags_mnt_point_str;
15
test_fs_mount_flags(void)16 void test_fs_mount_flags(void)
17 {
18 int ret = 0;
19 struct fs_file_t fs;
20 struct fs_mount_t *mp = mount_flags_mp;
21
22 fs_file_t_init(&fs);
23
24 /* Test FS_MOUNT_FLAG_NO_FORMAT flag */
25 mp->flags |= FS_MOUNT_FLAG_NO_FORMAT;
26 ret = fs_mount(mp);
27 TC_PRINT("Mount unformatted with FS_MOUNT_FLAG_NO_FORMAT set\n");
28 zassert_false(ret == 0, "Expected failure", ret);
29
30 /* Test FS_MOUNT_FLAG_READ_ONLY on non-formatted volume*/
31 mp->flags = FS_MOUNT_FLAG_READ_ONLY;
32 ret = fs_mount(mp);
33 TC_PRINT("Mount unformatted with FS_MOUNT_FLAG_READ_ONLY set\n");
34 zassert_false(ret == 0, "Expected failure", ret);
35
36 /* Format volume and add some files/dirs to check read-only flag */
37 mp->flags = 0;
38 ret = fs_mount(mp);
39 TC_PRINT("Mount again to format volume\n");
40 zassert_equal(ret, 0, "Expected success", ret);
41 TC_PRINT("Create some file\n");
42 ret = fs_open(&fs, "/sml/some", FS_O_CREATE);
43 zassert_equal(ret, 0, "Expected success", ret);
44 fs_close(&fs);
45 TC_PRINT("Create other directory\n");
46 ret = fs_mkdir("/sml/other");
47 zassert_equal(ret, 0, "Expected success", ret);
48
49 ret = fs_unmount(mp);
50 zassert_equal(ret, 0, "Expected success", ret);
51
52 /* Check fs operation on volume mounted with FS_MOUNT_FLAG_READ_ONLY */
53 mp->flags = FS_MOUNT_FLAG_READ_ONLY;
54 TC_PRINT("Mount as read-only\n");
55 ret = fs_mount(mp);
56 zassert_equal(ret, 0, "Expected success", ret);
57
58 /* Attempt creating new file */
59 ret = fs_open(&fs, "/sml/nosome", FS_O_CREATE);
60 zassert_equal(ret, -EROFS, "Expected EROFS", ret);
61 ret = fs_mkdir("/sml/another");
62 zassert_equal(ret, -EROFS, "Expected EROFS", ret);
63 ret = fs_rename("/sml/some", "/sml/nosome");
64 zassert_equal(ret, -EROFS, "Expected EROFS", ret);
65 ret = fs_unlink("/sml/some");
66 zassert_equal(ret, -EROFS, "Expected EROFS", ret);
67 ret = fs_open(&fs, "/sml/other", FS_O_CREATE);
68 zassert_equal(ret, -EROFS, "Expected EROFS", ret);
69 ret = fs_open(&fs, "/sml/some", FS_O_RDWR);
70 zassert_equal(ret, -EROFS, "Expected EROFS", ret);
71 ret = fs_open(&fs, "/sml/some", FS_O_READ);
72 zassert_equal(ret, 0, "Expected success", ret);
73 fs_close(&fs);
74
75 ret = fs_unmount(mp);
76 zassert_equal(ret, 0, "Expected success", ret);
77 }
78