1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include "test_fat.h"
9 
10 /* mounting info */
11 static struct fs_mount_t fatfs_mnt = {
12 	.type = FS_FATFS,
13 	.mnt_point = FATFS_MNTP,
14 	.fs_data = &fat_fs,
15 };
16 
test_prepare(void)17 static void test_prepare(void)
18 {
19 	struct fs_file_t fs;
20 
21 	fs_file_t_init(&fs);
22 	zassert_equal(fs_mount(&fatfs_mnt), 0);
23 	zassert_equal(fs_open(&fs, FATFS_MNTP"/testfile.txt", FS_O_CREATE),
24 		      0, NULL);
25 	zassert_equal(fs_close(&fs), 0);
26 	zassert_equal(fs_unmount(&fatfs_mnt), 0);
27 }
28 
test_unmount(void)29 static void test_unmount(void)
30 {
31 	zassert_true(fs_unmount(&fatfs_mnt) >= 0);
32 }
33 
test_ops_on_rd(void)34 static void test_ops_on_rd(void)
35 {
36 	struct fs_file_t fs;
37 	int ret;
38 
39 	fs_file_t_init(&fs);
40 	/* Check fs operation on volume mounted with FS_MOUNT_FLAG_READ_ONLY */
41 	fatfs_mnt.flags = FS_MOUNT_FLAG_READ_ONLY;
42 	TC_PRINT("Mount as read-only\n");
43 	ret = fs_mount(&fatfs_mnt);
44 	zassert_equal(ret, 0, "Expected success", ret);
45 
46 	/* Attempt creating new file */
47 	ret = fs_open(&fs, FATFS_MNTP"/nosome", FS_O_CREATE);
48 	zassert_equal(ret, -EROFS, "Expected EROFS", ret);
49 	ret = fs_mkdir(FATFS_MNTP"/another");
50 	zassert_equal(ret, -EROFS, "Expected EROFS", ret);
51 	ret = fs_rename(FATFS_MNTP"/testfile.txt", FATFS_MNTP"/bestfile.txt");
52 	zassert_equal(ret, -EROFS, "Expected EROFS", ret);
53 	ret = fs_unlink(FATFS_MNTP"/testfile.txt");
54 	zassert_equal(ret, -EROFS, "Expected EROFS", ret);
55 	ret = fs_open(&fs, FATFS_MNTP"/testfile.txt", FS_O_RDWR);
56 	zassert_equal(ret, -EROFS, "Expected EROFS", ret);
57 	ret = fs_open(&fs, FATFS_MNTP"/testfile.txt", FS_O_READ);
58 	zassert_equal(ret, 0, "Expected success", ret);
59 	fs_close(&fs);
60 }
61 
ZTEST(fat_fs_basic,test_fat_mount_rd_only)62 ZTEST(fat_fs_basic, test_fat_mount_rd_only)
63 {
64 	test_prepare();
65 	test_ops_on_rd();
66 	test_unmount();
67 }
68