diff --git a/components/newlib/test_apps/main/CMakeLists.txt b/components/newlib/test_apps/main/CMakeLists.txt index 76686cf622..b54d173420 100644 --- a/components/newlib/test_apps/main/CMakeLists.txt +++ b/components/newlib/test_apps/main/CMakeLists.txt @@ -2,5 +2,6 @@ idf_component_register(SRCS "test_newlib_main.c" "test_stdatomic.c" "test_misc.c" + "test_file.c" REQUIRES test_utils - PRIV_REQUIRES unity) + PRIV_REQUIRES unity vfs) diff --git a/components/newlib/test_apps/main/test_file.c b/components/newlib/test_apps/main/test_file.c new file mode 100644 index 0000000000..e25df6b11a --- /dev/null +++ b/components/newlib/test_apps/main/test_file.c @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include +#include +#include "esp_vfs.h" +#include "unity.h" +#include "unity_fixture.h" + +TEST_GROUP(file); + +TEST_SETUP(file) +{ +} + +TEST_TEAR_DOWN(file) +{ +} + +/* This test checks that st_blksize value set in struct stat correctly affects the + * FILE structure: + * - _blksize field should be equal to st_blksize + * - buffer size should be equal to st_blksize if it is nonzero, and __BUFSIZ__ otherwise. + * This is more of an integration test since some of the functions responsible for this are + * in ROM, and have been built without HAVE_BLKSIZE feature for the ESP32 chip. + */ + +typedef struct { + unsigned blksize; +} blksize_test_ctx_t; + +static int blksize_test_open(void* ctx, const char * path, int flags, int mode) +{ + return 1; +} + +static int blksize_test_fstat(void* ctx, int fd, struct stat * st) +{ + blksize_test_ctx_t* test_ctx = (blksize_test_ctx_t*) ctx; + memset(st, 0, sizeof(*st)); + st->st_mode = S_IFREG; + st->st_blksize = test_ctx->blksize; + return 0; +} + +static ssize_t blksize_test_write(void* ctx, int fd, const void * data, size_t size) +{ + return size; +} + +TEST(file, blksize) +{ + FILE* f; + blksize_test_ctx_t ctx = {}; + const char c = 42; + const esp_vfs_t desc = { + .flags = ESP_VFS_FLAG_CONTEXT_PTR, + .open_p = blksize_test_open, + .fstat_p = blksize_test_fstat, + .write_p = blksize_test_write, + }; + + TEST_ESP_OK(esp_vfs_register("/test", &desc, &ctx)); + + /* test with zero st_blksize (=not set) */ + ctx.blksize = 0; + f = fopen("/test/path", "w"); + TEST_ASSERT_NOT_NULL(f); + fwrite(&c, 1, 1, f); + TEST_ASSERT_EQUAL(0, f->_blksize); + TEST_ASSERT_EQUAL(__BUFSIZ__, __fbufsize(f)); + fclose(f); + + /* test with non-zero st_blksize */ + ctx.blksize = 4096; + f = fopen("/test/path", "w"); + TEST_ASSERT_NOT_NULL(f); + fwrite(&c, 1, 1, f); + TEST_ASSERT_EQUAL(ctx.blksize, f->_blksize); + TEST_ASSERT_EQUAL(ctx.blksize, __fbufsize(f)); + fclose(f); + + TEST_ESP_OK(esp_vfs_unregister("/test")); +} + +TEST_GROUP_RUNNER(file) +{ + RUN_TEST_CASE(file, blksize) +} diff --git a/components/newlib/test_apps/main/test_newlib_main.c b/components/newlib/test_apps/main/test_newlib_main.c index 00de90fa06..a18a88d85a 100644 --- a/components/newlib/test_apps/main/test_newlib_main.c +++ b/components/newlib/test_apps/main/test_newlib_main.c @@ -10,6 +10,7 @@ static void run_all_tests(void) { RUN_TEST_GROUP(stdatomic); RUN_TEST_GROUP(misc); + RUN_TEST_GROUP(file); } void app_main(void)