storage_test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. #include "../minunit.h"
  2. #include <furi.h>
  3. #include <storage/storage.h>
  4. // DO NOT USE THIS IN PRODUCTION CODE
  5. // This is a hack to access internal storage functions and definitions
  6. #include <storage/storage_i.h>
  7. #define UNIT_TESTS_PATH(path) EXT_PATH("unit_tests/" path)
  8. #define STORAGE_LOCKED_FILE EXT_PATH("locked_file.test")
  9. #define STORAGE_LOCKED_DIR STORAGE_INT_PATH_PREFIX
  10. #define STORAGE_TEST_DIR UNIT_TESTS_PATH("test_dir")
  11. static bool storage_file_create(Storage* storage, const char* path, const char* data) {
  12. File* file = storage_file_alloc(storage);
  13. bool result = false;
  14. do {
  15. if(!storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_NEW)) {
  16. break;
  17. }
  18. if(storage_file_write(file, data, strlen(data)) != strlen(data)) {
  19. break;
  20. }
  21. if(!storage_file_close(file)) {
  22. break;
  23. }
  24. result = true;
  25. } while(0);
  26. storage_file_free(file);
  27. return result;
  28. }
  29. static void storage_file_open_lock_setup() {
  30. Storage* storage = furi_record_open(RECORD_STORAGE);
  31. File* file = storage_file_alloc(storage);
  32. storage_simply_remove(storage, STORAGE_LOCKED_FILE);
  33. mu_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_WRITE, FSOM_CREATE_NEW));
  34. mu_check(storage_file_write(file, "0123", 4) == 4);
  35. mu_check(storage_file_close(file));
  36. storage_file_free(file);
  37. furi_record_close(RECORD_STORAGE);
  38. }
  39. static void storage_file_open_lock_teardown() {
  40. Storage* storage = furi_record_open(RECORD_STORAGE);
  41. mu_check(storage_simply_remove(storage, STORAGE_LOCKED_FILE));
  42. furi_record_close(RECORD_STORAGE);
  43. }
  44. static int32_t storage_file_locker(void* ctx) {
  45. Storage* storage = furi_record_open(RECORD_STORAGE);
  46. FuriSemaphore* semaphore = ctx;
  47. File* file = storage_file_alloc(storage);
  48. furi_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  49. furi_semaphore_release(semaphore);
  50. furi_delay_ms(1000);
  51. furi_check(storage_file_close(file));
  52. furi_record_close(RECORD_STORAGE);
  53. storage_file_free(file);
  54. return 0;
  55. }
  56. MU_TEST(storage_file_open_lock) {
  57. Storage* storage = furi_record_open(RECORD_STORAGE);
  58. bool result = false;
  59. FuriSemaphore* semaphore = furi_semaphore_alloc(1, 0);
  60. File* file = storage_file_alloc(storage);
  61. // file_locker thread start
  62. FuriThread* locker_thread =
  63. furi_thread_alloc_ex("StorageFileLocker", 2048, storage_file_locker, semaphore);
  64. furi_thread_start(locker_thread);
  65. // wait for file lock
  66. furi_semaphore_acquire(semaphore, FuriWaitForever);
  67. furi_semaphore_free(semaphore);
  68. result = storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
  69. storage_file_close(file);
  70. // file_locker thread stop
  71. mu_check(furi_thread_join(locker_thread));
  72. furi_thread_free(locker_thread);
  73. // clean data
  74. storage_file_free(file);
  75. furi_record_close(RECORD_STORAGE);
  76. mu_assert(result, "cannot open locked file");
  77. }
  78. MU_TEST(storage_file_open_close) {
  79. Storage* storage = furi_record_open(RECORD_STORAGE);
  80. File* file;
  81. file = storage_file_alloc(storage);
  82. mu_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  83. storage_file_close(file);
  84. storage_file_free(file);
  85. for(size_t i = 0; i < 10; i++) {
  86. file = storage_file_alloc(storage);
  87. mu_check(
  88. storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  89. storage_file_free(file);
  90. }
  91. furi_record_close(RECORD_STORAGE);
  92. }
  93. MU_TEST_SUITE(storage_file) {
  94. storage_file_open_lock_setup();
  95. MU_RUN_TEST(storage_file_open_close);
  96. MU_RUN_TEST(storage_file_open_lock);
  97. storage_file_open_lock_teardown();
  98. }
  99. MU_TEST(storage_dir_open_close) {
  100. Storage* storage = furi_record_open(RECORD_STORAGE);
  101. File* file;
  102. file = storage_file_alloc(storage);
  103. mu_check(storage_dir_open(file, STORAGE_LOCKED_DIR));
  104. storage_dir_close(file);
  105. storage_file_free(file);
  106. for(size_t i = 0; i < 10; i++) {
  107. file = storage_file_alloc(storage);
  108. mu_check(storage_dir_open(file, STORAGE_LOCKED_DIR));
  109. storage_file_free(file);
  110. }
  111. furi_record_close(RECORD_STORAGE);
  112. }
  113. static int32_t storage_dir_locker(void* ctx) {
  114. Storage* storage = furi_record_open(RECORD_STORAGE);
  115. FuriSemaphore* semaphore = ctx;
  116. File* file = storage_file_alloc(storage);
  117. furi_check(storage_dir_open(file, STORAGE_LOCKED_DIR));
  118. furi_semaphore_release(semaphore);
  119. furi_delay_ms(100);
  120. furi_check(storage_dir_close(file));
  121. furi_record_close(RECORD_STORAGE);
  122. storage_file_free(file);
  123. return 0;
  124. }
  125. MU_TEST(storage_dir_open_lock) {
  126. Storage* storage = furi_record_open(RECORD_STORAGE);
  127. bool result = false;
  128. FuriSemaphore* semaphore = furi_semaphore_alloc(1, 0);
  129. File* file = storage_file_alloc(storage);
  130. // file_locker thread start
  131. FuriThread* locker_thread =
  132. furi_thread_alloc_ex("StorageDirLocker", 2048, storage_dir_locker, semaphore);
  133. furi_thread_start(locker_thread);
  134. // wait for dir lock
  135. furi_semaphore_acquire(semaphore, FuriWaitForever);
  136. furi_semaphore_free(semaphore);
  137. result = storage_dir_open(file, STORAGE_LOCKED_DIR);
  138. storage_dir_close(file);
  139. // file_locker thread stop
  140. mu_check(furi_thread_join(locker_thread));
  141. furi_thread_free(locker_thread);
  142. // clean data
  143. storage_file_free(file);
  144. furi_record_close(RECORD_STORAGE);
  145. mu_assert(result, "cannot open locked dir");
  146. }
  147. MU_TEST(storage_dir_exists_test) {
  148. Storage* storage = furi_record_open(RECORD_STORAGE);
  149. mu_check(!storage_dir_exists(storage, STORAGE_TEST_DIR));
  150. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, STORAGE_TEST_DIR));
  151. mu_check(storage_dir_exists(storage, STORAGE_TEST_DIR));
  152. mu_assert_int_eq(FSE_OK, storage_common_remove(storage, STORAGE_TEST_DIR));
  153. furi_record_close(RECORD_STORAGE);
  154. }
  155. MU_TEST_SUITE(storage_dir) {
  156. MU_RUN_TEST(storage_dir_open_close);
  157. MU_RUN_TEST(storage_dir_open_lock);
  158. MU_RUN_TEST(storage_dir_exists_test);
  159. }
  160. static const char* const storage_copy_test_paths[] = {
  161. "1",
  162. "11",
  163. "111",
  164. "1/2",
  165. "1/22",
  166. "1/222",
  167. "11/1",
  168. "111/2",
  169. "111/22",
  170. "111/22/33",
  171. };
  172. static const char* const storage_copy_test_files[] = {
  173. "file.test",
  174. "1/file.test",
  175. "111/22/33/file.test",
  176. };
  177. static bool write_file_13DA(Storage* storage, const char* path) {
  178. File* file = storage_file_alloc(storage);
  179. bool result = false;
  180. if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  181. result = storage_file_write(file, "13DA", 4) == 4;
  182. }
  183. storage_file_close(file);
  184. storage_file_free(file);
  185. return result;
  186. }
  187. static bool check_file_13DA(Storage* storage, const char* path) {
  188. File* file = storage_file_alloc(storage);
  189. bool result = false;
  190. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  191. char data[10] = {0};
  192. result = storage_file_read(file, data, 4) == 4;
  193. if(result) {
  194. result = memcmp(data, "13DA", 4) == 0;
  195. }
  196. }
  197. storage_file_close(file);
  198. storage_file_free(file);
  199. return result;
  200. }
  201. static void storage_dir_create(Storage* storage, const char* base) {
  202. FuriString* path;
  203. path = furi_string_alloc();
  204. storage_common_mkdir(storage, base);
  205. for(size_t i = 0; i < COUNT_OF(storage_copy_test_paths); i++) {
  206. furi_string_printf(path, "%s/%s", base, storage_copy_test_paths[i]);
  207. storage_common_mkdir(storage, furi_string_get_cstr(path));
  208. }
  209. for(size_t i = 0; i < COUNT_OF(storage_copy_test_files); i++) {
  210. furi_string_printf(path, "%s/%s", base, storage_copy_test_files[i]);
  211. write_file_13DA(storage, furi_string_get_cstr(path));
  212. }
  213. furi_string_free(path);
  214. }
  215. static void storage_dir_remove(Storage* storage, const char* base) {
  216. storage_simply_remove_recursive(storage, base);
  217. }
  218. static bool storage_dir_rename_check(Storage* storage, const char* base) {
  219. bool result = false;
  220. FuriString* path;
  221. path = furi_string_alloc();
  222. result = (storage_common_stat(storage, base, NULL) == FSE_OK);
  223. if(result) {
  224. for(size_t i = 0; i < COUNT_OF(storage_copy_test_paths); i++) {
  225. furi_string_printf(path, "%s/%s", base, storage_copy_test_paths[i]);
  226. result = (storage_common_stat(storage, furi_string_get_cstr(path), NULL) == FSE_OK);
  227. if(!result) {
  228. break;
  229. }
  230. }
  231. }
  232. if(result) {
  233. for(size_t i = 0; i < COUNT_OF(storage_copy_test_files); i++) {
  234. furi_string_printf(path, "%s/%s", base, storage_copy_test_files[i]);
  235. result = check_file_13DA(storage, furi_string_get_cstr(path));
  236. if(!result) {
  237. break;
  238. }
  239. }
  240. }
  241. furi_string_free(path);
  242. return result;
  243. }
  244. MU_TEST(storage_file_rename) {
  245. Storage* storage = furi_record_open(RECORD_STORAGE);
  246. File* file = storage_file_alloc(storage);
  247. mu_check(write_file_13DA(storage, EXT_PATH("file.old")));
  248. mu_check(check_file_13DA(storage, EXT_PATH("file.old")));
  249. mu_assert_int_eq(
  250. FSE_OK, storage_common_rename(storage, EXT_PATH("file.old"), EXT_PATH("file.new")));
  251. mu_assert_int_eq(FSE_NOT_EXIST, storage_common_stat(storage, EXT_PATH("file.old"), NULL));
  252. mu_assert_int_eq(FSE_OK, storage_common_stat(storage, EXT_PATH("file.new"), NULL));
  253. mu_check(check_file_13DA(storage, EXT_PATH("file.new")));
  254. mu_assert_int_eq(FSE_OK, storage_common_remove(storage, EXT_PATH("file.new")));
  255. storage_file_free(file);
  256. furi_record_close(RECORD_STORAGE);
  257. }
  258. MU_TEST(storage_dir_rename) {
  259. Storage* storage = furi_record_open(RECORD_STORAGE);
  260. storage_dir_create(storage, EXT_PATH("dir.old"));
  261. mu_check(storage_dir_rename_check(storage, EXT_PATH("dir.old")));
  262. mu_assert_int_eq(
  263. FSE_OK, storage_common_rename(storage, EXT_PATH("dir.old"), EXT_PATH("dir.new")));
  264. mu_assert_int_eq(FSE_NOT_EXIST, storage_common_stat(storage, EXT_PATH("dir.old"), NULL));
  265. mu_check(storage_dir_rename_check(storage, EXT_PATH("dir.new")));
  266. storage_dir_remove(storage, EXT_PATH("dir.new"));
  267. mu_assert_int_eq(FSE_NOT_EXIST, storage_common_stat(storage, EXT_PATH("dir.new"), NULL));
  268. furi_record_close(RECORD_STORAGE);
  269. }
  270. MU_TEST_SUITE(storage_rename) {
  271. MU_RUN_TEST(storage_file_rename);
  272. MU_RUN_TEST(storage_dir_rename);
  273. Storage* storage = furi_record_open(RECORD_STORAGE);
  274. storage_dir_remove(storage, EXT_PATH("dir.old"));
  275. storage_dir_remove(storage, EXT_PATH("dir.new"));
  276. furi_record_close(RECORD_STORAGE);
  277. }
  278. #define APPSDATA_APP_PATH(path) APPS_DATA_PATH "/" path
  279. static const char* storage_test_apps[] = {
  280. "-_twilight_-",
  281. "-_rainbow_-",
  282. "-_pinkie_-",
  283. "-_apple_-",
  284. "-_flutter_-",
  285. "-_rare_-",
  286. };
  287. static size_t storage_test_apps_count = COUNT_OF(storage_test_apps);
  288. static int32_t storage_test_app(void* arg) {
  289. UNUSED(arg);
  290. Storage* storage = furi_record_open(RECORD_STORAGE);
  291. storage_common_remove(storage, "/data/test");
  292. int32_t ret = storage_file_create(storage, "/data/test", "test");
  293. furi_record_close(RECORD_STORAGE);
  294. return ret;
  295. }
  296. MU_TEST(test_storage_data_path_apps) {
  297. for(size_t i = 0; i < storage_test_apps_count; i++) {
  298. FuriThread* thread =
  299. furi_thread_alloc_ex(storage_test_apps[i], 1024, storage_test_app, NULL);
  300. furi_thread_set_appid(thread, storage_test_apps[i]);
  301. furi_thread_start(thread);
  302. furi_thread_join(thread);
  303. mu_assert_int_eq(true, furi_thread_get_return_code(thread));
  304. // Check if app data dir and file exists
  305. Storage* storage = furi_record_open(RECORD_STORAGE);
  306. FuriString* expected = furi_string_alloc();
  307. furi_string_printf(expected, APPSDATA_APP_PATH("%s"), storage_test_apps[i]);
  308. mu_check(storage_dir_exists(storage, furi_string_get_cstr(expected)));
  309. furi_string_cat(expected, "/test");
  310. mu_check(storage_file_exists(storage, furi_string_get_cstr(expected)));
  311. furi_string_printf(expected, APPSDATA_APP_PATH("%s"), storage_test_apps[i]);
  312. storage_simply_remove_recursive(storage, furi_string_get_cstr(expected));
  313. furi_record_close(RECORD_STORAGE);
  314. furi_string_free(expected);
  315. furi_thread_free(thread);
  316. }
  317. }
  318. MU_TEST(test_storage_data_path) {
  319. Storage* storage = furi_record_open(RECORD_STORAGE);
  320. File* file = storage_file_alloc(storage);
  321. mu_check(storage_dir_open(file, "/data"));
  322. mu_check(storage_dir_close(file));
  323. storage_file_free(file);
  324. // check that appsdata folder exists
  325. mu_check(storage_dir_exists(storage, APPS_DATA_PATH));
  326. // check that cli folder exists
  327. mu_check(storage_dir_exists(storage, APPSDATA_APP_PATH("cli")));
  328. storage_simply_remove(storage, APPSDATA_APP_PATH("cli"));
  329. furi_record_close(RECORD_STORAGE);
  330. }
  331. MU_TEST(test_storage_common_migrate) {
  332. Storage* storage = furi_record_open(RECORD_STORAGE);
  333. // Setup test folders
  334. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  335. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  336. // Test migration from non existing
  337. mu_assert_int_eq(
  338. FSE_OK,
  339. storage_common_migrate(
  340. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  341. // Test migration from existing folder to non existing
  342. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, UNIT_TESTS_PATH("migrate_old")));
  343. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old/file1"), "test1"));
  344. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old/file2.ext"), "test2"));
  345. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old/file3.ext.ext"), "test3"));
  346. mu_assert_int_eq(
  347. FSE_OK,
  348. storage_common_migrate(
  349. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  350. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file1")));
  351. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file2.ext")));
  352. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file3.ext.ext")));
  353. mu_check(storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  354. mu_check(!storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  355. // Test migration from existing folder to existing folder
  356. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, UNIT_TESTS_PATH("migrate_old")));
  357. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old/file1"), "test1"));
  358. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old/file2.ext"), "test2"));
  359. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old/file3.ext.ext"), "test3"));
  360. mu_assert_int_eq(
  361. FSE_OK,
  362. storage_common_migrate(
  363. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  364. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file1")));
  365. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file2.ext")));
  366. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file3.ext.ext")));
  367. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file11")));
  368. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file21.ext")));
  369. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new/file3.ext1.ext")));
  370. mu_check(storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  371. mu_check(!storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  372. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  373. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  374. // Test migration from empty folder to existing file
  375. // Expected result: FSE_OK, folder removed, file untouched
  376. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, UNIT_TESTS_PATH("migrate_old")));
  377. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_new"), "test1"));
  378. mu_assert_int_eq(
  379. FSE_OK,
  380. storage_common_migrate(
  381. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  382. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  383. mu_check(!storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  384. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  385. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  386. // Test migration from empty folder to existing folder
  387. // Expected result: FSE_OK, old folder removed, new folder untouched
  388. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, UNIT_TESTS_PATH("migrate_old")));
  389. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, UNIT_TESTS_PATH("migrate_new")));
  390. mu_assert_int_eq(
  391. FSE_OK,
  392. storage_common_migrate(
  393. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  394. mu_check(storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  395. mu_check(!storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  396. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  397. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  398. // Test migration from existing file to non existing, no extension
  399. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old"), "test1"));
  400. mu_assert_int_eq(
  401. FSE_OK,
  402. storage_common_migrate(
  403. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  404. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  405. mu_check(!storage_file_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  406. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  407. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  408. // Test migration from existing file to non existing, with extension
  409. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old.file"), "test1"));
  410. mu_assert_int_eq(
  411. FSE_OK,
  412. storage_common_migrate(
  413. storage, UNIT_TESTS_PATH("migrate_old.file"), UNIT_TESTS_PATH("migrate_new.file")));
  414. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new.file")));
  415. mu_check(!storage_file_exists(storage, UNIT_TESTS_PATH("migrate_old.file")));
  416. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old.file"));
  417. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new.file"));
  418. // Test migration from existing file to existing file, no extension
  419. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old"), "test1"));
  420. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_new"), "test2"));
  421. mu_assert_int_eq(
  422. FSE_OK,
  423. storage_common_migrate(
  424. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  425. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  426. mu_check(!storage_file_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  427. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new1")));
  428. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  429. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  430. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new1"));
  431. // Test migration from existing file to existing file, with extension
  432. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old.file"), "test1"));
  433. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_new.file"), "test2"));
  434. mu_assert_int_eq(
  435. FSE_OK,
  436. storage_common_migrate(
  437. storage, UNIT_TESTS_PATH("migrate_old.file"), UNIT_TESTS_PATH("migrate_new.file")));
  438. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new.file")));
  439. mu_check(!storage_file_exists(storage, UNIT_TESTS_PATH("migrate_old.file")));
  440. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new1.file")));
  441. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old.file"));
  442. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new.file"));
  443. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new1.file"));
  444. // Test migration from existing file to existing folder
  445. mu_check(storage_file_create(storage, UNIT_TESTS_PATH("migrate_old"), "test1"));
  446. mu_assert_int_eq(FSE_OK, storage_common_mkdir(storage, UNIT_TESTS_PATH("migrate_new")));
  447. mu_assert_int_eq(
  448. FSE_OK,
  449. storage_common_migrate(
  450. storage, UNIT_TESTS_PATH("migrate_old"), UNIT_TESTS_PATH("migrate_new")));
  451. mu_check(storage_dir_exists(storage, UNIT_TESTS_PATH("migrate_new")));
  452. mu_check(!storage_file_exists(storage, UNIT_TESTS_PATH("migrate_old")));
  453. mu_check(storage_file_exists(storage, UNIT_TESTS_PATH("migrate_new1")));
  454. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_old"));
  455. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new"));
  456. storage_simply_remove_recursive(storage, UNIT_TESTS_PATH("migrate_new1"));
  457. furi_record_close(RECORD_STORAGE);
  458. }
  459. MU_TEST_SUITE(test_data_path) {
  460. MU_RUN_TEST(test_storage_data_path);
  461. MU_RUN_TEST(test_storage_data_path_apps);
  462. }
  463. MU_TEST_SUITE(test_storage_common) {
  464. MU_RUN_TEST(test_storage_common_migrate);
  465. }
  466. int run_minunit_test_storage() {
  467. MU_RUN_SUITE(storage_file);
  468. MU_RUN_SUITE(storage_dir);
  469. MU_RUN_SUITE(storage_rename);
  470. MU_RUN_SUITE(test_data_path);
  471. MU_RUN_SUITE(test_storage_common);
  472. return MU_EXIT_CODE;
  473. }