flipper_file_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #include <furi.h>
  2. #include <flipper_file/flipper_file.h>
  3. #include "../minunit.h"
  4. #define TEST_DIR TEST_DIR_NAME "/"
  5. #define TEST_DIR_NAME "/ext/unit_tests_tmp"
  6. static const char* test_filetype = "Flipper File test";
  7. static const uint32_t test_version = 666;
  8. static const char* test_string_key = "String data";
  9. static const char* test_string_data = "String";
  10. static const char* test_string_updated_data = "New string";
  11. static const char* test_int_key = "Int32 data";
  12. static const int32_t test_int_data[] = {1234, -6345, 7813, 0};
  13. static const int32_t test_int_updated_data[] = {-1337, 69};
  14. static const char* test_uint_key = "Uint32 data";
  15. static const uint32_t test_uint_data[] = {1234, 0, 5678, 9098, 7654321};
  16. static const uint32_t test_uint_updated_data[] = {8, 800, 555, 35, 35};
  17. static const char* test_float_key = "Float data";
  18. static const float test_float_data[] = {1.5f, 1000.0f};
  19. static const float test_float_updated_data[] = {1.2f};
  20. static const char* test_hex_key = "Hex data";
  21. static const uint8_t test_hex_data[] = {0xDE, 0xAD, 0xBE};
  22. static const uint8_t test_hex_updated_data[] = {0xFE, 0xCA};
  23. #define READ_TEST_WIN "ff_win.test"
  24. static const char* test_data_win = "Filetype: Flipper File test\n"
  25. "Version: 666\n"
  26. "# This is comment\n"
  27. "String data: String\n"
  28. "Int32 data: 1234 -6345 7813 0\n"
  29. "Uint32 data: 1234 0 5678 9098 7654321\n"
  30. "Float data: 1.5 1000.0\n"
  31. "Hex data: DE AD BE";
  32. #define READ_TEST_NIX "ff_nix.test"
  33. static const char* test_data_nix = "Filetype: Flipper File test\r\n"
  34. "Version: 666\r\n"
  35. "# This is comment\r\n"
  36. "String data: String\r\n"
  37. "Int32 data: 1234 -6345 7813 0\r\n"
  38. "Uint32 data: 1234 0 5678 9098 7654321\r\n"
  39. "Float data: 1.5 1000.0\r\n"
  40. "Hex data: DE AD BE";
  41. #define READ_TEST_FLP "ff_flp.test"
  42. // data created by user on linux machine
  43. const char* test_file_linux = TEST_DIR READ_TEST_WIN;
  44. // data created by user on windows machine
  45. const char* test_file_windows = TEST_DIR READ_TEST_NIX;
  46. // data created by flipper itself
  47. const char* test_file_flipper = TEST_DIR READ_TEST_FLP;
  48. static bool storage_write_string(const char* path, const char* data) {
  49. Storage* storage = furi_record_open("storage");
  50. File* file = storage_file_alloc(storage);
  51. bool result = false;
  52. do {
  53. if(!storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) break;
  54. if(storage_file_write(file, data, strlen(data)) != strlen(data)) break;
  55. result = true;
  56. } while(false);
  57. storage_file_close(file);
  58. storage_file_free(file);
  59. furi_record_close("storage");
  60. return result;
  61. }
  62. static void tests_setup() {
  63. Storage* storage = furi_record_open("storage");
  64. mu_assert(storage_simply_remove_recursive(storage, TEST_DIR_NAME), "Cannot clean data");
  65. mu_assert(storage_simply_mkdir(storage, TEST_DIR_NAME), "Cannot create dir");
  66. furi_record_close("storage");
  67. }
  68. static void tests_teardown() {
  69. Storage* storage = furi_record_open("storage");
  70. mu_assert(storage_simply_remove_recursive(storage, TEST_DIR_NAME), "Cannot clean data");
  71. furi_record_close("storage");
  72. }
  73. static bool test_read(const char* file_name) {
  74. Storage* storage = furi_record_open("storage");
  75. bool result = false;
  76. FlipperFile* file = flipper_file_alloc(storage);
  77. string_t string_value;
  78. string_init(string_value);
  79. uint32_t uint32_value;
  80. void* scratchpad = malloc(512);
  81. do {
  82. if(!flipper_file_open_existing(file, file_name)) break;
  83. if(!flipper_file_read_header(file, string_value, &uint32_value)) break;
  84. if(string_cmp_str(string_value, test_filetype) != 0) break;
  85. if(uint32_value != test_version) break;
  86. if(!flipper_file_read_string(file, test_string_key, string_value)) break;
  87. if(string_cmp_str(string_value, test_string_data) != 0) break;
  88. if(!flipper_file_get_value_count(file, test_int_key, &uint32_value)) break;
  89. if(uint32_value != COUNT_OF(test_int_data)) break;
  90. if(!flipper_file_read_int32(file, test_int_key, scratchpad, uint32_value)) break;
  91. if(memcmp(scratchpad, test_int_data, sizeof(int32_t) * COUNT_OF(test_int_data)) != 0)
  92. break;
  93. if(!flipper_file_get_value_count(file, test_uint_key, &uint32_value)) break;
  94. if(uint32_value != COUNT_OF(test_uint_data)) break;
  95. if(!flipper_file_read_uint32(file, test_uint_key, scratchpad, uint32_value)) break;
  96. if(memcmp(scratchpad, test_uint_data, sizeof(uint32_t) * COUNT_OF(test_uint_data)) != 0)
  97. break;
  98. if(!flipper_file_get_value_count(file, test_float_key, &uint32_value)) break;
  99. if(uint32_value != COUNT_OF(test_float_data)) break;
  100. if(!flipper_file_read_float(file, test_float_key, scratchpad, uint32_value)) break;
  101. if(memcmp(scratchpad, test_float_data, sizeof(float) * COUNT_OF(test_float_data)) != 0)
  102. break;
  103. if(!flipper_file_get_value_count(file, test_hex_key, &uint32_value)) break;
  104. if(uint32_value != COUNT_OF(test_hex_data)) break;
  105. if(!flipper_file_read_hex(file, test_hex_key, scratchpad, uint32_value)) break;
  106. if(memcmp(scratchpad, test_hex_data, sizeof(uint8_t) * COUNT_OF(test_hex_data)) != 0)
  107. break;
  108. result = true;
  109. } while(false);
  110. free(scratchpad);
  111. string_clear(string_value);
  112. flipper_file_close(file);
  113. flipper_file_free(file);
  114. furi_record_close("storage");
  115. return result;
  116. }
  117. static bool test_read_updated(const char* file_name) {
  118. Storage* storage = furi_record_open("storage");
  119. bool result = false;
  120. FlipperFile* file = flipper_file_alloc(storage);
  121. string_t string_value;
  122. string_init(string_value);
  123. uint32_t uint32_value;
  124. void* scratchpad = malloc(512);
  125. do {
  126. if(!flipper_file_open_existing(file, file_name)) break;
  127. if(!flipper_file_read_header(file, string_value, &uint32_value)) break;
  128. if(string_cmp_str(string_value, test_filetype) != 0) break;
  129. if(uint32_value != test_version) break;
  130. if(!flipper_file_read_string(file, test_string_key, string_value)) break;
  131. if(string_cmp_str(string_value, test_string_updated_data) != 0) break;
  132. if(!flipper_file_get_value_count(file, test_int_key, &uint32_value)) break;
  133. if(uint32_value != COUNT_OF(test_int_updated_data)) break;
  134. if(!flipper_file_read_int32(file, test_int_key, scratchpad, uint32_value)) break;
  135. if(memcmp(
  136. scratchpad,
  137. test_int_updated_data,
  138. sizeof(int32_t) * COUNT_OF(test_int_updated_data)) != 0)
  139. break;
  140. if(!flipper_file_get_value_count(file, test_uint_key, &uint32_value)) break;
  141. if(uint32_value != COUNT_OF(test_uint_updated_data)) break;
  142. if(!flipper_file_read_uint32(file, test_uint_key, scratchpad, uint32_value)) break;
  143. if(memcmp(
  144. scratchpad,
  145. test_uint_updated_data,
  146. sizeof(uint32_t) * COUNT_OF(test_uint_updated_data)) != 0)
  147. break;
  148. if(!flipper_file_get_value_count(file, test_float_key, &uint32_value)) break;
  149. if(uint32_value != COUNT_OF(test_float_updated_data)) break;
  150. if(!flipper_file_read_float(file, test_float_key, scratchpad, uint32_value)) break;
  151. if(memcmp(
  152. scratchpad,
  153. test_float_updated_data,
  154. sizeof(float) * COUNT_OF(test_float_updated_data)) != 0)
  155. break;
  156. if(!flipper_file_get_value_count(file, test_hex_key, &uint32_value)) break;
  157. if(uint32_value != COUNT_OF(test_hex_updated_data)) break;
  158. if(!flipper_file_read_hex(file, test_hex_key, scratchpad, uint32_value)) break;
  159. if(memcmp(
  160. scratchpad,
  161. test_hex_updated_data,
  162. sizeof(uint8_t) * COUNT_OF(test_hex_updated_data)) != 0)
  163. break;
  164. result = true;
  165. } while(false);
  166. free(scratchpad);
  167. string_clear(string_value);
  168. flipper_file_close(file);
  169. flipper_file_free(file);
  170. furi_record_close("storage");
  171. return result;
  172. }
  173. static bool test_write(const char* file_name) {
  174. Storage* storage = furi_record_open("storage");
  175. bool result = false;
  176. FlipperFile* file = flipper_file_alloc(storage);
  177. do {
  178. if(!flipper_file_open_always(file, file_name)) break;
  179. if(!flipper_file_write_header_cstr(file, test_filetype, test_version)) break;
  180. if(!flipper_file_write_comment_cstr(file, "This is comment")) break;
  181. if(!flipper_file_write_string_cstr(file, test_string_key, test_string_data)) break;
  182. if(!flipper_file_write_int32(file, test_int_key, test_int_data, COUNT_OF(test_int_data)))
  183. break;
  184. if(!flipper_file_write_uint32(
  185. file, test_uint_key, test_uint_data, COUNT_OF(test_uint_data)))
  186. break;
  187. if(!flipper_file_write_float(
  188. file, test_float_key, test_float_data, COUNT_OF(test_float_data)))
  189. break;
  190. if(!flipper_file_write_hex(file, test_hex_key, test_hex_data, COUNT_OF(test_hex_data)))
  191. break;
  192. result = true;
  193. } while(false);
  194. flipper_file_close(file);
  195. flipper_file_free(file);
  196. furi_record_close("storage");
  197. return result;
  198. }
  199. static bool test_delete_last_key(const char* file_name) {
  200. Storage* storage = furi_record_open("storage");
  201. bool result = false;
  202. FlipperFile* file = flipper_file_alloc(storage);
  203. do {
  204. if(!flipper_file_open_existing(file, file_name)) break;
  205. if(!flipper_file_delete_key(file, test_hex_key)) break;
  206. result = true;
  207. } while(false);
  208. flipper_file_close(file);
  209. flipper_file_free(file);
  210. furi_record_close("storage");
  211. return result;
  212. }
  213. static bool test_append_key(const char* file_name) {
  214. Storage* storage = furi_record_open("storage");
  215. bool result = false;
  216. FlipperFile* file = flipper_file_alloc(storage);
  217. do {
  218. if(!flipper_file_open_append(file, file_name)) break;
  219. if(!flipper_file_write_hex(file, test_hex_key, test_hex_data, COUNT_OF(test_hex_data)))
  220. break;
  221. result = true;
  222. } while(false);
  223. flipper_file_close(file);
  224. flipper_file_free(file);
  225. furi_record_close("storage");
  226. return result;
  227. }
  228. static bool test_update(const char* file_name) {
  229. Storage* storage = furi_record_open("storage");
  230. bool result = false;
  231. FlipperFile* file = flipper_file_alloc(storage);
  232. do {
  233. if(!flipper_file_open_existing(file, file_name)) break;
  234. if(!flipper_file_update_string_cstr(file, test_string_key, test_string_updated_data))
  235. break;
  236. if(!flipper_file_update_int32(
  237. file, test_int_key, test_int_updated_data, COUNT_OF(test_int_updated_data)))
  238. break;
  239. if(!flipper_file_update_uint32(
  240. file, test_uint_key, test_uint_updated_data, COUNT_OF(test_uint_updated_data)))
  241. break;
  242. if(!flipper_file_update_float(
  243. file, test_float_key, test_float_updated_data, COUNT_OF(test_float_updated_data)))
  244. break;
  245. if(!flipper_file_update_hex(
  246. file, test_hex_key, test_hex_updated_data, COUNT_OF(test_hex_updated_data)))
  247. break;
  248. result = true;
  249. } while(false);
  250. flipper_file_close(file);
  251. flipper_file_free(file);
  252. furi_record_close("storage");
  253. return result;
  254. }
  255. static bool test_update_backward(const char* file_name) {
  256. Storage* storage = furi_record_open("storage");
  257. bool result = false;
  258. FlipperFile* file = flipper_file_alloc(storage);
  259. do {
  260. if(!flipper_file_open_existing(file, file_name)) break;
  261. if(!flipper_file_update_string_cstr(file, test_string_key, test_string_data)) break;
  262. if(!flipper_file_update_int32(file, test_int_key, test_int_data, COUNT_OF(test_int_data)))
  263. break;
  264. if(!flipper_file_update_uint32(
  265. file, test_uint_key, test_uint_data, COUNT_OF(test_uint_data)))
  266. break;
  267. if(!flipper_file_update_float(
  268. file, test_float_key, test_float_data, COUNT_OF(test_float_data)))
  269. break;
  270. if(!flipper_file_update_hex(file, test_hex_key, test_hex_data, COUNT_OF(test_hex_data)))
  271. break;
  272. result = true;
  273. } while(false);
  274. flipper_file_close(file);
  275. flipper_file_free(file);
  276. furi_record_close("storage");
  277. return result;
  278. }
  279. static bool test_write_multikey(const char* file_name) {
  280. Storage* storage = furi_record_open("storage");
  281. bool result = false;
  282. FlipperFile* file = flipper_file_alloc(storage);
  283. do {
  284. if(!flipper_file_open_always(file, file_name)) break;
  285. if(!flipper_file_write_header_cstr(file, test_filetype, test_version)) break;
  286. bool error = false;
  287. for(uint8_t index = 0; index < 100; index++) {
  288. if(!flipper_file_write_hex(file, test_hex_key, &index, 1)) {
  289. error = true;
  290. break;
  291. }
  292. }
  293. if(error) break;
  294. result = true;
  295. } while(false);
  296. flipper_file_close(file);
  297. flipper_file_free(file);
  298. furi_record_close("storage");
  299. return result;
  300. }
  301. static bool test_read_multikey(const char* file_name) {
  302. Storage* storage = furi_record_open("storage");
  303. bool result = false;
  304. FlipperFile* file = flipper_file_alloc(storage);
  305. string_t string_value;
  306. string_init(string_value);
  307. uint32_t uint32_value;
  308. do {
  309. if(!flipper_file_open_existing(file, file_name)) break;
  310. if(!flipper_file_read_header(file, string_value, &uint32_value)) break;
  311. if(string_cmp_str(string_value, test_filetype) != 0) break;
  312. if(uint32_value != test_version) break;
  313. bool error = false;
  314. uint8_t uint8_value;
  315. for(uint8_t index = 0; index < 100; index++) {
  316. if(!flipper_file_read_hex(file, test_hex_key, &uint8_value, 1)) {
  317. error = true;
  318. break;
  319. }
  320. if(uint8_value != index) {
  321. error = true;
  322. break;
  323. }
  324. }
  325. if(error) break;
  326. result = true;
  327. } while(false);
  328. string_clear(string_value);
  329. flipper_file_close(file);
  330. flipper_file_free(file);
  331. furi_record_close("storage");
  332. return result;
  333. }
  334. MU_TEST(flipper_file_write_test) {
  335. mu_assert(storage_write_string(test_file_linux, test_data_nix), "Write test error [Linux]");
  336. mu_assert(
  337. storage_write_string(test_file_windows, test_data_win), "Write test error [Windows]");
  338. mu_assert(test_write(test_file_flipper), "Write test error [Flipper]");
  339. }
  340. MU_TEST(flipper_file_read_test) {
  341. mu_assert(test_read(test_file_linux), "Read test error [Linux]");
  342. mu_assert(test_read(test_file_windows), "Read test error [Windows]");
  343. mu_assert(test_read(test_file_flipper), "Read test error [Flipper]");
  344. }
  345. MU_TEST(flipper_file_delete_test) {
  346. mu_assert(test_delete_last_key(test_file_linux), "Cannot delete key [Linux]");
  347. mu_assert(test_delete_last_key(test_file_windows), "Cannot delete key [Windows]");
  348. mu_assert(test_delete_last_key(test_file_flipper), "Cannot delete key [Flipper]");
  349. }
  350. MU_TEST(flipper_file_delete_result_test) {
  351. mu_assert(!test_read(test_file_linux), "Key deleted incorrectly [Linux]");
  352. mu_assert(!test_read(test_file_windows), "Key deleted incorrectly [Windows]");
  353. mu_assert(!test_read(test_file_flipper), "Key deleted incorrectly [Flipper]");
  354. }
  355. MU_TEST(flipper_file_append_test) {
  356. mu_assert(test_append_key(test_file_linux), "Cannot append data [Linux]");
  357. mu_assert(test_append_key(test_file_windows), "Cannot append data [Windows]");
  358. mu_assert(test_append_key(test_file_flipper), "Cannot append data [Flipper]");
  359. }
  360. MU_TEST(flipper_file_append_result_test) {
  361. mu_assert(test_read(test_file_linux), "Data appended incorrectly [Linux]");
  362. mu_assert(test_read(test_file_windows), "Data appended incorrectly [Windows]");
  363. mu_assert(test_read(test_file_flipper), "Data appended incorrectly [Flipper]");
  364. }
  365. MU_TEST(flipper_file_update_1_test) {
  366. mu_assert(test_update(test_file_linux), "Cannot update data #1 [Linux]");
  367. mu_assert(test_update(test_file_windows), "Cannot update data #1 [Windows]");
  368. mu_assert(test_update(test_file_flipper), "Cannot update data #1 [Flipper]");
  369. }
  370. MU_TEST(flipper_file_update_1_result_test) {
  371. mu_assert(test_read_updated(test_file_linux), "Data #1 updated incorrectly [Linux]");
  372. mu_assert(test_read_updated(test_file_windows), "Data #1 updated incorrectly [Windows]");
  373. mu_assert(test_read_updated(test_file_flipper), "Data #1 updated incorrectly [Flipper]");
  374. }
  375. MU_TEST(flipper_file_update_2_test) {
  376. mu_assert(test_update_backward(test_file_linux), "Cannot update data #2 [Linux]");
  377. mu_assert(test_update_backward(test_file_windows), "Cannot update data #2 [Windows]");
  378. mu_assert(test_update_backward(test_file_flipper), "Cannot update data #2 [Flipper]");
  379. }
  380. MU_TEST(flipper_file_update_2_result_test) {
  381. mu_assert(test_read(test_file_linux), "Data #2 updated incorrectly [Linux]");
  382. mu_assert(test_read(test_file_windows), "Data #2 updated incorrectly [Windows]");
  383. mu_assert(test_read(test_file_flipper), "Data #2 updated incorrectly [Flipper]");
  384. }
  385. MU_TEST(flipper_file_multikey_test) {
  386. mu_assert(test_write_multikey(TEST_DIR "ff_multiline.test"), "Multikey write test error");
  387. mu_assert(test_read_multikey(TEST_DIR "ff_multiline.test"), "Multikey read test error");
  388. }
  389. MU_TEST_SUITE(flipper_file) {
  390. tests_setup();
  391. MU_RUN_TEST(flipper_file_write_test);
  392. MU_RUN_TEST(flipper_file_read_test);
  393. MU_RUN_TEST(flipper_file_delete_test);
  394. MU_RUN_TEST(flipper_file_delete_result_test);
  395. MU_RUN_TEST(flipper_file_append_test);
  396. MU_RUN_TEST(flipper_file_append_result_test);
  397. MU_RUN_TEST(flipper_file_update_1_test);
  398. MU_RUN_TEST(flipper_file_update_1_result_test);
  399. MU_RUN_TEST(flipper_file_update_2_test);
  400. MU_RUN_TEST(flipper_file_update_2_result_test);
  401. MU_RUN_TEST(flipper_file_multikey_test);
  402. tests_teardown();
  403. }
  404. int run_minunit_test_flipper_file() {
  405. MU_RUN_SUITE(flipper_file);
  406. return MU_EXIT_CODE;
  407. }