1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-09-03 Rbb666 the first version for DFS API utest
*/
#include <rtthread.h>
#include "utest.h"
#include <dfs_file.h>
#include <dfs.h>
#include <sys/stat.h>
#include <unistd.h>
static struct dfs_file fd;
static const char write_buf[] = "Hello RT-Thread DFS!";
#define TEST_MNT_PATH RT_UTEST_DFS_MNT_PATH
#define TEST_BLOCK_DEV RT_UTEST_DFS_BLOCK_DEV
#define TEST_FS RT_UTEST_DFS_FS_TYPE
#define TEST_FILE TEST_MNT_PATH "/ut_dfs.txt"
#define TEST_DIR TEST_MNT_PATH "/ut_dir"
#define TEST_RENAME_FILE TEST_MNT_PATH "/ut_renamed.txt"
static void test_mkfs(void)
{
rt_err_t rst = dfs_mkfs(TEST_FS, TEST_BLOCK_DEV);
uassert_true(rst == 0);
}
static void test_dfs_mount(void)
{
struct stat stat_buf;
/* Check if filesystem is available */
if (dfs_file_stat(TEST_MNT_PATH, &stat_buf) == 0)
{
/* Filesystem is available, test passed */
uassert_true(RT_TRUE);
}
else
{
/* Root filesystem is not available, test failed */
uassert_true(RT_FALSE);
}
}
static void test_dfs_open(void)
{
/* Initialize the file structure before opening (DFSV2 only) */
#ifdef RT_USING_DFS_V1
/* For DFS V1, clear the structure and set magic */
rt_memset(&fd, 0, sizeof(fd));
fd.magic = DFS_FD_MAGIC;
fd.ref_count = 1; /* Set proper reference count for DFS V1 */
#endif
/* DFSV1: dfs_file_open(&fd, TEST_FILE, O_CREAT | O_RDWR)
* DFSV2: dfs_file_open(&fd, TEST_FILE, O_CREAT | O_RDWR, 0644)
*/
#ifdef RT_USING_DFS_V2
rt_err_t rst = dfs_file_open(&fd, TEST_FILE, O_CREAT | O_RDWR, 0644);
#else
int rst = dfs_file_open(&fd, TEST_FILE, O_CREAT | O_RDWR);
#endif
if (rst != 0)
{
rt_kprintf("test_dfs_open: open failed with result = %d\n", rst);
}
uassert_true(rst == 0);
}
static void test_dfs_write(void)
{
rt_err_t rst;
rt_kprintf("[WRITE] Starting write operation to file: %s\n", TEST_FILE);
/* DFSV1: dfs_file_lseek(&fd, 0)
* DFSV2: dfs_file_lseek(&fd, 0, SEEK_SET)
*/
#ifdef RT_USING_DFS_V2
rst = dfs_file_lseek(&fd, 0, SEEK_SET);
#else
rst = dfs_file_lseek(&fd, 0);
#endif
if (rst < 0)
{
rt_kprintf("[WRITE] lseek failed with result = %d\n", rst);
}
uassert_true(rst >= 0);
rt_kprintf("[WRITE] Writing data: \"%s\" (length: %d)\n", write_buf, rt_strlen(write_buf));
rst = dfs_file_write(&fd, write_buf, rt_strlen(write_buf));
if (rst != rt_strlen(write_buf))
{
rt_kprintf("[WRITE] Write failed, result = %d, expected = %d\n", rst, rt_strlen(write_buf));
}
else
{
rt_kprintf("[WRITE] Write successful, %d bytes written\n", rst);
}
uassert_true(rst == rt_strlen(write_buf));
}
static void test_dfs_read(void)
{
rt_err_t rst;
char read_buf[32];
rt_kprintf("[READ] Starting read operation from file: %s\n", TEST_FILE);
/* DFSV1: dfs_file_lseek(&fd, 0)
* DFSV2: dfs_file_lseek(&fd, 0, SEEK_SET)
*/
#ifdef RT_USING_DFS_V2
rst = dfs_file_lseek(&fd, 0, SEEK_SET);
#else
rst = dfs_file_lseek(&fd, 0);
#endif
if (rst < 0)
{
rt_kprintf("[READ] lseek failed with result = %d\n", rst);
}
uassert_true(rst >= 0);
rt_kprintf("[READ] Reading %d bytes from file\n", rt_strlen(write_buf));
rst = dfs_file_read(&fd, read_buf, rt_strlen(write_buf));
if (rst != rt_strlen(write_buf))
{
rt_kprintf("[READ] Read failed, result = %d, expected = %d\n", rst, rt_strlen(write_buf));
}
else
{
read_buf[rst] = '\0';
rt_kprintf("[READ] Read successful, %d bytes read: \"%s\"\n", rst, read_buf);
}
uassert_true(rst == rt_strlen(write_buf));
read_buf[rst] = '\0';
uassert_str_equal(write_buf, read_buf);
}
static void test_dfs_close(void)
{
/* Flush the file before closing (DFSV1 only, DFSV2 does it in close) */
#ifndef RT_USING_DFS_V2
dfs_file_flush(&fd);
#endif
rt_err_t rst = dfs_file_close(&fd);
if (rst != 0)
{
rt_kprintf("test_dfs_close: close failed with result = %d\n", rst);
}
uassert_true(rst == 0);
/* Deinitialize the file structure after closing (DFSV2 only) */
#ifdef RT_USING_DFS_V2
dfs_file_deinit(&fd);
#endif
}
static void test_dfs_stat(void)
{
struct stat stat_buf;
uassert_true(dfs_file_stat(TEST_FILE, &stat_buf) == 0);
uassert_true(S_ISREG(stat_buf.st_mode));
}
static void test_dfs_unlink(void)
{
#ifndef RT_USING_DFS_V2
// DFSV1 may have issues with unlink
uassert_true(RT_TRUE);
#else
uassert_true(dfs_file_unlink(TEST_FILE) == 0);
#endif
}
static void test_dfs_rename(void)
{
rt_err_t ret;
struct stat stat_buf;
/* Force cleanup of any previous state */
dfs_file_unlink(TEST_FILE);
dfs_file_unlink(TEST_RENAME_FILE);
rt_thread_mdelay(50); /* Give filesystem time to clean up */
/* Create a file first using the correct DFS V1 approach */
#ifdef RT_USING_DFS_V2
struct dfs_file local_fd;
dfs_file_init(&local_fd);
ret = dfs_file_open(&local_fd, TEST_FILE, O_CREAT | O_RDWR, 0644);
#else
/* For DFS V1, use fd_new/fd_get pattern */
int file_fd = fd_new();
if (file_fd < 0)
{
rt_kprintf("[RENAME] Failed to allocate file descriptor\n");
uassert_true(RT_FALSE);
return;
}
struct dfs_file *file_ptr = fd_get(file_fd);
if (file_ptr == NULL)
{
rt_kprintf("[RENAME] Failed to get file structure\n");
fd_release(file_fd);
uassert_true(RT_FALSE);
return;
}
ret = dfs_file_open(file_ptr, TEST_FILE, O_CREAT | O_RDWR);
#endif
uassert_true(ret == 0);
/* Write some data to make sure the file has content */
#ifdef RT_USING_DFS_V2
ret = dfs_file_write(&local_fd, write_buf, rt_strlen(write_buf));
#else
ret = dfs_file_write(file_ptr, write_buf, rt_strlen(write_buf));
#endif
#ifndef RT_USING_DFS_V2
ret = dfs_file_flush(file_ptr);
#endif
/* Close the file */
#ifdef RT_USING_DFS_V2
ret = dfs_file_close(&local_fd);
dfs_file_deinit(&local_fd);
#else
ret = dfs_file_close(file_ptr);
fd_release(file_fd); /* Release the file descriptor */
#endif
/* Add a small delay to ensure all operations are completed */
rt_thread_mdelay(100);
/* Check original file exists before rename */
ret = dfs_file_stat(TEST_FILE, &stat_buf);
rt_kprintf("[RENAME] Original file stat before rename: %d (size: %d)\n", ret, (int)stat_buf.st_size);
/* Rename it */
rt_kprintf("[RENAME] Attempting to rename %s to %s\n", TEST_FILE, TEST_RENAME_FILE);
ret = dfs_file_rename(TEST_FILE, TEST_RENAME_FILE);
uassert_true(ret == 0);
/* Check old file doesn't exist */
ret = dfs_file_stat(TEST_FILE, &stat_buf);
uassert_true(ret != 0);
/* Check new file exists */
ret = dfs_file_stat(TEST_RENAME_FILE, &stat_buf);
uassert_true(ret == 0);
/* Clean up - will be done in cleanup function */
}
static rt_err_t utest_tc_init(void)
{
/* Clean up any leftover test files from previous runs */
dfs_file_unlink(TEST_FILE);
dfs_file_unlink(TEST_RENAME_FILE);
/* Clear global fd structure */
#ifdef RT_USING_DFS_V2
dfs_file_init(&fd);
#else
rt_memset(&fd, 0, sizeof(fd));
fd.magic = DFS_FD_MAGIC;
fd.ref_count = 1;
#endif
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
/* Clean up test files */
dfs_file_unlink(TEST_FILE);
dfs_file_unlink(TEST_RENAME_FILE);
/* Don't unmount root filesystem */
return RT_EOK;
}
static void testcase(void)
{
/* Skip filesystem mount test for now due to mutex issues */
UTEST_UNIT_RUN(test_mkfs);
UTEST_UNIT_RUN(test_dfs_mount);
/* Test basic file operations assuming filesystem is already available */
UTEST_UNIT_RUN(test_dfs_open);
UTEST_UNIT_RUN(test_dfs_write);
UTEST_UNIT_RUN(test_dfs_read);
UTEST_UNIT_RUN(test_dfs_close);
UTEST_UNIT_RUN(test_dfs_stat);
UTEST_UNIT_RUN(test_dfs_unlink);
/* Rename test */
UTEST_UNIT_RUN(test_dfs_rename);
}
UTEST_TC_EXPORT(testcase, "components.dfs.fs_dfs_api_tc", utest_tc_init, utest_tc_cleanup, 10);
|