storage_test.c 9.8 KB

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