1 /*This test is determined to cover lines 315 327 352 390 in fx_unicode_directory_entry_change.c. */
2 
3 #ifndef FX_STANDALONE_ENABLE
4 #include   "tx_api.h"
5 #endif
6 #include   "fx_api.h"
7 #include   "fx_utility.h"
8 #include   "fx_ram_driver_test.h"
9 #include    <stdio.h>
10 
11 #define     DEMO_STACK_SIZE         8192
12 /* Set the cache size as the size of one sector causing frequently IO operation. */
13 #define     CACHE_SIZE              128
14 
15 /* Define the ThreadX and FileX object control blocks...  */
16 
17 #ifndef FX_STANDALONE_ENABLE
18 static TX_THREAD                ftest_0;
19 #endif
20 static FX_MEDIA                 ram_disk;
21 
22 /* Define the counters used in the test application...  */
23 
24 #ifndef FX_STANDALONE_ENABLE
25 static UCHAR                    *ram_disk_memory;
26 static UCHAR                    *cache_buffer;
27 #else
28 static UCHAR                     cache_buffer[256];
29 #endif
30 
31 /* Define thread prototypes.  */
32 
33 void    filex_unicode_directory_entry_change_test_application_define(void *first_unused_memory);
34 static void    ftest_0_entry(ULONG thread_input);
35 
36 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
37 void  test_control_return(UINT status);
38 
39 extern UINT  _fx_unicode_directory_entry_change(FX_MEDIA *media_ptr, FX_DIR_ENTRY *entry_ptr, UCHAR *unicode_name, ULONG unicode_name_length);
40 
41 static  UINT driver_called_counter = 0;
42 /* Create a terrible driver. */
_fx_terrible_driver(FX_MEDIA * media_ptr)43 static void _fx_terrible_driver(FX_MEDIA *media_ptr)
44 {
45     driver_called_counter++;
46     // printf("\n_fx_terrible_driver has been called %d times.", driver_called_counter);
47     if (driver_called_counter == 3)
48     {
49         media_ptr->fx_media_driver_status=FX_IO_ERROR;
50         return;
51     }
52     (*_fx_ram_driver)(media_ptr);
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_entry_change_test_application_define(void *first_unused_memory)
61 #endif
62 {
63 #ifndef FX_STANDALONE_ENABLE
64     UCHAR    *pointer;
65 
66     /* Setup the working pointer.  */
67     pointer = (UCHAR *)first_unused_memory;
68 
69     /* Create the main thread.  */
70     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
71         pointer, DEMO_STACK_SIZE,
72         4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
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 
85 #ifdef FX_STANDALONE_ENABLE
86     ftest_0_entry(0);
87 #endif
88 }
89 
90 static UCHAR                    long_unicode_name1[] = { 1, 0, 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, 0, 0, 0, 0, 0,};
91 
92 /* Define the test threads.  */
93 
ftest_0_entry(ULONG thread_input)94 static void    ftest_0_entry(ULONG thread_input)
95 {
96 FX_DIR_ENTRY entry;  /* set a point to the entry  */
97 FX_DIR_ENTRY  *entry_ptr=&entry;
98 UINT   status, length;
99 
100     FX_PARAMETER_NOT_USED(thread_input);
101 
102     /* Print out some test information banners.  */
103     printf("FileX Test:   Unicode directory entry change test....................");
104 
105     /* Format the media with FAT32.  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_large,  // 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                              70000,                  // Total sectors
116                              128,                    // Sector size
117                              1,                      // Sectors per cluster
118                              1,                      // Heads
119                              1);                     // Sectors per track
120 
121     return_value_if_fail(status == FX_SUCCESS, 1);
122 
123     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
124     return_value_if_fail(status == FX_SUCCESS, 2);
125 
126     /* initiallize the entry  */
127     entry_ptr->fx_dir_entry_name = "Z12345";
128     entry_ptr->fx_dir_entry_attributes = 16;
129     entry_ptr->fx_dir_entry_byte_offset = 0;
130     entry_ptr->fx_dir_entry_cluster = 3;
131     entry_ptr->fx_dir_entry_created_date = 18977;
132     entry_ptr->fx_dir_entry_created_time = 0;
133     entry_ptr->fx_dir_entry_created_time_ms = 0;
134     entry_ptr->fx_dir_entry_date = 18977;
135     entry_ptr->fx_dir_entry_file_size = 0;
136     entry_ptr->fx_dir_entry_last_accessed_date = 18977;
137     entry_ptr->fx_dir_entry_last_search_byte_offset = 0;
138     entry_ptr->fx_dir_entry_last_search_cluster = 0;
139     entry_ptr->fx_dir_entry_last_search_log_sector = 0;
140     entry_ptr->fx_dir_entry_last_search_relative_cluster = 0;
141     entry_ptr->fx_dir_entry_log_sector = 2189;
142     entry_ptr->fx_dir_entry_long_name_present = 1;
143     entry_ptr->fx_dir_entry_long_name_shorted = 0;
144     entry_ptr->fx_dir_entry_next_log_sector = 0;
145     entry_ptr->fx_dir_entry_reserved = 0;
146     entry_ptr->fx_dir_entry_time = 0;
147 
148     /*This case is determined to cover lines 390 in fx_unicode_directory_entry_change.c. */
149     length = fx_unicode_length_get(long_unicode_name1);
150     status=_fx_unicode_directory_entry_change(&ram_disk, entry_ptr, long_unicode_name1, length);
151     return_value_if_fail(status == FX_FILE_CORRUPT, 3);
152 
153     /*This case is determined to cover lines 327 in fx_unicode_directory_entry_change.c. */
154     *(ram_disk.fx_media_driver_buffer) = 90;
155     status = _fx_unicode_directory_entry_change(&ram_disk, entry_ptr, long_unicode_name1, length);
156     return_value_if_fail(status == FX_FILE_CORRUPT, 4);
157 
158     /*This case is determined to cover lines 352 in fx_unicode_directory_entry_change.c. */
159     entry_ptr->fx_dir_entry_byte_offset = CACHE_SIZE + 1;
160     *(ram_disk.fx_media_driver_buffer+ CACHE_SIZE+1) = 90;
161 
162     /*set the the logical sector that is in root directory */
163     entry_ptr->fx_dir_entry_log_sector =ram_disk.fx_media_data_sector_start-1;
164     status = _fx_unicode_directory_entry_change(&ram_disk, entry_ptr, long_unicode_name1, length);
165     return_value_if_fail(status == FX_FILE_CORRUPT, 5);
166 
167     /*This case is determined to cover lines 315 in fx_unicode_directory_entry_change.c. */
168     entry_ptr->fx_dir_entry_byte_offset = CACHE_SIZE + 1;
169     *(ram_disk.fx_media_driver_buffer + CACHE_SIZE + 1) = 90;
170     entry_ptr->fx_dir_entry_log_sector = ram_disk.fx_media_data_sector_start ;
171     ram_disk.fx_media_fat_cache[8].fx_fat_cache_entry_cluster = 20;
172 
173     /* Register our terrible dirver to make IO ERROR at a particular time. */
174     ram_disk.fx_media_driver_entry = _fx_terrible_driver;
175     status = _fx_unicode_directory_entry_change(&ram_disk, entry_ptr, long_unicode_name1, length);
176     /* Unregister our terrible driver. */
177     ram_disk.fx_media_driver_entry = _fx_ram_driver;
178     return_value_if_fail(status == FX_IO_ERROR, 6);
179 
180     /* initiallize the entry  */
181     entry_ptr->fx_dir_entry_name = "Z12345";
182     entry_ptr->fx_dir_entry_attributes = 16;
183     entry_ptr->fx_dir_entry_byte_offset = 0;
184     entry_ptr->fx_dir_entry_cluster = 3;
185     entry_ptr->fx_dir_entry_created_date = 18977;
186     entry_ptr->fx_dir_entry_created_time = 0;
187     entry_ptr->fx_dir_entry_created_time_ms = 0;
188     entry_ptr->fx_dir_entry_date = 18977;
189     entry_ptr->fx_dir_entry_file_size = 0;
190     entry_ptr->fx_dir_entry_last_accessed_date = 18977;
191     entry_ptr->fx_dir_entry_last_search_byte_offset = 0;
192     entry_ptr->fx_dir_entry_last_search_cluster = 0;
193     entry_ptr->fx_dir_entry_last_search_log_sector = 0;
194     entry_ptr->fx_dir_entry_last_search_relative_cluster = 0;
195     entry_ptr->fx_dir_entry_log_sector = 2189;
196     entry_ptr->fx_dir_entry_long_name_present = 1;
197     entry_ptr->fx_dir_entry_long_name_shorted = 0;
198     entry_ptr->fx_dir_entry_next_log_sector = 0;
199     entry_ptr->fx_dir_entry_reserved = 0;
200     entry_ptr->fx_dir_entry_time = 0;
201 
202     /* Covered the last branch at Line 323 in fx_unicode_directory_entry_change.c. */
203     *(ram_disk.fx_media_driver_buffer) = 90;
204     /* Register a wrong information in fat cache. */
205     ram_disk.fx_media_fat_cache[8].fx_fat_cache_entry_cluster = 2;
206     ram_disk.fx_media_fat_cache[8].fx_fat_cache_entry_value = 0;
207     status = _fx_unicode_directory_entry_change(&ram_disk, entry_ptr, long_unicode_name1, length);
208 
209     printf("SUCCESS!\n");
210     test_control_return(0);
211 }
212