From b80e9b171c86928df0eedcc5de2e74505e1d21b2 Mon Sep 17 00:00:00 2001 From: qgymib <4520263+qgymib@users.noreply.github.com> Date: Thu, 22 Aug 2024 09:10:26 +0800 Subject: [PATCH] add test case --- ev.c | 56 ++++++++++++++++---------------- ev.h | 10 +++--- include/ev/fs.h | 6 ++-- src/ev/unix/fs_unix.c | 38 +++++++++++----------- src/ev/win/fs_win.c | 10 +++--- test/CMakeLists.txt | 1 + test/cases/fs_mmap.c | 74 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 60 deletions(-) create mode 100644 test/cases/fs_mmap.c diff --git a/ev.c b/ev.c index 8925351ed..6e3ef50b6 100644 --- a/ev.c +++ b/ev.c @@ -6633,8 +6633,8 @@ void ev_async_wakeup(ev_async_t* handle) // #line 56 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/win/fs_win.c -// SIZE: 24743 -// SHA-256: a27bdf44e2a6124d451bc1c22f063c6ccefb264019f7dae659b1b6c99b56df45 +// SIZE: 24761 +// SHA-256: 771100221a38e7a674342487d507114dd768ae35d715a63723ef45a84cc9bfbe //////////////////////////////////////////////////////////////////////////////// // #line 1 "ev/win/fs_win.c" #include @@ -7136,11 +7136,11 @@ static int _ev_fs_readdir_w_on_dirent(ev_dirent_w_t* info, void* arg) static DWORD _ev_file_mmap_to_native_protect_win32(int flags) { - if (flags & EV_FS_S_IXUSR) - { - return (flags & EV_FS_S_IWUSR) ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ; - } - return (flags & EV_FS_S_IWUSR) ? PAGE_READWRITE : PAGE_READONLY; + if (flags & EV_FS_S_IXUSR) + { + return (flags & EV_FS_S_IWUSR) ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ; + } + return (flags & EV_FS_S_IWUSR) ? PAGE_READWRITE : PAGE_READONLY; } EV_LOCAL int ev__fs_open(ev_os_file_t* file, const char* path, int flags, int mode) @@ -13293,8 +13293,8 @@ void ev_async_wakeup(ev_async_t* handle) // #line 86 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/fs_unix.c -// SIZE: 10832 -// SHA-256: 485f5b7b48381f842cdcd422021890ecf483220530cf254c52fc585b7a8f5494 +// SIZE: 10916 +// SHA-256: de509585567d26895bc6a3d136916d9099502dfc2b876c7a84b322725e52a85e //////////////////////////////////////////////////////////////////////////////// // #line 1 "ev/unix/fs_unix.c" #define _GNU_SOURCE @@ -13379,20 +13379,20 @@ static int _ev_fs_mkpath(char* file_path, int mode) static int _ev_file_mmap_to_native_prot_unix(int flags) { - int prot = 0; - if (flags & EV_FS_S_IRUSR) - { - prot |= PROT_READ; - } - if (flags & EV_FS_S_IWUSR) - { - prot |= PROT_WRITE; - } - if (flags & EV_FS_S_IXUSR) - { - prot |= PROT_EXEC; - } - return prot; + int prot = 0; + if (flags & EV_FS_S_IRUSR) + { + prot |= PROT_READ; + } + if (flags & EV_FS_S_IWUSR) + { + prot |= PROT_WRITE; + } + if (flags & EV_FS_S_IXUSR) + { + prot |= PROT_EXEC; + } + return prot; } EV_LOCAL int ev__fs_fstat(ev_os_file_t file, ev_fs_stat_t* statbuf) @@ -13672,11 +13672,11 @@ int ev_file_mmap(ev_file_map_t* view, ev_file_t* file, uint64_t size, int flags) if (size == 0) { - ev_fs_stat_t stat; - if ((ret = ev__fs_fstat(file->file, &stat)) != 0) - { - return ret; - } + ev_fs_stat_t stat; + if ((ret = ev__fs_fstat(file->file, &stat)) != 0) + { + return ret; + } size = stat.st_size; } diff --git a/ev.h b/ev.h index 59046842d..4a3f1ef17 100644 --- a/ev.h +++ b/ev.h @@ -4139,8 +4139,8 @@ EV_API void ev_pipe_close(ev_os_pipe_t fd); // #line 97 "ev.h" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/fs.h -// SIZE: 20670 -// SHA-256: 6c31b5c29991fb7d7e365535ad74433d19487ae43999b2e77e577ef4549ca410 +// SIZE: 20679 +// SHA-256: 6e9aa38874af630f7c65b78b0f9934ae68829fe660334b0ce3c6cedaf4c570a3 //////////////////////////////////////////////////////////////////////////////// // #line 1 "ev/fs.h" #ifndef __EV_FILE_SYSTEM_H__ @@ -4248,9 +4248,9 @@ struct ev_file_s typedef struct ev_file_map { - void* addr; /**< The mapped address. */ - uint64_t size; /**< The size of mapped address. */ - ev_file_map_backend_t backend; /**< Backend */ + void* addr; /**< The mapped address. */ + uint64_t size; /**< The size of mapped address. */ + ev_file_map_backend_t backend; /**< Backend */ } ev_file_map_t; #define EV_FILE_MAP_INVALID \ {\ diff --git a/include/ev/fs.h b/include/ev/fs.h index dbc9d9194..43e38f272 100644 --- a/include/ev/fs.h +++ b/include/ev/fs.h @@ -103,9 +103,9 @@ struct ev_file_s typedef struct ev_file_map { - void* addr; /**< The mapped address. */ - uint64_t size; /**< The size of mapped address. */ - ev_file_map_backend_t backend; /**< Backend */ + void* addr; /**< The mapped address. */ + uint64_t size; /**< The size of mapped address. */ + ev_file_map_backend_t backend; /**< Backend */ } ev_file_map_t; #define EV_FILE_MAP_INVALID \ {\ diff --git a/src/ev/unix/fs_unix.c b/src/ev/unix/fs_unix.c index cdead3f16..ea882ba6b 100644 --- a/src/ev/unix/fs_unix.c +++ b/src/ev/unix/fs_unix.c @@ -80,20 +80,20 @@ static int _ev_fs_mkpath(char* file_path, int mode) static int _ev_file_mmap_to_native_prot_unix(int flags) { - int prot = 0; - if (flags & EV_FS_S_IRUSR) - { - prot |= PROT_READ; - } - if (flags & EV_FS_S_IWUSR) - { - prot |= PROT_WRITE; - } - if (flags & EV_FS_S_IXUSR) - { - prot |= PROT_EXEC; - } - return prot; + int prot = 0; + if (flags & EV_FS_S_IRUSR) + { + prot |= PROT_READ; + } + if (flags & EV_FS_S_IWUSR) + { + prot |= PROT_WRITE; + } + if (flags & EV_FS_S_IXUSR) + { + prot |= PROT_EXEC; + } + return prot; } EV_LOCAL int ev__fs_fstat(ev_os_file_t file, ev_fs_stat_t* statbuf) @@ -373,11 +373,11 @@ int ev_file_mmap(ev_file_map_t* view, ev_file_t* file, uint64_t size, int flags) if (size == 0) { - ev_fs_stat_t stat; - if ((ret = ev__fs_fstat(file->file, &stat)) != 0) - { - return ret; - } + ev_fs_stat_t stat; + if ((ret = ev__fs_fstat(file->file, &stat)) != 0) + { + return ret; + } size = stat.st_size; } diff --git a/src/ev/win/fs_win.c b/src/ev/win/fs_win.c index 032ffedab..aceb54398 100644 --- a/src/ev/win/fs_win.c +++ b/src/ev/win/fs_win.c @@ -497,11 +497,11 @@ static int _ev_fs_readdir_w_on_dirent(ev_dirent_w_t* info, void* arg) static DWORD _ev_file_mmap_to_native_protect_win32(int flags) { - if (flags & EV_FS_S_IXUSR) - { - return (flags & EV_FS_S_IWUSR) ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ; - } - return (flags & EV_FS_S_IWUSR) ? PAGE_READWRITE : PAGE_READONLY; + if (flags & EV_FS_S_IXUSR) + { + return (flags & EV_FS_S_IWUSR) ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ; + } + return (flags & EV_FS_S_IWUSR) ? PAGE_READWRITE : PAGE_READONLY; } EV_LOCAL int ev__fs_open(ev_os_file_t* file, const char* path, int flags, int mode) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 448f8704a..73084dcf7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(ev_test_lib SHARED "test/cases/async.c" "test/cases/buf.c" "test/cases/fs.c" + "test/cases/fs_mmap.c" "test/cases/fs_seek.c" "test/cases/ipv4_addr.c" "test/cases/list.c" diff --git a/test/cases/fs_mmap.c b/test/cases/fs_mmap.c new file mode 100644 index 000000000..5fb51fa78 --- /dev/null +++ b/test/cases/fs_mmap.c @@ -0,0 +1,74 @@ +#include "test.h" +#include "utils/file.h" +#include "utils/random.h" + +static const char* s_file_name = "2e8b75e7-ec65-46a3-ba02-ba136c093eb4"; +static const char* s_file_content = +"06af3c93-7002-4abb-8b14-1f03a175752b\n" +"c3962866-361e-40d8-a759-c735925f25f2\n"; + +TEST_FIXTURE_SETUP(fs) +{ + ev_fs_remove(NULL, NULL, s_file_name, 0, NULL); + test_write_file(s_file_name, s_file_content, strlen(s_file_content) + 1); +} + +TEST_FIXTURE_TEARDOWN(fs) +{ + ev_fs_remove(NULL, NULL, s_file_name, 0, NULL); +} + +TEST_F(fs, mmap_size_0_rd) +{ + ev_file_t* file = ev_malloc(sizeof(ev_file_t)); + ASSERT_EQ_INT(ev_file_open(NULL, file, NULL, s_file_name, EV_FS_O_RDONLY, 0, NULL), 0); + + ev_file_map_t* view = ev_malloc(sizeof(ev_file_map_t)); + ASSERT_EQ_INT(ev_file_mmap(view, file, 0, EV_FS_S_IRUSR), 0); + + const char* content = view->addr; + ASSERT_EQ_STR(content, s_file_content); + + ev_file_munmap(view); + ev_free(view); + + ev_file_close(file, NULL); + ev_free(file); +} + +TEST_F(fs, mmap_size_0_rdwr) +{ + const char* overwrite_content = "hello world"; + const size_t overwrite_content_sz = strlen(overwrite_content); + + /* Check and overwrite file. */ + { + ev_file_t* file = ev_malloc(sizeof(ev_file_t)); + ASSERT_EQ_INT(ev_file_open(NULL, file, NULL, s_file_name, EV_FS_O_RDWR, 0, NULL), 0); + + ev_file_map_t* view = ev_malloc(sizeof(ev_file_map_t)); + ASSERT_EQ_INT(ev_file_mmap(view, file, 0, EV_FS_S_IRUSR | EV_FS_S_IWUSR), 0); + + char* content = view->addr; + ASSERT_EQ_STR(content, s_file_content); + + memcpy(content, overwrite_content, overwrite_content_sz); + + ev_file_munmap(view); + ev_free(view); + + ev_file_close(file, NULL); + ev_free(file); + } + + /* Check file content. */ + { + char* content = NULL; + test_read_file(s_file_name, &content); + + ASSERT_EQ_INT(strncmp(content, overwrite_content, overwrite_content_sz), 0); + ASSERT_EQ_INT(strcmp(content + overwrite_content_sz, s_file_content + overwrite_content_sz), 0); + + ev_free(content); + } +}