1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #include "test_fat.h"
9 #include <stdio.h>
10
11 extern int test_file_write(void);
12 extern int test_file_close(void);
13 static int test_rmdir(void);
14
test_mkdir(void)15 static int test_mkdir(void)
16 {
17 int res;
18
19 TC_PRINT("\nmkdir tests:\n");
20
21 if (check_file_dir_exists(TEST_DIR)) {
22 TC_PRINT("[%s] exists, delete it\n", TEST_DIR);
23 if (test_rmdir()) {
24 TC_PRINT("Error deleting dir %s\n", TEST_DIR);
25 return TC_FAIL;
26 }
27 } else {
28 TC_PRINT("Creating new dir %s\n", TEST_DIR);
29 }
30
31 /* Verify fs_mkdir() */
32 res = fs_mkdir(TEST_DIR);
33 if (res) {
34 TC_PRINT("Error creating dir[%d]\n", res);
35 return res;
36 }
37
38 res = fs_open(&filep, TEST_DIR_FILE, FS_O_CREATE | FS_O_RDWR);
39 if (res) {
40 TC_PRINT("Failed opening file [%d]\n", res);
41 return res;
42 }
43
44 res = test_file_write();
45 if (res) {
46 return res;
47 }
48
49 res = fs_close(&filep);
50 if (res) {
51 TC_PRINT("Error closing file [%d]\n", res);
52 return res;
53 }
54
55 TC_PRINT("Created dir %s!\n", TEST_DIR);
56
57 return res;
58 }
59
test_lsdir(const char * path)60 static int test_lsdir(const char *path)
61 {
62 int res;
63 struct fs_dir_t dirp;
64 struct fs_dirent entry;
65
66 TC_PRINT("\nlsdir tests:\n");
67
68 fs_dir_t_init(&dirp);
69
70 /* Verify fs_opendir() */
71 res = fs_opendir(&dirp, path);
72 if (res) {
73 TC_PRINT("Error opening dir %s [%d]\n", path, res);
74 return res;
75 }
76
77 TC_PRINT("\nListing dir %s:\n", path);
78 for (;;) {
79 /* Verify fs_readdir() */
80 res = fs_readdir(&dirp, &entry);
81
82 /* entry.name[0] == 0 means end-of-dir */
83 if (res || entry.name[0] == 0) {
84 break;
85 }
86
87 if (entry.type == FS_DIR_ENTRY_DIR) {
88 TC_PRINT("[DIR ] %s\n", entry.name);
89 } else {
90 TC_PRINT("[FILE] %s (size = %zu)\n",
91 entry.name, entry.size);
92 }
93 }
94
95 /* Verify fs_closedir() */
96 fs_closedir(&dirp);
97
98 return res;
99 }
100
test_rmdir(void)101 static int test_rmdir(void)
102 {
103 int res;
104 struct fs_dir_t dirp;
105 static struct fs_dirent entry;
106 char file_path[80 + MAX_FILE_NAME];
107
108 TC_PRINT("\nrmdir tests:\n");
109
110 fs_dir_t_init(&dirp);
111
112 if (!check_file_dir_exists(TEST_DIR)) {
113 TC_PRINT("%s doesn't exist\n", TEST_DIR);
114 return TC_FAIL;
115 }
116
117 res = fs_opendir(&dirp, TEST_DIR);
118 if (res) {
119 TC_PRINT("Error opening dir[%d]\n", res);
120 return res;
121 }
122
123 TC_PRINT("\nRemoving files and sub directories in %s\n", TEST_DIR);
124 for (;;) {
125 res = fs_readdir(&dirp, &entry);
126
127 /* entry.name[0] == 0 means end-of-dir */
128 if (res || entry.name[0] == 0) {
129 break;
130 }
131
132 /* Delete file or sub directory */
133 sprintf(file_path, "%s/%s", TEST_DIR, entry.name);
134 TC_PRINT("Removing %s\n", file_path);
135
136 res = fs_unlink(file_path);
137 if (res) {
138 TC_PRINT("Error deleting file/dir [%d]\n", res);
139 fs_closedir(&dirp);
140 return res;
141 }
142 }
143
144 fs_closedir(&dirp);
145
146 /* Verify fs_unlink() */
147 res = fs_unlink(TEST_DIR);
148 if (res) {
149 TC_PRINT("Error removing dir [%d]\n", res);
150 return res;
151 }
152
153 TC_PRINT("Removed dir %s!\n", TEST_DIR);
154
155 return res;
156 }
157
test_fat_dir(void)158 void test_fat_dir(void)
159 {
160 zassert_true(test_mkdir() == TC_PASS);
161 zassert_true(test_lsdir(FATFS_MNTP) == TC_PASS);
162 zassert_true(test_lsdir(TEST_DIR) == TC_PASS);
163 zassert_true(test_rmdir() == TC_PASS);
164 zassert_true(test_lsdir(FATFS_MNTP) == TC_PASS);
165 }
166