summaryrefslogtreecommitdiff
path: root/lib/fatfs/test_apps
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fatfs/test_apps')
-rw-r--r--lib/fatfs/test_apps/.build-test-rules.yml48
-rw-r--r--lib/fatfs/test_apps/README.md4
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/CMakeLists.txt7
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/README.md8
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/main/CMakeLists.txt3
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/main/test_fatfs_dyn_buffers.c101
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/partitions.csv5
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/pytest_fatfs_dyn_buffers.py22
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.dyn_buffers1
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.no_dyn_buffers1
-rw-r--r--lib/fatfs/test_apps/dyn_buffers/sdkconfig.defaults14
-rw-r--r--lib/fatfs/test_apps/flash_ro/README.md4
-rw-r--r--lib/fatfs/test_apps/flash_ro/main/test_fatfs_flash_ro.c3
-rw-r--r--lib/fatfs/test_apps/flash_ro/pytest_fatfs_flash_ro.py12
-rw-r--r--lib/fatfs/test_apps/flash_wl/README.md4
-rw-r--r--lib/fatfs/test_apps/flash_wl/main/CMakeLists.txt2
-rw-r--r--lib/fatfs/test_apps/flash_wl/main/Kconfig.projbuild10
-rw-r--r--lib/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c92
-rw-r--r--lib/fatfs/test_apps/flash_wl/main/test_fatfs_small_partition.c76
-rw-r--r--lib/fatfs/test_apps/flash_wl/partitions.csv1
-rw-r--r--lib/fatfs/test_apps/flash_wl/pytest_fatfs_flash_wl.py27
-rw-r--r--lib/fatfs/test_apps/flash_wl/sdkconfig.ci.auto_fsync2
-rw-r--r--lib/fatfs/test_apps/flash_wl/sdkconfig.ci.dyn_buffers1
-rw-r--r--lib/fatfs/test_apps/flash_wl/sdkconfig.ci.psram.esp32 (renamed from lib/fatfs/test_apps/flash_wl/sdkconfig.ci.psram)1
-rw-r--r--lib/fatfs/test_apps/sdcard/README.md4
-rw-r--r--lib/fatfs/test_apps/sdcard/main/CMakeLists.txt2
-rw-r--r--lib/fatfs/test_apps/sdcard/main/test_fatfs_sdmmc.c275
-rw-r--r--lib/fatfs/test_apps/sdcard/main/test_fatfs_sdspi.c61
-rw-r--r--lib/fatfs/test_apps/sdcard/pytest_fatfs_sdcard.py48
-rw-r--r--lib/fatfs/test_apps/sdcard/sdkconfig.ci.psram.esp32 (renamed from lib/fatfs/test_apps/sdcard/sdkconfig.ci.psram)1
-rw-r--r--lib/fatfs/test_apps/test_fatfs_common/CMakeLists.txt2
-rw-r--r--lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c245
-rw-r--r--lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h10
33 files changed, 970 insertions, 127 deletions
diff --git a/lib/fatfs/test_apps/.build-test-rules.yml b/lib/fatfs/test_apps/.build-test-rules.yml
index d8277804..86cfac0d 100644
--- a/lib/fatfs/test_apps/.build-test-rules.yml
+++ b/lib/fatfs/test_apps/.build-test-rules.yml
@@ -1,11 +1,47 @@
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
-components/fatfs/test_apps/sdcard:
+components/fatfs/test_apps/dyn_buffers:
disable_test:
- - if: IDF_TARGET in ["esp32s3", "esp32c2", "esp32c6", "esp32h2"]
- temporary: true
- reason: No sdspi runners for these targets
+ - if: IDF_TARGET != "esp32"
+ reason: only one target required
+
+ depends_components:
+ - fatfs
+
+components/fatfs/test_apps/flash_ro:
+ disable_test:
+ - if: IDF_TARGET not in ["esp32", "esp32c3"]
+ reason: only one target per arch needed
+
+ depends_components:
+ - esp_partition
+ - spi_flash
+ - fatfs
+ - vfs
+
+components/fatfs/test_apps/flash_wl:
+ disable_test:
+ - if: IDF_TARGET not in ["esp32", "esp32c3", "linux"]
+ reason: only one target per arch needed
+ depends_components:
+ - esp_partition
+ - spi_flash
+ - fatfs
+ - vfs
+ - wear_leveling
+
+components/fatfs/test_apps/sdcard:
disable:
- - if: IDF_TARGET == "esp32p4"
+ - if: IDF_TARGET in ["esp32h21", "esp32h4"]
+ temporary: true
+ reason: not supported yet # TODO: [esp32h21] IDF-11593 [ESP32H4] IDF-12372
+ disable_test:
+ - if: IDF_TARGET not in ["esp32", "esp32c3"]
temporary: true
- reason: target esp32p4 is not supported yet # TODO: IDF-7501
+ reason: lack of runners
+ depends_components:
+ - esp_driver_sdmmc
+ - esp_driver_spi
+ - sdmmc
+ - fatfs
+ - vfs
diff --git a/lib/fatfs/test_apps/README.md b/lib/fatfs/test_apps/README.md
index 2140e8c4..78a9dcd9 100644
--- a/lib/fatfs/test_apps/README.md
+++ b/lib/fatfs/test_apps/README.md
@@ -1,3 +1,6 @@
+| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
+| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- |
+
# fatfs component target tests
This directory contains tests for `fatfs` component which are run on chip targets.
@@ -9,6 +12,7 @@ Fatfs tests can be executed with different `diskio` backends: `diskio_sdmmc` (SD
- [sdcard](sdcard/) — runs fatfs tests with an SD card over SDMMC or SDSPI interface
- [flash_wl](flash_wl/) - runs fatfs test in a wear_levelling partition in SPI flash
- [flash_ro](flash_ro/) - runs fatfs test in a read-only (no wear levelling) partition in SPI flash
+- [dyn_buffers](dyn_buffers/) - check if enabling dynamic buffers in FATFS has an effect
These test apps define:
- test functions
diff --git a/lib/fatfs/test_apps/dyn_buffers/CMakeLists.txt b/lib/fatfs/test_apps/dyn_buffers/CMakeLists.txt
new file mode 100644
index 00000000..4e6f01d2
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/CMakeLists.txt
@@ -0,0 +1,7 @@
+# The following five lines of boilerplate have to be in your project's
+# CMakeLists in this exact order for cmake to work correctly
+cmake_minimum_required(VERSION 3.16)
+
+include($ENV{IDF_PATH}/tools/cmake/project.cmake)
+idf_build_set_property(MINIMAL_BUILD ON)
+project(dyn_buffers)
diff --git a/lib/fatfs/test_apps/dyn_buffers/README.md b/lib/fatfs/test_apps/dyn_buffers/README.md
new file mode 100644
index 00000000..261aeee2
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/README.md
@@ -0,0 +1,8 @@
+| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
+| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | -------- |
+
+This test app checks if `CONFIG_FATFS_USE_DYN_BUFFERS` has any effect.
+
+These tests should be possible to run on any ESP development board, not extra hardware is necessary.
+
+See [../README.md](../README.md) for more information about FATFS test apps.
diff --git a/lib/fatfs/test_apps/dyn_buffers/main/CMakeLists.txt b/lib/fatfs/test_apps/dyn_buffers/main/CMakeLists.txt
new file mode 100644
index 00000000..c3cd1aa7
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/main/CMakeLists.txt
@@ -0,0 +1,3 @@
+idf_component_register(SRCS "test_fatfs_dyn_buffers.c"
+ INCLUDE_DIRS "."
+ REQUIRES wear_levelling fatfs vfs)
diff --git a/lib/fatfs/test_apps/dyn_buffers/main/test_fatfs_dyn_buffers.c b/lib/fatfs/test_apps/dyn_buffers/main/test_fatfs_dyn_buffers.c
new file mode 100644
index 00000000..c945d97b
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/main/test_fatfs_dyn_buffers.c
@@ -0,0 +1,101 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Unlicense OR CC0-1.0
+ */
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "wear_levelling.h"
+#include "esp_partition.h"
+#include "esp_vfs.h"
+#include "esp_vfs_fat.h"
+#include "esp_heap_caps.h"
+#include "sdkconfig.h"
+#include "ff.h"
+#include "esp_debug_helpers.h"
+
+static const char* TAG = "Test dynamic buffers";
+
+static volatile bool g_alloc_count_enable = false;
+static volatile int g_buffer_alloc_count = 0;
+
+static esp_vfs_fat_mount_config_t g_mount_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+};
+
+void esp_heap_trace_alloc_hook(void* ptr, size_t size, uint32_t caps)
+{
+ (void) ptr;
+ (void) caps;
+
+ if (!g_alloc_count_enable) {
+ return;
+ }
+
+ // This will work only on SPI flash
+ // Different flash types might break this check
+ if (size == FF_MAX_SS) {
+ g_buffer_alloc_count++;
+ }
+}
+
+void app_main(void)
+{
+ esp_err_t err = ESP_OK;
+
+ wl_handle_t wl_handle;
+
+ err = esp_vfs_fat_spiflash_format_cfg_rw_wl("/spiflash", NULL, &g_mount_config);
+
+ ESP_LOGI(TAG, "Mounting FATFS");
+
+ g_mount_config.format_if_mount_failed = false,
+
+ g_alloc_count_enable = true;
+
+ err = esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &g_mount_config, &wl_handle);
+ if (err != ESP_OK) {
+ ESP_LOGE(TAG, "FATFS mount failed with error: %d", err);
+ return;
+ }
+
+ ESP_LOGI(TAG, "Mounted");
+
+ int fd = open("/spiflash/test.txt", O_RDWR|O_CREAT);
+ if (fd < 0) {
+ ESP_LOGE(TAG, "Failed opening file");
+ }
+
+ close(fd);
+
+ g_alloc_count_enable = false;
+
+ ESP_LOGI(TAG, "Unmounting FATFS");
+
+ esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle);
+
+ ESP_LOGI(TAG, "Unmounted");
+
+ ESP_LOGI(TAG, "Allocs called:\n\tBuffer: %d"
+ , g_buffer_alloc_count);
+
+#if CONFIG_FATFS_USE_DYN_BUFFERS
+
+ if (g_buffer_alloc_count != 2) {
+ ESP_LOGE(TAG, "FATFS buffer should have been allocated once for each context (file and fatfs)");
+ return;
+ }
+#else
+
+ if (g_buffer_alloc_count != 0) {
+ ESP_LOGE(TAG, "FATFS buffer should not have been allocated");
+ return;
+ }
+
+#endif
+
+ ESP_LOGI(TAG, "Done");
+}
diff --git a/lib/fatfs/test_apps/dyn_buffers/partitions.csv b/lib/fatfs/test_apps/dyn_buffers/partitions.csv
new file mode 100644
index 00000000..d68a9de0
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/partitions.csv
@@ -0,0 +1,5 @@
+# Name, Type, SubType, Offset, Size, Flags
+factory, app, factory, 0x10000, 768k,
+storage, data, fat, , 528k,
+storage2, data, fat, , 528k,
+storage1, data, fat, , 32k,
diff --git a/lib/fatfs/test_apps/dyn_buffers/pytest_fatfs_dyn_buffers.py b/lib/fatfs/test_apps/dyn_buffers/pytest_fatfs_dyn_buffers.py
new file mode 100644
index 00000000..a60ac87e
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/pytest_fatfs_dyn_buffers.py
@@ -0,0 +1,22 @@
+# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
+# SPDX-License-Identifier: CC0-1.0
+import pytest
+from pytest_embedded import Dut
+from pytest_embedded_idf.utils import idf_parametrize
+
+
+@pytest.mark.generic
+@pytest.mark.parametrize(
+ 'config',
+ [
+ 'dyn_buffers',
+ 'no_dyn_buffers',
+ ],
+)
+@idf_parametrize('target', ['esp32'], indirect=['target'])
+def test_fatfs_flash_dyn_buffers(config: str, dut: Dut) -> None:
+ dut.expect('Mounting FATFS')
+ dut.expect('Mounted')
+ dut.expect('Unmounting FATFS')
+ dut.expect('Unmounted')
+ dut.expect('Done')
diff --git a/lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.dyn_buffers b/lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.dyn_buffers
new file mode 100644
index 00000000..380dafbf
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.dyn_buffers
@@ -0,0 +1 @@
+CONFIG_FATFS_USE_DYN_BUFFERS=y
diff --git a/lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.no_dyn_buffers b/lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.no_dyn_buffers
new file mode 100644
index 00000000..bc9b68f3
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/sdkconfig.ci.no_dyn_buffers
@@ -0,0 +1 @@
+CONFIG_FATFS_USE_DYN_BUFFERS=n
diff --git a/lib/fatfs/test_apps/dyn_buffers/sdkconfig.defaults b/lib/fatfs/test_apps/dyn_buffers/sdkconfig.defaults
new file mode 100644
index 00000000..8b57aacd
--- /dev/null
+++ b/lib/fatfs/test_apps/dyn_buffers/sdkconfig.defaults
@@ -0,0 +1,14 @@
+# General options for additional checks
+CONFIG_HEAP_POISONING_COMPREHENSIVE=y
+CONFIG_COMPILER_WARN_WRITE_STRINGS=y
+CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
+CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
+CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
+CONFIG_COMPILER_STACK_CHECK=y
+
+# use custom partition table
+CONFIG_PARTITION_TABLE_CUSTOM=y
+CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
+
+# to measure allocations
+CONFIG_HEAP_USE_HOOKS=y
diff --git a/lib/fatfs/test_apps/flash_ro/README.md b/lib/fatfs/test_apps/flash_ro/README.md
index 4d341fbc..90297186 100644
--- a/lib/fatfs/test_apps/flash_ro/README.md
+++ b/lib/fatfs/test_apps/flash_ro/README.md
@@ -1,5 +1,5 @@
-| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
-| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
+| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
+| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | -------- |
This test app runs a few FATFS test cases in a read-only FAT partition.
diff --git a/lib/fatfs/test_apps/flash_ro/main/test_fatfs_flash_ro.c b/lib/fatfs/test_apps/flash_ro/main/test_fatfs_flash_ro.c
index 61786bfe..cca3dc0d 100644
--- a/lib/fatfs/test_apps/flash_ro/main/test_fatfs_flash_ro.c
+++ b/lib/fatfs/test_apps/flash_ro/main/test_fatfs_flash_ro.c
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
+#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -263,7 +264,7 @@ TEST_CASE("(raw) multiple tasks can use same volume", "[fatfs]")
read_test_arg_t args4 = READ_TEST_ARG_INIT(names[3], 0x34343434);
const int cpuid_0 = 0;
- const int cpuid_1 = portNUM_PROCESSORS - 1;
+ const int cpuid_1 = CONFIG_FREERTOS_NUMBER_OF_CORES - 1;
const int stack_size = 4096;
printf("reading files 1.txt 2.txt 3.txt 4.txt \n");
diff --git a/lib/fatfs/test_apps/flash_ro/pytest_fatfs_flash_ro.py b/lib/fatfs/test_apps/flash_ro/pytest_fatfs_flash_ro.py
index eda296ef..0339f8b6 100644
--- a/lib/fatfs/test_apps/flash_ro/pytest_fatfs_flash_ro.py
+++ b/lib/fatfs/test_apps/flash_ro/pytest_fatfs_flash_ro.py
@@ -1,15 +1,11 @@
-# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
-
import pytest
from pytest_embedded import Dut
+from pytest_embedded_idf.utils import idf_parametrize
-@pytest.mark.supported_targets
@pytest.mark.generic
+@idf_parametrize('target', ['esp32', 'esp32c3'], indirect=['target'])
def test_fatfs_flash_ro(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('*')
- dut.expect_unity_test_output()
+ dut.run_all_single_board_cases()
diff --git a/lib/fatfs/test_apps/flash_wl/README.md b/lib/fatfs/test_apps/flash_wl/README.md
index 9112db39..a90777a3 100644
--- a/lib/fatfs/test_apps/flash_wl/README.md
+++ b/lib/fatfs/test_apps/flash_wl/README.md
@@ -1,5 +1,5 @@
-| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
-| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
+| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
+| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | -------- |
This test app runs a few FATFS test cases in a wear levelling FAT partition.
diff --git a/lib/fatfs/test_apps/flash_wl/main/CMakeLists.txt b/lib/fatfs/test_apps/flash_wl/main/CMakeLists.txt
index 20b600fc..f6501bda 100644
--- a/lib/fatfs/test_apps/flash_wl/main/CMakeLists.txt
+++ b/lib/fatfs/test_apps/flash_wl/main/CMakeLists.txt
@@ -1,4 +1,4 @@
-idf_component_register(SRCS "test_fatfs_flash_wl.c"
+idf_component_register(SRCS "test_fatfs_flash_wl.c" "test_fatfs_small_partition.c"
INCLUDE_DIRS "."
PRIV_REQUIRES unity spi_flash fatfs vfs test_fatfs_common
WHOLE_ARCHIVE)
diff --git a/lib/fatfs/test_apps/flash_wl/main/Kconfig.projbuild b/lib/fatfs/test_apps/flash_wl/main/Kconfig.projbuild
new file mode 100644
index 00000000..c7292513
--- /dev/null
+++ b/lib/fatfs/test_apps/flash_wl/main/Kconfig.projbuild
@@ -0,0 +1,10 @@
+menu "Test configuration"
+ config SPI_WL_TEST_ERASE_PARTITION
+ bool "Erase partition"
+ default y if IDF_TARGET_LINUX
+ help
+ Erase the partition before each format operation.
+ This will destroy the flash fairly quickly in CI, but is necessary to
+ ensure that the test is not affected by previous test runs.
+ Run with caution.
+endmenu
diff --git a/lib/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c b/lib/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c
index 1af50fe6..fa383803 100644
--- a/lib/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c
+++ b/lib/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c
@@ -1,5 +1,5 @@
/*
- * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -23,6 +23,7 @@
#include "wear_levelling.h"
#include "esp_partition.h"
#include "esp_memory_utils.h"
+#include "vfs_fat_internal.h"
void app_main(void)
{
@@ -32,7 +33,7 @@ void app_main(void)
static wl_handle_t s_test_wl_handle;
static void test_setup(void)
{
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+ esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
};
@@ -45,14 +46,47 @@ static void test_teardown(void)
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", s_test_wl_handle));
}
-TEST_CASE("(WL) can format partition", "[fatfs][wear_levelling][timeout=180]")
+#ifdef CONFIG_SPI_WL_TEST_ERASE_PARTITION
+static void corrupt_wl_data(void)
{
+ const esp_partition_t* part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
+ TEST_ASSERT_NOT_NULL(part);
+ TEST_ESP_OK(esp_partition_erase_range(part, 0, part->size));
+}
+#endif
+
+TEST_CASE("(WL) can format partition", "[fatfs][wear_levelling][timeout=120]")
+{
+#ifdef CONFIG_SPI_WL_TEST_ERASE_PARTITION
+ corrupt_wl_data();
+#endif
TEST_ESP_OK(esp_vfs_fat_spiflash_format_rw_wl("/spiflash", NULL));
test_setup();
+ vfs_fat_spiflash_ctx_t* ctx = get_vfs_fat_spiflash_ctx(s_test_wl_handle);
+ TEST_ASSERT_NOT_NULL(ctx);
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 2); // 2 FATs are created by default
+ test_teardown();
+}
+
+TEST_CASE("(WL) can format partition with config", "[fatfs][wear_levelling][timeout=120]")
+{
+#ifdef CONFIG_SPI_WL_TEST_ERASE_PARTITION
+ corrupt_wl_data();
+#endif
+ esp_vfs_fat_mount_config_t format_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+ .use_one_fat = true,
+ };
+ TEST_ESP_OK(esp_vfs_fat_spiflash_format_cfg_rw_wl("/spiflash", NULL, &format_config));
+ test_setup();
+ vfs_fat_spiflash_ctx_t* ctx = get_vfs_fat_spiflash_ctx(s_test_wl_handle);
+ TEST_ASSERT_NOT_NULL(ctx);
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 1);
test_teardown();
}
-TEST_CASE("(WL) can format when the FAT is mounted already", "[fatfs][wear_levelling][timeout=180]")
+TEST_CASE("(WL) can format when the FAT is mounted already", "[fatfs][wear_levelling][timeout=120]")
{
test_setup();
TEST_ESP_OK(esp_vfs_fat_spiflash_format_rw_wl("/spiflash", NULL));
@@ -61,9 +95,28 @@ TEST_CASE("(WL) can format when the FAT is mounted already", "[fatfs][wear_level
test_teardown();
}
-TEST_CASE("(WL) can format specified FAT when more are mounted", "[fatfs][wear_levelling][timeout=180]")
+TEST_CASE("(WL) can format when the FAT is mounted already with config", "[fatfs][wear_levelling][timeout=120]")
+{
+ TEST_ESP_OK(esp_vfs_fat_spiflash_format_rw_wl("/spiflash", NULL)); // To reset the FAT number to 2
+ test_setup();
+ vfs_fat_spiflash_ctx_t* ctx = get_vfs_fat_spiflash_ctx(s_test_wl_handle);
+ TEST_ASSERT_NOT_NULL(ctx);
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 2);
+ esp_vfs_fat_mount_config_t format_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+ .use_one_fat = true,
+ };
+ TEST_ESP_OK(esp_vfs_fat_spiflash_format_cfg_rw_wl("/spiflash", NULL, &format_config));
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 1);
+ test_fatfs_create_file_with_text("/spiflash/hello.txt", fatfs_test_hello_str);
+ test_fatfs_pread_file("/spiflash/hello.txt");
+ test_teardown();
+}
+
+TEST_CASE("(WL) can format specified FAT when more are mounted", "[fatfs][wear_levelling][timeout=120]")
{
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+ esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
};
@@ -126,7 +179,7 @@ TEST_CASE("(WL) pwrite() works well", "[fatfs][wear_levelling]")
TEST_CASE("(WL) can open maximum number of files", "[fatfs][wear_levelling]")
{
size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+ esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = max_files
};
@@ -152,9 +205,25 @@ TEST_CASE("(WL) can lseek", "[fatfs][wear_levelling]")
TEST_CASE("(WL) can truncate", "[fatfs][wear_levelling]")
{
test_setup();
- test_fatfs_truncate_file("/spiflash/truncate.txt");
+ test_fatfs_truncate_file("/spiflash/truncate.txt", true);
+ test_teardown();
+}
+
+TEST_CASE("(WL) can ftruncate", "[fatfs][wear_levelling]")
+{
+ test_setup();
+ test_fatfs_ftruncate_file("/spiflash/ftrunc.txt", true);
+ test_teardown();
+}
+
+#if FF_USE_EXPAND
+TEST_CASE("(WL) can esp_vfs_fat_create_contiguous_file", "[fatfs][wear_levelling]")
+{
+ test_setup();
+ test_fatfs_create_contiguous_file("/spiflash", "/spiflash/expand.txt");
test_teardown();
}
+#endif
TEST_CASE("(WL) stat returns correct values", "[fatfs][wear_levelling]")
{
@@ -205,6 +274,13 @@ TEST_CASE("(WL) can opendir root directory of FS", "[fatfs][wear_levelling]")
test_teardown();
}
+TEST_CASE("(WL) readdir, stat work as expected", "[fatfs][wear_levelling]")
+{
+ test_setup();
+ test_fatfs_readdir_stat("/spiflash/dir");
+ test_teardown();
+}
+
TEST_CASE("(WL) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][wear_levelling]")
{
test_setup();
diff --git a/lib/fatfs/test_apps/flash_wl/main/test_fatfs_small_partition.c b/lib/fatfs/test_apps/flash_wl/main/test_fatfs_small_partition.c
new file mode 100644
index 00000000..fd65a4db
--- /dev/null
+++ b/lib/fatfs/test_apps/flash_wl/main/test_fatfs_small_partition.c
@@ -0,0 +1,76 @@
+/*
+ * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/unistd.h>
+#include "unity.h"
+#include "esp_vfs_fat.h"
+
+static wl_handle_t s_test_wl_handle;
+static void test_setup(void)
+{
+ // With this configuration, for 32k partition size,
+ // 4 sectors will be used for WL and 4 sectors for FATFS
+ // (1 FAT, 1 root directory, 1 reserved and 1 data sector)
+ esp_vfs_fat_mount_config_t mount_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+ .use_one_fat = true,
+ };
+
+ TEST_ESP_OK(esp_vfs_fat_spiflash_format_cfg_rw_wl("/spiflash", "storage1", &mount_config));
+ TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", "storage1", &mount_config, &s_test_wl_handle));
+}
+
+static void test_teardown(void)
+{
+ TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", s_test_wl_handle));
+}
+
+static void test_write_data_sec(int num_data_sec)
+{
+ int fd = open("/spiflash/test.txt", O_CREAT | O_WRONLY);
+ TEST_ASSERT_NOT_EQUAL(-1, fd);
+
+ // Generate data
+ uint32_t data_size = 4096*num_data_sec;
+
+ char *data = (char*) malloc(data_size);
+ char *read_data = (char*) malloc(data_size);
+
+ for(uint32_t i = 0; i < (data_size); i += sizeof(i))
+ {
+ *((uint32_t*)(data + i)) = i;
+ }
+ ssize_t wr = write(fd, data, data_size);
+ if (num_data_sec == 1) {
+ TEST_ASSERT_EQUAL(data_size, wr);
+ } else {
+ TEST_ASSERT_NOT_EQUAL(data_size, wr);
+ }
+ TEST_ASSERT_EQUAL(0, close(fd));
+
+ fd = open("/spiflash/test.txt", O_RDONLY);
+ int r = read(fd, read_data, data_size);
+ if (num_data_sec == 1) {
+ TEST_ASSERT_EQUAL(data_size, r);
+ } else {
+ TEST_ASSERT_NOT_EQUAL(data_size, r);
+ }
+ TEST_ASSERT_EQUAL(0, strcmp(data, read_data));
+ TEST_ASSERT_EQUAL(0, close(fd));
+}
+
+TEST_CASE("(WL) can format small partition and read-write data", "[fatfs][wear_levelling][timeout=120]")
+{
+ test_setup();
+ test_write_data_sec(1); //for 1 data sectors, write and read func should work
+ test_write_data_sec(2); //for 2 data sectors, write and read func should fail
+ test_teardown();
+}
diff --git a/lib/fatfs/test_apps/flash_wl/partitions.csv b/lib/fatfs/test_apps/flash_wl/partitions.csv
index d1dcbae6..d68a9de0 100644
--- a/lib/fatfs/test_apps/flash_wl/partitions.csv
+++ b/lib/fatfs/test_apps/flash_wl/partitions.csv
@@ -2,3 +2,4 @@
factory, app, factory, 0x10000, 768k,
storage, data, fat, , 528k,
storage2, data, fat, , 528k,
+storage1, data, fat, , 32k,
diff --git a/lib/fatfs/test_apps/flash_wl/pytest_fatfs_flash_wl.py b/lib/fatfs/test_apps/flash_wl/pytest_fatfs_flash_wl.py
index af8c5db6..a9863635 100644
--- a/lib/fatfs/test_apps/flash_wl/pytest_fatfs_flash_wl.py
+++ b/lib/fatfs/test_apps/flash_wl/pytest_fatfs_flash_wl.py
@@ -1,11 +1,10 @@
-# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
+# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
-
import pytest
from pytest_embedded import Dut
+from pytest_embedded_idf.utils import idf_parametrize
-@pytest.mark.supported_targets
@pytest.mark.generic
@pytest.mark.parametrize(
'config',
@@ -13,27 +12,23 @@ from pytest_embedded import Dut
'default',
'release',
'fastseek',
- ]
+ 'auto_fsync',
+ 'dyn_buffers',
+ ],
)
+@idf_parametrize('target', ['esp32', 'esp32c3'], indirect=['target'])
def test_fatfs_flash_wl_generic(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('*')
- dut.expect_unity_test_output(timeout=180)
+ dut.run_all_single_board_cases(timeout=240)
-@pytest.mark.supported_targets
+@pytest.mark.generic
@pytest.mark.psram
@pytest.mark.parametrize(
'config',
[
'psram',
- ]
+ ],
)
+@idf_parametrize('target', ['esp32'], indirect=['target'])
def test_fatfs_flash_wl_psram(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('*')
- dut.expect_unity_test_output(timeout=180)
+ dut.run_all_single_board_cases(timeout=180)
diff --git a/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.auto_fsync b/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.auto_fsync
index b74d5124..9ab32baa 100644
--- a/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.auto_fsync
+++ b/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.auto_fsync
@@ -1 +1,3 @@
CONFIG_FATFS_IMMEDIATE_FSYNC=y
+CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
+CONFIG_FATFS_VFS_FSTAT_BLKSIZE=2048
diff --git a/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.dyn_buffers b/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.dyn_buffers
new file mode 100644
index 00000000..380dafbf
--- /dev/null
+++ b/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.dyn_buffers
@@ -0,0 +1 @@
+CONFIG_FATFS_USE_DYN_BUFFERS=y
diff --git a/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.psram b/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.psram.esp32
index b3b45db9..5becfb94 100644
--- a/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.psram
+++ b/lib/fatfs/test_apps/flash_wl/sdkconfig.ci.psram.esp32
@@ -1,3 +1,4 @@
+CONFIG_IDF_TARGET="esp32"
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0
CONFIG_FATFS_ALLOC_PREFER_EXTRAM=y
diff --git a/lib/fatfs/test_apps/sdcard/README.md b/lib/fatfs/test_apps/sdcard/README.md
index 639f1d38..381b38a9 100644
--- a/lib/fatfs/test_apps/sdcard/README.md
+++ b/lib/fatfs/test_apps/sdcard/README.md
@@ -1,5 +1,5 @@
-| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
-| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |
+| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
+| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- |
This test app runs a few FATFS test cases in a FAT-formatted SD card.
diff --git a/lib/fatfs/test_apps/sdcard/main/CMakeLists.txt b/lib/fatfs/test_apps/sdcard/main/CMakeLists.txt
index be2a969a..e4a519cb 100644
--- a/lib/fatfs/test_apps/sdcard/main/CMakeLists.txt
+++ b/lib/fatfs/test_apps/sdcard/main/CMakeLists.txt
@@ -1,6 +1,6 @@
idf_component_register(SRCS "test_fatfs_sdcard_main.c" "test_fatfs_sdspi.c"
INCLUDE_DIRS "."
- PRIV_REQUIRES unity fatfs vfs sdmmc driver test_fatfs_common
+ PRIV_REQUIRES unity fatfs vfs sdmmc driver test_fatfs_common esp_timer
WHOLE_ARCHIVE)
if(CONFIG_SOC_SDMMC_HOST_SUPPORTED)
diff --git a/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdmmc.c b/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdmmc.c
index 4504a5d6..e6478ad9 100644
--- a/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdmmc.c
+++ b/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdmmc.c
@@ -1,5 +1,5 @@
/*
- * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -13,6 +13,7 @@
#include "unity.h"
#include "esp_log.h"
#include "esp_random.h"
+#include "esp_timer.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "freertos/FreeRTOS.h"
@@ -22,6 +23,7 @@
#include "ff.h"
#include "test_fatfs_common.h"
#include "soc/soc_caps.h"
+#include "vfs_fat_internal.h"
#if CONFIG_IDF_TARGET_ESP32
#define SDSPI_MISO_PIN 2
@@ -50,6 +52,7 @@
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
// No runner
#include "driver/sdmmc_host.h"
+const char* base_path = "/sdcard";
static void test_setup_sdmmc(sdmmc_card_t **out_card)
{
@@ -61,7 +64,7 @@ static void test_setup_sdmmc(sdmmc_card_t **out_card)
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
- TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));
+ TEST_ESP_OK(esp_vfs_fat_sdmmc_mount(base_path, &host, &slot_config, &mount_config, &card));
*out_card = card;
}
@@ -102,6 +105,35 @@ TEST_CASE("(SD) can format partition", "[fatfs][sdmmc][timeout=180]")
test_teardown_sdmmc(card);
}
+TEST_CASE("(SD) can format partition with config", "[fatfs][sdmmc][timeout=180]")
+{
+ sdmmc_card_t *card = NULL;
+ test_setup_sdmmc(&card);
+ vfs_fat_sd_ctx_t* ctx = get_vfs_fat_get_sd_ctx(card);
+ TEST_ASSERT_NOT_NULL(ctx);
+
+ esp_vfs_fat_mount_config_t format_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+ .allocation_unit_size = 16 * 1024,
+ .use_one_fat = true,
+ };
+ TEST_ESP_OK(esp_vfs_fat_sdcard_format_cfg("/sdcard", card, &format_config));
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 1);
+
+ test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
+ test_fatfs_read_file(test_filename);
+
+ format_config.use_one_fat = false;
+ TEST_ESP_OK(esp_vfs_fat_sdcard_format_cfg("/sdcard", card, &format_config));
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 2);
+
+ test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
+ test_fatfs_read_file(test_filename);
+
+ test_teardown_sdmmc(card);
+}
+
TEST_CASE("(SD) can create and write file", "[fatfs][sdmmc]")
{
sdmmc_card_t *card = NULL;
@@ -156,7 +188,7 @@ TEST_CASE("(SD) can truncate", "[fatfs][sdmmc]")
{
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
- test_fatfs_truncate_file("/sdcard/truncate.txt");
+ test_fatfs_truncate_file("/sdcard/truncate.txt", true);
test_teardown_sdmmc(card);
}
@@ -164,9 +196,19 @@ TEST_CASE("(SD) can ftruncate", "[fatfs][sdmmc]")
{
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
- test_fatfs_ftruncate_file("/sdcard/ftrunc.txt");
+ test_fatfs_ftruncate_file("/sdcard/ftrunc.txt", true);
+ test_teardown_sdmmc(card);
+}
+
+#if FF_USE_EXPAND
+TEST_CASE("(SD) can esp_vfs_fat_create_contiguous_file", "[fatfs][sdmmc]")
+{
+ sdmmc_card_t *card = NULL;
+ test_setup_sdmmc(&card);
+ test_fatfs_create_contiguous_file("/sdcard", "/sdcard/expand.txt");
test_teardown_sdmmc(card);
}
+#endif
TEST_CASE("(SD) stat returns correct values", "[fatfs][sdmmc]")
{
@@ -275,6 +317,231 @@ static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
}
+TEST_CASE("(SD) mount FAT partitions and readdir to get stat structure", "[fatfs][sdmmc]")
+{
+ char name_dir_file[64];
+ char name_dir_stat[64] = {0};
+ const char* dir_prefix = "/sdcard";
+ int dir_prefix_len = strlen(dir_prefix);
+ int file_num = 300;
+
+ /* Mount FATFS in SD can WL at the same time. Create a file on each FS */
+ sdmmc_card_t* card = NULL;
+ test_setup_sdmmc(&card);
+ TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
+
+ //Create multiple files with text on sdcard. Each file size is 14 bytes
+ //Total files created are file_num (300 in this case)
+ //So directory size will be 300*14 bytes
+ for(int i=0;i<file_num;i++) {
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_%d.bin", dir_prefix, i);
+ test_fatfs_create_file_with_text(name_dir_file, fatfs_test_hello_str);
+ }
+
+ //Start the timer to get time needed to calculate the directory size
+ int64_t start = esp_timer_get_time();
+ DIR* dir = opendir(dir_prefix);
+ TEST_ASSERT_NOT_NULL(dir);
+ struct stat st;
+ struct dirent* de;
+ uint32_t dir_size = 0;
+
+ // Call readdir before stat function and record the time needed to calculate the directory size
+ while(1) {
+ de = readdir(dir);
+ if (!de) {
+ break;
+ }
+ snprintf(name_dir_stat, dir_prefix_len+sizeof(de->d_name)+1, "%s/%s", dir_prefix, de->d_name);
+ TEST_ASSERT_EQUAL(0, stat(name_dir_stat, &st));
+ dir_size += st.st_size;
+ }
+ TEST_ASSERT_EQUAL(0, closedir(dir));
+ int64_t end = esp_timer_get_time();
+ int64_t total_time_readdir = end-start;
+ printf("Time in us for calculating directory size by calling readdir first and then stat func: %lld \n",total_time_readdir);
+ printf("Size of the directory %s is %"PRIu32"Kb\n", dir_prefix, (dir_size/1000));
+ TEST_ASSERT_EQUAL(file_num*strlen(fatfs_test_hello_str), dir_size); //each file size is 14 bytes
+
+ // Call stat function directly without calling readdir and record the time needed to calculate the directory size
+ dir_size = 0;
+ start = esp_timer_get_time();
+ for(int i=0;i<file_num;i++) {
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_%d.bin", dir_prefix, i);
+ TEST_ASSERT_EQUAL(0, stat(name_dir_file, &st));
+ dir_size += st.st_size;
+ }
+ end = esp_timer_get_time();
+ int64_t total_time_stat = end-start;
+ printf("Time in us for calculating directory size by calling stat func: %lld \n",total_time_stat);
+ printf("Size of the directory %s is %"PRIu32"Kb\n", dir_prefix, (dir_size/1000));
+ printf("%d\n", strlen(fatfs_test_hello_str));
+ TEST_ASSERT_EQUAL(file_num*strlen(fatfs_test_hello_str), dir_size); //each file size is 14 bytes
+
+ for(int i=0;i<file_num;i++) {
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_%d.bin", dir_prefix,i);
+ unlink(name_dir_file);
+ }
+
+ test_teardown_sdmmc(card);
+}
+
+typedef struct {
+ const char *dir;
+ const char *filename;
+ const char *str;
+ SemaphoreHandle_t sem;
+} test_task_param_t;
+
+static void test_task(void *param)
+{
+ DIR* dir = NULL;
+ struct dirent* de = NULL;
+ struct stat st;
+ char name_dir_stat[64] = {0};
+ const test_task_param_t *test_task_param = param;
+
+ dir = opendir(test_task_param->dir);
+ TEST_ASSERT_NOT_NULL(dir);
+ while(1) {
+ de = readdir(dir);
+ if (!de) {
+ break;
+ }
+ //Intentionally introduced a delay to ensure that the second task is triggered simultaneously.
+ vTaskDelay(10 / portTICK_PERIOD_MS);
+ snprintf(name_dir_stat, sizeof(test_task_param->dir)+sizeof(de->d_name), "%s/%s", test_task_param->dir, de->d_name);
+ TEST_ASSERT_EQUAL(0, stat(name_dir_stat, &st));
+ if (strcasecmp(de->d_name, test_task_param->filename) == 0) {
+ TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
+ TEST_ASSERT_EQUAL(strlen(test_task_param->str), st.st_size);
+ } else {
+ TEST_FAIL_MESSAGE("unexpected directory entry");
+ }
+ }
+
+ if (test_task_param->sem) {
+ xSemaphoreGive(test_task_param->sem);
+ }
+ vTaskDelete(NULL);
+}
+
+TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time and readdir to get stat structure", "[fatfs][sdmmc]")
+{
+ esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5
+ };
+
+ const char *dir_prefix[FF_VOLUMES] = {"/sdcard", "/spiflash"};
+ const char *dir_filename[FF_VOLUMES] = {"sd.txt", "wl.txt"};
+ const char* str[FF_VOLUMES] = {"this is sd\n", "this is spiflash\n"};
+ const char* filename_sd = "/sdcard/sd.txt";
+ const char* filename_wl = "/spiflash/wl.txt";
+
+ /* Erase flash before the first use */
+ const esp_partition_t *test_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
+ TEST_ASSERT_NOT_NULL(test_partition);
+ esp_partition_erase_range(test_partition, 0, test_partition->size);
+
+ /* Mount FATFS in SD can WL at the same time. Create a file on each FS */
+ wl_handle_t wl_handle = WL_INVALID_HANDLE;
+ sdmmc_card_t *card = NULL;
+ test_setup_sdmmc(&card);
+ TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
+ unlink(filename_sd);
+ unlink(filename_wl);
+ test_fatfs_create_file_with_text(filename_sd, str[0]);
+ test_fatfs_create_file_with_text(filename_wl, str[1]);
+
+ test_task_param_t test_task_param_sd = {
+ .dir = dir_prefix[0],
+ .filename = dir_filename[0],
+ .str = str[0],
+ .sem = xSemaphoreCreateBinary(),
+ };
+
+ test_task_param_t test_task_param_spiflash = {
+ .dir = dir_prefix[1],
+ .filename = dir_filename[1],
+ .str = str[1],
+ .sem = xSemaphoreCreateBinary(),
+ };
+
+ //Create two tasks with same priority to check file size on two different FAT partitions at the same time
+ xTaskCreate(test_task, "test_task_1", 8*1024, (void *) &test_task_param_sd, 5, NULL);
+ xTaskCreate(test_task, "test_task_2", 8*1024, (void *) &test_task_param_spiflash, 5, NULL);
+
+ TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(test_task_param_sd.sem, 1000 / portTICK_PERIOD_MS));
+ vSemaphoreDelete(test_task_param_sd.sem);
+ TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(test_task_param_spiflash.sem, 1000 / portTICK_PERIOD_MS));
+ vSemaphoreDelete(test_task_param_spiflash.sem);
+ TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle));
+ test_teardown_sdmmc(card);
+}
+
+TEST_CASE("(SD) read two directories and get stat structure for respective file at the same time", "[fatfs][sdmmc]")
+{
+ char name_dir_file[64];
+ char name_dir1_stat[64] = {0};
+ char name_dir2_stat[64] = {0};
+ const char* dir1_prefix = "/sdcard/dir1";
+ const char* dir2_prefix = "/sdcard/dir2";
+ int dir1_prefix_len = strlen(dir1_prefix);
+ int dir2_prefix_len = strlen(dir2_prefix);
+ const char* test_str1 = "Hello, World!\n";
+ const char* test_str2 = "Hello, ESP Community\n";
+
+ /* Mount FATFS in SD can WL at the same time. Create a file on each FS */
+ sdmmc_card_t* card = NULL;
+ test_setup_sdmmc(&card);
+
+ TEST_ASSERT_EQUAL(0, mkdir(dir1_prefix, 0755));
+ TEST_ASSERT_EQUAL(0, mkdir(dir2_prefix, 0755));
+
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_1.bin", dir1_prefix);
+ test_fatfs_create_file_with_text(name_dir_file, test_str1);
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_1.bin", dir2_prefix);
+ test_fatfs_create_file_with_text(name_dir_file, test_str2);
+
+ DIR* dir1 = opendir(dir1_prefix);
+ TEST_ASSERT_NOT_NULL(dir1);
+ DIR* dir2 = opendir(dir2_prefix);
+ TEST_ASSERT_NOT_NULL(dir2);
+ struct dirent* de1;
+ struct dirent* de2;
+ struct stat st1;
+ struct stat st2;
+
+ while(1) {
+ de1 = readdir(dir1);
+ if (!de1) {
+ break;
+ }
+ de2 = readdir(dir2);
+ if (!de2) {
+ break;
+ }
+ snprintf(name_dir1_stat, dir1_prefix_len+sizeof(de1->d_name)+1, "%s/%s", dir1_prefix, de1->d_name);
+ snprintf(name_dir2_stat, dir2_prefix_len+sizeof(de2->d_name)+1, "%s/%s", dir2_prefix, de2->d_name);
+ TEST_ASSERT_EQUAL(0, stat(name_dir1_stat, &st1));
+ TEST_ASSERT_EQUAL(0, stat(name_dir2_stat, &st2));
+ TEST_ASSERT_EQUAL(strlen(test_str1), st1.st_size); //size of dir1/boo_1.bin is 14
+ TEST_ASSERT_EQUAL(strlen(test_str2), st2.st_size); //size of dir2/boo_1.bin is 21
+ }
+ TEST_ASSERT_EQUAL(0, closedir(dir1));
+ TEST_ASSERT_EQUAL(0, closedir(dir2));
+
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_1.bin", dir1_prefix);
+ unlink(name_dir_file);
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_1.bin", dir2_prefix);
+ unlink(name_dir_file);
+ rmdir(dir1_prefix);
+ rmdir(dir2_prefix);
+
+ test_teardown_sdmmc(card);
+}
+
TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sdmmc]")
{
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
diff --git a/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdspi.c b/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdspi.c
index c1d567c0..a3247bd2 100644
--- a/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdspi.c
+++ b/lib/fatfs/test_apps/sdcard/main/test_fatfs_sdspi.c
@@ -1,5 +1,5 @@
/*
- * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -22,6 +22,7 @@
#include "ff.h"
#include "test_fatfs_common.h"
#include "soc/soc_caps.h"
+#include "vfs_fat_internal.h"
#if CONFIG_IDF_TARGET_ESP32
#define SDSPI_MISO_PIN 2
@@ -34,13 +35,13 @@
#define SDSPI_MOSI_PIN 35
#define SDSPI_CLK_PIN 36
#define SDSPI_CS_PIN 34
-#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C6
+#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32C61
#define SDSPI_MISO_PIN 6
#define SDSPI_MOSI_PIN 4
#define SDSPI_CLK_PIN 5
#define SDSPI_CS_PIN 1
#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
-#elif CONFIG_IDF_TARGET_ESP32H2
+#elif CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32P4
#define SDSPI_MISO_PIN 0
#define SDSPI_MOSI_PIN 5
#define SDSPI_CLK_PIN 4
@@ -89,7 +90,7 @@ static void test_teardown_sdspi(sdspi_mem_t* mem)
HEAP_SIZE_CHECK(mem->heap_size, 0);
}
-TEST_CASE("(SDSPI) write/read speed test", "[fatfs][sdspi]")
+TEST_CASE("(SDSPI) write/read speed test", "[fatfs][sdspi][timeout=120]")
{
sdspi_mem_t mem;
size_t file_size = 1 * 1024 * 1024;
@@ -197,3 +198,55 @@ TEST_CASE("(SDSPI) can format card", "[fatfs][sdspi][timeout=180]")
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
test_teardown_sdspi(&mem);
}
+
+
+TEST_CASE("(SDSPI) can format card with config", "[fatfs][sdspi][timeout=180]")
+{
+ sdspi_mem_t mem;
+ test_setup_sdspi(&mem);
+
+ const char path[] = "/sdcard";
+ sdmmc_card_t *card;
+ card = NULL;
+ sdspi_device_config_t device_cfg = {
+ .gpio_cs = SDSPI_CS_PIN,
+ .host_id = SDSPI_HOST_ID,
+ .gpio_cd = SDSPI_SLOT_NO_CD,
+ .gpio_wp = SDSPI_SLOT_NO_WP,
+ .gpio_int = SDSPI_SLOT_NO_INT,
+ };
+
+ sdmmc_host_t host = SDSPI_HOST_DEFAULT();
+ host.slot = SDSPI_HOST_ID;
+ esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+ .allocation_unit_size = 64 * 1024,
+ };
+ TEST_ESP_OK(esp_vfs_fat_sdspi_mount(path, &host, &device_cfg, &mount_config, &card));
+
+ vfs_fat_sd_ctx_t* ctx = get_vfs_fat_get_sd_ctx(card);
+ TEST_ASSERT_NOT_NULL(ctx);
+
+ esp_vfs_fat_mount_config_t format_config = {
+ .format_if_mount_failed = true,
+ .max_files = 5,
+ .allocation_unit_size = 64 * 1024,
+ .use_one_fat = true,
+ };
+ TEST_ESP_OK(esp_vfs_fat_sdcard_format_cfg("/sdcard", card, &format_config));
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 1);
+
+ test_fatfs_create_file_with_text(s_test_filename, fatfs_test_hello_str);
+ test_fatfs_read_file(s_test_filename);
+
+ format_config.use_one_fat = false;
+ TEST_ESP_OK(esp_vfs_fat_sdcard_format_cfg("/sdcard", card, &format_config));
+ TEST_ASSERT_TRUE(ctx->fs->n_fats == 2);
+
+ test_fatfs_create_file_with_text(s_test_filename, fatfs_test_hello_str);
+ test_fatfs_read_file(s_test_filename);
+
+ TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
+ test_teardown_sdspi(&mem);
+}
diff --git a/lib/fatfs/test_apps/sdcard/pytest_fatfs_sdcard.py b/lib/fatfs/test_apps/sdcard/pytest_fatfs_sdcard.py
index fe720a02..b0657186 100644
--- a/lib/fatfs/test_apps/sdcard/pytest_fatfs_sdcard.py
+++ b/lib/fatfs/test_apps/sdcard/pytest_fatfs_sdcard.py
@@ -1,75 +1,59 @@
-# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
+# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
-
import pytest
from pytest_embedded import Dut
+from pytest_embedded_idf.utils import idf_parametrize
-@pytest.mark.esp32
@pytest.mark.sdcard_sdmode
@pytest.mark.parametrize(
'config',
[
'default',
'release',
- ]
+ ],
)
+@idf_parametrize('target', ['esp32', 'esp32c3'], indirect=['target'])
def test_fatfs_sdcard_generic_sdmmc(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('[sdmmc]')
- dut.expect_unity_test_output(timeout=180)
+ dut.run_all_single_board_cases(group='sdmmc', timeout=180)
-@pytest.mark.esp32
-@pytest.mark.esp32s2
-@pytest.mark.esp32c3
+@pytest.mark.temp_skip_ci(targets=['esp32'], reason='IDFCI-2058, temporary lack runner')
@pytest.mark.sdcard_spimode
@pytest.mark.parametrize(
'config',
[
'default',
'release',
- ]
+ ],
)
+@idf_parametrize('target', ['esp32', 'esp32c3'], indirect=['target'])
def test_fatfs_sdcard_generic_sdspi(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('[sdspi]')
- dut.expect_unity_test_output(timeout=180)
+ dut.run_all_single_board_cases(group='sdspi', timeout=180)
-@pytest.mark.esp32
@pytest.mark.sdcard_sdmode
@pytest.mark.psram
@pytest.mark.parametrize(
'config',
[
'psram',
- ]
+ ],
)
+@idf_parametrize('target', ['esp32'], indirect=['target'])
def test_fatfs_sdcard_psram_sdmmc(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('[sdmmc]')
- dut.expect_unity_test_output(timeout=180)
+ dut.run_all_single_board_cases(group='sdmmc', timeout=180)
-@pytest.mark.esp32
+@pytest.mark.temp_skip_ci(targets=['esp32'], reason='IDFCI-2058, temporary lack runner')
@pytest.mark.sdcard_spimode
@pytest.mark.psram
@pytest.mark.parametrize(
'config',
[
'psram',
- ]
+ ],
)
+@idf_parametrize('target', ['esp32'], indirect=['target'])
def test_fatfs_sdcard_psram_sdspi(dut: Dut) -> None:
- dut.expect_exact('Press ENTER to see the list of tests')
- dut.write('')
- dut.expect_exact('Enter test for running.')
- dut.write('[sdspi]')
- dut.expect_unity_test_output(timeout=180)
+ dut.run_all_single_board_cases(group='sdspi', timeout=180)
diff --git a/lib/fatfs/test_apps/sdcard/sdkconfig.ci.psram b/lib/fatfs/test_apps/sdcard/sdkconfig.ci.psram.esp32
index b3b45db9..5becfb94 100644
--- a/lib/fatfs/test_apps/sdcard/sdkconfig.ci.psram
+++ b/lib/fatfs/test_apps/sdcard/sdkconfig.ci.psram.esp32
@@ -1,3 +1,4 @@
+CONFIG_IDF_TARGET="esp32"
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0
CONFIG_FATFS_ALLOC_PREFER_EXTRAM=y
diff --git a/lib/fatfs/test_apps/test_fatfs_common/CMakeLists.txt b/lib/fatfs/test_apps/test_fatfs_common/CMakeLists.txt
index 8f06878d..74ec85e7 100644
--- a/lib/fatfs/test_apps/test_fatfs_common/CMakeLists.txt
+++ b/lib/fatfs/test_apps/test_fatfs_common/CMakeLists.txt
@@ -1,3 +1,3 @@
idf_component_register(SRCS "test_fatfs_common.c"
INCLUDE_DIRS "."
- PRIV_REQUIRES unity fatfs vfs unity)
+ PRIV_REQUIRES unity fatfs vfs unity esp_timer)
diff --git a/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c b/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c
index 77372fbb..887123cb 100644
--- a/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c
+++ b/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c
@@ -1,9 +1,10 @@
/*
- * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
+#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -16,10 +17,12 @@
#include <utime.h>
#include "unity.h"
#include "esp_vfs.h"
+#include "esp_timer.h"
#include "esp_vfs_fat.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "test_fatfs_common.h"
+#include "ff.h"
const char* fatfs_test_hello_str = "Hello, World!\n";
const char* fatfs_test_hello_str_utf = "世界,你好!\n";
@@ -252,7 +255,7 @@ void test_fatfs_lseek(const char* filename)
}
-void test_fatfs_truncate_file(const char* filename)
+void test_fatfs_truncate_file(const char* filename, bool allow_expanding_files)
{
int read = 0;
int truncated_len = 0;
@@ -267,14 +270,44 @@ void test_fatfs_truncate_file(const char* filename)
TEST_ASSERT_EQUAL(0, fclose(f));
+ struct stat st;
+ size_t size;
- // Extending file beyond size is not supported
- TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
- TEST_ASSERT_EQUAL(errno, EPERM);
+ stat(filename, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(strlen(input), size);
- TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
- TEST_ASSERT_EQUAL(errno, EINVAL);
+ if (allow_expanding_files) {
+ size_t trunc_add = 2;
+ size_t new_size = strlen(input) + trunc_add;
+ TEST_ASSERT_EQUAL(0, truncate(filename, new_size));
+
+ stat(filename, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(new_size, size);
+
+ f = fopen(filename, "rb");
+ TEST_ASSERT_NOT_NULL(f);
+
+ char expanded_output[sizeof(input) + trunc_add];
+ memset(expanded_output, 42, sizeof(expanded_output)); // set to something else than 0 (42)
+
+ read = fread(expanded_output, 1, sizeof(input) + trunc_add, f);
+ TEST_ASSERT_EQUAL(new_size, read);
+
+ TEST_ASSERT_EQUAL('Z', expanded_output[strlen(input) - 1]); // 'Z' character
+ TEST_ASSERT_EQUAL('\0', expanded_output[sizeof(input) + trunc_add - 3]); // zeroed expanded space
+ TEST_ASSERT_EQUAL('\0', expanded_output[sizeof(input) + trunc_add - 2]); // zeroed expanded space
+ TEST_ASSERT_EQUAL(42, expanded_output[sizeof(input) + trunc_add - 1]); // 42 set with memset, end of the array
+ TEST_ASSERT_EQUAL(0, fclose(f));
+ } else {
+ TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
+ TEST_ASSERT_EQUAL(errno, EPERM);
+
+ TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
+ TEST_ASSERT_EQUAL(errno, EINVAL);
+ }
// Truncating should succeed
const char truncated_1[] = "ABCDEFGHIJ";
@@ -282,6 +315,10 @@ void test_fatfs_truncate_file(const char* filename)
TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len));
+ stat(filename, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(strlen(truncated_1), size);
+
f = fopen(filename, "rb");
TEST_ASSERT_NOT_NULL(f);
@@ -293,28 +330,34 @@ void test_fatfs_truncate_file(const char* filename)
TEST_ASSERT_EQUAL(0, fclose(f));
+ if (allow_expanding_files) {
+ TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len + 1));
+ } else {
+ // Once truncated, the new file size should be the basis
+ // whether truncation should succeed or not when `allow_expanding_files == false`
+ TEST_ASSERT_EQUAL(-1, truncate(filename, truncated_len + 1));
+ TEST_ASSERT_EQUAL(EPERM, errno);
- // Once truncated, the new file size should be the basis
- // whether truncation should succeed or not
- TEST_ASSERT_EQUAL(-1, truncate(filename, truncated_len + 1));
- TEST_ASSERT_EQUAL(EPERM, errno);
-
- TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input)));
- TEST_ASSERT_EQUAL(EPERM, errno);
+ TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input)));
+ TEST_ASSERT_EQUAL(EPERM, errno);
- TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
- TEST_ASSERT_EQUAL(EPERM, errno);
+ TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
+ TEST_ASSERT_EQUAL(EPERM, errno);
+ }
TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
TEST_ASSERT_EQUAL(EINVAL, errno);
-
// Truncating a truncated file should succeed
const char truncated_2[] = "ABCDE";
truncated_len = strlen(truncated_2);
TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len));
+ stat(filename, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(strlen(truncated_2), size);
+
f = fopen(filename, "rb");
TEST_ASSERT_NOT_NULL(f);
@@ -327,29 +370,63 @@ void test_fatfs_truncate_file(const char* filename)
TEST_ASSERT_EQUAL(0, fclose(f));
}
-void test_fatfs_ftruncate_file(const char* filename)
+void test_fatfs_ftruncate_file(const char* filename, bool allow_expanding_files)
{
int truncated_len = 0;
const char input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char output[sizeof(input)];
- int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
+ int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC);
TEST_ASSERT_NOT_EQUAL(-1, fd);
TEST_ASSERT_EQUAL(strlen(input), write(fd, input, strlen(input)));
- // Extending file beyond size is not supported
- TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input) + 1));
- TEST_ASSERT_EQUAL(errno, EPERM);
-
- TEST_ASSERT_EQUAL(-1, ftruncate(fd, -1));
- TEST_ASSERT_EQUAL(errno, EINVAL);
+ struct stat st;
+ size_t size;
+
+ fstat(fd, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(strlen(input), size);
+
+ if (allow_expanding_files) {
+ size_t trunc_add = 2;
+ size_t new_size = strlen(input) + trunc_add;
+ TEST_ASSERT_EQUAL(0, ftruncate(fd, new_size));
+
+ fstat(fd, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(new_size, size);
+
+ char expanded_output[sizeof(input) + trunc_add];
+ memset(expanded_output, 42, sizeof(expanded_output)); // set to something else than 0 (42)
+
+ lseek(fd, 0, SEEK_SET);
+ int r = read(fd, expanded_output, sizeof(input) + trunc_add);
+ TEST_ASSERT_NOT_EQUAL(-1, r);
+ TEST_ASSERT_EQUAL(new_size, r);
+
+ TEST_ASSERT_EQUAL('Z', expanded_output[strlen(input) - 1]); // 'Z' character
+ TEST_ASSERT_EQUAL('\0', expanded_output[sizeof(input) + trunc_add - 3]); // zeroed expanded space
+ TEST_ASSERT_EQUAL('\0', expanded_output[sizeof(input) + trunc_add - 2]); // zeroed expanded space
+ TEST_ASSERT_EQUAL(42, expanded_output[sizeof(input) + trunc_add - 1]); // 42 set with memset, end of the array
+ } else {
+ TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input) + 1));
+ TEST_ASSERT_EQUAL(errno, EPERM);
+
+ TEST_ASSERT_EQUAL(-1, ftruncate(fd, -1));
+ TEST_ASSERT_EQUAL(errno, EINVAL);
+ }
// Truncating should succeed
const char truncated_1[] = "ABCDEFGHIJ";
truncated_len = strlen(truncated_1);
TEST_ASSERT_EQUAL(0, ftruncate(fd, truncated_len));
+
+ fstat(fd, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(truncated_len, size);
+
TEST_ASSERT_EQUAL(0, close(fd));
// open file for reading and validate the content
@@ -367,25 +444,35 @@ void test_fatfs_ftruncate_file(const char* filename)
// further truncate the file
fd = open(filename, O_WRONLY);
TEST_ASSERT_NOT_EQUAL(-1, fd);
- // Once truncated, the new file size should be the basis
- // whether truncation should succeed or not
- TEST_ASSERT_EQUAL(-1, ftruncate(fd, truncated_len + 1));
- TEST_ASSERT_EQUAL(EPERM, errno);
- TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input)));
- TEST_ASSERT_EQUAL(EPERM, errno);
+ if (allow_expanding_files) {
+ TEST_ASSERT_EQUAL(0, ftruncate(fd, truncated_len + 1));
+ } else {
+ // Once truncated, the new file size should be the basis
+ // whether truncation should succeed or not when `allow_expanding_files == false`
+ TEST_ASSERT_EQUAL(-1, ftruncate(fd, truncated_len + 1));
+ TEST_ASSERT_EQUAL(EPERM, errno);
- TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input) + 1));
- TEST_ASSERT_EQUAL(EPERM, errno);
+ TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input)));
+ TEST_ASSERT_EQUAL(EPERM, errno);
- TEST_ASSERT_EQUAL(-1, ftruncate(fd, -1));
- TEST_ASSERT_EQUAL(EINVAL, errno);
+ TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input) + 1));
+ TEST_ASSERT_EQUAL(EPERM, errno);
+
+ TEST_ASSERT_EQUAL(-1, ftruncate(fd, -1));
+ TEST_ASSERT_EQUAL(EINVAL, errno);
+ }
// Truncating a truncated file should succeed
const char truncated_2[] = "ABCDE";
truncated_len = strlen(truncated_2);
TEST_ASSERT_EQUAL(0, ftruncate(fd, truncated_len));
+
+ fstat(fd, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(truncated_len, size);
+
TEST_ASSERT_EQUAL(0, close(fd));
// open file for reading and validate the content
@@ -556,7 +643,7 @@ void test_fatfs_utime(const char* filename, const char* root_dir)
TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
- //WARNING: it has the Unix Millenium bug (Y2K38)
+ //WARNING: it has the Unix Millennium bug (Y2K38)
// 00:00:00. January 1st, 1970 - FATFS cannot handle years before 1980
desired_tm.tm_mon = 1 - 1;
@@ -670,6 +757,65 @@ void test_fatfs_can_opendir(const char* path)
unlink(name_dir_file);
}
+void test_fatfs_readdir_stat(const char* dir_prefix)
+{
+ char name_dir_file[64];
+ char name_dir_stat[64];
+ int file_num = 25;
+
+ rmdir(dir_prefix);
+ TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755));
+
+ for(int i=0;i<file_num;i++) {
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_%d.bin", dir_prefix,i);
+ test_fatfs_create_file_with_text(name_dir_file, fatfs_test_hello_str);
+ }
+
+ printf("Start counting\n");
+ int64_t start = esp_timer_get_time();
+ DIR* dir = opendir(dir_prefix);
+ TEST_ASSERT_NOT_NULL(dir);
+ struct stat st;
+ struct dirent* de;
+ uint32_t dir_size = 0;
+
+ // Call readdir before stat function and record the time needed to calculate the directory size
+ while(1) {
+ de = readdir(dir);
+ if (!de) {
+ break;
+ }
+ snprintf(name_dir_stat, sizeof(dir_prefix)+sizeof(de->d_name), "%s/%s", dir_prefix, de->d_name);
+ TEST_ASSERT_EQUAL(0, stat(name_dir_stat, &st));
+ dir_size += st.st_size;
+ }
+ TEST_ASSERT_EQUAL(0, closedir(dir));
+ int64_t end = esp_timer_get_time();
+ int64_t total_time_readdir = end-start;
+ printf("Time in us for calculating directory size by calling readdir first and then stat func: %lld \n",total_time_readdir);
+ printf("Size of the directory %s is %"PRIu32"bytes\n", dir_prefix, dir_size);
+ TEST_ASSERT_EQUAL(file_num*14, dir_size); //each file size is 14 bytes
+
+ // Call stat function directly and record the time needed to calculate the directory size
+ dir_size = 0;
+ start = esp_timer_get_time();
+ for(int i=0;i<file_num;i++) {
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_%d.bin", dir_prefix, i);
+ TEST_ASSERT_EQUAL(0, stat(name_dir_file, &st));
+ dir_size += st.st_size;
+ }
+ end = esp_timer_get_time();
+ int64_t total_time_stat = end-start;
+ printf("Time in us for calculating directory size by calling stat func: %lld \n",total_time_stat);
+ printf("Size of the directory %s is %"PRIu32"bytes\n", dir_prefix, dir_size);
+ TEST_ASSERT_EQUAL(file_num*14, dir_size); //each file size is 14 bytes
+
+ for(int i=0;i<file_num;i++) {
+ snprintf(name_dir_file, sizeof(name_dir_file), "%s/boo_%d.bin", dir_prefix,i);
+ unlink(name_dir_file);
+ }
+}
+
void test_fatfs_opendir_readdir_rewinddir(const char* dir_prefix)
{
char name_dir_inner_file[64];
@@ -902,7 +1048,7 @@ void test_fatfs_concurrent(const char* filename_prefix)
printf("writing f1 and f2\n");
const int cpuid_0 = 0;
- const int cpuid_1 = portNUM_PROCESSORS - 1;
+ const int cpuid_1 = CONFIG_FREERTOS_NUMBER_OF_CORES - 1;
const int stack_size = 4096;
xTaskCreatePinnedToCore(&read_write_task, "rw1", stack_size, &args1, 3, NULL, cpuid_0);
xTaskCreatePinnedToCore(&read_write_task, "rw2", stack_size, &args2, 3, NULL, cpuid_1);
@@ -1011,3 +1157,28 @@ void test_fatfs_info(const char* base_path, const char* filepath)
ESP_LOGD("fatfs info", "total_bytes=%llu, free_bytes_after_delete=%llu", total_bytes, free_bytes_new);
TEST_ASSERT_EQUAL(free_bytes, free_bytes_new);
}
+
+#if FF_USE_EXPAND
+void test_fatfs_create_contiguous_file(const char* base_path, const char* full_path)
+{
+ size_t desired_file_size = 64;
+
+ // Don't check for errors, file may not exist at first
+ remove(full_path); // esp_vfs_fat_create_contiguous_file will fail if the file already exists
+
+ esp_err_t err = esp_vfs_fat_create_contiguous_file(base_path, full_path, desired_file_size, true);
+ TEST_ASSERT_EQUAL(ESP_OK, err);
+
+ struct stat st;
+ size_t size;
+
+ stat(full_path, &st);
+ size = st.st_size;
+ TEST_ASSERT_EQUAL(desired_file_size, size);
+
+ bool is_contiguous = false;
+ err = esp_vfs_fat_test_contiguous_file(base_path, full_path, &is_contiguous);
+ TEST_ASSERT_EQUAL(ESP_OK, err);
+ TEST_ASSERT_TRUE(is_contiguous);
+}
+#endif
diff --git a/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h b/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h
index dad352e2..1a8d56af 100644
--- a/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h
+++ b/lib/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h
@@ -45,9 +45,9 @@ void test_fatfs_open_max_files(const char* filename_prefix, size_t files_count);
void test_fatfs_lseek(const char* filename);
-void test_fatfs_truncate_file(const char* path);
+void test_fatfs_truncate_file(const char* path, bool allow_expanding_files);
-void test_fatfs_ftruncate_file(const char* path);
+void test_fatfs_ftruncate_file(const char* path, bool allow_expanding_files);
void test_fatfs_stat(const char* filename, const char* root_dir);
@@ -76,3 +76,9 @@ void test_leading_spaces(void);
void test_fatfs_rw_speed(const char* filename, void* buf, size_t buf_size, size_t file_size, bool write);
void test_fatfs_info(const char* base_path, const char* filepath);
+
+#if FF_USE_EXPAND
+void test_fatfs_create_contiguous_file(const char* base_path, const char* full_path);
+#endif
+
+void test_fatfs_readdir_stat(const char* path);