mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
feat(storage/vfs): add nullfs test cases for vfs
This commit is contained in:
parent
2b75ed37b9
commit
a30d27e01e
@ -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);
|
||||
|
||||
|
@ -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}
|
||||
|
138
components/vfs/test_apps/main/test_vfs_nullfs.c
Normal file
138
components/vfs/test_apps/main/test_vfs_nullfs.c
Normal file
@ -0,0 +1,138 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user