storage_test.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "../minunit.h"
  2. #include <furi.h>
  3. #include <furi_hal_delay.h>
  4. #include <storage/storage.h>
  5. #define STORAGE_LOCKED_FILE "/ext/locked_file.test"
  6. #define STORAGE_LOCKED_DIR "/int"
  7. static void storage_file_open_lock_setup() {
  8. Storage* storage = furi_record_open("storage");
  9. File* file = storage_file_alloc(storage);
  10. storage_simply_remove(storage, STORAGE_LOCKED_FILE);
  11. mu_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_WRITE, FSOM_CREATE_NEW));
  12. mu_check(storage_file_write(file, "0123", 4) == 4);
  13. mu_check(storage_file_close(file));
  14. storage_file_free(file);
  15. furi_record_close("storage");
  16. }
  17. static void storage_file_open_lock_teardown() {
  18. Storage* storage = furi_record_open("storage");
  19. mu_check(storage_simply_remove(storage, STORAGE_LOCKED_FILE));
  20. furi_record_close("storage");
  21. }
  22. static int32_t storage_file_locker(void* ctx) {
  23. Storage* storage = furi_record_open("storage");
  24. osSemaphoreId_t semaphore = ctx;
  25. File* file = storage_file_alloc(storage);
  26. furi_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  27. osSemaphoreRelease(semaphore);
  28. furi_hal_delay_ms(1000);
  29. furi_check(storage_file_close(file));
  30. furi_record_close("storage");
  31. storage_file_free(file);
  32. return 0;
  33. }
  34. MU_TEST(storage_file_open_lock) {
  35. Storage* storage = furi_record_open("storage");
  36. bool result = false;
  37. osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL);
  38. File* file = storage_file_alloc(storage);
  39. // file_locker thread start
  40. FuriThread* locker_thread = furi_thread_alloc();
  41. furi_thread_set_name(locker_thread, "StorageFileLocker");
  42. furi_thread_set_stack_size(locker_thread, 2048);
  43. furi_thread_set_context(locker_thread, semaphore);
  44. furi_thread_set_callback(locker_thread, storage_file_locker);
  45. mu_check(furi_thread_start(locker_thread));
  46. // wait for file lock
  47. osSemaphoreAcquire(semaphore, osWaitForever);
  48. osSemaphoreDelete(semaphore);
  49. result = storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
  50. storage_file_close(file);
  51. // file_locker thread stop
  52. mu_check(furi_thread_join(locker_thread) == osOK);
  53. furi_thread_free(locker_thread);
  54. // clean data
  55. storage_file_free(file);
  56. furi_record_close("storage");
  57. mu_assert(result, "cannot open locked file");
  58. }
  59. MU_TEST(storage_file_open_close) {
  60. Storage* storage = furi_record_open("storage");
  61. File* file;
  62. file = storage_file_alloc(storage);
  63. mu_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  64. storage_file_close(file);
  65. storage_file_free(file);
  66. for(size_t i = 0; i < 10; i++) {
  67. file = storage_file_alloc(storage);
  68. mu_check(
  69. storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  70. storage_file_free(file);
  71. }
  72. furi_record_close("storage");
  73. }
  74. MU_TEST_SUITE(storage_file) {
  75. storage_file_open_lock_setup();
  76. MU_RUN_TEST(storage_file_open_close);
  77. MU_RUN_TEST(storage_file_open_lock);
  78. storage_file_open_lock_teardown();
  79. }
  80. MU_TEST(storage_dir_open_close) {
  81. Storage* storage = furi_record_open("storage");
  82. File* file;
  83. file = storage_file_alloc(storage);
  84. mu_check(storage_dir_open(file, STORAGE_LOCKED_DIR));
  85. storage_dir_close(file);
  86. storage_file_free(file);
  87. for(size_t i = 0; i < 10; i++) {
  88. file = storage_file_alloc(storage);
  89. mu_check(storage_dir_open(file, STORAGE_LOCKED_DIR));
  90. storage_file_free(file);
  91. }
  92. furi_record_close("storage");
  93. }
  94. static int32_t storage_dir_locker(void* ctx) {
  95. Storage* storage = furi_record_open("storage");
  96. osSemaphoreId_t semaphore = ctx;
  97. File* file = storage_file_alloc(storage);
  98. furi_check(storage_dir_open(file, STORAGE_LOCKED_DIR));
  99. osSemaphoreRelease(semaphore);
  100. furi_hal_delay_ms(1000);
  101. furi_check(storage_dir_close(file));
  102. furi_record_close("storage");
  103. storage_file_free(file);
  104. return 0;
  105. }
  106. MU_TEST(storage_dir_open_lock) {
  107. Storage* storage = furi_record_open("storage");
  108. bool result = false;
  109. osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL);
  110. File* file = storage_file_alloc(storage);
  111. // file_locker thread start
  112. FuriThread* locker_thread = furi_thread_alloc();
  113. furi_thread_set_name(locker_thread, "StorageDirLocker");
  114. furi_thread_set_stack_size(locker_thread, 2048);
  115. furi_thread_set_context(locker_thread, semaphore);
  116. furi_thread_set_callback(locker_thread, storage_dir_locker);
  117. mu_check(furi_thread_start(locker_thread));
  118. // wait for dir lock
  119. osSemaphoreAcquire(semaphore, osWaitForever);
  120. osSemaphoreDelete(semaphore);
  121. result = storage_dir_open(file, STORAGE_LOCKED_DIR);
  122. storage_dir_close(file);
  123. // file_locker thread stop
  124. mu_check(furi_thread_join(locker_thread) == osOK);
  125. furi_thread_free(locker_thread);
  126. // clean data
  127. storage_file_free(file);
  128. furi_record_close("storage");
  129. mu_assert(result, "cannot open locked dir");
  130. }
  131. MU_TEST_SUITE(storage_dir) {
  132. MU_RUN_TEST(storage_dir_open_close);
  133. MU_RUN_TEST(storage_dir_open_lock);
  134. }
  135. static const char* const storage_copy_test_paths[] = {
  136. "1",
  137. "11",
  138. "111",
  139. "1/2",
  140. "1/22",
  141. "1/222",
  142. "11/1",
  143. "111/2",
  144. "111/22",
  145. "111/22/33",
  146. };
  147. static const char* const storage_copy_test_files[] = {
  148. "file.test",
  149. "1/file.test",
  150. "111/22/33/file.test",
  151. };
  152. static bool write_file_13DA(Storage* storage, const char* path) {
  153. File* file = storage_file_alloc(storage);
  154. bool result = false;
  155. if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  156. result = storage_file_write(file, "13DA", 4) == 4;
  157. }
  158. storage_file_close(file);
  159. storage_file_free(file);
  160. return result;
  161. }
  162. static bool check_file_13DA(Storage* storage, const char* path) {
  163. File* file = storage_file_alloc(storage);
  164. bool result = false;
  165. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  166. char data[10] = {0};
  167. result = storage_file_read(file, data, 4) == 4;
  168. if(result) {
  169. result = memcmp(data, "13DA", 4) == 0;
  170. }
  171. }
  172. storage_file_close(file);
  173. storage_file_free(file);
  174. return result;
  175. }
  176. static void storage_dir_create(Storage* storage, const char* base) {
  177. string_t path;
  178. string_init(path);
  179. storage_common_mkdir(storage, base);
  180. for(size_t i = 0; i < COUNT_OF(storage_copy_test_paths); i++) {
  181. string_printf(path, "%s/%s", base, storage_copy_test_paths[i]);
  182. storage_common_mkdir(storage, string_get_cstr(path));
  183. }
  184. for(size_t i = 0; i < COUNT_OF(storage_copy_test_files); i++) {
  185. string_printf(path, "%s/%s", base, storage_copy_test_files[i]);
  186. write_file_13DA(storage, string_get_cstr(path));
  187. }
  188. string_clear(path);
  189. }
  190. static void storage_dir_remove(Storage* storage, const char* base) {
  191. storage_simply_remove_recursive(storage, base);
  192. }
  193. static bool storage_dir_rename_check(Storage* storage, const char* base) {
  194. bool result = false;
  195. string_t path;
  196. string_init(path);
  197. result = (storage_common_stat(storage, base, NULL) == FSE_OK);
  198. if(result) {
  199. for(size_t i = 0; i < COUNT_OF(storage_copy_test_paths); i++) {
  200. string_printf(path, "%s/%s", base, storage_copy_test_paths[i]);
  201. result = (storage_common_stat(storage, string_get_cstr(path), NULL) == FSE_OK);
  202. if(!result) {
  203. break;
  204. }
  205. }
  206. }
  207. if(result) {
  208. for(size_t i = 0; i < COUNT_OF(storage_copy_test_files); i++) {
  209. string_printf(path, "%s/%s", base, storage_copy_test_files[i]);
  210. result = check_file_13DA(storage, string_get_cstr(path));
  211. if(!result) {
  212. break;
  213. }
  214. }
  215. }
  216. string_clear(path);
  217. return result;
  218. }
  219. MU_TEST(storage_file_rename) {
  220. Storage* storage = furi_record_open("storage");
  221. File* file = storage_file_alloc(storage);
  222. mu_check(write_file_13DA(storage, "/ext/file.old"));
  223. mu_check(check_file_13DA(storage, "/ext/file.old"));
  224. mu_assert_int_eq(FSE_OK, storage_common_rename(storage, "/ext/file.old", "/ext/file.new"));
  225. mu_assert_int_eq(FSE_NOT_EXIST, storage_common_stat(storage, "/ext/file.old", NULL));
  226. mu_assert_int_eq(FSE_OK, storage_common_stat(storage, "/ext/file.new", NULL));
  227. mu_check(check_file_13DA(storage, "/ext/file.new"));
  228. mu_assert_int_eq(FSE_OK, storage_common_remove(storage, "/ext/file.new"));
  229. storage_file_free(file);
  230. furi_record_close("storage");
  231. }
  232. MU_TEST(storage_dir_rename) {
  233. Storage* storage = furi_record_open("storage");
  234. storage_dir_create(storage, "/ext/dir.old");
  235. mu_check(storage_dir_rename_check(storage, "/ext/dir.old"));
  236. mu_assert_int_eq(FSE_OK, storage_common_rename(storage, "/ext/dir.old", "/ext/dir.new"));
  237. mu_assert_int_eq(FSE_NOT_EXIST, storage_common_stat(storage, "/ext/dir.old", NULL));
  238. mu_check(storage_dir_rename_check(storage, "/ext/dir.new"));
  239. storage_dir_remove(storage, "/ext/dir.new");
  240. mu_assert_int_eq(FSE_NOT_EXIST, storage_common_stat(storage, "/ext/dir.new", NULL));
  241. furi_record_close("storage");
  242. }
  243. MU_TEST_SUITE(storage_rename) {
  244. MU_RUN_TEST(storage_file_rename);
  245. MU_RUN_TEST(storage_dir_rename);
  246. Storage* storage = furi_record_open("storage");
  247. storage_dir_remove(storage, "/ext/dir.old");
  248. storage_dir_remove(storage, "/ext/dir.new");
  249. furi_record_close("storage");
  250. }
  251. int run_minunit_test_storage() {
  252. MU_RUN_SUITE(storage_file);
  253. MU_RUN_SUITE(storage_dir);
  254. MU_RUN_SUITE(storage_rename);
  255. return MU_EXIT_CODE;
  256. }