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   <stdio.h>
9 
10 #define     DEMO_STACK_SIZE         8192
11 #define     CACHE_SIZE              16*128
12 
13 /* Define the ThreadX and FileX object control blocks...  */
14 
15 #ifndef FX_STANDALONE_ENABLE
16 static TX_THREAD                ftest_0;
17 #endif
18 static FX_MEDIA                 ram_disk;
19 
20 
21 /* Define the counters used in the test application...  */
22 
23 #ifndef FX_STANDALONE_ENABLE
24 static UCHAR                    *ram_disk_memory;
25 static UCHAR                    *cache_buffer;
26 #else
27 static UCHAR                     cache_buffer[CACHE_SIZE];
28 #endif
29 
30 static UCHAR                    old_directory_name[]  =  {3, 0, 4, 0, 5, 0, 6, 0, 0, 0};
31 static UCHAR                    new_directory_name1[] =  {3, 0, 4, 0, 5, 0, 6, 0, 0, 0};
32 static UCHAR                    new_directory_name2[] =  {3, 0, 4, 0, 5, 0, 0, 0};
33 static UCHAR                    new_directory_name3[] =  {3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, 0};
34 static UCHAR                    new_directory_name4[] =  {2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 0, 0};
35 static UCHAR                    new_file_name5[] =  {'a', 0, 'b', 0, 'c', 0, 0, 0};
36 static UCHAR                    new_file_name5_bak[] =  {'a', 0, 'b', 0, 'c', 0, 0, 0};
37 static UCHAR                    new_file_name6[] =  {'a', 0, 'y', 0, 'c', 0, 0, 0};
38 static UCHAR                    new_file_name7[] =  {'a', 1, 'z', 0, 'c', 0, 0, 0};
39 static UCHAR                    new_file_name8[] =  {'z' + 1, 0, 'z', 0, 'c', 0, 'd', 0, 0, 0};
40 static UCHAR                    new_file_name9[] =  {'z' + 1, 1, 'z', 0, 'c', 0, 'd', 0, 0, 0};
41 static UCHAR                    destination_name[100];
42 
43 #define TEST_COUNT              3
44 
45 /* Define thread prototypes.  */
46 
47 void    filex_unicode_directory_rename_application_define(void *first_unused_memory);
48 static void    ftest_0_entry(ULONG thread_input);
49 
50 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
51 void  test_control_return(UINT status);
52 
53 
54 
55 /* Define what the initial system looks like.  */
56 
57 #ifdef CTEST
test_application_define(void * first_unused_memory)58 void test_application_define(void *first_unused_memory)
59 #else
60 void    filex_unicode_directory_rename_application_define(void *first_unused_memory)
61 #endif
62 {
63 
64 #ifndef FX_STANDALONE_ENABLE
65 UCHAR    *pointer;
66 
67 
68     /* Setup the working pointer.  */
69     pointer =  (UCHAR *) first_unused_memory;
70 
71     /* Create the main thread.  */
72     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
73             pointer, DEMO_STACK_SIZE,
74             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
75 
76     pointer =  pointer + DEMO_STACK_SIZE;
77 
78     /* Setup memory for the RAM disk and the sector cache.  */
79     cache_buffer =  pointer;
80     pointer =  pointer + CACHE_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        i;
100 UINT        status, count;
101 ULONG       length;
102 ULONG       old_length;
103 ULONG       new_length;
104 
105     FX_PARAMETER_NOT_USED(thread_input);
106 
107     /* Print out some test information banners.  */
108     printf("FileX Test:   Unicode Directory rename test..........................");
109 
110     /* Loop to test FAT 12, 16, 32.   */
111     for (i = 0; i < TEST_COUNT; i ++)
112     {
113         if (i == 0)
114         {
115             /* Format the media with FAT12.  This needs to be done before opening it!  */
116             status =  fx_media_format(&ram_disk,
117                                      _fx_ram_driver,         // Driver entry
118                                      ram_disk_memory_large,  // RAM disk memory pointer
119                                      cache_buffer,           // Media buffer pointer
120                                      CACHE_SIZE,             // Media buffer size
121                                      "MY_RAM_DISK",          // Volume Name
122                                      1,                      // Number of FATs
123                                      32,                     // Directory Entries
124                                      0,                      // Hidden sectors
125                                      256,                    // Total sectors
126                                      256,                    // Sector size
127                                      8,                      // Sectors per cluster
128                                      1,                      // Heads
129                                      1);                     // Sectors per track
130         }
131         else if (i == 1)
132         {
133             /* Format the media with FAT16.  This needs to be done before opening it!  */
134             status =  fx_media_format(&ram_disk,
135                                      _fx_ram_driver,         // Driver entry
136                                      ram_disk_memory_large,  // RAM disk memory pointer
137                                      cache_buffer,           // Media buffer pointer
138                                      CACHE_SIZE,             // Media buffer size
139                                      "MY_RAM_DISK",          // Volume Name
140                                      1,                      // Number of FATs
141                                      32,                     // Directory Entries
142                                      0,                      // Hidden sectors
143                                      4200 * 8,               // Total sectors
144                                      256,                    // Sector size
145                                      8,                      // Sectors per cluster
146                                      1,                      // Heads
147                                      1);                     // Sectors per track
148         }
149         else if (i == 2)
150         {
151             /* Format the media with FAT32.  This needs to be done before opening it!  */
152             status =  fx_media_format(&ram_disk,
153                                      _fx_ram_driver,         // Driver entry
154                                      ram_disk_memory_large,  // RAM disk memory pointer
155                                      cache_buffer,           // Media buffer pointer
156                                      CACHE_SIZE,             // Media buffer size
157                                      "MY_RAM_DISK",          // Volume Name
158                                      1,                      // Number of FATs
159                                      32,                     // Directory Entries
160                                      0,                      // Hidden sectors
161                                      70000 * 8,              // Total sectors
162                                      256,                    // Sector size
163                                      8,                      // Sectors per cluster
164                                      1,                      // Heads
165                                      1);                     // Sectors per track
166         }
167 
168         /* Determine if the format had an error.  */
169         if (status)
170         {
171 
172             printf("ERROR!\n");
173             test_control_return(1);
174         }
175 
176         /* Attempt to rename a directory while the media is not open */
177         status = fx_unicode_directory_rename(&ram_disk, (UCHAR *)"name", 1, (UCHAR *)"name", 1, (CHAR *) destination_name);
178         if (status != FX_MEDIA_NOT_OPEN)
179         {
180             printf("ERROR!\n");
181             test_control_return(2);
182         }
183 
184         /* Open the ram_disk.  */
185         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
186 
187         /* Check the status.  */
188         if (status != FX_SUCCESS)
189         {
190 
191             /* Error, return error code.  */
192             printf("ERROR!\n");
193             test_control_return(3);
194         }
195 
196         /* Attempt to rename a directory that does not exist */
197         status = fx_unicode_directory_rename(&ram_disk, (UCHAR *)"does_not_exist", 1, (UCHAR *)"does_not_exist", 1, (CHAR *) destination_name);
198         if (status == FX_SUCCESS)
199         {
200             printf("ERROR!\n");
201             test_control_return(4);
202         }
203 
204         /* Create the unicode directory name to create the same unicode file names in the sub directory.  */
205         length =  fx_unicode_length_get(old_directory_name);
206         status =  fx_unicode_directory_create(&ram_disk, old_directory_name, length, (CHAR *) destination_name);
207 
208         /* Check for expected error.  */
209         if (status)
210         {
211 
212             /* Error creating unicode file names.  Return to caller.  */
213             printf("ERROR!\n");
214             test_control_return(5);
215         }
216 
217 /* Only run this if error checking is enabled */
218 #ifndef FX_DISABLE_ERROR_CHECKING
219     /* Send null pointer to generate an error. */
220     new_length =  0;
221     old_length =  0;
222     status = fx_unicode_directory_rename(FX_NULL, old_directory_name, old_length, new_directory_name1, new_length, (CHAR *) destination_name);
223     if (status != FX_PTR_ERROR)
224     {
225         printf("ERROR!\n");
226         test_control_return(6);
227     }
228 #endif /* FX_DISABLE_ERROR_CHECKING */
229 
230         /* Rename the old directory name to new_directory_name1.  */
231         old_length =  fx_unicode_length_get(old_directory_name);
232         new_length =  fx_unicode_length_get(new_directory_name1);
233         status =  fx_unicode_directory_rename(&ram_disk, old_directory_name, old_length, new_directory_name1, new_length, (CHAR *) destination_name);
234 
235         /* Check for expected error.  */
236         if (status)
237         {
238 
239             /* Error creating unicode file names.  Return to caller.  */
240             printf("ERROR!\n");
241             test_control_return(7);
242         }
243 
244         /* Rename the new_directory_name1 to new_directory_name2.  */
245         old_length =  fx_unicode_length_get(new_directory_name1);
246         new_length =  fx_unicode_length_get(new_directory_name2);
247         status =  fx_unicode_directory_rename(&ram_disk, new_directory_name1, old_length, new_directory_name2, new_length, (CHAR *) destination_name);
248 
249         /* Check for expected error.  */
250         if (status)
251         {
252 
253             /* Error creating unicode file names.  Return to caller.  */
254             printf("ERROR!\n");
255             test_control_return(8);
256         }
257 
258         /* Rename the new_directory_name2 to new_directory_name3.  */
259         old_length =  fx_unicode_length_get(new_directory_name2);
260         new_length =  fx_unicode_length_get(new_directory_name3);
261         status =  fx_unicode_directory_rename(&ram_disk, new_directory_name2, old_length, new_directory_name3, new_length, (CHAR *) destination_name);
262 
263         /* Check for expected error.  */
264         if (status)
265         {
266 
267             /* Error creating unicode file names.  Return to caller.  */
268             printf("ERROR!\n");
269             test_control_return(9);
270         }
271 
272         /* Rename the new_directory_name3 to new_directory_name4.  */
273         old_length =  fx_unicode_length_get(new_directory_name3);
274         new_length =  fx_unicode_length_get(new_directory_name4);
275         status =  fx_unicode_directory_rename(&ram_disk, new_directory_name3, old_length, new_directory_name4, new_length, (CHAR *) destination_name);
276 
277         /* Check for expected error.  */
278         if (status)
279         {
280 
281             /* Error creating unicode file names.  Return to caller.  */
282             printf("ERROR!\n");
283             test_control_return(10);
284         }
285 
286         /* Attempt to create the old_directory_name again.  */
287         length =  fx_unicode_length_get(old_directory_name);
288         status =  fx_unicode_directory_create(&ram_disk,  old_directory_name, length, (CHAR *) destination_name);
289 
290         /* Check for expected error.  */
291         if (status)
292         {
293 
294             /* Error creating unicode directory name.  Return to caller.  */
295             printf("ERROR!\n");
296             test_control_return(11);
297         }
298 
299         /* Attempt to create the new_directory_name1 that same as old_directory_name again.  */
300         length =  fx_unicode_length_get(new_directory_name1);
301         status =  fx_unicode_directory_create(&ram_disk,  new_directory_name1, length, (CHAR *) destination_name);
302 
303         /* Check for expected error.  */
304         if (status != FX_ALREADY_CREATED)
305         {
306 
307             /* Error creating unicode directory name.  Return to caller.  */
308             printf("ERROR!\n");
309             test_control_return(12);
310         }
311 
312 
313         /* Attempt to create the new_directory_name2 again.  */
314         length =  fx_unicode_length_get(new_directory_name2);
315         status =  fx_unicode_directory_create(&ram_disk,  new_directory_name2, length, (CHAR *) destination_name);
316 
317         /* Check for expected error.  */
318         if (status)
319         {
320 
321             /* Error creating unicode directory name.  Return to caller.  */
322             printf("ERROR!\n");
323             test_control_return(13);
324         }
325 
326         /* Attempt to create the new_directory_name1 again.  */
327         length =  fx_unicode_length_get(new_directory_name3);
328         status =  fx_unicode_directory_create(&ram_disk,  new_directory_name3, length, (CHAR *) destination_name);
329 
330         /* Check for expected error.  */
331         if (status)
332         {
333 
334             /* Error creating unicode directory name.  Return to caller.  */
335             printf("ERROR!\n");
336             test_control_return(14);
337         }
338 
339         /* Attempt to create the new_directory_name4 again.  */
340         length =  fx_unicode_length_get(new_directory_name4);
341         status =  fx_unicode_directory_create(&ram_disk,  new_directory_name4, length, (CHAR *) destination_name);
342 
343         /* Check for expected error.  */
344         if (status != FX_ALREADY_CREATED)
345         {
346 
347             /* Error creating unicode directory name.  Return to caller.  */
348             printf("ERROR!\n");
349             test_control_return(15);
350         }
351 
352         /* Create a directory whose name is consists of lowcase characters. */
353         length = fx_unicode_length_get(new_file_name5);
354         status = fx_unicode_directory_create(&ram_disk, new_file_name5, length, (CHAR *)destination_name);
355 
356         /* Check for expected error. */
357         return_value_if_fail( status == FX_SUCCESS, 16);
358 
359         /* Rename as the same directory name. */
360         length = fx_unicode_length_get(new_file_name5);
361         status = fx_unicode_directory_rename(&ram_disk, new_file_name5, length, new_file_name5, length, (CHAR *)destination_name);
362 
363         /* Check for expected error. */
364         return_value_if_fail( status == FX_SUCCESS, 17);
365 
366         length = fx_unicode_length_get(new_file_name6);
367         status = fx_unicode_directory_create(&ram_disk, new_file_name6, length, (CHAR *)destination_name);
368 
369         /* Check for expected error. */
370         return_value_if_fail( status == FX_SUCCESS, 18);
371 
372         /* Attempt to  rename new_file_name6 as existed new_file_name5 */
373         old_length = fx_unicode_length_get(new_file_name6);
374         new_length = fx_unicode_length_get(new_file_name5);
375         status = fx_unicode_directory_rename(&ram_disk, new_file_name6, old_length, new_file_name5, new_length, (CHAR *)destination_name);
376 
377         /* Check for expected error. */
378         return_value_if_fail( status == FX_ALREADY_CREATED, 19);
379 
380         /* Attempt to  rename new_file_name6 as new_file_name7 */
381         old_length = fx_unicode_length_get(new_file_name6);
382         new_length = fx_unicode_length_get(new_file_name7);
383         status = fx_unicode_directory_rename(&ram_disk, new_file_name6, old_length, new_file_name7, new_length, (CHAR *)destination_name);
384 
385         /* Check for expected error. */
386         return_value_if_fail( status == FX_SUCCESS, 20);
387 
388         /* Attempt to  rename new_file_name5 as exised new_file_name7 */
389         old_length = fx_unicode_length_get(new_file_name5);
390         new_length = fx_unicode_length_get(new_file_name7);
391         status = fx_unicode_directory_rename(&ram_disk, new_file_name5, old_length, new_file_name7, new_length, (CHAR *)destination_name);
392 
393         /* Check for expected error. */
394         return_value_if_fail( status == FX_ALREADY_CREATED, 21);
395 
396         /* Attempt to  rename new_file_name7 as itself. */
397         old_length = fx_unicode_length_get(new_file_name7);
398         new_length = fx_unicode_length_get(new_file_name7);
399         status = fx_unicode_directory_rename(&ram_disk, new_file_name7, old_length, new_file_name7, new_length, (CHAR *)destination_name);
400 
401         /* Check for expected error. */
402         return_value_if_fail( status == FX_SUCCESS, 22);
403 
404         /* Created  two directories whose name's first byte is 'z'+1. */
405         length = fx_unicode_length_get(new_file_name8);
406         status = fx_unicode_directory_create(&ram_disk, new_file_name8, length, (CHAR *)destination_name);
407         length = fx_unicode_length_get(new_file_name9);
408         status += fx_unicode_directory_create(&ram_disk, new_file_name9, length, (CHAR *)destination_name);
409         return_value_if_fail( status == FX_SUCCESS, 23);
410 
411         /* Attempt to rename new_file_name9 as existed new_file_name8 */
412         old_length = fx_unicode_length_get(new_file_name9);
413         new_length = fx_unicode_length_get(new_file_name8);
414         status = fx_unicode_directory_rename(&ram_disk, new_file_name9, old_length, new_file_name8, new_length, (CHAR *)destination_name);
415         return_value_if_fail( status == FX_ALREADY_CREATED, 24);
416 
417         /* Attempt to rename new_file_name9 as existed new_file_name7 */
418         old_length = fx_unicode_length_get(new_file_name9);
419         new_length = fx_unicode_length_get(new_file_name7);
420         status = fx_unicode_directory_rename(&ram_disk, new_file_name9, old_length, new_file_name7, new_length, (CHAR *)destination_name);
421         return_value_if_fail( status == FX_ALREADY_CREATED, 25);
422 
423         /* Attempt to create a lot of directories with similar names to overflow. */
424         length = fx_unicode_length_get(new_file_name5);
425         for ( count = 0; count < 27; count++)
426         {
427             new_file_name5[0]++;
428             status = fx_unicode_directory_create(&ram_disk, new_file_name5, length, (CHAR *)destination_name);
429             return_value_if_fail( (status == FX_SUCCESS) || (status == FX_ALREADY_CREATED) || (status == FX_NO_MORE_SPACE), 24 + count);
430         }
431         old_length = fx_unicode_length_get(new_file_name5_bak);
432         new_length = fx_unicode_length_get(new_file_name5);
433         status = fx_unicode_directory_rename(&ram_disk, new_file_name5_bak, old_length, new_file_name5, new_length, (CHAR *)destination_name);
434         return_value_if_fail( (status == FX_NO_MORE_SPACE) || ( status == FX_NOT_FOUND), 53);
435         /* Close the media.  */
436         status =  fx_media_close(&ram_disk);
437 
438         /* Determine if the test was successful.  */
439         return_value_if_fail( status == FX_SUCCESS, 51);
440     }
441 
442     printf("SUCCESS!\n");
443     test_control_return(0);
444 }
445