1 /*
2 * Copyright (c) 2018 Pushpal Sidhu
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8
9 #include "test_fat.h"
10
delete_it(const char * path,int quiet)11 static int delete_it(const char *path, int quiet)
12 {
13 int res = 0;
14 if (check_file_dir_exists(path)) {
15 res = fs_unlink(path);
16 if (res && !quiet) {
17 TC_PRINT("Couldn't delete %s [%d]\n", path, res);
18 }
19 }
20
21 return res;
22 }
23
create_file(const char * path)24 static int create_file(const char *path)
25 {
26 struct fs_file_t fp;
27 int res = 0;
28
29 fs_file_t_init(&fp);
30 if (!check_file_dir_exists(path)) {
31 res = fs_open(&fp, path, FS_O_CREATE | FS_O_RDWR);
32 if (!res) {
33 res = fs_close(&fp);
34 } else {
35 TC_PRINT("Couldn't open %s [%d]\n", path, res);
36 }
37 }
38
39 return res;
40 }
41
create_dir(const char * path)42 static int create_dir(const char *path)
43 {
44 int res = 0;
45 if (!check_file_dir_exists(path)) {
46 res = fs_mkdir(path);
47 if (res) {
48 TC_PRINT("Couldn't create %s [%d]\n", path, res);
49 }
50 }
51
52 return res;
53 }
54
55
test_rename_dir(void)56 static int test_rename_dir(void)
57 {
58 const char *dn = FATFS_MNTP"/td";
59 const char *ndn = FATFS_MNTP"/ntd";
60 int res = TC_FAIL;
61
62 TC_PRINT("\nRename directory tests:\n");
63
64 if (delete_it(dn, 0) || delete_it(ndn, 0)) {
65 goto cleanup;
66 }
67
68 /* Rename non-existing dir to non-existing dir */
69 res = fs_rename(dn, ndn);
70 if (!res) {
71 TC_PRINT("Renamed non-existent directory\n");
72 res = TC_FAIL;
73 goto cleanup;
74 }
75
76 /* Rename existing dir to non-existing dir */
77 res = create_dir(dn);
78 if (!!res) {
79 goto cleanup;
80 }
81
82 res = fs_rename(dn, ndn);
83 if (!!res ||
84 !check_file_dir_exists(ndn) ||
85 check_file_dir_exists(dn)) {
86 TC_PRINT("Renaming %s to %s failed [%d]\n", dn, ndn, res);
87 res = TC_FAIL;
88 goto cleanup;
89 }
90
91 /* Rename existing file to existing file */
92 create_file(dn);
93 res = fs_rename(dn, ndn);
94 if (!!res ||
95 !check_file_dir_exists(ndn) ||
96 check_file_dir_exists(dn)) {
97 TC_PRINT("Renaming %s to %s failed [%d]\n", dn, ndn, res);
98 res = TC_FAIL;
99 goto cleanup;
100 }
101
102 cleanup:
103 delete_it(dn, 1);
104 delete_it(ndn, 1);
105
106 return res;
107 }
108
test_rename_file(void)109 static int test_rename_file(void)
110 {
111 const char *fn = FATFS_MNTP"/tf.txt";
112 const char *nfn = FATFS_MNTP"/ntf.txt";
113 int res = TC_FAIL;
114
115 TC_PRINT("\nRename file tests:\n");
116
117 if (delete_it(fn, 0) || delete_it(nfn, 0)) {
118 goto cleanup;
119 }
120
121 /* Rename non-existing file to non-existing file */
122 res = fs_rename(fn, nfn);
123 if (!res) {
124 TC_PRINT("Renamed non-existent file\n");
125 res = TC_FAIL;
126 goto cleanup;
127 }
128
129 /* Rename existing file to non-existing file */
130 res = create_file(fn);
131 if (!!res) {
132 goto cleanup;
133 }
134
135 res = fs_rename(fn, nfn);
136 if (!!res ||
137 !check_file_dir_exists(nfn) ||
138 check_file_dir_exists(fn)) {
139 TC_PRINT("Renaming %s to %s failed [%d]\n", fn, nfn, res);
140 res = TC_FAIL;
141 goto cleanup;
142 }
143
144 /* Rename existing file to existing file */
145 create_file(fn);
146 res = fs_rename(fn, nfn);
147 if (!!res ||
148 !check_file_dir_exists(nfn) ||
149 check_file_dir_exists(fn)) {
150 TC_PRINT("Renaming %s to %s failed [%d]\n", fn, nfn, res);
151 res = TC_FAIL;
152 goto cleanup;
153 }
154
155 cleanup:
156 delete_it(fn, 1);
157 delete_it(nfn, 1);
158
159 return res;
160 }
161
test_fat_rename(void)162 void test_fat_rename(void)
163 {
164 zassert_true(test_rename_file() == TC_PASS);
165 zassert_true(test_rename_dir() == TC_PASS);
166 }
167