1 /* This FileX test concentrates on the directory rename operations. */
2
3 #ifndef FX_STANDALONE_ENABLE
4 #include "tx_api.h"
5 #endif
6 #include "fx_api.h"
7 #include "fx_ram_driver_test.h"
8 #include "fx_fault_tolerant.h"
9 #include "fx_utility.h"
10 #include <stdio.h>
11
12 #define DEMO_STACK_SIZE 8192
13 #define CACHE_SIZE 4096
14 #ifdef FX_ENABLE_FAULT_TOLERANT
15 #define FAULT_TOLERANT_SIZE FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
16 #else
17 #define FAULT_TOLERANT_SIZE 0
18 #endif
19
20
21 /* Define the ThreadX and FileX object control blocks... */
22
23 #ifndef FX_STANDALONE_ENABLE
24 static TX_THREAD ftest_0;
25 #endif
26 static FX_MEDIA ram_disk;
27 static CHAR max_name[FX_MAX_LONG_NAME_LEN + 2];
28 static CHAR max_newname[FX_MAX_LONG_NAME_LEN + 1];
29
30
31 /* Define the counters used in the test application... */
32
33 #ifndef FX_STANDALONE_ENABLE
34 static UCHAR *ram_disk_memory;
35 static UCHAR *cache_buffer;
36 static UCHAR *fault_tolerant_buffer;
37 #else
38 static UCHAR cache_buffer[CACHE_SIZE];
39 static UCHAR fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
40 #endif
41
42
43 /* Define thread prototypes. */
44
45 void filex_directory_rename_application_define(void *first_unused_memory);
46 static void ftest_0_entry(ULONG thread_input);
47
48 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
49 void test_control_return(UINT status);
50
51
52
53 /* Define what the initial system looks like. */
54
55 #ifdef CTEST
test_application_define(void * first_unused_memory)56 void test_application_define(void *first_unused_memory)
57 #else
58 void filex_directory_rename_application_define(void *first_unused_memory)
59 #endif
60 {
61
62 #ifndef FX_STANDALONE_ENABLE
63 UCHAR *pointer;
64
65
66 /* Setup the working pointer. */
67 pointer = (UCHAR *) first_unused_memory;
68
69 /* Create the main thread. */
70 tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
71 pointer, DEMO_STACK_SIZE,
72 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
73
74 pointer = pointer + DEMO_STACK_SIZE;
75
76 /* Setup memory for the RAM disk and the sector cache. */
77 cache_buffer = pointer;
78 pointer = pointer + CACHE_SIZE;
79 fault_tolerant_buffer = pointer;
80 pointer += FAULT_TOLERANT_SIZE;
81 ram_disk_memory = pointer;
82
83 #endif
84
85 /* Initialize the FileX system. */
86 fx_system_initialize();
87 #ifdef FX_STANDALONE_ENABLE
88 ftest_0_entry(0);
89 #endif
90 }
91
92
93
94 /* Define the test threads. */
95
ftest_0_entry(ULONG thread_input)96 static void ftest_0_entry(ULONG thread_input)
97 {
98
99 UINT status;
100 UINT i;
101
102 FX_PARAMETER_NOT_USED(thread_input);
103
104 /* Print out some test information banners. */
105 printf("FileX Test: Directory rename test..................................");
106
107 /* Format the media. This needs to be done before opening it! */
108 status = fx_media_format(&ram_disk,
109 _fx_ram_driver, // Driver entry
110 ram_disk_memory, // RAM disk memory pointer
111 cache_buffer, // Media buffer pointer
112 CACHE_SIZE, // Media buffer size
113 "MY_RAM_DISK", // Volume Name
114 1, // Number of FATs
115 128, // Directory Entries
116 0, // Hidden sectors
117
118 /* We need a larger disk to test fault tolerant feature. */
119 #ifdef FX_ENABLE_FAULT_TOLERANT
120 4096 * 8, // Total sectors
121 256, // Sector size
122 8, // Sectors per cluster
123 #else
124 4096, // Total sectors
125 128, // Sector size
126 1, // Sectors per cluster
127 #endif
128
129 1, // Heads
130 1); // Sectors per track
131 return_if_fail( status == FX_SUCCESS);
132
133 /* Test a directory name before the media is opened to generate an error */
134 status = fx_directory_name_test(&ram_disk, "/A0");
135 return_if_fail( status == FX_MEDIA_NOT_OPEN);
136
137 /* Try to rename a directory to an invalid name */
138 status = fx_directory_rename(&ram_disk, "/A0", "");
139 return_if_fail( status == FX_INVALID_NAME);
140
141 /* Open the ram_disk. */
142 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
143 return_if_fail( status == FX_SUCCESS);
144
145 #ifdef FX_ENABLE_FAULT_TOLERANT
146 /* Enable fault tolerant if FX_ENABLE_FAULT_TOLERANT is defined. */
147 status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
148 return_if_fail( status == FX_SUCCESS);
149 #endif
150
151 /* Attempt to rename a directory while the media is write protected */
152 ram_disk.fx_media_driver_write_protect = FX_TRUE;
153 status = fx_directory_rename(&ram_disk, "/A0", "/A1");
154 ram_disk.fx_media_driver_write_protect = FX_FALSE;
155 return_if_fail( status == FX_WRITE_PROTECT);
156
157 /* Attempt to rename a directory that does not exist */
158 status = fx_directory_rename(&ram_disk, "/DOES_NOT_EXIST", "/VALID");
159 return_if_fail( status != FX_SUCCESS);
160
161 /* Attempt to rename something that is not a directory */
162 status = fx_file_create(&ram_disk, "NOT_A_DIR");
163 status += fx_directory_rename(&ram_disk, "NOT_A_DIR", "/VALID");
164 status += fx_file_delete(&ram_disk, "NOT_A_DIR");
165 return_if_fail( status == FX_NOT_DIRECTORY);
166
167 /* Only run this if error checking is enabled */
168 #ifndef FX_DISABLE_ERROR_CHECKING
169
170 /* send null pointer to generate an error */
171 status = fx_directory_rename(FX_NULL, "MYTEST", "OURTEST");
172 return_if_fail( status == FX_PTR_ERROR);
173
174 #endif /* FX_DISABLE_ERROR_CHECKING */
175
176 /* Simple 8.3 rename in the root directory. */
177 status = fx_directory_create(&ram_disk, "MYTEST");
178 status += fx_directory_rename(&ram_disk, "MYTEST", "OURTEST");
179 return_if_fail( status == FX_SUCCESS);
180
181 /* Now create a sub-directory in the "OURTEST" directory. */
182 status = fx_directory_create(&ram_disk, "OURTEST/newdir");
183 status += fx_directory_rename(&ram_disk, "OURTEST/newdir", "newnewdir");
184 status += fx_directory_rename(&ram_disk, "newnewdir", "OURTEST/newdir");
185 status += fx_directory_create(&ram_disk, "/OURTEST/newdir/finaltest");
186 return_if_fail( status == FX_SUCCESS);
187
188 /* Attempt to rename a directory to a name that is taken */
189 status = fx_directory_rename(&ram_disk, "/OURTEST/newdir/finaltest", "/OURTEST/newdir");
190 return_if_fail( status == FX_ALREADY_CREATED);
191
192 /* Attempt to rename a directory to a subdirectory with an invalid name */
193 status = fx_directory_rename(&ram_disk, "OURTEST/newdir/finaltest", "OURTEST/newdir/does_not_exist/this_wont_work");
194 return_if_fail( status == FX_INVALID_NAME);
195
196 /* Rename a directory and make it hidden */
197 status = fx_directory_rename(&ram_disk, "OURTEST/newdir/finaltest", "/.hidden");
198 return_if_fail( status == FX_SUCCESS);
199
200 /* Create a root directory so we can rename it. */
201 status = fx_directory_create(&ram_disk, "newroot");
202
203 /* Now try to rename the root directory, but with leading and trailing spaces. */
204 status += fx_directory_rename(&ram_disk, "newroot", " newnewroot ");
205 return_if_fail( status == FX_SUCCESS);
206
207 /* Now test the maximum size of the new directory name. */
208 for (i = 0; i < FX_MAX_LONG_NAME_LEN; i++)
209 {
210 max_name[i] = 'a';
211 }
212 max_name[i] = 0;
213
214 /* Now try to test the maximum new name length. */
215 status = fx_directory_rename(&ram_disk, "newnewroot", max_name);
216 return_if_fail( status == FX_INVALID_NAME);
217
218 max_name[i] = 'a';
219 max_name[i + 1] = 'a';
220
221 /* Now try to test the maximum new name length. */
222 status = fx_directory_rename(&ram_disk, "newnewroot", max_name);
223 return_if_fail(status == FX_INVALID_NAME);
224
225 /* Now make the max name valid. */
226 max_name[FX_MAX_LONG_NAME_LEN-1] = 0;
227 max_name[FX_MAX_LONG_NAME_LEN-2] = '~';
228
229 /* Rename the directory to a valid maximum name. */
230 status = fx_directory_rename(&ram_disk, "newnewroot", max_name);
231 return_if_fail( status == FX_SUCCESS);
232
233 /* Now build a new long file name with only one cap character different. */
234 for (i = 0; i < FX_MAX_LONG_NAME_LEN+1; i++)
235 {
236 max_newname[i] = max_name[i];
237 }
238 /* Change just the first character to upper case. */
239 max_newname[0] = 'A';
240
241 /* Rename the directory to a valid maximum name with just an upper case change. */
242 status = fx_directory_rename(&ram_disk, max_name, max_newname);
243
244 /* If we rename a file with longest allowed long file, the file size will overflow FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE, which is defined as 2048 in test enviroment. */
245 #if defined(FX_ENABLE_FAULT_TOLERANT) && (FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE < 3072)
246 return_if_fail( status == FX_NO_MORE_SPACE);
247 #else
248 return_if_fail( status == FX_SUCCESS);
249 #endif
250
251 /* Close the media. */
252 status = fx_media_close(&ram_disk);
253 return_if_fail( status == FX_SUCCESS);
254
255 /* Open the ram_disk - but with only one sector cache. */
256 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, 256);
257 return_if_fail( status == FX_SUCCESS);
258
259 /* Now try to get an I/O error on the directory free search call. */
260 #ifndef FX_ENABLE_FAULT_TOLERANT
261 _fx_utility_logical_sector_read_error_request = 74;
262 #else
263 _fx_utility_logical_sector_read_error_request = 22;
264 #endif
265 status = fx_directory_rename(&ram_disk, max_newname, "newroot");
266 _fx_utility_logical_sector_read_error_request = 0;
267 return_if_fail( status == FX_IO_ERROR);
268
269 /* Now try to get an I/O error on the directory write call. */
270 _fx_utility_logical_sector_write_error_request = 1;
271 status = fx_directory_rename(&ram_disk, max_newname, "newroot");
272 _fx_utility_logical_sector_write_error_request = 0;
273 return_if_fail( status == FX_IO_ERROR);
274
275 /* Now rename the direction with a root directory path. */
276 status = fx_directory_rename(&ram_disk, max_newname, "\\newroot");
277 return_if_fail( status == FX_SUCCESS);
278
279 /* Fail to update old directory entry. */
280 _fx_directory_entry_write_error_request = 2;
281 status = fx_directory_rename(&ram_disk, "\\newroot", "aaa");
282 return_if_fail( status == FX_IO_ERROR);
283
284 /* Close the media. */
285 status = fx_media_close(&ram_disk);
286 return_if_fail( status == FX_SUCCESS);
287
288 printf("SUCCESS!\n");
289 test_control_return(0);
290 }
291