Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
qgymib committed Aug 22, 2024
1 parent 4ba7b48 commit b80e9b1
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 60 deletions.
56 changes: 28 additions & 28 deletions ev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <assert.h>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions ev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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 \
{\
Expand Down
6 changes: 3 additions & 3 deletions include/ev/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
{\
Expand Down
38 changes: 19 additions & 19 deletions src/ev/unix/fs_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/ev/win/fs_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
74 changes: 74 additions & 0 deletions test/cases/fs_mmap.c
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit b80e9b1

Please sign in to comment.