filex_unicode_directory_entry_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* This test is determined to cover lines in fx_unicode_directory_entry_read.c. */
  2. /* We need a dir_entry whose first byte, ordinal number byte, is big enoutgh to exceed the the */
  3. /* limit of FX_MAX_LONG_NAME_LEN, so we created a disk with a corrupt dir_entry. */
  4. #ifndef FX_STANDALONE_ENABLE
  5. #include "tx_api.h"
  6. #include "tx_thread.h"
  7. #endif
  8. #include "fx_api.h"
  9. #include "fx_ram_driver_test.h"
  10. #include "fx_utility.h"
  11. #include "fx_unicode.h"
  12. #include <stdio.h>
  13. #define DEMO_STACK_SIZE 8192
  14. #define CACHE_SIZE 16*128
  15. /* Define the ThreadX and FileX object control blocks... */
  16. #ifndef FX_STANDALONE_ENABLE
  17. static TX_THREAD ftest_0;
  18. #endif
  19. static FX_MEDIA ram_disk;
  20. /* Define the counters used in the test application... */
  21. #ifndef FX_STANDALONE_ENABLE
  22. static UCHAR *ram_disk_memory;
  23. static UCHAR *cache_buffer;
  24. #else
  25. static UCHAR cache_buffer[CACHE_SIZE];
  26. #endif
  27. /* Define thread prototypes. */
  28. void filex_unicode_directory_entry_test_application_define(void *first_unused_memory);
  29. static void ftest_0_entry(ULONG thread_input);
  30. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  31. void test_control_return(UINT status);
  32. /* Create a terrible driver. */
  33. static UINT driver_called_counter = 0;
  34. static void _fx_terrible_driver(FX_MEDIA *media_ptr)
  35. {
  36. driver_called_counter++;
  37. // printf("\n_fx_terrible_driver has been called %d times.", driver_called_counter);
  38. if (
  39. (driver_called_counter <= 3)
  40. )
  41. {
  42. media_ptr -> fx_media_driver_status = FX_IO_ERROR;
  43. return;
  44. }
  45. (* _fx_ram_driver)(media_ptr);
  46. }
  47. /* Define what the initial system looks like. */
  48. #ifdef CTEST
  49. void test_application_define(void *first_unused_memory)
  50. #else
  51. void filex_unicode_directory_entry_test_application_define(void *first_unused_memory)
  52. #endif
  53. {
  54. #ifndef FX_STANDALONE_ENABLE
  55. UCHAR *pointer;
  56. /* Setup the working pointer. */
  57. pointer = (UCHAR *) first_unused_memory;
  58. /* Create the main thread. */
  59. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  60. pointer, DEMO_STACK_SIZE,
  61. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  62. pointer = pointer + DEMO_STACK_SIZE;
  63. /* Setup memory for the RAM disk and the sector cache. */
  64. cache_buffer = pointer;
  65. pointer = pointer + CACHE_SIZE;
  66. ram_disk_memory = pointer;
  67. #endif
  68. /* Initialize the FileX system. */
  69. fx_system_initialize();
  70. #ifdef FX_STANDALONE_ENABLE
  71. ftest_0_entry(0);
  72. #endif
  73. }
  74. /* Define the test threads. */
  75. static void ftest_0_entry(ULONG thread_input)
  76. {
  77. UINT status;
  78. UCHAR destination_name[100] = {0};
  79. UCHAR buffer[512];
  80. UCHAR long_unicode_name[] = {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};
  81. ULONG length = 14;
  82. ULONG dir_num1, dir_num2;
  83. FX_LOCAL_PATH local_path;
  84. FX_DIR_ENTRY source_dir, destination_dir;
  85. FX_PARAMETER_NOT_USED(thread_input);
  86. /* Print out some test information banners. */
  87. printf("FileX Test: Unicode directory entry test...........................");
  88. /* Format the media. This needs to be done before opening it! */
  89. status = fx_media_format(&ram_disk,
  90. _fx_ram_driver, // Driver entry
  91. ram_disk_memory, // RAM disk memory pointer
  92. cache_buffer, // Media buffer pointer
  93. CACHE_SIZE, // Media buffer size
  94. "MY_RAM_DISK", // Volume Name
  95. 1, // Number of FATs
  96. 32, // Directory Entries
  97. 0, // Hidden sectors
  98. 512, // Total sectors
  99. 128, // Sector size
  100. 1, // Sectors per cluster
  101. 1, // Heads
  102. 1); // Sectors per track
  103. /* Determine if the format had an error. */
  104. return_value_if_fail( status == FX_SUCCESS, 2);
  105. /* Open the ram_disk. */
  106. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  107. return_value_if_fail( status == FX_SUCCESS, 3);
  108. /* For coverage test in fx_directory_free_search.c.
  109. 387 : 4380 : status = _fx_directory_entry_write(media_ptr, entry_ptr);
  110. 388 [ - + ]: 4380 : if(status != FX_SUCCESS)
  111. 389 : : {
  112. 390 : 0 : return(status);
  113. 391 : : }
  114. */
  115. _fx_directory_entry_write_error_request = 1;
  116. /* call fx_unicode_directory_create -call-> _fx_unicode_directory_search -call-> _fx_unicode_directory_entry_read */
  117. status = fx_unicode_directory_create(&ram_disk, long_unicode_name, length, (CHAR *) destination_name);
  118. return_value_if_fail( status == FX_IO_ERROR, 3);
  119. /* Close the media to flush buffer. Now we have a disk with a corrupt dir_entry. */
  120. status = fx_media_close(&ram_disk);
  121. return_value_if_fail( status == FX_SUCCESS, 4);
  122. /* Format the media. This needs to be done before opening it! */
  123. status = fx_media_format(&ram_disk,
  124. _fx_ram_driver, // Driver entry
  125. ram_disk_memory, // RAM disk memory pointer
  126. cache_buffer, // Media buffer pointer
  127. CACHE_SIZE, // Media buffer size
  128. "MY_RAM_DISK", // Volume Name
  129. 1, // Number of FATs
  130. 32, // Directory Entries
  131. 0, // Hidden sectors
  132. 512, // Total sectors
  133. 128, // Sector size
  134. 1, // Sectors per cluster
  135. 1, // Heads
  136. 1); // Sectors per track
  137. /* Determine if the format had an error. */
  138. return_value_if_fail( status == FX_SUCCESS, 2);
  139. /* Open the ram_disk. */
  140. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  141. return_value_if_fail( status == FX_SUCCESS, 3);
  142. /* Disable write protect */
  143. ram_disk.fx_media_driver_write_protect = FX_FALSE;
  144. /* call fx_unicode_directory_create -call-> _fx_unicode_directory_search -call-> _fx_unicode_directory_entry_read */
  145. status = fx_unicode_directory_create(&ram_disk, long_unicode_name, length, (CHAR *) destination_name);
  146. return_value_if_fail( status == FX_SUCCESS, 3);
  147. /* After creating first directory on a new disk, the first dir_entry must be located at the head of the disk memory buffer. */
  148. /* Modify the first byte, ordinal number byte, to make a mistake. */
  149. *ram_disk.fx_media_memory_buffer |= 0x1f;
  150. /* Close the media to flush buffer. Now we have a disk with a corrupt dir_entry. */
  151. status = fx_media_close(&ram_disk);
  152. return_value_if_fail( status == FX_SUCCESS, 4);
  153. /* Open the media and try to create the directory again to see what happened. */
  154. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  155. return_value_if_fail( status == FX_SUCCESS, 5);
  156. #if !defined(FX_ENABLE_FAULT_TOLERANT) && !defined(FX_DISABLE_CACHE)
  157. /* Try to recreate the same directory to see what happened. */
  158. status = fx_unicode_directory_create(&ram_disk, long_unicode_name, length, (CHAR *) destination_name);
  159. return_value_if_fail( status == FX_SUCCESS, 6);
  160. #endif
  161. /* At this point the three dir_entry belongs to original directory is invalid(new dir_entry's byteoffset in memory buffer is 96 bytes). */
  162. /* Close the media to flush buffer. Now we have a disk with a corrupt dir_entry. */
  163. status = fx_media_close(&ram_disk);
  164. return_value_if_fail( status == FX_SUCCESS, 7);
  165. /* This time, we'll modify the second dir_entry's first byte, ordinal byte, which will make the filesystem keep reading entries until FX_FILE_CORRUPT. */
  166. /* Format the media. This needs to be done before opening it! */
  167. status = fx_media_format(&ram_disk,
  168. _fx_ram_driver, // Driver entry
  169. ram_disk_memory, // RAM disk memory pointer
  170. cache_buffer, // Media buffer pointer
  171. CACHE_SIZE, // Media buffer size
  172. "MY_RAM_DISK", // Volume Name
  173. 1, // Number of FATs
  174. 32, // Directory Entries
  175. 0, // Hidden sectors
  176. 512, // Total sectors
  177. 128, // Sector size
  178. 1, // Sectors per cluster
  179. 1, // Heads
  180. 1); // Sectors per track
  181. /* Open the ram_disk. */
  182. status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  183. return_value_if_fail( status == FX_SUCCESS, 8);
  184. /* Disable write protect */
  185. ram_disk.fx_media_driver_write_protect = FX_FALSE;
  186. /* call fx_unicode_directory_create -call-> _fx_unicode_directory_search -call-> _fx_unicode_directory_entry_read */
  187. status = fx_unicode_directory_create(&ram_disk, long_unicode_name, length, (CHAR *) destination_name);
  188. return_value_if_fail( status == FX_SUCCESS, 9);
  189. /* Modify second dir_entry this time to make a mistake. */
  190. ram_disk.fx_media_memory_buffer[32] |= 0x1f;
  191. /* Close the media to flush buffer. Now we have a disk with a corrupt dir_entry. */
  192. status = fx_media_close(&ram_disk);
  193. return_value_if_fail( status == FX_SUCCESS, 10);
  194. /* Open the media and try to create the directory again to see what happened. */
  195. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  196. return_value_if_fail( status == FX_SUCCESS, 11);
  197. #if !defined(FX_ENABLE_FAULT_TOLERANT) && !defined(FX_DISABLE_CACHE)
  198. /* Try to recreate the same directory to see what happened. */
  199. status = fx_unicode_directory_create(&ram_disk, long_unicode_name, length, (CHAR *) destination_name);
  200. /* Since there are no appropriate long name dir_entry behind, the filesystem will read data continously untill logic sector overflow. */
  201. return_value_if_fail( status == FX_FILE_CORRUPT, 12);
  202. #endif
  203. /* Add terrible driver to make IO mistake. */
  204. status = fx_media_close(&ram_disk);
  205. return_value_if_fail( status == FX_SUCCESS, 13);
  206. /* Format the media. This needs to be done before opening it! */
  207. status = fx_media_format(&ram_disk,
  208. _fx_ram_driver, // Driver entry
  209. ram_disk_memory, // RAM disk memory pointer
  210. cache_buffer, // Media buffer pointer
  211. CACHE_SIZE, // Media buffer size
  212. "MY_RAM_DISK", // Volume Name
  213. 1, // Number of FATs
  214. 32, // Directory Entries
  215. 0, // Hidden sectors
  216. 512, // Total sectors
  217. 128, // Sector size
  218. 1, // Sectors per cluster
  219. 1, // Heads
  220. 1); // Sectors per track
  221. return_value_if_fail( status == FX_SUCCESS, 14);
  222. /* Open the ram_disk. */
  223. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  224. return_value_if_fail( status == FX_SUCCESS, 15);
  225. /* Register our terrible driver. */
  226. ram_disk.fx_media_driver_entry = _fx_terrible_driver;
  227. status = fx_unicode_directory_create(&ram_disk, long_unicode_name, length, (CHAR *) destination_name);
  228. return_value_if_fail( status == FX_IO_ERROR, 16);
  229. /* Unregister our terrible driver. */
  230. ram_disk.fx_media_driver_entry = _fx_ram_driver;
  231. status = fx_media_abort( &ram_disk);
  232. return_value_if_fail( status == FX_SUCCESS, 17);
  233. /* Format the media. This needs to be done before opening it! */
  234. status = fx_media_format(&ram_disk,
  235. _fx_ram_driver, // Driver entry
  236. ram_disk_memory, // RAM disk memory pointer
  237. cache_buffer, // Media buffer pointer
  238. 128, // Media buffer size
  239. "MY_RAM_DISK", // Volume Name
  240. 1, // Number of FATs
  241. 32, // Directory Entries
  242. 0, // Hidden sectors
  243. 512, // Total sectors
  244. 128, // Sector size
  245. 1, // Sectors per cluster
  246. 1, // Heads
  247. 1); // Sectors per track
  248. /* Open the ram_disk. */
  249. status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  250. return_value_if_fail( status == FX_SUCCESS, 18);
  251. /* Set local path at a sub_directory. */
  252. length = fx_unicode_length_get( long_unicode_name);
  253. status = fx_unicode_directory_create( &ram_disk, long_unicode_name, length, (CHAR *)destination_name);
  254. #ifndef FX_STANDALONE_ENABLE
  255. status += fx_directory_local_path_set(&ram_disk, &local_path, (CHAR *)destination_name);
  256. #else
  257. status += fx_directory_default_set(&ram_disk, (CHAR *)destination_name);
  258. #endif
  259. return_value_if_fail( status == FX_SUCCESS, 19);
  260. /* Call _fx_unicode_directory_entry_read with uncorrect initial information. */
  261. destination_dir.fx_dir_entry_name = (CHAR *)buffer;
  262. source_dir.fx_dir_entry_last_search_cluster = 1;
  263. source_dir.fx_dir_entry_last_search_relative_cluster = 1;
  264. dir_num1 = 0;
  265. status = _fx_unicode_directory_entry_read( &ram_disk, &source_dir, &dir_num1, &destination_dir, destination_name, &dir_num2);
  266. /* Call _fx_unicode_directory_entry_read with uncorrect initial information. */
  267. source_dir.fx_dir_entry_last_search_cluster = 1;
  268. source_dir.fx_dir_entry_last_search_relative_cluster = 0;
  269. source_dir.fx_dir_entry_last_search_log_sector = 1;
  270. source_dir.fx_dir_entry_log_sector = 0;
  271. status = _fx_unicode_directory_entry_read( &ram_disk, &source_dir, &dir_num1, &destination_dir, destination_name, &dir_num2);
  272. /* Call _fx_unicode_directory_entry_read with uncorrect initial information. */
  273. source_dir.fx_dir_entry_last_search_cluster = 1;
  274. source_dir.fx_dir_entry_last_search_relative_cluster = 0;
  275. source_dir.fx_dir_entry_last_search_byte_offset = 0x20;
  276. source_dir.fx_dir_entry_byte_offset = 0;
  277. source_dir.fx_dir_entry_last_search_log_sector = 0;
  278. source_dir.fx_dir_entry_log_sector = 0;
  279. status = _fx_unicode_directory_entry_read( &ram_disk, &source_dir, &dir_num1, &destination_dir, destination_name, &dir_num2);
  280. /* Attempt to cover the branch at Line 140 in fx_utility_FAT_map_flush.c. */
  281. for (UINT i = 0; i < 128; i++)
  282. ram_disk.fx_media_fat_secondary_update_map[i] = 0xff;
  283. _fx_utility_FAT_map_flush( &ram_disk);
  284. printf("SUCCESS!\n");
  285. test_control_return(0);
  286. }