Skip to content

Commit

Permalink
[FL-3664] 64k does not enough (#3216)
Browse files Browse the repository at this point in the history
* Unit tests: add "exists" to furi_record tests
* Unit tests: mu_warn, storage 64k test
* Storage: read/write over 64k
* Unit tests: moar tests for storage r/w for >64k cases
* Apps, libs: replace uint16_t with size_t on storage r/w operations
* Unit tests: better data pattern, subghz: warning if transmission is prohibited

Co-authored-by: あく <[email protected]>
  • Loading branch information
DrZlo13 and skotopes authored Nov 15, 2023
1 parent 98d5718 commit 4b3e8ab
Show file tree
Hide file tree
Showing 25 changed files with 186 additions and 79 deletions.
27 changes: 19 additions & 8 deletions applications/debug/unit_tests/furi/furi_record_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
#include <furi.h>
#include "../minunit.h"

#define TEST_RECORD_NAME "test/holding"

void test_furi_create_open() {
// 1. Create record
// Test that record does not exist
mu_check(furi_record_exists(TEST_RECORD_NAME) == false);

// Create record
uint8_t test_data = 0;
furi_record_create("test/holding", (void*)&test_data);
furi_record_create(TEST_RECORD_NAME, (void*)&test_data);

// 2. Open it
void* record = furi_record_open("test/holding");
// Test that record exists
mu_check(furi_record_exists(TEST_RECORD_NAME) == true);

// Open it
void* record = furi_record_open(TEST_RECORD_NAME);
mu_assert_pointers_eq(record, &test_data);

// 3. Close it
furi_record_close("test/holding");
// Close it
furi_record_close(TEST_RECORD_NAME);

// Clean up
furi_record_destroy(TEST_RECORD_NAME);

// 4. Clean up
furi_record_destroy("test/holding");
// Test that record does not exist
mu_check(furi_record_exists(TEST_RECORD_NAME) == false);
}
5 changes: 5 additions & 0 deletions applications/debug/unit_tests/minunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ __attribute__((unused)) static void (*minunit_teardown)(void) = NULL;

void minunit_print_progress(void);
void minunit_print_fail(const char* error);
void minunit_printf_warning(const char* format, ...);

/* Definitions */
#define MU_TEST(method_name) static void method_name(void)
Expand Down Expand Up @@ -150,6 +151,10 @@ void minunit_print_fail(const char* error);
minunit_end_proc_timer - minunit_proc_timer);)
#define MU_EXIT_CODE minunit_fail

/* Warnings */
#define mu_warn(message) \
MU__SAFE_BLOCK(minunit_printf_warning("%s:%d: %s", __FILE__, __LINE__, message);)

/* Assertions */
#define mu_check(test) \
MU__SAFE_BLOCK( \
Expand Down
2 changes: 1 addition & 1 deletion applications/debug/unit_tests/storage/dirwalk_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static bool write_file_13DA(Storage* storage, const char* path) {
File* file = storage_file_alloc(storage);
bool result = false;
if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
result = storage_file_write(file, "13DA", 4) == 4;
result = (storage_file_write(file, "13DA", 4) == 4);
}
storage_file_close(file);
storage_file_free(file);
Expand Down
65 changes: 65 additions & 0 deletions applications/debug/unit_tests/storage/storage_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,77 @@ MU_TEST(storage_file_open_close) {
furi_record_close(RECORD_STORAGE);
}

static bool storage_file_read_write_test(File* file, uint8_t* data, size_t test_size) {
const char* filename = UNIT_TESTS_PATH("storage_chunk.test");

// fill with pattern
for(size_t i = 0; i < test_size; i++) {
data[i] = (i % 113);
}

bool result = false;
do {
if(!storage_file_open(file, filename, FSAM_WRITE, FSOM_CREATE_ALWAYS)) break;
if(test_size != storage_file_write(file, data, test_size)) break;
storage_file_close(file);

// reset data
memset(data, 0, test_size);

if(!storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) break;
if(test_size != storage_file_read(file, data, test_size)) break;
storage_file_close(file);

// check that data is correct
for(size_t i = 0; i < test_size; i++) {
if(data[i] != (i % 113)) {
break;
}
}

result = true;
} while(false);

return result;
}

MU_TEST(storage_file_read_write_64k) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);

size_t size_1k = 1024;
size_t size_64k = size_1k + size_1k * 63;
size_t size_65k = size_64k + size_1k;
size_t size_max = size_65k + 8;

size_t max_ram_block = memmgr_heap_get_max_free_block();

