flipper_format_test.c 18 KB

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