1 /* This FileX test concentrates on the Fault-Tolerant truncate release operation.  */
2 /*
3 For FAT 12, 16, 32, one cluster size is 1024 bytes;
4 
5 Check directory interrupt for fx_file_truncate_release():
6 1: Format and open the media;
7 2. Create and write 4K bytes into TEST.TXT;
8 3. Close file and close media;
9 4. Open the media;
10 5. Enable fault tolerant feature;
11 6. Check available bytes;
12 7. Truncate release to size 0 of TEST.TXT;
13 8. Write 4K into TEST.TXT;
14 9. Check available bytes. It should be the same as the one at (6);
15 10. Close the media;
16 11. Open the media;
17 12. Check available bytes. It should be the same as the one at (6).
18 */
19 
20 #ifndef FX_STANDALONE_ENABLE
21 #include   "tx_api.h"
22 #endif
23 #include   "fx_api.h"
24 #include   "fx_system.h"
25 #include   "fx_fault_tolerant.h"
26 #include   <stdio.h>
27 #include   "fx_ram_driver_test.h"
28 extern void    test_control_return(UINT status);
29 void    filex_fault_tolerant_file_truncate_release_available_test_application_define(void *first_unused_memory);
30 
31 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT_DATA)
32 
33 #define     DEMO_STACK_SIZE         4096
34 #define CACHE_SIZE                  2048
35 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
36 
37 
38 #define     FILE_SIZE               4096
39 
40 
41 /* Define the ThreadX and FileX object control blocks...  */
42 
43 #ifndef FX_STANDALONE_ENABLE
44 static TX_THREAD               ftest_0;
45 #endif
46 static FX_MEDIA                ram_disk;
47 static FX_FILE                 my_file;
48 static UCHAR                   *pointer;
49 
50 /* Define the counters used in the test application...  */
51 
52 #ifndef FX_STANDALONE_ENABLE
53 static UCHAR                   *cache_buffer;
54 static UCHAR                   *fault_tolerant_buffer;
55 static UCHAR                   *thread_buffer;
56 #else
57 static UCHAR                   cache_buffer[CACHE_SIZE];
58 static UCHAR                   fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
59 #endif
60 static CHAR                    write_buffer[FILE_SIZE];
61 
62 #define TEST_COUNT              3
63 
64 /* Define thread prototypes.  */
65 
66 static void    ftest_0_entry(ULONG thread_input);
67 
68 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
69 extern void    test_control_return(UINT status);
70 extern UINT    _filex_fault_tolerant_log_check(FX_MEDIA *media_ptr);
71 
72 
73 /* Define what the initial system looks like.  */
74 
75 #ifdef CTEST
test_application_define(void * first_unused_memory)76 void test_application_define(void *first_unused_memory)
77 #else
78 void    filex_fault_tolerant_file_truncate_release_available_test_application_define(void *first_unused_memory)
79 #endif
80 {
81 
82 
83 #ifndef FX_STANDALONE_ENABLE
84     /* Setup the working pointer.  */
85     pointer =  (UCHAR *) first_unused_memory;
86 
87     /* Create the main thread.  */
88 
89     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
90             pointer, DEMO_STACK_SIZE,
91             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
92 
93     pointer =  pointer + DEMO_STACK_SIZE;
94 
95     /* Setup memory for the RAM disk and the sector cache.  */
96     cache_buffer =  pointer;
97     pointer += CACHE_SIZE;
98     fault_tolerant_buffer = pointer;
99     pointer += FAULT_TOLERANT_SIZE;
100     thread_buffer = pointer;
101     pointer += DEMO_STACK_SIZE;
102 #endif
103 
104     /* Initialize the FileX system.  */
105     fx_system_initialize();
106 #ifdef FX_STANDALONE_ENABLE
107     ftest_0_entry(0);
108 #endif
109 }
110 
111 
112 
113 /* Define the test threads.  */
114 
ftest_0_entry(ULONG thread_input)115 static void    ftest_0_entry(ULONG thread_input)
116 {
117 
118 UINT        status;
119 UINT        i;
120 ULONG       available_bytes[3];
121 
122     FX_PARAMETER_NOT_USED(thread_input);
123 
124     /* Print out some test information banners.  */
125     printf("FileX Test:   Fault Tolerant File Truncate Release Available Test....");
126 
127     /* Loop to test FAT 12, 16, 32.   */
128     for (i = 0; i < TEST_COUNT; i ++)
129     {
130         if (i == 0)
131         {
132             /* Format the media with FAT12.  This needs to be done before opening it!  */
133             status =  fx_media_format(&ram_disk,
134                                      _fx_ram_driver,         // Driver entry
135                                      ram_disk_memory_large,  // RAM disk memory pointer
136                                      cache_buffer,           // Media buffer pointer
137                                      CACHE_SIZE,             // Media buffer size
138                                      "MY_RAM_DISK",          // Volume Name
139                                      1,                      // Number of FATs
140                                      32,                     // Directory Entries
141                                      0,                      // Hidden sectors
142                                      256,                    // Total sectors
143                                      256,                    // Sector size
144                                      8,                      // Sectors per cluster
145                                      1,                      // Heads
146                                      1);                     // Sectors per track
147         }
148         else if (i == 1)
149         {
150             /* Format the media with FAT16.  This needs to be done before opening it!  */
151             status =  fx_media_format(&ram_disk,
152                                      _fx_ram_driver,         // Driver entry
153                                      ram_disk_memory_large,  // RAM disk memory pointer
154                                      cache_buffer,           // Media buffer pointer
155                                      CACHE_SIZE,             // Media buffer size
156                                      "MY_RAM_DISK",          // Volume Name
157                                      1,                      // Number of FATs
158                                      32,                     // Directory Entries
159                                      0,                      // Hidden sectors
160                                      4200 * 8,               // Total sectors
161                                      256,                    // Sector size
162                                      8,                      // Sectors per cluster
163                                      1,                      // Heads
164                                      1);                     // Sectors per track
165         }
166         else if (i == 2)
167         {
168             /* Format the media with FAT32.  This needs to be done before opening it!  */
169             status =  fx_media_format(&ram_disk,
170                                      _fx_ram_driver,         // Driver entry
171                                      ram_disk_memory_large,  // RAM disk memory pointer
172                                      cache_buffer,           // Media buffer pointer
173                                      CACHE_SIZE,             // Media buffer size
174                                      "MY_RAM_DISK",          // Volume Name
175                                      1,                      // Number of FATs
176                                      32,                     // Directory Entries
177                                      0,                      // Hidden sectors
178                                      70000 * 8,              // Total sectors
179                                      256,                    // Sector size
180                                      8,                      // Sectors per cluster
181                                      1,                      // Heads
182                                      1);                     // Sectors per track
183         }
184 
185         /* Determine if the format had an error.  */
186         return_if_fail(status == FX_SUCCESS);
187 
188         /* Open the ram_disk.  */
189         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
190         return_if_fail(status == FX_SUCCESS);
191 
192         /* Create a file called TEST.TXT in the root directory.  */
193         status =  fx_file_create(&ram_disk, "TEST.TXT");
194         return_if_fail(status == FX_SUCCESS);
195 
196         /* Open the test file.  */
197         status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
198         return_if_fail(status == FX_SUCCESS);
199 
200         /* Write 4K bytes into the test file.  */
201         status =  fx_file_write(&my_file, write_buffer, sizeof(write_buffer));
202         return_if_fail(status == FX_SUCCESS);
203 
204         /* Close the test file.  */
205         status =  fx_file_close(&my_file);
206         return_if_fail(status == FX_SUCCESS);
207 
208         /* Close the media.  */
209         status =  fx_media_close(&ram_disk);
210         return_if_fail(status == FX_SUCCESS);
211 
212         /* Open the ram_disk.  */
213         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
214         return_if_fail(status == FX_SUCCESS);
215 
216         /* Enable the Fault-tolerant feature.  */
217         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
218         return_if_fail(status == FX_SUCCESS);
219 
220         /* Get available_bytes before truncate release. */
221         status = fx_media_space_available(&ram_disk, &available_bytes[0]);
222         return_if_fail(status == FX_SUCCESS);
223 
224         /* Open the test file.  */
225         status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
226         return_if_fail(status == FX_SUCCESS);
227 
228         /* Truncate the "TEST.TXT" file from 4K to 0.  */
229         fx_file_truncate_release(&my_file, 0);
230         return_if_fail(status == FX_SUCCESS);
231 
232         /* Write 4K bytes into the test file.  */
233         status =  fx_file_write(&my_file, write_buffer, sizeof(write_buffer));
234         return_if_fail(status == FX_SUCCESS);
235 
236         /* Close the test file.  */
237         status =  fx_file_close(&my_file);
238         return_if_fail(status == FX_SUCCESS);
239 
240         /* Get available_bytes before truncate release. */
241         status = fx_media_space_available(&ram_disk, &available_bytes[1]);
242         return_if_fail(status == FX_SUCCESS);
243 
244         /* Check available bytes. */
245         return_if_fail(available_bytes[0] == available_bytes[1]);
246 
247         /* Close the media.  */
248         status =  fx_media_close(&ram_disk);
249         return_if_fail(status == FX_SUCCESS);
250 
251         /* Open the ram_disk.  */
252         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
253         return_if_fail(status == FX_SUCCESS);
254 
255         /* Enable the Fault-tolerant feature.  */
256         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
257         return_if_fail(status == FX_SUCCESS);
258 
259         /* Get available_bytes after write. */
260         status = fx_media_space_available(&ram_disk, &available_bytes[2]);
261         return_if_fail(status == FX_SUCCESS);
262 
263         /* Close the media.  */
264         status =  fx_media_close(&ram_disk);
265         return_if_fail(status == FX_SUCCESS);
266 
267         /* Check available bytes. */
268         return_if_fail(available_bytes[0] == available_bytes[2]);
269     }
270 
271     /* for coverage */
272 
273     /* Format the media with FAT32.  This needs to be done before opening it!  */
274     status =  fx_media_format(&ram_disk,
275                              _fx_ram_driver,         // Driver entry
276                              ram_disk_memory_large,  // RAM disk memory pointer
277                              cache_buffer,           // Media buffer pointer
278                              CACHE_SIZE,             // Media buffer size
279                              "MY_RAM_DISK",          // Volume Name
280                              1,                      // Number of FATs
281                              32,                     // Directory Entries
282                              0,                      // Hidden sectors
283                              70000 * 8,              // Total sectors
284                              256,                    // Sector size
285                              8,                      // Sectors per cluster
286                              1,                      // Heads
287                              1);                     // Sectors per track
288     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
289     status += fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
290     status += fx_file_create(&ram_disk, "TEST.TXT");
291     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
292     status += fx_file_allocate(&my_file, 4 * 512);
293     return_if_fail(status == FX_SUCCESS);
294 
295     /* Fail while setting undo log. */
296     _fx_utility_logical_sector_write_error_request = 1;
297     status = fx_file_truncate_release(&my_file, 0);
298     return_if_fail(status == FX_IO_ERROR);
299     _fx_utility_logical_sector_write_error_request = 0;
300 
301     fx_file_close(&my_file);
302     fx_media_close(&ram_disk);
303 
304     for (i = 1; i < 100; i++)
305     {
306 
307         /* Format the media with FAT32.  This needs to be done before opening it!  */
308         status =  fx_media_format(&ram_disk,
309                                  _fx_ram_driver,         // Driver entry
310                                  ram_disk_memory_large,  // RAM disk memory pointer
311                                  cache_buffer,           // Media buffer pointer
312                                  CACHE_SIZE,             // Media buffer size
313                                  "MY_RAM_DISK",          // Volume Name
314                                  1,                      // Number of FATs
315                                  32,                     // Directory Entries
316                                  0,                      // Hidden sectors
317                                  70000 * 8,              // Total sectors
318                                  256,                    // Sector size
319                                  8,                      // Sectors per cluster
320                                  1,                      // Heads
321                                  1);                     // Sectors per track
322         status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
323         status += fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
324         status += fx_file_create(&ram_disk, "TEST.TXT");
325         status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
326         status += fx_file_write(&my_file, write_buffer, sizeof(write_buffer));
327         status += fx_file_close(&my_file);
328         status += fx_file_create(&ram_disk, "AAA.TXT");
329         status += fx_file_open(&ram_disk, &my_file, "AAA.TXT", FX_OPEN_FOR_WRITE);
330         status += fx_file_write(&my_file, write_buffer, sizeof(write_buffer));
331         status += fx_file_close(&my_file);
332         status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
333         status += fx_file_allocate(&my_file, 512);
334         return_if_fail(status == FX_SUCCESS);
335 
336         _fx_utility_fat_entry_read_error_request = i;
337         status = fx_file_truncate_release(&my_file, 256);
338         return_if_fail((status == FX_IO_ERROR) || (i > 6));
339         _fx_utility_fat_entry_read_error_request = 0;
340 
341         fx_file_close(&my_file);
342         fx_media_close(&ram_disk);
343 
344         if (status != FX_IO_ERROR)
345         {
346             break;
347         }
348     }
349 
350     /* Output successful.  */
351     printf("SUCCESS!\n");
352     test_control_return(0);
353 }
354 #else
355 
356 #ifdef CTEST
test_application_define(void * first_unused_memory)357 void test_application_define(void *first_unused_memory)
358 #else
359 void    filex_fault_tolerant_file_truncate_release_available_test_application_define(void *first_unused_memory)
360 #endif
361 {
362 
363     FX_PARAMETER_NOT_USED(first_unused_memory);
364 
365     /* Print out some test information banners.  */
366     printf("FileX Test:   Fault Tolerant File Truncate Release Available Test....N/A\n");
367 
368     test_control_return(255);
369 }
370 #endif
371 
372