storage_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. int run_minunit_test_storage() {
  136. MU_RUN_SUITE(storage_file);
  137. MU_RUN_SUITE(storage_dir);
  138. return MU_EXIT_CODE;
  139. }