1 /* This FileX test concentrates on the file date/time 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                 file_0;
21 static FX_FILE                 file_1;
22 static FX_FILE                 file_2;
23 static FX_FILE                 file_3;
24 static FX_FILE                 file_4;
25 static FX_FILE                 file_5;
26 static FX_FILE                 file_6;
27 
28 
29 /* Define the counters used in the test application...  */
30 
31 #ifndef FX_STANDALONE_ENABLE
32 static UCHAR                  *ram_disk_memory;
33 static UCHAR                  *cache_buffer;
34 #else
35 static UCHAR                   cache_buffer[CACHE_SIZE];
36 #endif
37 
38 
39 /* Define thread prototypes.  */
40 
41 void    filex_file_date_time_set_application_define(void *first_unused_memory);
42 static void    ftest_0_entry(ULONG thread_input);
43 
44 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
45 void  test_control_return(UINT status);
46 
47 
48 
49 /* Define what the initial system looks like.  */
50 
51 #ifdef CTEST
test_application_define(void * first_unused_memory)52 void test_application_define(void *first_unused_memory)
53 #else
54 void    filex_file_date_time_set_application_define(void *first_unused_memory)
55 #endif
56 {
57 
58 #ifndef FX_STANDALONE_ENABLE
59 UCHAR    *pointer;
60 
61 
62     /* Setup the working pointer.  */
63     pointer =  (UCHAR *) first_unused_memory;
64 
65     /* Create the main thread.  */
66     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
67             pointer, DEMO_STACK_SIZE,
68             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
69 
70     pointer =  pointer + DEMO_STACK_SIZE;
71 
72     /* Setup memory for the RAM disk and the sector cache.  */
73     cache_buffer =  pointer;
74     pointer =  pointer + CACHE_SIZE;
75     ram_disk_memory =  pointer;
76 
77 #endif
78 
79     /* Initialize the FileX system.  */
80     fx_system_initialize();
81 #ifdef FX_STANDALONE_ENABLE
82     ftest_0_entry(0);
83 #endif
84 }
85 
86 
87 
88 /* Define the test threads.  */
89 
ftest_0_entry(ULONG thread_input)90 static void    ftest_0_entry(ULONG thread_input)
91 {
92 
93 UINT        status;
94 UINT        attributes;
95 ULONG       size;
96 UINT        year, month, day, hour, minute, second;
97 #ifndef FX_DISABLE_ERROR_CHECKING
98 UINT        i;
99 #endif /* FX_DISABLE_ERROR_CHECKING */
100 FX_FILE     my_file;
101 
102     FX_PARAMETER_NOT_USED(thread_input);
103 
104     /* Print out some test information banners.  */
105     printf("FileX Test:   File date/time set 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                             32,                     // Directory Entries
116                             0,                      // Hidden sectors
117                             512,                    // Total sectors
118                             128,                    // Sector size
119                             1,                      // Sectors per cluster
120                             1,                      // Heads
121                             1);                     // Sectors per track
122 
123     /* Determine if the format had an error.  */
124     if (status)
125     {
126 
127         printf("ERROR!\n");
128         test_control_return(1);
129     }
130 
131     /* try to retrieve directory information before the media has been opened to generate an error */
132     status =  fx_directory_information_get(&ram_disk, "DOES_NOT_EXIST", &attributes, &size,
133                                         &year, &month, &day, &hour, &minute, &second);
134     if (status != FX_MEDIA_NOT_OPEN)
135     {
136         printf("ERROR!\n");
137         test_control_return(2);
138     }
139 
140     /* Attempt to set the date and time for a file before the media is opened to generate an error */
141     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2000, 1, 1, 1, 1, 1);
142     if (status != FX_MEDIA_NOT_OPEN)
143     {
144         printf("ERROR!\n");
145         test_control_return(3);
146     }
147 
148     /* Open the ram_disk.  */
149     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
150 
151     /* Check the status.  */
152     if (status != FX_SUCCESS)
153     {
154 
155         /* Error, return error code.  */
156         printf("ERROR!\n");
157         test_control_return(4);
158     }
159 
160     /* Create a few files file in the root directory.  */
161     status  = fx_file_create(&ram_disk, "TEST.TXT");
162     status += fx_file_create(&ram_disk, "OPENED_FOR_WRITE.TXT");
163     status += fx_file_create(&ram_disk, "NOT_OPENED.TXT");
164 
165     /* Check the create status.  */
166     if (status != FX_SUCCESS)
167     {
168 
169         printf("ERROR!\n");
170         test_control_return(5);
171     }
172 
173 /* test error checking */
174 #ifndef FX_DISABLE_ERROR_CHECKING
175 
176     /* send a null pointer to generate an error */
177     status =  fx_directory_information_get(&ram_disk, "DOES_NOT_EXIST", FX_NULL, FX_NULL,
178                                         FX_NULL, FX_NULL, FX_NULL, FX_NULL, FX_NULL, FX_NULL);
179     if (status != FX_PTR_ERROR)
180     {
181         printf("ERROR!\n");
182         test_control_return(6);
183     }
184 
185     /* send null pointer to generate an error */
186     status = fx_file_date_time_set(FX_NULL, "TEST.TXT", 0, 0, 0, 0, 0, 0);
187     if (status != FX_PTR_ERROR)
188     {
189         printf("ERROR!\n");
190         test_control_return(7);
191     }
192 
193     /* send an invalid year to generate an error */
194     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 0, 0, 0, 0, 0, 0);
195     if (status != FX_INVALID_YEAR)
196     {
197         printf("ERROR!\n");
198         test_control_return(8);
199     }
200 
201     /* send an invalid month to generate an error */
202     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2015, 0, 1, 0, 0, 0);
203     if (status != FX_INVALID_MONTH)
204     {
205         printf("ERROR!\n");
206         test_control_return(9);
207     }
208 
209     /* send a valid and invalid day for each month to generate an error */
210     for (i = 1; i <= 12; i++)
211     {
212         status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2015, i, 0, 0, 0, 0);
213         if (status != FX_INVALID_DAY)
214         {
215             printf("ERROR!\n");
216             test_control_return(10);
217         }
218 
219         status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2015, i, 1, 1, 1, 1);
220         if (status != FX_SUCCESS)
221         {
222             printf("ERROR!\n");
223             test_control_return(11);
224         }
225     }
226 
227     /* send an invalid day for feb of a leap year */
228     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2016, 2, 0, 0, 0, 0);
229     if (status != FX_INVALID_DAY)
230     {
231         printf("ERROR!\n");
232         test_control_return(12);
233     }
234 
235     /* send an invalid hour to generate an error */
236     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2000, 1, 1, 99, 0, 0);
237     if (status != FX_INVALID_HOUR)
238     {
239         printf("ERROR!\n");
240         test_control_return(13);
241     }
242 
243     /* send an invalid minute to generate an error */
244     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2000, 1, 1, 1, 99, 0);
245     if (status != FX_INVALID_MINUTE)
246     {
247         printf("ERROR!\n");
248         test_control_return(14);
249     }
250 
251     /* send an invalid second to generate an error */
252     status = fx_file_date_time_set(&ram_disk, "TEST.TXT", 2000, 1, 1, 1, 1, 99);
253     if (status != FX_INVALID_SECOND)
254     {
255         printf("ERROR!\n");
256         test_control_return(15);
257     }
258 
259 #endif
260 
261     /* Attempt to set the date and time for a file that does not exist to generate an error */
262     status = fx_file_date_time_set(&ram_disk, "DOES_NOT_EXIST", 2000, 1, 1, 1, 1, 1);
263     if (status == FX_SUCCESS)
264     {
265         printf("ERROR!\n");
266         test_control_return(16);
267     }
268 
269     /* Invalidate the media cache.  */
270     fx_media_cache_invalidate(&ram_disk);
271 
272     /* Set the date and time for the file.  */
273     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
274                 1999, 12, 31, 23, 59, 58);
275 
276     /* Check the date/time set status.  */
277     if (status != FX_SUCCESS)
278     {
279 
280         printf("ERROR!\n");
281         test_control_return(17);
282     }
283 
284     /* try to retrieve directory information for something that doesnt exist to generate an error */
285     status =  fx_directory_information_get(&ram_disk, "DOES_NOT_EXIST", &attributes, &size,
286                                         &year, &month, &day, &hour, &minute, &second);
287     if (status == FX_SUCCESS)
288     {
289         printf("ERROR!\n");
290         test_control_return(18);
291     }
292 
293     /* Invalidate the media cache.  */
294     fx_media_cache_invalidate(&ram_disk);
295 
296     /* Now pickup the date/time for the file.  */
297     status =  fx_directory_information_get(&ram_disk, "TEST.TXT", &attributes, &size,
298                                         &year, &month, &day, &hour, &minute, &second);
299 
300     /* Check the date/time status.  */
301     if ((status != FX_SUCCESS) || (attributes != FX_ARCHIVE) || (size != 0) ||
302         (year != 1999) || (month != 12) || (day != 31) || (hour != 23) || (minute != 59) || (second != 58))
303     {
304 
305         printf("ERROR!\n");
306         test_control_return(19);
307     }
308 
309     /* get the date/time for the other files while some are opened to get better code coverage */
310     status  = fx_file_open(&ram_disk, &my_file, "OPENED_FOR_WRITE.TXT", FX_OPEN_FOR_WRITE);
311     status += fx_directory_information_get(&ram_disk, "OPENED_FOR_WRITE.TXT", &attributes, &size,
312                                         &year, &month, &day, &hour, &minute, &second);
313     status += fx_directory_information_get(&ram_disk, "NOT_OPENED.TXT", &attributes, &size,
314                                         &year, &month, &day, &hour, &minute, &second);
315     if (status != FX_SUCCESS)
316     {
317         printf("ERROR!\n");
318         test_control_return(20);
319     }
320 
321     /* Close the media.  */
322     status =  fx_media_close(&ram_disk);
323 
324     /* Determine if the test was successful.  */
325     if (status != FX_SUCCESS)
326     {
327 
328         printf("ERROR!\n");
329         test_control_return(21);
330     }
331 #ifndef FX_DISABLE_ERROR_CHECKING
332     /* Set the date and time for with an invalid file name.  */
333     status =  fx_file_date_time_set(&ram_disk, NULL,
334                 1999, 12, 31, 23, 59, 58);
335 
336     /* Check the date/time set status.  */
337     if (status != FX_PTR_ERROR)
338     {
339 
340         printf("ERROR!\n");
341         test_control_return(22);
342     }
343 
344     /* Set the date and time for with an invalid year.  */
345     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
346                 FX_MAXIMUM_YEAR+1, 12, 31, 23, 59, 58);
347 
348     /* Check the date/time set status.  */
349     if (status != FX_INVALID_YEAR)
350     {
351 
352         printf("ERROR!\n");
353         test_control_return(23);
354     }
355 
356     /* Set the date and time for with an invalid day for January.  */
357     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
358                 2016, 1, 32, 23, 59, 58);
359 
360     /* Check the date/time set status.  */
361     if (status != FX_INVALID_DAY)
362     {
363 
364         printf("ERROR!\n");
365         test_control_return(24);
366     }
367 
368     /* Set the date and time for with an invalid day for Febuary leap year.  */
369     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
370                 2016, 2, 30, 23, 59, 58);
371 
372     /* Check the date/time set status.  */
373     if (status != FX_INVALID_DAY)
374     {
375 
376         printf("ERROR!\n");
377         test_control_return(25);
378     }
379 
380     /* Set the date and time for with a valid day for Febuary leap year.  */
381     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
382                 2016, 2, 29, 24, 59, 58);
383 
384     /* Check the date/time set status.  */
385     if (status != FX_INVALID_HOUR)
386     {
387 
388         printf("ERROR!\n");
389         test_control_return(26);
390     }
391 
392     /* Set the date and time for with an invalid day for Febuary.  */
393     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
394                 2017, 2, 29, 23, 59, 58);
395 
396     /* Check the date/time set status.  */
397     if (status != FX_INVALID_DAY)
398     {
399 
400         printf("ERROR!\n");
401         test_control_return(27);
402     }
403 
404     /* Set the date and time for with an invalid day for March.  */
405     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
406                 2017, 3, 32, 23, 59, 58);
407 
408     /* Check the date/time set status.  */
409     if (status != FX_INVALID_DAY)
410     {
411 
412         printf("ERROR!\n");
413         test_control_return(28);
414     }
415 
416     /* Set the date and time for with an invalid day for April.  */
417     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
418                 2017, 4, 31, 23, 59, 58);
419 
420     /* Check the date/time set status.  */
421     if (status != FX_INVALID_DAY)
422     {
423 
424         printf("ERROR!\n");
425         test_control_return(29);
426     }
427 
428     /* Set the date and time for with an invalid day for May.  */
429     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
430                 2017, 5, 32, 23, 59, 58);
431 
432     /* Check the date/time set status.  */
433     if (status != FX_INVALID_DAY)
434     {
435 
436         printf("ERROR!\n");
437         test_control_return(30);
438     }
439 
440     /* Set the date and time for with an invalid day for June.  */
441     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
442                 2017, 6, 31, 23, 59, 58);
443 
444     /* Check the date/time set status.  */
445     if (status != FX_INVALID_DAY)
446     {
447 
448         printf("ERROR!\n");
449         test_control_return(31);
450     }
451 
452     /* Set the date and time for with an invalid day for July.  */
453     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
454                 2017, 7, 32, 23, 59, 58);
455 
456     /* Check the date/time set status.  */
457     if (status != FX_INVALID_DAY)
458     {
459 
460         printf("ERROR!\n");
461         test_control_return(32);
462     }
463 
464     /* Set the date and time for with an invalid day for August.  */
465     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
466                 2017, 8, 32, 23, 59, 58);
467 
468     /* Check the date/time set status.  */
469     if (status != FX_INVALID_DAY)
470     {
471 
472         printf("ERROR!\n");
473         test_control_return(33);
474     }
475 
476     /* Set the date and time for with an invalid day for September.  */
477     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
478                 2017, 9, 31, 23, 59, 58);
479 
480     /* Check the date/time set status.  */
481     if (status != FX_INVALID_DAY)
482     {
483 
484         printf("ERROR!\n");
485         test_control_return(34);
486     }
487 
488     /* Set the date and time for with an invalid day for October.  */
489     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
490                 2017, 10, 32, 23, 59, 58);
491 
492     /* Check the date/time set status.  */
493     if (status != FX_INVALID_DAY)
494     {
495 
496         printf("ERROR!\n");
497         test_control_return(35);
498     }
499 
500     /* Set the date and time for with an invalid day for November.  */
501     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
502                 2017, 11, 31, 23, 59, 58);
503 
504     /* Check the date/time set status.  */
505     if (status != FX_INVALID_DAY)
506     {
507 
508         printf("ERROR!\n");
509         test_control_return(36);
510     }
511 
512     /* Set the date and time for with an invalid day for December.  */
513     status =  fx_file_date_time_set(&ram_disk, "TEST.TXT",
514                 2017, 12, 32, 23, 59, 58);
515 
516     /* Check the date/time set status.  */
517     if (status != FX_INVALID_DAY)
518     {
519 
520         printf("ERROR!\n");
521         test_control_return(37);
522     }
523 #endif
524     /* Format the media.  This needs to be done before opening it!  */
525     status =  fx_media_format(&ram_disk,
526                             _fx_ram_driver,         // Driver entry
527                             ram_disk_memory,        // RAM disk memory pointer
528                             cache_buffer,           // Media buffer pointer
529                             CACHE_SIZE,             // Media buffer size
530                             "MY_RAM_DISK",          // Volume Name
531                             1,                      // Number of FATs
532                             64,                     // Directory Entries
533                             0,                      // Hidden sectors
534                             4096,                   // Total sectors
535                             128,                    // Sector size
536                             1,                      // Sectors per cluster
537                             1,                      // Heads
538                             1);                     // Sectors per track
539 
540     /* Determine if the format had an error.  */
541     if (status)
542     {
543 
544         printf("ERROR!\n");
545         test_control_return(38);
546     }
547 
548     /* Open the ram_disk.  */
549     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
550 
551     /* Check the status.  */
552     if (status != FX_SUCCESS)
553     {
554 
555         /* Error, return error code.  */
556         printf("ERROR!\n");
557         test_control_return(39);
558     }
559 
560     /* Create a bunch of files.  */
561     status =   fx_file_create(&ram_disk, "TEST.TXT");
562     status +=  fx_file_create(&ram_disk, "TEST1.TXT");
563     status +=  fx_file_create(&ram_disk, "TEST2.TXT");
564     status +=  fx_file_create(&ram_disk, "TEST3.TXT");
565     status +=  fx_file_create(&ram_disk, "TEST4.TXT");
566     status +=  fx_file_create(&ram_disk, "TEST5.TXT");
567     status +=  fx_file_create(&ram_disk, "TEST6.TXT");
568     status +=  fx_file_create(&ram_disk, "TEST7.TXT");
569     status +=  fx_file_create(&ram_disk, "TEST8.TXT");
570     status +=  fx_file_create(&ram_disk, "TEST9.TXT");
571     status +=  fx_file_create(&ram_disk, "TEST10.TXT");
572     status +=  fx_file_create(&ram_disk, "TEST11.TXT");
573     status +=  fx_file_create(&ram_disk, "TEST12.TXT");
574     status +=  fx_file_create(&ram_disk, "TEST13.TXT");
575     status +=  fx_file_create(&ram_disk, "TEST14.TXT");
576     status +=  fx_file_create(&ram_disk, "TEST15.TXT");
577     status +=  fx_file_create(&ram_disk, "TEST16.TXT");
578     status +=  fx_file_create(&ram_disk, "TEST17.TXT");
579     status +=  fx_file_create(&ram_disk, "TEST18.TXT");
580     status +=  fx_file_create(&ram_disk, "TEST19.TXT");
581     status +=  fx_file_create(&ram_disk, "TEST20.TXT");
582     status +=  fx_file_create(&ram_disk, "TEST21.TXT");
583     status +=  fx_file_create(&ram_disk, "TEST22.TXT");
584     status +=  fx_file_create(&ram_disk, "TEST23.TXT");
585     status +=  fx_file_create(&ram_disk, "TEST24.TXT");
586     status +=  fx_file_create(&ram_disk, "TEST25.TXT");
587     status +=  fx_file_create(&ram_disk, "TEST26.TXT");
588     status +=  fx_file_create(&ram_disk, "TEST27.TXT");
589     status +=  fx_file_create(&ram_disk, "TEST28.TXT");
590     status +=  fx_file_create(&ram_disk, "TEST29.TXT");
591     status +=  fx_file_create(&ram_disk, "TEST30.TXT");
592     status +=  fx_file_create(&ram_disk, "TEST31.TXT");
593 
594     /* Now open a set of files so we get all combinations through the open count check in fx_directory_information_get.c.  */
595     status +=  fx_file_open(&ram_disk, &file_0, "TEST.TXT", FX_OPEN_FOR_READ);
596     status +=  fx_file_open(&ram_disk, &file_1, "TEST1.TXT", FX_OPEN_FOR_WRITE);
597     status +=  fx_file_open(&ram_disk, &file_2, "TEST2.TXT", FX_OPEN_FOR_WRITE);
598     status +=  fx_file_open(&ram_disk, &file_3, "TEST9.TXT", FX_OPEN_FOR_WRITE);
599     status +=  fx_file_open(&ram_disk, &file_4, "TEST11.TXT", FX_OPEN_FOR_WRITE);
600     status +=  fx_file_open(&ram_disk, &file_5, "TEST19.TXT", FX_OPEN_FOR_WRITE);
601     status +=  fx_file_open(&ram_disk, &file_6, "TEST30.TXT", FX_OPEN_FOR_WRITE);
602 
603     /* Now get information on the first file... requesting on the "second" parameter.  */
604     status +=  fx_directory_information_get(&ram_disk, "TEST.TXT", FX_NULL, FX_NULL,
605                                                         FX_NULL, FX_NULL, FX_NULL,
606                                                         FX_NULL, FX_NULL, &second);
607 
608     /* Determine if there was an error.  */
609     if (status != FX_SUCCESS)
610     {
611 
612         printf("ERROR!\n");
613         test_control_return(40);
614     }
615 
616     /* Now get information on the first file... requesting on the "minute" parameter.  */
617     status +=  fx_directory_information_get(&ram_disk, "TEST.TXT", FX_NULL, FX_NULL,
618                                                         FX_NULL, FX_NULL, FX_NULL,
619                                                         FX_NULL, &minute, FX_NULL);
620 
621     /* Determine if there was an error.  */
622     if (status != FX_SUCCESS)
623     {
624 
625         printf("ERROR!\n");
626         test_control_return(41);
627     }
628 
629     /* Close the media.  */
630     status =  fx_media_close(&ram_disk);
631 
632     /* Determine if there was an error.  */
633     if (status != FX_SUCCESS)
634     {
635 
636         printf("ERROR!\n");
637         test_control_return(42);
638     }
639     else
640     {
641 
642         printf("SUCCESS!\n");
643         test_control_return(0);
644     }
645 }
646 
647