1 /* This FileX test concentrates on the file attributes read/set operation.  */
2 
3 #ifndef FX_STANDALONE_ENABLE
4 #include   "tx_api.h"
5 #endif
6 #include   "fx_api.h"
7 #include   <stdio.h>
8 #include   "fx_ram_driver_test.h"
9 
10 #define     DEMO_STACK_SIZE         4096
11 #define     CACHE_SIZE              128*128
12 
13 
14 /* Define the ThreadX and FileX object control blocks...  */
15 
16 #ifndef FX_STANDALONE_ENABLE
17 static TX_THREAD               ftest_0;
18 #endif
19 static FX_MEDIA                ram_disk;
20 static FX_FILE                 my_file;
21 static FX_FILE                 my_file1;
22 static FX_FILE                 my_file2;
23 static FX_FILE                 my_file3;
24 static FX_FILE                 my_file4;
25 static FX_FILE                 my_file5;
26 static FX_FILE                 my_file6;
27 static FX_FILE                 my_file7;
28 static FX_FILE                 my_file8;
29 static FX_FILE                 my_file9;
30 
31 
32 /* Define the counters used in the test application...  */
33 
34 #ifndef FX_STANDALONE_ENABLE
35 static UCHAR                  *ram_disk_memory;
36 static UCHAR                  *cache_buffer;
37 #else
38 static UCHAR                   cache_buffer[CACHE_SIZE];
39 #endif
40 
41 
42 /* Define thread prototypes.  */
43 
44 void    filex_file_attributes_read_set_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_attributes_read_set_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     ram_disk_memory =  pointer;
79 
80 #endif
81 
82     /* Initialize the FileX system.  */
83     fx_system_initialize();
84 #ifdef FX_STANDALONE_ENABLE
85     ftest_0_entry(0);
86 #endif
87 }
88 
89 
90 
91 /* Define the test threads.  */
92 
ftest_0_entry(ULONG thread_input)93 static void    ftest_0_entry(ULONG thread_input)
94 {
95 
96 UINT        status;
97 UINT        attributes;
98 FX_FILE     open_file;
99 
100     FX_PARAMETER_NOT_USED(thread_input);
101 
102     /* Print out some test information banners.  */
103     printf("FileX Test:   File attributes read/set test..........................");
104 
105     /* Format the media.  This needs to be done before opening it!  */
106     status =  fx_media_format(&ram_disk,
107                             _fx_ram_driver,         // Driver entry
108                             ram_disk_memory,        // RAM disk memory pointer
109                             cache_buffer,           // Media buffer pointer
110                             CACHE_SIZE,             // Media buffer size
111                             "MY_RAM_DISK",          // Volume Name
112                             1,                      // Number of FATs
113                             32,                     // Directory Entries
114                             0,                      // Hidden sectors
115                             512,                    // Total sectors
116                             128,                    // Sector size
117                             1,                      // Sectors per cluster
118                             1,                      // Heads
119                             1);                     // Sectors per track
120 
121     /* Determine if the format had an error.  */
122     if (status)
123     {
124 
125         printf("ERROR!\n");
126         test_control_return(1);
127     }
128 
129     /* Attempt to read file attributes before the media has been opened */
130     status = fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
131     if (status != FX_MEDIA_NOT_OPEN)
132     {
133         printf("ERROR!\n");
134         test_control_return(2);
135     }
136 
137     /* Attempt to set file attributes before the media has been opened */
138     status = fx_file_attributes_set(&ram_disk, "TEST.TXT", 0);
139     if (status != FX_MEDIA_NOT_OPEN)
140     {
141         printf("ERROR!\n");
142         test_control_return(3);
143     }
144 
145     /* Open the ram_disk.  */
146     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
147 
148     /* Check the status.  */
149     if (status != FX_SUCCESS)
150     {
151 
152         /* Error, return error code.  */
153         printf("ERROR!\n");
154         test_control_return(4);
155     }
156 
157     /* Attempt to set file attributes while the media is write protected */
158     ram_disk.fx_media_driver_write_protect = FX_TRUE;
159     status = fx_file_attributes_set(&ram_disk, "TEST.TXT", FX_READ_ONLY);
160     if (status != FX_WRITE_PROTECT)
161     {
162         printf("ERROR!\n");
163         test_control_return(5);
164     }
165     ram_disk.fx_media_driver_write_protect = FX_FALSE;
166 
167     /* attempt to read file attributes from a file that does not exist */
168     status = fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
169     if (status == FX_SUCCESS)
170     {
171         printf("ERROR!\n");
172         test_control_return(6);
173     }
174 
175     /* Attempt to set file attributes before the media has been opened */
176     status = fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes);
177     if (status == FX_SUCCESS)
178     {
179         printf("ERROR!\n");
180         test_control_return(7);
181     }
182 
183     /* attempt to read file attributes from something that is not a file */
184     status  = fx_directory_create(&ram_disk, "NOT_A_FILE");
185     status += fx_file_attributes_read(&ram_disk, "NOT_A_FILE", &attributes);
186     if (status != FX_NOT_A_FILE)
187     {
188         printf("ERROR!\n");
189         test_control_return(8);
190     }
191 
192     /* Attempt to set file attributes before the media has been opened */
193     status = fx_file_attributes_set(&ram_disk, "NOT_A_FILE", FX_READ_ONLY);
194     if (status != FX_NOT_A_FILE)
195     {
196         printf("ERROR!\n");
197         test_control_return(9);
198     }
199 
200     /* Attempt to set file attributes with invlaid file name.  */
201     status = fx_file_attributes_set(&ram_disk, "NOT_EXIST", FX_READ_ONLY);
202     if (status != FX_NOT_FOUND)
203     {
204         printf("ERROR!\n");
205         test_control_return(9);
206     }
207 
208     /* Create a file called TEST.TXT in the root directory.  */
209     status =  fx_file_create(&ram_disk, "TEST.TXT");
210 
211     /* Check the create status.  */
212     if (status != FX_SUCCESS)
213     {
214 
215         printf("ERROR!\n");
216         test_control_return(10);
217     }
218 
219 #ifndef FX_DISABLE_ERROR_CHECKING
220 
221     /* send null pointer to generate an error */
222     status = fx_file_attributes_set(FX_NULL, "filename", FX_READ_ONLY);
223     if (status != FX_PTR_ERROR)
224     {
225         printf("ERROR!\n");
226         test_control_return(11);
227     }
228 
229     /* send null pointer to generate an error */
230     status = fx_file_attributes_set(&ram_disk, "filename", 0x40);
231     if (status != FX_INVALID_ATTR)
232     {
233         printf("ERROR!\n");
234         test_control_return(12);
235     }
236 
237     /* send null pointer to generate an error */
238     status = fx_file_attributes_read(FX_NULL, "filename", FX_NULL);
239     if (status != FX_PTR_ERROR)
240     {
241         printf("ERROR!\n");
242         test_control_return(13);
243     }
244 
245 #endif
246 
247     /* Invalidate the media cache.  */
248     fx_media_cache_invalidate(&ram_disk);
249 
250     /* Pickup the attributes of the file.  */
251     status =  fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
252 
253     /* Check the attributes read status.  */
254     if ((status != FX_SUCCESS) || (attributes != FX_ARCHIVE))
255     {
256 
257         printf("ERROR!\n");
258         test_control_return(14);
259     }
260 
261     /* Invalidate the media cache.  */
262     fx_media_cache_invalidate(&ram_disk);
263 
264     /* Now write the attributes out for the file.  */
265     status =  fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes | FX_READ_ONLY);
266 
267     /* Check the attributes set status.  */
268     if (status != FX_SUCCESS)
269     {
270 
271         printf("ERROR!\n");
272         test_control_return(15);
273     }
274 
275     /* Write the attributes out to the file while another file is opened to get better code coverage */
276     status  = fx_file_create(&ram_disk, "open_file.txt");
277     status += fx_file_open(&ram_disk, &open_file, "open_file.txt", FX_OPEN_FOR_WRITE);
278     status += fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes | FX_READ_ONLY);
279     if (status != FX_SUCCESS)
280     {
281         printf("ERROR!\n");
282         test_control_return(16);
283     }
284 
285     /* Invalidate the media cache.  */
286     fx_media_cache_invalidate(&ram_disk);
287 
288     /* Pickup the attributes of the file again.  */
289     status =  fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
290 
291     /* Check the attributes read status.  */
292     if ((status != FX_SUCCESS) || (attributes != (FX_ARCHIVE | FX_READ_ONLY)))
293     {
294 
295         printf("ERROR!\n");
296         test_control_return(17);
297     }
298 
299     /* Open the test file... this should fail since we set the attributes to read only!  */
300     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
301 
302     /* Check the file open status.  */
303     if (status != FX_ACCESS_ERROR)
304     {
305 
306         printf("ERROR!\n");
307         test_control_return(18);
308     }
309 
310     /* Attempt to set file attributes for a file that is open */
311     status  = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
312     status += fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes);
313     if (status != FX_ACCESS_ERROR)
314     {
315         printf("ERROR!\n");
316         test_control_return(19);
317     }
318 
319     /* Close the media.  */
320     status =  fx_media_close(&ram_disk);
321 
322     /* Determine if the test was successful.  */
323     if (status != FX_SUCCESS)
324     {
325 
326         printf("ERROR!\n");
327         test_control_return(20);
328     }
329 
330     /* Now test the attributes set with multiple files open that have directory entries that reside on
331        different logical sectors.  */
332 
333     /* Format the media.  This needs to be done before opening it!  */
334     status =  fx_media_format(&ram_disk,
335                             _fx_ram_driver,         // Driver entry
336                             ram_disk_memory,        // RAM disk memory pointer
337                             cache_buffer,           // Media buffer pointer
338                             CACHE_SIZE,             // Media buffer size
339                             "MY_RAM_DISK",          // Volume Name
340                             1,                      // Number of FATs
341                             32,                     // Directory Entries
342                             0,                      // Hidden sectors
343                             512,                    // Total sectors
344                             128,                    // Sector size
345                             1,                      // Sectors per cluster
346                             1,                      // Heads
347                             1);                     // Sectors per track
348 
349     /* Determine if the format had an error.  */
350     if (status)
351     {
352 
353         printf("ERROR!\n");
354         test_control_return(21);
355     }
356 
357     /* Open the ram_disk.  */
358     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
359 
360     /* Check the status.  */
361     if (status != FX_SUCCESS)
362     {
363 
364         /* Error, return error code.  */
365         printf("ERROR!\n");
366         test_control_return(22);
367     }
368 
369     /* Create a set of test files.  */
370     status =  fx_file_create(&ram_disk, "TEST.TXT");
371     status += fx_file_create(&ram_disk, "TEST1.TXT");
372     status += fx_file_create(&ram_disk, "TEST2.TXT");
373     status += fx_file_create(&ram_disk, "TEST3.TXT");
374     status += fx_file_create(&ram_disk, "TEST4.TXT");
375     status += fx_file_create(&ram_disk, "TEST5.TXT");
376     status += fx_file_create(&ram_disk, "TEST6.TXT");
377     status += fx_file_create(&ram_disk, "TEST7.TXT");
378     status += fx_file_create(&ram_disk, "TEST8.TXT");
379     status += fx_file_create(&ram_disk, "TEST9.TXT");
380     status += fx_file_create(&ram_disk, "TEST10.TXT");
381 
382     /* Now open all the files except the TEST.TXT.  */
383     status += fx_file_open(&ram_disk, &my_file, "TEST1.TXT", FX_OPEN_FOR_WRITE);
384     status += fx_file_open(&ram_disk, &my_file1, "TEST2.TXT", FX_OPEN_FOR_WRITE);
385     status += fx_file_open(&ram_disk, &my_file2, "TEST3.TXT", FX_OPEN_FOR_WRITE);
386     status += fx_file_open(&ram_disk, &my_file3, "TEST4.TXT", FX_OPEN_FOR_WRITE);
387     status += fx_file_open(&ram_disk, &my_file4, "TEST5.TXT", FX_OPEN_FOR_WRITE);
388     status += fx_file_open(&ram_disk, &my_file5, "TEST6.TXT", FX_OPEN_FOR_WRITE);
389     status += fx_file_open(&ram_disk, &my_file6, "TEST7.TXT", FX_OPEN_FOR_WRITE);
390     status += fx_file_open(&ram_disk, &my_file7, "TEST8.TXT", FX_OPEN_FOR_WRITE);
391     status += fx_file_open(&ram_disk, &my_file8, "TEST9.TXT", FX_OPEN_FOR_WRITE);
392     status += fx_file_open(&ram_disk, &my_file9, "TEST10.TXT", FX_OPEN_FOR_WRITE);
393 
394     /* Now set the attributes for TEST.TXT.  */
395     status += fx_file_attributes_set(&ram_disk, "TEST.TXT", FX_READ_ONLY);
396 
397     /* Check the status.  */
398     if (status != FX_SUCCESS)
399     {
400 
401         /* Error, return error code.  */
402         printf("ERROR!\n");
403         test_control_return(23);
404     }
405 
406     /* Close the media.  */
407     status =  fx_media_close(&ram_disk);
408 
409     /* Determine if the test was successful.  */
410     if (status != FX_SUCCESS)
411     {
412 
413         printf("ERROR!\n");
414         test_control_return(24);
415     }
416     else
417     {
418 
419         printf("SUCCESS!\n");
420         test_control_return(0);
421     }
422 }
423 
424