diff --git a/components/esp_system/test_apps/console/main/test_app_main.c b/components/esp_system/test_apps/console/main/test_app_main.c index 48ffcb2b3f..68a0c48bed 100644 --- a/components/esp_system/test_apps/console/main/test_app_main.c +++ b/components/esp_system/test_apps/console/main/test_app_main.c @@ -54,11 +54,11 @@ void app_main(void) printf("Hello World\n"); int fd = open("/dev/null", O_RDWR); - assert(fd >= 0); // Standard check + assert(fd >= 0 && "Could not open file"); // Standard check // Check if correct file descriptor is returned // In this case it should be neither of 0, 1, 2 (== stdin, stdout, stderr) - assert(fd > 2); + assert(fd > 2 && "Incorrect file descriptor returned, stdin, stdout, stderr were not correctly assigned"); close(fd); diff --git a/components/vfs/test_apps/main/CMakeLists.txt b/components/vfs/test_apps/main/CMakeLists.txt index f9a5e774c9..7ab4968a9e 100644 --- a/components/vfs/test_apps/main/CMakeLists.txt +++ b/components/vfs/test_apps/main/CMakeLists.txt @@ -2,7 +2,7 @@ set(src "test_app_main.c" "test_vfs_access.c" "test_vfs_append.c" "test_vfs_eventfd.c" "test_vfs_fd.c" "test_vfs_lwip.c" "test_vfs_open.c" "test_vfs_paths.c" - "test_vfs_select.c" + "test_vfs_select.c" "test_vfs_nullfs.c" ) idf_component_register(SRCS ${src} diff --git a/components/vfs/test_apps/main/test_vfs_nullfs.c b/components/vfs/test_apps/main/test_vfs_nullfs.c new file mode 100644 index 0000000000..74fc95e0cd --- /dev/null +++ b/components/vfs/test_apps/main/test_vfs_nullfs.c @@ -0,0 +1,138 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include + +#include "unity.h" +#include "esp_vfs.h" + +#include "esp_vfs_null.h" +#include "unity_test_runner.h" + +TEST_CASE("Can open and close /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + close(fd); +} + +TEST_CASE("Can write to /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + // Can write to /dev/null + ssize_t ret = write(fd, "hello", 5); + TEST_ASSERT_EQUAL(5, ret); + + // Write does not change the file offset + off_t offset = lseek(fd, 0, SEEK_CUR); + TEST_ASSERT_EQUAL(0, offset); + + close(fd); +} + +TEST_CASE("Can read from /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + // Can read from /dev/null + char buf[5] = {0}; + ssize_t ret = read(fd, buf, 5); + + // Read always returns 0 bytes -> EOF + TEST_ASSERT_EQUAL(0, ret); + + // Read does not modify the buffer + for (int i = 0; i < 5; i++) { + TEST_ASSERT_EQUAL(0, buf[i]); + } + + // Read does not change the file offset + off_t offset = lseek(fd, 0, SEEK_CUR); + TEST_ASSERT_EQUAL(0, offset); + + + close(fd); +} + +TEST_CASE("Can lseek /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + off_t offset = lseek(fd, 0, SEEK_SET); + TEST_ASSERT_EQUAL(0, offset); + + offset = lseek(fd, 0, SEEK_CUR); + TEST_ASSERT_EQUAL(0, offset); + + offset = lseek(fd, 0, SEEK_END); + TEST_ASSERT_EQUAL(0, offset); + + close(fd); +} + +TEST_CASE("Can fstat /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + struct stat st; + int ret = fstat(fd, &st); + TEST_ASSERT_EQUAL(0, ret); + + TEST_ASSERT_EQUAL(0, st.st_size); + TEST_ASSERT_EQUAL(S_IFCHR, st.st_mode & S_IFMT); + + close(fd); +} + +TEST_CASE("Can fsync /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + int ret = fsync(fd); + TEST_ASSERT_EQUAL(0, ret); + + close(fd); +} + +TEST_CASE("Can pread /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + char buf[5] = {0}; + ssize_t ret = pread(fd, buf, 5, 0); + TEST_ASSERT_EQUAL(0, ret); + + close(fd); +} + +TEST_CASE("Can pwrite /dev/null", "[vfs_nullfs]") +{ + int fd = open("/dev/null", O_RDWR); + TEST_ASSERT(fd >= 0); + + ssize_t ret = pwrite(fd, "hello", 5, 0); + TEST_ASSERT_EQUAL(5, ret); + + close(fd); +} + +TEST_CASE("Can stat /dev/null", "[vfs_nullfs]") +{ + struct stat st; + int ret = stat("/dev/null", &st); + TEST_ASSERT_EQUAL(0, ret); + + TEST_ASSERT_EQUAL(0, st.st_size); + TEST_ASSERT_EQUAL(S_IFCHR, st.st_mode & S_IFMT); +}