flipper_format_test.c 20 KB

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