if(max_ram_block < size_max) {
mu_warn("Not enough RAM for >64k block test");
} else {
uint8_t* data = malloc(size_max);
mu_check(storage_file_read_write_test(file, data, size_1k));
mu_check(storage_file_read_write_test(file, data, size_64k));
mu_check(storage_file_read_write_test(file, data, size_65k));
mu_check(storage_file_read_write_test(file, data, size_max));
free(data);
}

storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}

MU_TEST_SUITE(storage_file) {
storage_file_open_lock_setup();
MU_RUN_TEST(storage_file_open_close);
MU_RUN_TEST(storage_file_open_lock);
storage_file_open_lock_teardown();
}

MU_TEST_SUITE(storage_file_64k) {
MU_RUN_TEST(storage_file_read_write_64k);
}

MU_TEST(storage_dir_open_close) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file;
Expand Down Expand Up @@ -640,6 +704,7 @@ MU_TEST_SUITE(test_md5_calc_suite) {

int run_minunit_test_storage() {
MU_RUN_SUITE(storage_file);
MU_RUN_SUITE(storage_file_64k);
MU_RUN_SUITE(storage_dir);
MU_RUN_SUITE(storage_rename);
MU_RUN_SUITE(test_data_path);
Expand Down
1 change: 1 addition & 0 deletions applications/debug/unit_tests/subghz/subghz_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ bool subghz_hal_async_tx_test_run(SubGhzHalAsyncTxTestType type) {
furi_hal_subghz_set_frequency_and_path(433920000);

if(!furi_hal_subghz_start_async_tx(subghz_hal_async_tx_test_yield, &test)) {
mu_warn("SubGHZ transmission is prohibited");
return false;
}

Expand Down
10 changes: 10 additions & 0 deletions applications/debug/unit_tests/test_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ void minunit_print_fail(const char* str) {
printf(_FURI_LOG_CLR_E "%s\r\n" _FURI_LOG_CLR_RESET, str);
}

void minunit_printf_warning(const char* format, ...) {
FuriString* str = furi_string_alloc();
va_list args;
va_start(args, format);
furi_string_vprintf(str, format, args);
va_end(args);
printf(_FURI_LOG_CLR_W "%s\r\n" _FURI_LOG_CLR_RESET, furi_string_get_cstr(str));
furi_string_free(str);
}

void unit_tests_cli(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
UNUSED(args);
Expand Down
4 changes: 2 additions & 2 deletions applications/main/archive/helpers/archive_favorites.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ static bool archive_favorites_read_line(File* file, FuriString* str_result) {
bool result = false;

do {
uint16_t read_count = storage_file_read(file, buffer, ARCHIVE_FAV_FILE_BUF_LEN);
size_t read_count = storage_file_read(file, buffer, ARCHIVE_FAV_FILE_BUF_LEN);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}

for(uint16_t i = 0; i < read_count; i++) {
for(size_t i = 0; i < read_count; i++) {
if(buffer[i] == '\n') {
uint32_t position = storage_file_tell(file);
if(storage_file_get_error(file) != FSE_OK) {
Expand Down
2 changes: 1 addition & 1 deletion applications/main/subghz/subghz_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ static void subghz_cli_command(Cli* cli, FuriString* args, void* context) {
static bool
subghz_on_system_start_istream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
File* file = istream->state;
uint16_t ret = storage_file_read(file, buf, count);
size_t ret = storage_file_read(file, buf, count);
return (count == ret);
}

Expand Down
4 changes: 2 additions & 2 deletions applications/services/notification/notification_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ static bool notification_load_settings(NotificationApp* app) {
storage_file_open(file, NOTIFICATION_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);

if(fs_result) {
uint16_t bytes_count = storage_file_read(file, &settings, settings_size);
size_t bytes_count = storage_file_read(file, &settings, settings_size);

if(bytes_count != settings_size) {
fs_result = false;
Expand Down Expand Up @@ -488,7 +488,7 @@ static bool notification_save_settings(NotificationApp* app) {
storage_file_open(file, NOTIFICATION_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);

if(fs_result) {
uint16_t bytes_count = storage_file_write(file, &settings, settings_size);
size_t bytes_count = storage_file_write(file, &settings, settings_size);

if(bytes_count != settings_size) {
fs_result = false;
Expand Down
2 changes: 1 addition & 1 deletion applications/services/rpc/rpc_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ static void rpc_system_storage_write_process(const PB_Main* request, void* conte
request->content.storage_write_request.file.data->size) {
uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
size_t buffer_size = request->content.storage_write_request.file.data->size;
uint16_t written_size = storage_file_write(file, buffer, buffer_size);
size_t written_size = storage_file_write(file, buffer, buffer_size);
fs_operation_success = (written_size == buffer_size);
}

Expand Down
6 changes: 3 additions & 3 deletions applications/services/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool storage_file_is_dir(File* file);
* @param bytes_to_read number of bytes to read. Must be less than or equal to the size of the buffer.
* @return actual number of bytes read (may be fewer than requested).
*/
uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read);
size_t storage_file_read(File* file, void* buff, size_t bytes_to_read);

/**
* @brief Write bytes from a buffer to a file.
Expand All @@ -133,7 +133,7 @@ uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read);
* @param bytes_to_write number of bytes to write. Must be less than or equal to the size of the buffer.
* @return actual number of bytes written (may be fewer than requested).
*/
uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write);
size_t storage_file_write(File* file, const void* buff, size_t bytes_to_write);

/**
* @brief Change the current access position in a file.
Expand Down Expand Up @@ -207,7 +207,7 @@ bool storage_file_exists(Storage* storage, const char* path);
* @param size data size to be copied, in bytes.
* @return true if the data was successfully copied, false otherwise.
*/
bool storage_file_copy_to_file(File* source, File* destination, uint32_t size);
bool storage_file_copy_to_file(File* source, File* destination, size_t size);

/******************* Directory Functions *******************/

Expand Down
22 changes: 11 additions & 11 deletions applications/services/storage/storage_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ static void storage_cli_read(Cli* cli, FuriString* path) {
File* file = storage_file_alloc(api);

if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
const uint16_t buffer_size = 128;
uint16_t read_size = 0;
const size_t buffer_size = 128;
size_t read_size = 0;
uint8_t* data = malloc(buffer_size);

printf("Size: %lu\r\n", (uint32_t)storage_file_size(file));

do {
read_size = storage_file_read(file, data, buffer_size);
for(uint16_t i = 0; i < read_size; i++) {
for(size_t i = 0; i < read_size; i++) {
printf("%c", data[i]);
}
} while(read_size > 0);
Expand All @@ -227,7 +227,7 @@ static void storage_cli_write(Cli* cli, FuriString* path) {
Storage* api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(api);

const uint16_t buffer_size = 512;
const size_t buffer_size = 512;
uint8_t* buffer = malloc(buffer_size);

if(storage_file_open(file, furi_string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
Expand All @@ -239,10 +239,10 @@ static void storage_cli_write(Cli* cli, FuriString* path) {
uint8_t symbol = cli_getc(cli);

if(symbol == CliSymbolAsciiETX) {
uint16_t write_size = read_index % buffer_size;
size_t write_size = read_index % buffer_size;

if(write_size > 0) {
uint16_t written_size = storage_file_write(file, buffer, write_size);
size_t written_size = storage_file_write(file, buffer, write_size);

if(written_size != write_size) {
storage_cli_print_error(storage_file_get_error(file));
Expand All @@ -257,7 +257,7 @@ static void storage_cli_write(Cli* cli, FuriString* path) {
read_index++;

if(((read_index % buffer_size) == 0)) {
uint16_t written_size = storage_file_write(file, buffer, buffer_size);
size_t written_size = storage_file_write(file, buffer, buffer_size);

if(written_size != buffer_size) {
storage_cli_print_error(storage_file_get_error(file));
Expand Down Expand Up @@ -289,16 +289,16 @@ static void storage_cli_read_chunks(Cli* cli, FuriString* path, FuriString* args
} else if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
uint64_t file_size = storage_file_size(file);

printf("Size: %lu\r\n", (uint32_t)file_size);
printf("Size: %llu\r\n", file_size);

if(buffer_size) {
uint8_t* data = malloc(buffer_size);
while(file_size > 0) {
printf("\r\nReady?\r\n");
cli_getc(cli);

uint16_t read_size = storage_file_read(file, data, buffer_size);
for(uint16_t i = 0; i < read_size; i++) {
size_t read_size = storage_file_read(file, data, buffer_size);
for(size_t i = 0; i < read_size; i++) {
putchar(data[i]);
}
file_size -= read_size;
Expand Down Expand Up @@ -335,7 +335,7 @@ static void storage_cli_write_chunk(Cli* cli, FuriString* path, FuriString* args

size_t read_bytes = cli_read(cli, buffer, buffer_size);

uint16_t written_size = storage_file_write(file, buffer, read_bytes);
size_t written_size = storage_file_write(file, buffer, read_bytes);

if(written_size != buffer_size) {
storage_cli_print_error(storage_file_get_error(file));
Expand Down
Loading

0 comments on commit 4b3e8ab

Please sign in to comment.