1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include "test_fs.h"
9 
10 static struct test_fs_data test_data;
11 
12 static struct fs_mount_t mp = {
13 		.type = TEST_FS_1,
14 		.mnt_point = TEST_FS_MNTP,
15 		.fs_data = &test_data,
16 };
17 
test_mount_flags(void)18 void test_mount_flags(void)
19 {
20 	int ret = 0;
21 	struct fs_file_t fs;
22 
23 	fs_file_t_init(&fs);
24 
25 	/* Format volume and add some files/dirs to check read-only flag */
26 	mp.flags = 0;
27 	TC_PRINT("Mount to prepare tests\n");
28 	ret = fs_mount(&mp);
29 	zassert_equal(ret, 0, "Expected success (%d)", ret);
30 	TC_PRINT("Create some file\n");
31 	ret = fs_open(&fs, TEST_FS_MNTP"/some", FS_O_CREATE);
32 	zassert_equal(ret, 0, "Expected success fs_open(FS_O_CREATE) (%d)",
33 		      ret);
34 	ret = fs_close(&fs);
35 	zassert_equal(ret, 0, "Expected fs_close success (%d)", ret);
36 	TC_PRINT("Create other directory\n");
37 	ret = fs_mkdir(TEST_FS_MNTP"/other");
38 	zassert_equal(ret, 0, "Expected fs_mkdir success (%d)", ret);
39 	ret = fs_unmount(&mp);
40 	zassert_equal(ret, 0, "Expected fs_umount success (%d)", ret);
41 
42 	/* Check fs operation on volume mounted with FS_MOUNT_FLAG_READ_ONLY */
43 	mp.flags = FS_MOUNT_FLAG_READ_ONLY;
44 	TC_PRINT("Mount as read-only\n");
45 	ret = fs_mount(&mp);
46 	zassert_equal(ret, 0, "Expected fs_mount success (%d)", ret);
47 
48 	/* Attempt creating new file */
49 	ret = fs_open(&fs, TEST_FS_MNTP"/nosome", FS_O_CREATE);
50 	zassert_equal(ret, -EROFS, "Expected EROFS got %d", ret);
51 	ret = fs_mkdir(TEST_FS_MNTP"/another");
52 	zassert_equal(ret, -EROFS, "Expected EROFS got %d", ret);
53 	ret = fs_rename(TEST_FS_MNTP"/some", TEST_FS_MNTP"/nosome");
54 	zassert_equal(ret, -EROFS, "Expected EROFS got %d", ret);
55 	ret = fs_unlink(TEST_FS_MNTP"/some");
56 	zassert_equal(ret, -EROFS, "Expected EROFS got %d", ret);
57 	ret = fs_open(&fs, TEST_FS_MNTP"/other", FS_O_CREATE);
58 	zassert_equal(ret, -EROFS, "Expected EROFS got %d", ret);
59 	ret = fs_open(&fs, TEST_FS_MNTP"/some", FS_O_RDWR);
60 	zassert_equal(ret, -EROFS, "Expected EROFS got %d", ret);
61 	ret = fs_open(&fs, TEST_FS_MNTP"/some", FS_O_READ);
62 	zassert_equal(ret, 0, "Expected fs_open(FS_O_READ) success (%d)", ret);
63 	ret = fs_close(&fs);
64 	zassert_equal(ret, 0, "Expected fs_close success (%d)", ret);
65 	ret = fs_unmount(&mp);
66 	zassert_equal(ret, 0, "Expected fs_unmount success (%d)", ret);
67 }
68