[FL-2627] Flipper applications: SDK, build and debug system (#1387)
* Added support for running applications from SD card (FAPs - Flipper Application Packages) * Added plugin_dist target for fbt to build FAPs * All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default * Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them * Added debugging support for FAPs with fbt debug & VSCode * Added public firmware API with automated versioning Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -0,0 +1,337 @@
|
||||
#include <furi.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
#include <flipper_format/flipper_format_i.h>
|
||||
#include <toolbox/stream/stream.h>
|
||||
#include <storage/storage.h>
|
||||
#include "../minunit.h"
|
||||
|
||||
static const char* test_filetype = "Flipper Format test";
|
||||
static const uint32_t test_version = 666;
|
||||
|
||||
static const char* test_string_key = "String data";
|
||||
static const char* test_string_data = "String";
|
||||
static const char* test_string_updated_data = "New string";
|
||||
static const char* test_string_updated_2_data = "And some more";
|
||||
|
||||
static const char* test_int_key = "Int32 data";
|
||||
static const int32_t test_int_data[] = {1234, -6345, 7813, 0};
|
||||
static const int32_t test_int_updated_data[] = {-1337, 69};
|
||||
static const int32_t test_int_updated_2_data[] = {-3, -2, -1, 0, 1, 2, 3};
|
||||
|
||||
static const char* test_uint_key = "Uint32 data";
|
||||
static const uint32_t test_uint_data[] = {1234, 0, 5678, 9098, 7654321};
|
||||
static const uint32_t test_uint_updated_data[] = {8, 800, 555, 35, 35};
|
||||
static const uint32_t test_uint_updated_2_data[] = {20, 21};
|
||||
|
||||
static const char* test_float_key = "Float data";
|
||||
static const float test_float_data[] = {1.5f, 1000.0f};
|
||||
static const float test_float_updated_data[] = {1.2f};
|
||||
static const float test_float_updated_2_data[] = {0.01f, 0.0f, -51.6f};
|
||||
|
||||
static const char* test_hex_key = "Hex data";
|
||||
static const uint8_t test_hex_data[] = {0xDE, 0xAD, 0xBE};
|
||||
static const uint8_t test_hex_updated_data[] = {0xFE, 0xCA};
|
||||
static const uint8_t test_hex_updated_2_data[] = {0xCA, 0xCA, 0x05};
|
||||
|
||||
static const char* test_hex_new_key = "New Hex data";
|
||||
static const uint8_t test_hex_new_data[] = {0xFF, 0x6A, 0x91};
|
||||
|
||||
static const char* test_data_nix = "Filetype: Flipper Format test\n"
|
||||
"Version: 666\n"
|
||||
"# This is comment\n"
|
||||
"String data: String\n"
|
||||
"Int32 data: 1234 -6345 7813 0\n"
|
||||
"Uint32 data: 1234 0 5678 9098 7654321\n"
|
||||
"Float data: 1.5 1000.0\n"
|
||||
"Hex data: DE AD BE";
|
||||
|
||||
static const char* test_data_win = "Filetype: Flipper Format test\r\n"
|
||||
"Version: 666\r\n"
|
||||
"# This is comment\r\n"
|
||||
"String data: String\r\n"
|
||||
"Int32 data: 1234 -6345 7813 0\r\n"
|
||||
"Uint32 data: 1234 0 5678 9098 7654321\r\n"
|
||||
"Float data: 1.5 1000.0\r\n"
|
||||
"Hex data: DE AD BE";
|
||||
|
||||
#define ARRAY_W_COUNT(x) (x), (COUNT_OF(x))
|
||||
#define ARRAY_W_BSIZE(x) (x), (sizeof(x))
|
||||
|
||||
MU_TEST_1(flipper_format_read_and_update_test, FlipperFormat* flipper_format) {
|
||||
string_t tmpstr;
|
||||
uint32_t version;
|
||||
uint32_t uint32_data[COUNT_OF(test_uint_data)];
|
||||
int32_t int32_data[COUNT_OF(test_int_data)];
|
||||
float float_data[COUNT_OF(test_float_data)];
|
||||
uint8_t hex_data[COUNT_OF(test_hex_data)];
|
||||
|
||||
uint32_t count;
|
||||
|
||||
// key exist test
|
||||
size_t position_before = stream_tell(flipper_format_get_raw_stream(flipper_format));
|
||||
mu_check(flipper_format_key_exist(flipper_format, test_hex_key));
|
||||
mu_assert_int_eq(position_before, stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
mu_check(!flipper_format_key_exist(flipper_format, "invalid key"));
|
||||
mu_assert_int_eq(position_before, stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
// stream seek to end test
|
||||
mu_check(flipper_format_seek_to_end(flipper_format));
|
||||
mu_assert_int_eq(
|
||||
stream_size(flipper_format_get_raw_stream(flipper_format)),
|
||||
stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
// key exist test
|
||||
position_before = stream_tell(flipper_format_get_raw_stream(flipper_format));
|
||||
mu_check(flipper_format_key_exist(flipper_format, test_hex_key));
|
||||
mu_assert_int_eq(position_before, stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
mu_check(!flipper_format_key_exist(flipper_format, "invalid key"));
|
||||
mu_assert_int_eq(position_before, stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
// rewind
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
|
||||
// key exist test
|
||||
position_before = stream_tell(flipper_format_get_raw_stream(flipper_format));
|
||||
mu_check(flipper_format_key_exist(flipper_format, test_hex_key));
|
||||
mu_assert_int_eq(position_before, stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
mu_check(!flipper_format_key_exist(flipper_format, "invalid key"));
|
||||
mu_assert_int_eq(position_before, stream_tell(flipper_format_get_raw_stream(flipper_format)));
|
||||
|
||||
// read test
|
||||
string_init(tmpstr);
|
||||
|
||||
mu_check(flipper_format_read_header(flipper_format, tmpstr, &version));
|
||||
mu_assert_string_eq(test_filetype, string_get_cstr(tmpstr));
|
||||
mu_assert_int_eq(test_version, version);
|
||||
|
||||
mu_check(flipper_format_read_string(flipper_format, test_string_key, tmpstr));
|
||||
mu_assert_string_eq(test_string_data, string_get_cstr(tmpstr));
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_int_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_int_data), count);
|
||||
mu_check(flipper_format_read_int32(flipper_format, test_int_key, ARRAY_W_COUNT(int32_data)));
|
||||
mu_check(memcmp(test_int_data, ARRAY_W_BSIZE(int32_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_uint_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_uint_data), count);
|
||||
mu_check(
|
||||
flipper_format_read_uint32(flipper_format, test_uint_key, ARRAY_W_COUNT(uint32_data)));
|
||||
mu_check(memcmp(test_uint_data, ARRAY_W_BSIZE(uint32_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_float_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_float_data), count);
|
||||
mu_check(flipper_format_read_float(flipper_format, test_float_key, ARRAY_W_COUNT(float_data)));
|
||||
mu_check(memcmp(test_float_data, ARRAY_W_BSIZE(float_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_hex_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_hex_data), count);
|
||||
mu_check(flipper_format_read_hex(flipper_format, test_hex_key, ARRAY_W_COUNT(hex_data)));
|
||||
mu_check(memcmp(test_hex_data, ARRAY_W_BSIZE(hex_data)) == 0);
|
||||
|
||||
mu_check(!flipper_format_read_string(flipper_format, "Key that doesn't exist", tmpstr));
|
||||
|
||||
string_clear(tmpstr);
|
||||
|
||||
// update data
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
mu_check(flipper_format_update_string_cstr(
|
||||
flipper_format, test_string_key, test_string_updated_data));
|
||||
mu_check(flipper_format_update_int32(
|
||||
flipper_format, test_int_key, ARRAY_W_COUNT(test_int_updated_data)));
|
||||
mu_check(flipper_format_update_uint32(
|
||||
flipper_format, test_uint_key, ARRAY_W_COUNT(test_uint_updated_data)));
|
||||
mu_check(flipper_format_update_float(
|
||||
flipper_format, test_float_key, ARRAY_W_COUNT(test_float_updated_data)));
|
||||
mu_check(flipper_format_update_hex(
|
||||
flipper_format, test_hex_key, ARRAY_W_COUNT(test_hex_updated_data)));
|
||||
|
||||
// read updated data test
|
||||
uint32_t uint32_updated_data[COUNT_OF(test_uint_updated_data)];
|
||||
int32_t int32_updated_data[COUNT_OF(test_int_updated_data)];
|
||||
float float_updated_data[COUNT_OF(test_float_updated_data)];
|
||||
uint8_t hex_updated_data[COUNT_OF(test_hex_updated_data)];
|
||||
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
string_init(tmpstr);
|
||||
|
||||
mu_check(flipper_format_read_header(flipper_format, tmpstr, &version));
|
||||
mu_assert_string_eq(test_filetype, string_get_cstr(tmpstr));
|
||||
mu_assert_int_eq(test_version, version);
|
||||
|
||||
mu_check(flipper_format_read_string(flipper_format, test_string_key, tmpstr));
|
||||
mu_assert_string_eq(test_string_updated_data, string_get_cstr(tmpstr));
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_int_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_int_updated_data), count);
|
||||
mu_check(flipper_format_read_int32(
|
||||
flipper_format, test_int_key, ARRAY_W_COUNT(int32_updated_data)));
|
||||
mu_check(memcmp(test_int_updated_data, ARRAY_W_BSIZE(int32_updated_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_uint_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_uint_updated_data), count);
|
||||
mu_check(flipper_format_read_uint32(
|
||||
flipper_format, test_uint_key, ARRAY_W_COUNT(uint32_updated_data)));
|
||||
mu_check(memcmp(test_uint_updated_data, ARRAY_W_BSIZE(uint32_updated_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_float_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_float_updated_data), count);
|
||||
mu_check(flipper_format_read_float(
|
||||
flipper_format, test_float_key, ARRAY_W_COUNT(float_updated_data)));
|
||||
mu_check(memcmp(test_float_updated_data, ARRAY_W_BSIZE(float_updated_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_hex_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_hex_updated_data), count);
|
||||
mu_check(
|
||||
flipper_format_read_hex(flipper_format, test_hex_key, ARRAY_W_COUNT(hex_updated_data)));
|
||||
mu_check(memcmp(test_hex_updated_data, ARRAY_W_BSIZE(hex_updated_data)) == 0);
|
||||
|
||||
mu_check(!flipper_format_read_string(flipper_format, "Key that doesn't exist", tmpstr));
|
||||
|
||||
string_clear(tmpstr);
|
||||
|
||||
// update data
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
mu_check(flipper_format_insert_or_update_string_cstr(
|
||||
flipper_format, test_string_key, test_string_updated_2_data));
|
||||
mu_check(flipper_format_insert_or_update_int32(
|
||||
flipper_format, test_int_key, ARRAY_W_COUNT(test_int_updated_2_data)));
|
||||
mu_check(flipper_format_insert_or_update_uint32(
|
||||
flipper_format, test_uint_key, ARRAY_W_COUNT(test_uint_updated_2_data)));
|
||||
mu_check(flipper_format_insert_or_update_float(
|
||||
flipper_format, test_float_key, ARRAY_W_COUNT(test_float_updated_2_data)));
|
||||
mu_check(flipper_format_insert_or_update_hex(
|
||||
flipper_format, test_hex_key, ARRAY_W_COUNT(test_hex_updated_2_data)));
|
||||
mu_check(flipper_format_insert_or_update_hex(
|
||||
flipper_format, test_hex_new_key, ARRAY_W_COUNT(test_hex_new_data)));
|
||||
|
||||
uint32_t uint32_updated_2_data[COUNT_OF(test_uint_updated_2_data)];
|
||||
int32_t int32_updated_2_data[COUNT_OF(test_int_updated_2_data)];
|
||||
float float_updated_2_data[COUNT_OF(test_float_updated_2_data)];
|
||||
uint8_t hex_updated_2_data[COUNT_OF(test_hex_updated_2_data)];
|
||||
uint8_t hex_new_data[COUNT_OF(test_hex_new_data)];
|
||||
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
string_init(tmpstr);
|
||||
|
||||
mu_check(flipper_format_read_header(flipper_format, tmpstr, &version));
|
||||
mu_assert_string_eq(test_filetype, string_get_cstr(tmpstr));
|
||||
mu_assert_int_eq(test_version, version);
|
||||
|
||||
mu_check(flipper_format_read_string(flipper_format, test_string_key, tmpstr));
|
||||
mu_assert_string_eq(test_string_updated_2_data, string_get_cstr(tmpstr));
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_int_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_int_updated_2_data), count);
|
||||
mu_check(flipper_format_read_int32(
|
||||
flipper_format, test_int_key, ARRAY_W_COUNT(int32_updated_2_data)));
|
||||
mu_check(memcmp(test_int_updated_2_data, ARRAY_W_BSIZE(int32_updated_2_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_uint_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_uint_updated_2_data), count);
|
||||
mu_check(flipper_format_read_uint32(
|
||||
flipper_format, test_uint_key, ARRAY_W_COUNT(uint32_updated_2_data)));
|
||||
mu_check(memcmp(test_uint_updated_2_data, ARRAY_W_BSIZE(uint32_updated_2_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_float_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_float_updated_2_data), count);
|
||||
mu_check(flipper_format_read_float(
|
||||
flipper_format, test_float_key, ARRAY_W_COUNT(float_updated_2_data)));
|
||||
mu_check(memcmp(test_float_updated_2_data, ARRAY_W_BSIZE(float_updated_2_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_hex_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_hex_updated_2_data), count);
|
||||
mu_check(
|
||||
flipper_format_read_hex(flipper_format, test_hex_key, ARRAY_W_COUNT(hex_updated_2_data)));
|
||||
mu_check(memcmp(test_hex_updated_2_data, ARRAY_W_BSIZE(hex_updated_2_data)) == 0);
|
||||
|
||||
mu_check(flipper_format_get_value_count(flipper_format, test_hex_new_key, &count));
|
||||
mu_assert_int_eq(COUNT_OF(test_hex_new_data), count);
|
||||
mu_check(
|
||||
flipper_format_read_hex(flipper_format, test_hex_new_key, ARRAY_W_COUNT(hex_new_data)));
|
||||
mu_check(memcmp(test_hex_new_data, ARRAY_W_BSIZE(hex_new_data)) == 0);
|
||||
|
||||
mu_check(!flipper_format_read_string(flipper_format, "Key that doesn't exist", tmpstr));
|
||||
|
||||
string_clear(tmpstr);
|
||||
|
||||
// delete key test
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
mu_check(flipper_format_delete_key(flipper_format, test_uint_key));
|
||||
|
||||
// deleted key read test
|
||||
mu_check(flipper_format_rewind(flipper_format));
|
||||
mu_check(!flipper_format_read_uint32(
|
||||
flipper_format, test_uint_key, ARRAY_W_COUNT(uint32_updated_data)));
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_string_test) {
|
||||
FlipperFormat* flipper_format = flipper_format_string_alloc();
|
||||
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
||||
|
||||
mu_check(flipper_format_write_header_cstr(flipper_format, test_filetype, test_version));
|
||||
mu_check(flipper_format_write_comment_cstr(flipper_format, "This is comment"));
|
||||
mu_check(flipper_format_write_string_cstr(flipper_format, test_string_key, test_string_data));
|
||||
mu_check(
|
||||
flipper_format_write_int32(flipper_format, test_int_key, ARRAY_W_COUNT(test_int_data)));
|
||||
mu_check(
|
||||
flipper_format_write_uint32(flipper_format, test_uint_key, ARRAY_W_COUNT(test_uint_data)));
|
||||
mu_check(flipper_format_write_float(
|
||||
flipper_format, test_float_key, ARRAY_W_COUNT(test_float_data)));
|
||||
mu_check(flipper_format_write_hex(flipper_format, test_hex_key, ARRAY_W_COUNT(test_hex_data)));
|
||||
|
||||
MU_RUN_TEST_1(flipper_format_read_and_update_test, flipper_format);
|
||||
|
||||
stream_clean(stream);
|
||||
stream_write_cstring(stream, test_data_nix);
|
||||
MU_RUN_TEST_1(flipper_format_read_and_update_test, flipper_format);
|
||||
|
||||
stream_clean(stream);
|
||||
stream_write_cstring(stream, test_data_win);
|
||||
MU_RUN_TEST_1(flipper_format_read_and_update_test, flipper_format);
|
||||
|
||||
flipper_format_free(flipper_format);
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_file_test) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
||||
mu_check(flipper_format_file_open_always(flipper_format, EXT_PATH("flipper.fff")));
|
||||
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
||||
|
||||
mu_check(flipper_format_write_header_cstr(flipper_format, test_filetype, test_version));
|
||||
mu_check(flipper_format_write_comment_cstr(flipper_format, "This is comment"));
|
||||
mu_check(flipper_format_write_string_cstr(flipper_format, test_string_key, test_string_data));
|
||||
mu_check(
|
||||
flipper_format_write_int32(flipper_format, test_int_key, ARRAY_W_COUNT(test_int_data)));
|
||||
mu_check(
|
||||
flipper_format_write_uint32(flipper_format, test_uint_key, ARRAY_W_COUNT(test_uint_data)));
|
||||
mu_check(flipper_format_write_float(
|
||||
flipper_format, test_float_key, ARRAY_W_COUNT(test_float_data)));
|
||||
mu_check(flipper_format_write_hex(flipper_format, test_hex_key, ARRAY_W_COUNT(test_hex_data)));
|
||||
|
||||
MU_RUN_TEST_1(flipper_format_read_and_update_test, flipper_format);
|
||||
|
||||
stream_clean(stream);
|
||||
stream_write_cstring(stream, test_data_nix);
|
||||
MU_RUN_TEST_1(flipper_format_read_and_update_test, flipper_format);
|
||||
|
||||
stream_clean(stream);
|
||||
stream_write_cstring(stream, test_data_win);
|
||||
MU_RUN_TEST_1(flipper_format_read_and_update_test, flipper_format);
|
||||
|
||||
flipper_format_free(flipper_format);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(flipper_format_string_suite) {
|
||||
MU_RUN_TEST(flipper_format_string_test);
|
||||
MU_RUN_TEST(flipper_format_file_test);
|
||||
}
|
||||
|
||||
int run_minunit_test_flipper_format_string() {
|
||||
MU_RUN_SUITE(flipper_format_string_suite);
|
||||
return MU_EXIT_CODE;
|
||||
}
|
@@ -0,0 +1,525 @@
|
||||
#include <furi.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
#include <flipper_format/flipper_format_i.h>
|
||||
#include <toolbox/stream/stream.h>
|
||||
#include "../minunit.h"
|
||||
|
||||
#define TEST_DIR TEST_DIR_NAME "/"
|
||||
#define TEST_DIR_NAME EXT_PATH("unit_tests_tmp")
|
||||
|
||||
static const char* test_filetype = "Flipper File test";
|
||||
static const uint32_t test_version = 666;
|
||||
|
||||
static const char* test_string_key = "String data";
|
||||
static const char* test_string_data = "String";
|
||||
static const char* test_string_updated_data = "New string";
|
||||
|
||||
static const char* test_int_key = "Int32 data";
|
||||
static const int32_t test_int_data[] = {1234, -6345, 7813, 0};
|
||||
static const int32_t test_int_updated_data[] = {-1337, 69};
|
||||
|
||||
static const char* test_uint_key = "Uint32 data";
|
||||
static const uint32_t test_uint_data[] = {1234, 0, 5678, 9098, 7654321};
|
||||
static const uint32_t test_uint_updated_data[] = {8, 800, 555, 35, 35};
|
||||
|
||||
static const char* test_float_key = "Float data";
|
||||
static const float test_float_data[] = {1.5f, 1000.0f};
|
||||
static const float test_float_updated_data[] = {1.2f};
|
||||
|
||||
static const char* test_bool_key = "Bool data";
|
||||
static const bool test_bool_data[] = {true, false};
|
||||
static const bool test_bool_updated_data[] = {false, true, true};
|
||||
|
||||
static const char* test_hex_key = "Hex data";
|
||||
static const uint8_t test_hex_data[] = {0xDE, 0xAD, 0xBE};
|
||||
static const uint8_t test_hex_updated_data[] = {0xFE, 0xCA};
|
||||
|
||||
#define READ_TEST_NIX "ff_nix.test"
|
||||
static const char* test_data_nix = "Filetype: Flipper File test\n"
|
||||
"Version: 666\n"
|
||||
"# This is comment\n"
|
||||
"String data: String\n"
|
||||
"Int32 data: 1234 -6345 7813 0\n"
|
||||
"Uint32 data: 1234 0 5678 9098 7654321\n"
|
||||
"Float data: 1.5 1000.0\n"
|
||||
"Bool data: true false\n"
|
||||
"Hex data: DE AD BE";
|
||||
|
||||
#define READ_TEST_WIN "ff_win.test"
|
||||
static const char* test_data_win = "Filetype: Flipper File test\r\n"
|
||||
"Version: 666\r\n"
|
||||
"# This is comment\r\n"
|
||||
"String data: String\r\n"
|
||||
"Int32 data: 1234 -6345 7813 0\r\n"
|
||||
"Uint32 data: 1234 0 5678 9098 7654321\r\n"
|
||||
"Float data: 1.5 1000.0\r\n"
|
||||
"Bool data: true false\r\n"
|
||||
"Hex data: DE AD BE";
|
||||
|
||||
#define READ_TEST_FLP "ff_flp.test"
|
||||
|
||||
// data created by user on linux machine
|
||||
static const char* test_file_linux = TEST_DIR READ_TEST_NIX;
|
||||
// data created by user on windows machine
|
||||
static const char* test_file_windows = TEST_DIR READ_TEST_WIN;
|
||||
// data created by flipper itself
|
||||
static const char* test_file_flipper = TEST_DIR READ_TEST_FLP;
|
||||
|
||||
static bool storage_write_string(const char* path, const char* data) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(storage);
|
||||
bool result = false;
|
||||
|
||||
do {
|
||||
if(!storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) break;
|
||||
if(storage_file_write(file, data, strlen(data)) != strlen(data)) break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
storage_file_close(file);
|
||||
storage_file_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void tests_setup() {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
mu_assert(storage_simply_remove_recursive(storage, TEST_DIR_NAME), "Cannot clean data");
|
||||
mu_assert(storage_simply_mkdir(storage, TEST_DIR_NAME), "Cannot create dir");
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void tests_teardown() {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
mu_assert(storage_simply_remove_recursive(storage, TEST_DIR_NAME), "Cannot clean data");
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static bool test_read(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
string_t string_value;
|
||||
string_init(string_value);
|
||||
uint32_t uint32_value;
|
||||
void* scratchpad = malloc(512);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_name)) break;
|
||||
|
||||
if(!flipper_format_read_header(file, string_value, &uint32_value)) break;
|
||||
if(string_cmp_str(string_value, test_filetype) != 0) break;
|
||||
if(uint32_value != test_version) break;
|
||||
|
||||
if(!flipper_format_read_string(file, test_string_key, string_value)) break;
|
||||
if(string_cmp_str(string_value, test_string_data) != 0) break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_int_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_int_data)) break;
|
||||
if(!flipper_format_read_int32(file, test_int_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(scratchpad, test_int_data, sizeof(int32_t) * COUNT_OF(test_int_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_uint_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_uint_data)) break;
|
||||
if(!flipper_format_read_uint32(file, test_uint_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(scratchpad, test_uint_data, sizeof(uint32_t) * COUNT_OF(test_uint_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_float_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_float_data)) break;
|
||||
if(!flipper_format_read_float(file, test_float_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(scratchpad, test_float_data, sizeof(float) * COUNT_OF(test_float_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_bool_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_bool_data)) break;
|
||||
if(!flipper_format_read_bool(file, test_bool_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(scratchpad, test_bool_data, sizeof(bool) * COUNT_OF(test_bool_data)) != 0) break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_hex_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_hex_data)) break;
|
||||
if(!flipper_format_read_hex(file, test_hex_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(scratchpad, test_hex_data, sizeof(uint8_t) * COUNT_OF(test_hex_data)) != 0)
|
||||
break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
free(scratchpad);
|
||||
string_clear(string_value);
|
||||
|
||||
flipper_format_free(file);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_read_updated(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
string_t string_value;
|
||||
string_init(string_value);
|
||||
uint32_t uint32_value;
|
||||
void* scratchpad = malloc(512);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_name)) break;
|
||||
|
||||
if(!flipper_format_read_header(file, string_value, &uint32_value)) break;
|
||||
if(string_cmp_str(string_value, test_filetype) != 0) break;
|
||||
if(uint32_value != test_version) break;
|
||||
|
||||
if(!flipper_format_read_string(file, test_string_key, string_value)) break;
|
||||
if(string_cmp_str(string_value, test_string_updated_data) != 0) break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_int_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_int_updated_data)) break;
|
||||
if(!flipper_format_read_int32(file, test_int_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(
|
||||
scratchpad,
|
||||
test_int_updated_data,
|
||||
sizeof(int32_t) * COUNT_OF(test_int_updated_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_uint_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_uint_updated_data)) break;
|
||||
if(!flipper_format_read_uint32(file, test_uint_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(
|
||||
scratchpad,
|
||||
test_uint_updated_data,
|
||||
sizeof(uint32_t) * COUNT_OF(test_uint_updated_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_float_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_float_updated_data)) break;
|
||||
if(!flipper_format_read_float(file, test_float_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(
|
||||
scratchpad,
|
||||
test_float_updated_data,
|
||||
sizeof(float) * COUNT_OF(test_float_updated_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_bool_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_bool_updated_data)) break;
|
||||
if(!flipper_format_read_bool(file, test_bool_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(
|
||||
scratchpad,
|
||||
test_bool_updated_data,
|
||||
sizeof(bool) * COUNT_OF(test_bool_updated_data)) != 0)
|
||||
break;
|
||||
|
||||
if(!flipper_format_get_value_count(file, test_hex_key, &uint32_value)) break;
|
||||
if(uint32_value != COUNT_OF(test_hex_updated_data)) break;
|
||||
if(!flipper_format_read_hex(file, test_hex_key, scratchpad, uint32_value)) break;
|
||||
if(memcmp(
|
||||
scratchpad,
|
||||
test_hex_updated_data,
|
||||
sizeof(uint8_t) * COUNT_OF(test_hex_updated_data)) != 0)
|
||||
break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
free(scratchpad);
|
||||
string_clear(string_value);
|
||||
|
||||
flipper_format_free(file);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_write(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_always(file, file_name)) break;
|
||||
if(!flipper_format_write_header_cstr(file, test_filetype, test_version)) break;
|
||||
if(!flipper_format_write_comment_cstr(file, "This is comment")) break;
|
||||
if(!flipper_format_write_string_cstr(file, test_string_key, test_string_data)) break;
|
||||
if(!flipper_format_write_int32(file, test_int_key, test_int_data, COUNT_OF(test_int_data)))
|
||||
break;
|
||||
if(!flipper_format_write_uint32(
|
||||
file, test_uint_key, test_uint_data, COUNT_OF(test_uint_data)))
|
||||
break;
|
||||
if(!flipper_format_write_float(
|
||||
file, test_float_key, test_float_data, COUNT_OF(test_float_data)))
|
||||
break;
|
||||
if(!flipper_format_write_bool(
|
||||
file, test_bool_key, test_bool_data, COUNT_OF(test_bool_data)))
|
||||
break;
|
||||
if(!flipper_format_write_hex(file, test_hex_key, test_hex_data, COUNT_OF(test_hex_data)))
|
||||
break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_delete_last_key(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_name)) break;
|
||||
if(!flipper_format_delete_key(file, test_hex_key)) break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_append_key(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_append(file, file_name)) break;
|
||||
if(!flipper_format_write_hex(file, test_hex_key, test_hex_data, COUNT_OF(test_hex_data)))
|
||||
break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_update(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_name)) break;
|
||||
if(!flipper_format_update_string_cstr(file, test_string_key, test_string_updated_data))
|
||||
break;
|
||||
if(!flipper_format_update_int32(
|
||||
file, test_int_key, test_int_updated_data, COUNT_OF(test_int_updated_data)))
|
||||
break;
|
||||
if(!flipper_format_update_uint32(
|
||||
file, test_uint_key, test_uint_updated_data, COUNT_OF(test_uint_updated_data)))
|
||||
break;
|
||||
if(!flipper_format_update_float(
|
||||
file, test_float_key, test_float_updated_data, COUNT_OF(test_float_updated_data)))
|
||||
break;
|
||||
if(!flipper_format_update_bool(
|
||||
file, test_bool_key, test_bool_updated_data, COUNT_OF(test_bool_updated_data)))
|
||||
break;
|
||||
if(!flipper_format_update_hex(
|
||||
file, test_hex_key, test_hex_updated_data, COUNT_OF(test_hex_updated_data)))
|
||||
break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_update_backward(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_name)) break;
|
||||
if(!flipper_format_update_string_cstr(file, test_string_key, test_string_data)) break;
|
||||
if(!flipper_format_update_int32(file, test_int_key, test_int_data, COUNT_OF(test_int_data)))
|
||||
break;
|
||||
if(!flipper_format_update_uint32(
|
||||
file, test_uint_key, test_uint_data, COUNT_OF(test_uint_data)))
|
||||
break;
|
||||
if(!flipper_format_update_float(
|
||||
file, test_float_key, test_float_data, COUNT_OF(test_float_data)))
|
||||
break;
|
||||
if(!flipper_format_update_bool(
|
||||
file, test_bool_key, test_bool_data, COUNT_OF(test_bool_data)))
|
||||
break;
|
||||
if(!flipper_format_update_hex(file, test_hex_key, test_hex_data, COUNT_OF(test_hex_data)))
|
||||
break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_write_multikey(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_always(file, file_name)) break;
|
||||
if(!flipper_format_write_header_cstr(file, test_filetype, test_version)) break;
|
||||
|
||||
bool error = false;
|
||||
for(uint8_t index = 0; index < 100; index++) {
|
||||
if(!flipper_format_write_hex(file, test_hex_key, &index, 1)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(error) break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool test_read_multikey(const char* file_name) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool result = false;
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
string_t string_value;
|
||||
string_init(string_value);
|
||||
uint32_t uint32_value;
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_name)) break;
|
||||
if(!flipper_format_read_header(file, string_value, &uint32_value)) break;
|
||||
if(string_cmp_str(string_value, test_filetype) != 0) break;
|
||||
if(uint32_value != test_version) break;
|
||||
|
||||
bool error = false;
|
||||
uint8_t uint8_value;
|
||||
for(uint8_t index = 0; index < 100; index++) {
|
||||
if(!flipper_format_read_hex(file, test_hex_key, &uint8_value, 1)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if(uint8_value != index) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(error) break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
string_clear(string_value);
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_write_test) {
|
||||
mu_assert(storage_write_string(test_file_linux, test_data_nix), "Write test error [Linux]");
|
||||
mu_assert(
|
||||
storage_write_string(test_file_windows, test_data_win), "Write test error [Windows]");
|
||||
mu_assert(test_write(test_file_flipper), "Write test error [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_read_test) {
|
||||
mu_assert(test_read(test_file_linux), "Read test error [Linux]");
|
||||
mu_assert(test_read(test_file_windows), "Read test error [Windows]");
|
||||
mu_assert(test_read(test_file_flipper), "Read test error [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_delete_test) {
|
||||
mu_assert(test_delete_last_key(test_file_linux), "Cannot delete key [Linux]");
|
||||
mu_assert(test_delete_last_key(test_file_windows), "Cannot delete key [Windows]");
|
||||
mu_assert(test_delete_last_key(test_file_flipper), "Cannot delete key [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_delete_result_test) {
|
||||
mu_assert(!test_read(test_file_linux), "Key deleted incorrectly [Linux]");
|
||||
mu_assert(!test_read(test_file_windows), "Key deleted incorrectly [Windows]");
|
||||
mu_assert(!test_read(test_file_flipper), "Key deleted incorrectly [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_append_test) {
|
||||
mu_assert(test_append_key(test_file_linux), "Cannot append data [Linux]");
|
||||
mu_assert(test_append_key(test_file_windows), "Cannot append data [Windows]");
|
||||
mu_assert(test_append_key(test_file_flipper), "Cannot append data [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_append_result_test) {
|
||||
mu_assert(test_read(test_file_linux), "Data appended incorrectly [Linux]");
|
||||
mu_assert(test_read(test_file_windows), "Data appended incorrectly [Windows]");
|
||||
mu_assert(test_read(test_file_flipper), "Data appended incorrectly [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_update_1_test) {
|
||||
mu_assert(test_update(test_file_linux), "Cannot update data #1 [Linux]");
|
||||
mu_assert(test_update(test_file_windows), "Cannot update data #1 [Windows]");
|
||||
mu_assert(test_update(test_file_flipper), "Cannot update data #1 [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_update_1_result_test) {
|
||||
mu_assert(test_read_updated(test_file_linux), "Data #1 updated incorrectly [Linux]");
|
||||
mu_assert(test_read_updated(test_file_windows), "Data #1 updated incorrectly [Windows]");
|
||||
mu_assert(test_read_updated(test_file_flipper), "Data #1 updated incorrectly [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_update_2_test) {
|
||||
mu_assert(test_update_backward(test_file_linux), "Cannot update data #2 [Linux]");
|
||||
mu_assert(test_update_backward(test_file_windows), "Cannot update data #2 [Windows]");
|
||||
mu_assert(test_update_backward(test_file_flipper), "Cannot update data #2 [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_update_2_result_test) {
|
||||
mu_assert(test_read(test_file_linux), "Data #2 updated incorrectly [Linux]");
|
||||
mu_assert(test_read(test_file_windows), "Data #2 updated incorrectly [Windows]");
|
||||
mu_assert(test_read(test_file_flipper), "Data #2 updated incorrectly [Flipper]");
|
||||
}
|
||||
|
||||
MU_TEST(flipper_format_multikey_test) {
|
||||
mu_assert(test_write_multikey(TEST_DIR "ff_multiline.test"), "Multikey write test error");
|
||||
mu_assert(test_read_multikey(TEST_DIR "ff_multiline.test"), "Multikey read test error");
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(flipper_format) {
|
||||
tests_setup();
|
||||
MU_RUN_TEST(flipper_format_write_test);
|
||||
MU_RUN_TEST(flipper_format_read_test);
|
||||
MU_RUN_TEST(flipper_format_delete_test);
|
||||
MU_RUN_TEST(flipper_format_delete_result_test);
|
||||
MU_RUN_TEST(flipper_format_append_test);
|
||||
MU_RUN_TEST(flipper_format_append_result_test);
|
||||
MU_RUN_TEST(flipper_format_update_1_test);
|
||||
MU_RUN_TEST(flipper_format_update_1_result_test);
|
||||
MU_RUN_TEST(flipper_format_update_2_test);
|
||||
MU_RUN_TEST(flipper_format_update_2_result_test);
|
||||
MU_RUN_TEST(flipper_format_multikey_test);
|
||||
tests_teardown();
|
||||
}
|
||||
|
||||
int run_minunit_test_flipper_format() {
|
||||
MU_RUN_SUITE(flipper_format);
|
||||
return MU_EXIT_CODE;
|
||||
}
|
Reference in New Issue
Block a user