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 
10 #include "utils.h"
11 
ZTEST(ext2tests,test_dirops_basic)12 ZTEST(ext2tests, test_dirops_basic)
13 {
14 	struct fs_mount_t *mp = &testfs_mnt;
15 
16 	zassert_equal(fs_mount(mp), 0, "Mount failed");
17 
18 	struct fs_file_t file;
19 	struct fs_dirent stat;
20 
21 	fs_file_t_init(&file);
22 
23 	/* Create some directories */
24 	zassert_equal(fs_mkdir("/sml/dir1"), 0, "Create dir1 failed");
25 	zassert_equal(fs_mkdir("/sml/dir2"), 0, "Create dir2 failed");
26 
27 	/* Create some files */
28 	zassert_equal(fs_open(&file, "/sml/file1", FS_O_CREATE), 0, "Create file1 failed");
29 	zassert_equal(fs_close(&file), 0, "Close file error");
30 	zassert_equal(fs_open(&file, "/sml/dir1/file2", FS_O_CREATE), 0, "Create file2 failed");
31 	zassert_equal(fs_close(&file), 0, "Close file error");
32 	zassert_equal(fs_open(&file, "/sml/dir2/file3", FS_O_CREATE), 0, "Create file3 failed");
33 	zassert_equal(fs_close(&file), 0, "Close file error");
34 
35 	/* Check if directories will open as files */
36 	zassert_equal(fs_open(&file, "/sml/dir1", 0), -EINVAL, "Should return error");
37 	zassert_equal(fs_open(&file, "/sml/dir2", 0), -EINVAL, "Should return error");
38 
39 	/* Check directories with stat */
40 	zassert_equal(fs_stat("/sml/dir1", &stat), 0, "Stat dir1 failed");
41 	zassert_equal(stat.type, FS_DIR_ENTRY_DIR, "Wrong type");
42 	zassert_equal(stat.size, 0, "Wrong directory size");
43 	zassert_mem_equal(stat.name, "dir1", 5, "Wrong directory name");
44 
45 	zassert_equal(fs_stat("/sml/dir2", &stat), 0, "Stat dir2 failed");
46 	zassert_equal(stat.type, FS_DIR_ENTRY_DIR, "Wrong type");
47 	zassert_equal(stat.size, 0, "Wrong directory size");
48 	zassert_mem_equal(stat.name, "dir2", 5, "Wrong directory name");
49 
50 	/* Check if files will open correctly */
51 	zassert_equal(fs_open(&file, "/sml/file1", 0), 0, "Open file1 should succeed");
52 	zassert_equal(fs_close(&file), 0, "Close file error");
53 	zassert_equal(fs_open(&file, "/sml/dir1/file2", 0), 0, "Open file2 should succeed");
54 	zassert_equal(fs_close(&file), 0, "Close file error");
55 	zassert_equal(fs_open(&file, "/sml/dir2/file3", 0), 0, "Open file3 should succeed");
56 	zassert_equal(fs_close(&file), 0, "Close file error");
57 
58 	/* Check files with stat */
59 	zassert_equal(fs_stat("/sml/file1", &stat), 0, "Stat file1 failed");
60 	zassert_equal(stat.type, FS_DIR_ENTRY_FILE, "Wrong type");
61 	zassert_equal(stat.size, 0, "Wrong file size");
62 	zassert_mem_equal(stat.name, "file1", 6, "Wrong file name");
63 
64 	zassert_equal(fs_stat("/sml/dir1/file2", &stat), 0, "Stat file1 failed");
65 	zassert_equal(stat.type, FS_DIR_ENTRY_FILE, "Wrong type");
66 	zassert_equal(stat.size, 0, "Wrong file size");
67 	zassert_mem_equal(stat.name, "file2", 6, "Wrong file name");
68 
69 	zassert_equal(fs_stat("/sml/dir2/file3", &stat), 0, "Stat file1 failed");
70 	zassert_equal(stat.type, FS_DIR_ENTRY_FILE, "Wrong type");
71 	zassert_equal(stat.size, 0, "Wrong file size");
72 	zassert_mem_equal(stat.name, "file3", 6, "Wrong file name");
73 
74 	/* Check for some nonexisting files */
75 	zassert_equal(fs_open(&file, "/sml/file2", 0), -ENOENT, "Should not exist");
76 	zassert_equal(fs_open(&file, "/sml/file3", 0), -ENOENT, "Should not exist");
77 	zassert_equal(fs_open(&file, "/sml/dir1/file1", 0), -ENOENT, "Should not exist");
78 	zassert_equal(fs_open(&file, "/sml/dir1/file3", 0), -ENOENT, "Should not exist");
79 	zassert_equal(fs_open(&file, "/sml/dir2/file1", 0), -ENOENT, "Should not exist");
80 	zassert_equal(fs_open(&file, "/sml/dir2/file2", 0), -ENOENT, "Should not exist");
81 
82 	zassert_equal(fs_unmount(mp), 0, "Unmount failed");
83 }
84