1 /*
2  * Copyright (c) 2022 Antmicro
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/fs/fs.h>
10 
11 /* Mount point and paths must be provided by test runner. */
12 extern struct fs_mount_t *fs_mkfs_mp;
13 extern const int fs_mkfs_type;
14 extern uintptr_t fs_mkfs_dev_id;
15 extern int fs_mkfs_flags;
16 extern const char *some_file_path;
17 extern const char *other_dir_path;
18 
19 /* This test assumes that storage device is erased. */
test_fs_mkfs_simple(void)20 void test_fs_mkfs_simple(void)
21 {
22 	int ret = 0;
23 
24 	TC_PRINT("Mount with flag no format\n");
25 	fs_mkfs_mp->flags = FS_MOUNT_FLAG_NO_FORMAT;
26 	ret = fs_mount(fs_mkfs_mp);
27 
28 	if (fs_mkfs_mp->type == FS_LITTLEFS) {
29 		zassert_equal(ret, -EFAULT, "Expected EFAULT got %d", ret);
30 	} else if (fs_mkfs_mp->type == FS_FATFS) {
31 		zassert_equal(ret, -ENODEV, "Expected ENODEV got %d", ret);
32 	}
33 
34 	TC_PRINT("Try mkfs with RW access\n");
35 	ret = fs_mkfs(fs_mkfs_type, fs_mkfs_dev_id, NULL, fs_mkfs_dev_id);
36 	zassert_equal(ret, 0, "Expected successful mkfs (%d)", ret);
37 
38 	TC_PRINT("Mount created file system without formatting\n");
39 	fs_mkfs_mp->flags = FS_MOUNT_FLAG_NO_FORMAT;
40 	ret = fs_mount(fs_mkfs_mp);
41 	zassert_equal(ret, 0, "Expected successful mount (%d)", ret);
42 	ret = fs_unmount(fs_mkfs_mp);
43 	zassert_equal(ret, 0, "Expected fs_unmount success (%d)", ret);
44 }
45 
46 /* This test assumes that storage device is erased. */
test_fs_mkfs_ops(void)47 void test_fs_mkfs_ops(void)
48 {
49 	int ret = 0;
50 	struct fs_file_t fs;
51 	struct fs_dir_t dir;
52 	struct fs_dirent entry;
53 
54 	fs_file_t_init(&fs);
55 	fs_dir_t_init(&dir);
56 
57 	/* Format volume and add some files/dirs */
58 	TC_PRINT("Mount to prepare tests\n");
59 	fs_mkfs_mp->flags = 0;
60 	ret = fs_mount(fs_mkfs_mp);
61 	zassert_equal(ret, 0, "Expected success (%d)", ret);
62 	TC_PRINT("Create some file\n");
63 	ret = fs_open(&fs, some_file_path, FS_O_CREATE);
64 	zassert_equal(ret, 0, "Expected success fs_open(FS_O_CREATE) (%d)",
65 		      ret);
66 	ret = fs_close(&fs);
67 	zassert_equal(ret, 0, "Expected fs_close success (%d)", ret);
68 	TC_PRINT("Create other directory\n");
69 	ret = fs_mkdir(other_dir_path);
70 	zassert_equal(ret, 0, "Expected fs_mkdir success (%d)", ret);
71 	ret = fs_unmount(fs_mkfs_mp);
72 	zassert_equal(ret, 0, "Expected fs_umount success (%d)", ret);
73 
74 	/* Format file system using fs_mkfs and check if it was formatted */
75 	TC_PRINT("Try mkfs\n");
76 	ret = fs_mkfs(fs_mkfs_type, fs_mkfs_dev_id, NULL, fs_mkfs_dev_id);
77 	zassert_equal(ret, 0, "Expected successful format (%d)", ret);
78 	fs_mkfs_mp->flags = FS_MOUNT_FLAG_NO_FORMAT;
79 	ret = fs_mount(fs_mkfs_mp);
80 	zassert_equal(ret, 0, "Expected successful mount (%d)", ret);
81 	ret = fs_stat(some_file_path, &entry);
82 	zassert_equal(ret, -ENOENT, "Expected ENOENT got %d", ret);
83 	ret = fs_stat(other_dir_path, &entry);
84 	zassert_equal(ret, -ENOENT, "Expected ENOENT got %d", ret);
85 	TC_PRINT("Create some file\n");
86 	ret = fs_open(&fs, some_file_path, FS_O_CREATE);
87 	zassert_equal(ret, 0, "Expected success fs_open(FS_O_CREATE) (%d)",
88 		      ret);
89 	ret = fs_close(&fs);
90 	zassert_equal(ret, 0, "Expected fs_close success (%d)", ret);
91 	TC_PRINT("Create other directory\n");
92 	ret = fs_mkdir(other_dir_path);
93 	zassert_equal(ret, 0, "Expected fs_mkdir success (%d)", ret);
94 	ret = fs_unmount(fs_mkfs_mp);
95 	zassert_equal(ret, 0, "Expected fs_unmount success (%d)", ret);
96 }
97