filex_file_attributes_read_set_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* This FileX test concentrates on the file attributes read/set operation. */
  2. #ifndef FX_STANDALONE_ENABLE
  3. #include "tx_api.h"
  4. #endif
  5. #include "fx_api.h"
  6. #include <stdio.h>
  7. #include "fx_ram_driver_test.h"
  8. #define DEMO_STACK_SIZE 4096
  9. #define CACHE_SIZE 128*128
  10. /* Define the ThreadX and FileX object control blocks... */
  11. #ifndef FX_STANDALONE_ENABLE
  12. static TX_THREAD ftest_0;
  13. #endif
  14. static FX_MEDIA ram_disk;
  15. static FX_FILE my_file;
  16. static FX_FILE my_file1;
  17. static FX_FILE my_file2;
  18. static FX_FILE my_file3;
  19. static FX_FILE my_file4;
  20. static FX_FILE my_file5;
  21. static FX_FILE my_file6;
  22. static FX_FILE my_file7;
  23. static FX_FILE my_file8;
  24. static FX_FILE my_file9;
  25. /* Define the counters used in the test application... */
  26. #ifndef FX_STANDALONE_ENABLE
  27. static UCHAR *ram_disk_memory;
  28. static UCHAR *cache_buffer;
  29. #else
  30. static UCHAR cache_buffer[CACHE_SIZE];
  31. #endif
  32. /* Define thread prototypes. */
  33. void filex_file_attributes_read_set_application_define(void *first_unused_memory);
  34. static void ftest_0_entry(ULONG thread_input);
  35. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  36. void test_control_return(UINT status);
  37. /* Define what the initial system looks like. */
  38. #ifdef CTEST
  39. void test_application_define(void *first_unused_memory)
  40. #else
  41. void filex_file_attributes_read_set_application_define(void *first_unused_memory)
  42. #endif
  43. {
  44. #ifndef FX_STANDALONE_ENABLE
  45. UCHAR *pointer;
  46. /* Setup the working pointer. */
  47. pointer = (UCHAR *) first_unused_memory;
  48. /* Create the main thread. */
  49. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  50. pointer, DEMO_STACK_SIZE,
  51. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  52. pointer = pointer + DEMO_STACK_SIZE;
  53. /* Setup memory for the RAM disk and the sector cache. */
  54. cache_buffer = pointer;
  55. pointer = pointer + CACHE_SIZE;
  56. ram_disk_memory = pointer;
  57. #endif
  58. /* Initialize the FileX system. */
  59. fx_system_initialize();
  60. #ifdef FX_STANDALONE_ENABLE
  61. ftest_0_entry(0);
  62. #endif
  63. }
  64. /* Define the test threads. */
  65. static void ftest_0_entry(ULONG thread_input)
  66. {
  67. UINT status;
  68. UINT attributes;
  69. FX_FILE open_file;
  70. FX_PARAMETER_NOT_USED(thread_input);
  71. /* Print out some test information banners. */
  72. printf("FileX Test: File attributes read/set test..........................");
  73. /* Format the media. This needs to be done before opening it! */
  74. status = fx_media_format(&ram_disk,
  75. _fx_ram_driver, // Driver entry
  76. ram_disk_memory, // RAM disk memory pointer
  77. cache_buffer, // Media buffer pointer
  78. CACHE_SIZE, // Media buffer size
  79. "MY_RAM_DISK", // Volume Name
  80. 1, // Number of FATs
  81. 32, // Directory Entries
  82. 0, // Hidden sectors
  83. 512, // Total sectors
  84. 128, // Sector size
  85. 1, // Sectors per cluster
  86. 1, // Heads
  87. 1); // Sectors per track
  88. /* Determine if the format had an error. */
  89. if (status)
  90. {
  91. printf("ERROR!\n");
  92. test_control_return(1);
  93. }
  94. /* Attempt to read file attributes before the media has been opened */
  95. status = fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
  96. if (status != FX_MEDIA_NOT_OPEN)
  97. {
  98. printf("ERROR!\n");
  99. test_control_return(2);
  100. }
  101. /* Attempt to set file attributes before the media has been opened */
  102. status = fx_file_attributes_set(&ram_disk, "TEST.TXT", 0);
  103. if (status != FX_MEDIA_NOT_OPEN)
  104. {
  105. printf("ERROR!\n");
  106. test_control_return(3);
  107. }
  108. /* Open the ram_disk. */
  109. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  110. /* Check the status. */
  111. if (status != FX_SUCCESS)
  112. {
  113. /* Error, return error code. */
  114. printf("ERROR!\n");
  115. test_control_return(4);
  116. }
  117. /* Attempt to set file attributes while the media is write protected */
  118. ram_disk.fx_media_driver_write_protect = FX_TRUE;
  119. status = fx_file_attributes_set(&ram_disk, "TEST.TXT", FX_READ_ONLY);
  120. if (status != FX_WRITE_PROTECT)
  121. {
  122. printf("ERROR!\n");
  123. test_control_return(5);
  124. }
  125. ram_disk.fx_media_driver_write_protect = FX_FALSE;
  126. /* attempt to read file attributes from a file that does not exist */
  127. status = fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
  128. if (status == FX_SUCCESS)
  129. {
  130. printf("ERROR!\n");
  131. test_control_return(6);
  132. }
  133. /* Attempt to set file attributes before the media has been opened */
  134. status = fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes);
  135. if (status == FX_SUCCESS)
  136. {
  137. printf("ERROR!\n");
  138. test_control_return(7);
  139. }
  140. /* attempt to read file attributes from something that is not a file */
  141. status = fx_directory_create(&ram_disk, "NOT_A_FILE");
  142. status += fx_file_attributes_read(&ram_disk, "NOT_A_FILE", &attributes);
  143. if (status != FX_NOT_A_FILE)
  144. {
  145. printf("ERROR!\n");
  146. test_control_return(8);
  147. }
  148. /* Attempt to set file attributes before the media has been opened */
  149. status = fx_file_attributes_set(&ram_disk, "NOT_A_FILE", FX_READ_ONLY);
  150. if (status != FX_NOT_A_FILE)
  151. {
  152. printf("ERROR!\n");
  153. test_control_return(9);
  154. }
  155. /* Attempt to set file attributes with invlaid file name. */
  156. status = fx_file_attributes_set(&ram_disk, "NOT_EXIST", FX_READ_ONLY);
  157. if (status != FX_NOT_FOUND)
  158. {
  159. printf("ERROR!\n");
  160. test_control_return(9);
  161. }
  162. /* Create a file called TEST.TXT in the root directory. */
  163. status = fx_file_create(&ram_disk, "TEST.TXT");
  164. /* Check the create status. */
  165. if (status != FX_SUCCESS)
  166. {
  167. printf("ERROR!\n");
  168. test_control_return(10);
  169. }
  170. #ifndef FX_DISABLE_ERROR_CHECKING
  171. /* send null pointer to generate an error */
  172. status = fx_file_attributes_set(FX_NULL, "filename", FX_READ_ONLY);
  173. if (status != FX_PTR_ERROR)
  174. {
  175. printf("ERROR!\n");
  176. test_control_return(11);
  177. }
  178. /* send null pointer to generate an error */
  179. status = fx_file_attributes_set(&ram_disk, "filename", 0x40);
  180. if (status != FX_INVALID_ATTR)
  181. {
  182. printf("ERROR!\n");
  183. test_control_return(12);
  184. }
  185. /* send null pointer to generate an error */
  186. status = fx_file_attributes_read(FX_NULL, "filename", FX_NULL);
  187. if (status != FX_PTR_ERROR)
  188. {
  189. printf("ERROR!\n");
  190. test_control_return(13);
  191. }
  192. #endif
  193. /* Invalidate the media cache. */
  194. fx_media_cache_invalidate(&ram_disk);
  195. /* Pickup the attributes of the file. */
  196. status = fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
  197. /* Check the attributes read status. */
  198. if ((status != FX_SUCCESS) || (attributes != FX_ARCHIVE))
  199. {
  200. printf("ERROR!\n");
  201. test_control_return(14);
  202. }
  203. /* Invalidate the media cache. */
  204. fx_media_cache_invalidate(&ram_disk);
  205. /* Now write the attributes out for the file. */
  206. status = fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes | FX_READ_ONLY);
  207. /* Check the attributes set status. */
  208. if (status != FX_SUCCESS)
  209. {
  210. printf("ERROR!\n");
  211. test_control_return(15);
  212. }
  213. /* Write the attributes out to the file while another file is opened to get better code coverage */
  214. status = fx_file_create(&ram_disk, "open_file.txt");
  215. status += fx_file_open(&ram_disk, &open_file, "open_file.txt", FX_OPEN_FOR_WRITE);
  216. status += fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes | FX_READ_ONLY);
  217. if (status != FX_SUCCESS)
  218. {
  219. printf("ERROR!\n");
  220. test_control_return(16);
  221. }
  222. /* Invalidate the media cache. */
  223. fx_media_cache_invalidate(&ram_disk);
  224. /* Pickup the attributes of the file again. */
  225. status = fx_file_attributes_read(&ram_disk, "TEST.TXT", &attributes);
  226. /* Check the attributes read status. */
  227. if ((status != FX_SUCCESS) || (attributes != (FX_ARCHIVE | FX_READ_ONLY)))
  228. {
  229. printf("ERROR!\n");
  230. test_control_return(17);
  231. }
  232. /* Open the test file... this should fail since we set the attributes to read only! */
  233. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  234. /* Check the file open status. */
  235. if (status != FX_ACCESS_ERROR)
  236. {
  237. printf("ERROR!\n");
  238. test_control_return(18);
  239. }
  240. /* Attempt to set file attributes for a file that is open */
  241. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
  242. status += fx_file_attributes_set(&ram_disk, "TEST.TXT", attributes);
  243. if (status != FX_ACCESS_ERROR)
  244. {
  245. printf("ERROR!\n");
  246. test_control_return(19);
  247. }
  248. /* Close the media. */
  249. status = fx_media_close(&ram_disk);
  250. /* Determine if the test was successful. */
  251. if (status != FX_SUCCESS)
  252. {
  253. printf("ERROR!\n");
  254. test_control_return(20);
  255. }
  256. /* Now test the attributes set with multiple files open that have directory entries that reside on
  257. different logical sectors. */
  258. /* Format the media. This needs to be done before opening it! */
  259. status = fx_media_format(&ram_disk,
  260. _fx_ram_driver, // Driver entry
  261. ram_disk_memory, // RAM disk memory pointer
  262. cache_buffer, // Media buffer pointer
  263. CACHE_SIZE, // Media buffer size
  264. "MY_RAM_DISK", // Volume Name
  265. 1, // Number of FATs
  266. 32, // Directory Entries
  267. 0, // Hidden sectors
  268. 512, // Total sectors
  269. 128, // Sector size
  270. 1, // Sectors per cluster
  271. 1, // Heads
  272. 1); // Sectors per track
  273. /* Determine if the format had an error. */
  274. if (status)
  275. {
  276. printf("ERROR!\n");
  277. test_control_return(21);
  278. }
  279. /* Open the ram_disk. */
  280. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  281. /* Check the status. */
  282. if (status != FX_SUCCESS)
  283. {
  284. /* Error, return error code. */
  285. printf("ERROR!\n");
  286. test_control_return(22);
  287. }
  288. /* Create a set of test files. */
  289. status = fx_file_create(&ram_disk, "TEST.TXT");
  290. status += fx_file_create(&ram_disk, "TEST1.TXT");
  291. status += fx_file_create(&ram_disk, "TEST2.TXT");
  292. status += fx_file_create(&ram_disk, "TEST3.TXT");
  293. status += fx_file_create(&ram_disk, "TEST4.TXT");
  294. status += fx_file_create(&ram_disk, "TEST5.TXT");
  295. status += fx_file_create(&ram_disk, "TEST6.TXT");
  296. status += fx_file_create(&ram_disk, "TEST7.TXT");
  297. status += fx_file_create(&ram_disk, "TEST8.TXT");
  298. status += fx_file_create(&ram_disk, "TEST9.TXT");
  299. status += fx_file_create(&ram_disk, "TEST10.TXT");
  300. /* Now open all the files except the TEST.TXT. */
  301. status += fx_file_open(&ram_disk, &my_file, "TEST1.TXT", FX_OPEN_FOR_WRITE);
  302. status += fx_file_open(&ram_disk, &my_file1, "TEST2.TXT", FX_OPEN_FOR_WRITE);
  303. status += fx_file_open(&ram_disk, &my_file2, "TEST3.TXT", FX_OPEN_FOR_WRITE);
  304. status += fx_file_open(&ram_disk, &my_file3, "TEST4.TXT", FX_OPEN_FOR_WRITE);
  305. status += fx_file_open(&ram_disk, &my_file4, "TEST5.TXT", FX_OPEN_FOR_WRITE);
  306. status += fx_file_open(&ram_disk, &my_file5, "TEST6.TXT", FX_OPEN_FOR_WRITE);
  307. status += fx_file_open(&ram_disk, &my_file6, "TEST7.TXT", FX_OPEN_FOR_WRITE);
  308. status += fx_file_open(&ram_disk, &my_file7, "TEST8.TXT", FX_OPEN_FOR_WRITE);
  309. status += fx_file_open(&ram_disk, &my_file8, "TEST9.TXT", FX_OPEN_FOR_WRITE);
  310. status += fx_file_open(&ram_disk, &my_file9, "TEST10.TXT", FX_OPEN_FOR_WRITE);
  311. /* Now set the attributes for TEST.TXT. */
  312. status += fx_file_attributes_set(&ram_disk, "TEST.TXT", FX_READ_ONLY);
  313. /* Check the status. */
  314. if (status != FX_SUCCESS)
  315. {
  316. /* Error, return error code. */
  317. printf("ERROR!\n");
  318. test_control_return(23);
  319. }
  320. /* Close the media. */
  321. status = fx_media_close(&ram_disk);
  322. /* Determine if the test was successful. */
  323. if (status != FX_SUCCESS)
  324. {
  325. printf("ERROR!\n");
  326. test_control_return(24);
  327. }
  328. else
  329. {
  330. printf("SUCCESS!\n");
  331. test_control_return(0);
  332. }
  333. }