1 /* This FileX test concentrates on the file 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 16*128
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 FX_FILE my_file;
28
29
30 /* Define the counters used in the test application... */
31
32 #ifndef FX_STANDALONE_ENABLE
33 static UCHAR *ram_disk_memory;
34 static UCHAR *cache_buffer;
35 static UCHAR *fault_tolerant_buffer;
36 #else
37 static UCHAR cache_buffer[CACHE_SIZE];
38 static UCHAR fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
39 #endif
40
41
42 /* Define thread prototypes. */
43
44 void filex_file_rename_application_define(void *first_unused_memory);
45 static void ftest_0_entry(ULONG thread_input);
46
47 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
48 void test_control_return(UINT status);
49
50
51
52 /* Define what the initial system looks like. */
53
54 #ifdef CTEST
test_application_define(void * first_unused_memory)55 void test_application_define(void *first_unused_memory)
56 #else
57 void filex_file_rename_application_define(void *first_unused_memory)
58 #endif
59 {
60
61 #ifndef FX_STANDALONE_ENABLE
62 UCHAR *pointer;
63
64
65 /* Setup the working pointer. */
66 pointer = (UCHAR *) first_unused_memory;
67
68 /* Create the main thread. */
69 tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
70 pointer, DEMO_STACK_SIZE,
71 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
72
73 pointer = pointer + DEMO_STACK_SIZE;
74
75 /* Setup memory for the RAM disk and the sector cache. */
76 cache_buffer = pointer;
77 pointer = pointer + CACHE_SIZE;
78 fault_tolerant_buffer = pointer;
79 pointer += FAULT_TOLERANT_SIZE;
80 ram_disk_memory = pointer;
81
82 #endif
83
84 /* Initialize the FileX system. */
85 fx_system_initialize();
86 #ifdef FX_STANDALONE_ENABLE
87 ftest_0_entry(0);
88 #endif
89 }
90
91
92
93 /* Define the test threads. */
94
ftest_0_entry(ULONG thread_input)95 static void ftest_0_entry(ULONG thread_input)
96 {
97
98 UINT status;
99 FX_FILE open_file;
100 CHAR long_name1[FX_MAX_LONG_NAME_LEN+1];
101 CHAR long_name2[FX_MAX_LONG_NAME_LEN+1];
102 UINT i;
103
104 FX_PARAMETER_NOT_USED(thread_input);
105
106 /* Print out some test information banners. */
107 printf("FileX Test: File rename test.......................................");
108
109 /* Format the media. This needs to be done before opening it! */
110 status = fx_media_format(&ram_disk,
111 _fx_ram_driver, // Driver entry
112 ram_disk_memory, // RAM disk memory pointer
113 cache_buffer, // Media buffer pointer
114 CACHE_SIZE, // Media buffer size
115 "MY_RAM_DISK", // Volume Name
116 1, // Number of FATs
117 32, // Directory Entries
118 0, // Hidden sectors
119 /* Allocated a larger disk to enable fault tolerant feature. */
120 #ifdef FX_ENABLE_FAULT_TOLERANT
121 512 * 8, // Total sectors
122 256, // Sector size
123 8, // Sectors per cluster
124 #else
125 512, // Total sectors
126 128, // Sector size
127 1, // Sectors per cluster
128 #endif
129 1, // Heads
130 1); // Sectors per track
131 return_if_fail( status == FX_SUCCESS);
132
133 /* try to rename a file to something invalid */
134 status = fx_file_rename(&ram_disk, "MYTEST", "");
135 return_if_fail( status == FX_INVALID_NAME);
136
137 /* try to rename a file before the media is opened */
138 status = fx_file_rename(&ram_disk, "MYTEST", "OURTEST");
139 return_if_fail( status == FX_MEDIA_NOT_OPEN);
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 /* try to rename a file while the media is write protected */
152 ram_disk.fx_media_driver_write_protect = FX_TRUE;
153 status = fx_file_rename(&ram_disk, "MYTEST", "OURTEST");
154 ram_disk.fx_media_driver_write_protect = FX_FALSE;
155 return_if_fail( status == FX_WRITE_PROTECT);
156
157 /* try to rename a file that cant be found */
158 status = fx_file_rename(&ram_disk, "MYTEST", "OURTEST");
159 return_if_fail( status != FX_SUCCESS);
160
161 /* Only run this if error checking is enabled */
162 #ifndef FX_DISABLE_ERROR_CHECKING
163
164 /* send null pointer to generate an error */
165 status = fx_file_rename(FX_NULL, "", "");
166 return_if_fail( status == FX_PTR_ERROR);
167
168 #endif /* FX_DISABLE_ERROR_CHECKING */
169
170 /* Simple 8.3 rename in the root directory. */
171 status = fx_file_create(&ram_disk, "MYTEST");
172 status += fx_file_rename(&ram_disk, "MYTEST", "OURTEST");
173 return_if_fail( status == FX_SUCCESS);
174
175 /* rename the files to lower case and upper case versions of themselves */
176 status = fx_file_rename(&ram_disk, "OURTEST", "ourtest");
177 status += fx_file_rename(&ram_disk, "ourtest", "OURTEST");
178 return_if_fail( status == FX_SUCCESS);
179
180 /* try to rename a file into a subdirectory that does not exist */
181 status = fx_file_rename(&ram_disk, "OURTEST", "DOES_NOT_EXITS/OURTEST");
182 return_if_fail( status == FX_INVALID_NAME);
183
184 /* Now create a 2 sub-directories. */
185 status = fx_directory_create(&ram_disk, "/subdir1");
186 status += fx_directory_create(&ram_disk, "/subdir2");
187 return_if_fail( status == FX_SUCCESS);
188
189 /* try to rename a file to something that already exists */
190 status = fx_file_rename(&ram_disk, "OURTEST", "subdir1");
191 return_if_fail( status == FX_ALREADY_CREATED);
192
193 #ifndef FX_DONT_UPDATE_OPEN_FILES
194
195 /* rename a file while it is open and while another is opened to get code coverage */
196 status = fx_file_create(&ram_disk, "OPEN_FILE");
197 status += fx_file_open(&ram_disk, &open_file, "OPEN_FILE", FX_OPEN_FOR_WRITE);
198 status += fx_file_rename(&ram_disk, "OURTEST", "NEWTEST");
199 status += fx_file_rename(&ram_disk, "NEWTEST", "OURTEST");
200 status += fx_file_rename(&ram_disk, "OPEN_FILE", "NEW_OPEN_FILE");
201 return_if_fail( status == FX_SUCCESS);
202
203 #endif
204
205 /* move the file through the subdirectories */
206 status = fx_file_rename(&ram_disk, "OURTEST", "/subdir1/NEWOURTEST");
207 status += fx_file_rename(&ram_disk, "/subdir1/NEWOURTEST", "/subdir2/OURTEST");
208 status += fx_file_rename(&ram_disk, "/subdir2/OURTEST", "OURTEST");
209 status += fx_file_rename(&ram_disk, "OURTEST", ".OURTEST");
210 status += fx_file_delete(&ram_disk, ".OURTEST");
211 return_if_fail( status == FX_SUCCESS);
212
213 /* try to rename a file that isnt a file */
214 status = fx_file_rename(&ram_disk, "subdir1", "OURTEST");
215 return_if_fail( status == FX_NOT_A_FILE);
216
217 /* Close the media. */
218 status = fx_media_close(&ram_disk);
219 return_if_fail( status == FX_SUCCESS);
220
221 /* Format the media. This needs to be done before opening it! */
222 status = fx_media_format(&ram_disk,
223 _fx_ram_driver, // Driver entry
224 ram_disk_memory, // RAM disk memory pointer
225 cache_buffer, // Media buffer pointer
226 CACHE_SIZE, // Media buffer size
227 "MY_RAM_DISK", // Volume Name
228 1, // Number of FATs
229 128, // Directory Entries
230 0, // Hidden sectors
231 6000, // Total sectors
232 128, // Sector size
233 1, // Sectors per cluster
234 1, // Heads
235 1); // Sectors per track
236 return_if_fail( status == FX_SUCCESS);
237
238 /* Open the ram_disk. */
239 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, 128);
240 return_if_fail( status == FX_SUCCESS);
241
242 /* Loop to create a very long name. */
243 for (i = 0; i < FX_MAX_LONG_NAME_LEN; i++)
244 {
245 long_name1[i] = 'a';
246 long_name2[i] = 'a';
247 }
248
249 /* Put NULL and special character at the end of the file. */
250 long_name1[i] = 0;
251 long_name1[i-1] = 0;
252 long_name1[i-2] = '~';
253 long_name2[i] = 0;
254 long_name2[i-1] = 0;
255 long_name2[i-2] = '~';
256
257 /* Create a super short file name. */
258 status = fx_file_create(&ram_disk, "sname");
259 status += fx_file_create(&ram_disk, long_name1);
260 return_if_fail( status == FX_SUCCESS);
261
262 /* Attempt to change the short name with leading spaces. */
263 status = fx_file_rename(&ram_disk, "sname", " name");
264 return_if_fail( status == FX_SUCCESS);
265
266 /* Now attempt to change the short name with middle spaces. */
267 status = fx_file_rename(&ram_disk, "name", "\\new name");
268 return_if_fail( status == FX_SUCCESS);
269
270 /* Make the second long name too big, to test the max size of the new long file name. */
271 long_name2[FX_MAX_LONG_NAME_LEN-1] = '~';
272 status = fx_file_rename(&ram_disk, long_name1, long_name2);
273 return_if_fail( status == FX_INVALID_NAME);
274
275 /* Make the second long file name the correct max length again. */
276 long_name2[FX_MAX_LONG_NAME_LEN-1] = 0;
277
278 /* Change the name of the second long file name. */
279 long_name2[FX_MAX_LONG_NAME_LEN-3] = 'a';
280 status = fx_file_rename(&ram_disk, long_name1, long_name2);
281 return_if_fail( status == FX_SUCCESS);
282
283 /* Change back to the first long file name. */
284 status = fx_file_rename(&ram_disk, long_name2, long_name1);
285 return_if_fail( status == FX_SUCCESS);
286
287 /* Now open the long file name so we can get that logic excercised in the rename logic. */
288 status = fx_file_open(&ram_disk, &my_file, long_name1, FX_OPEN_FOR_WRITE);
289 status += fx_file_rename(&ram_disk, long_name1, long_name2);
290 return_if_fail( status == FX_SUCCESS);
291
292 /* Close the file. */
293 fx_file_close(&my_file);
294
295 /* Now test the directory free search logic when there are no more entries. */
296
297 /* First, fill up the root directory. */
298 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1000");
299 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1001");
300 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1002");
301 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1003");
302 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1004");
303 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1005");
304 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1006");
305 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1007");
306 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1008");
307 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1009");
308 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1010");
309 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1011");
310 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1012");
311 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1013");
312 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1014");
313 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1015");
314 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1016");
315 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1017");
316 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1018");
317 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1019");
318 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1020");
319 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1021");
320 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1022");
321 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1023");
322 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1024");
323 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1025");
324 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1026");
325 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1027");
326 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1028");
327 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1029");
328 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1030");
329 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1031");
330 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1032");
331 status += fx_file_create(&ram_disk, "aaaaaaaaaaaaa1033");
332
333 /* Now attempt to rename the long file again. */
334 status += fx_file_rename(&ram_disk, long_name2, long_name1);
335
336 /* Check status - this should fail because we do not have enough room in the root directory. */
337 return_if_fail( status == FX_NO_MORE_SPACE);
338
339 /* Now test I/O error on directory entry write inside of the rename processing. */
340 fx_media_flush(&ram_disk);
341 _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
342 _fx_utility_logical_sector_write_error_request = 1;
343 status = fx_file_rename(&ram_disk, "new name", "sname");
344 _fx_utility_logical_sector_write_error_request = 0;
345 return_if_fail( status == FX_IO_ERROR);
346
347 /* Close the media. */
348 status = fx_media_close(&ram_disk);
349 return_if_fail( status == FX_SUCCESS);
350
351 printf("SUCCESS!\n");
352 test_control_return(0);
353 }
354