storage_test.c 9.5 KB

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