From 518abe7477125a56acc4c5daa3779a577f30688e Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Mon, 23 Sep 2024 11:43:32 +0200 Subject: [PATCH] mmap: add test for unaligned len JIRA: RTOS-923 --- mem/Makefile | 2 + mem/test.yaml | 5 ++- mem/test_mmap_new.c | 97 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 mem/test_mmap_new.c diff --git a/mem/Makefile b/mem/Makefile index eca36bef2..f0bcf64c3 100644 --- a/mem/Makefile +++ b/mem/Makefile @@ -23,3 +23,5 @@ LOCAL_SRCS := test_mprotect_fault.c DEP_LIBS := unity include $(binary.mk) + +$(eval $(call add_unity_test, test_mmap_new)) diff --git a/mem/test.yaml b/mem/test.yaml index fc22ec0f1..8624a6d00 100644 --- a/mem/test.yaml +++ b/mem/test.yaml @@ -1,5 +1,9 @@ test: tests: + - name: mmap + type: unity + execute: test_mmap_new + - name: mprotect-fault harness: fault_harness.py execute: test-mprotect-fault @@ -17,4 +21,3 @@ test: targets: # mprotect: is noop on NOMMU architecture exclude: [armv7m7-imxrt106x-evk, armv7m7-imxrt117x-evk, armv7m4-stm32l4x6-nucleo] - diff --git a/mem/test_mmap_new.c b/mem/test_mmap_new.c new file mode 100644 index 000000000..82d0a7258 --- /dev/null +++ b/mem/test_mmap_new.c @@ -0,0 +1,97 @@ +/* + * Phoenix-RTOS + * + * phoenix-rtos-test + * + * mmap syscall tests + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include +#include +#include +#include +#include + +#include "unity_fixture.h" + + +static long page_size; +static const char *filename = "./mmap_testfile"; + + +TEST_GROUP(test_mmap); + + +TEST_SETUP(test_mmap) +{ + page_size = sysconf(_SC_PAGESIZE); +} + + +TEST_TEAR_DOWN(test_mmap) +{ + unlink(filename); +} + + +TEST(test_mmap, len__zero) +{ + TEST_ASSERT_EQUAL(MAP_FAILED, mmap(NULL, 0, PROT_READ, MAP_ANONYMOUS, -1, 0)); +} + + +TEST(test_mmap, len__not_page_aligned) +{ + unsigned char *area = mmap(NULL, page_size + 1, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + TEST_ASSERT_NOT_EQUAL(MAP_FAILED, area); + + TEST_ASSERT_EQUAL(0, munmap(area, page_size + 1)); +} + + +TEST(test_mmap, len__not_page_aligned_file) +{ + const char *data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"; // 32 characters + + // Create file and write 32 characters + int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); + TEST_ASSERT_NOT_EQUAL(-1, fd); + TEST_ASSERT_EQUAL(32, write(fd, data, 32)); + TEST_ASSERT_EQUAL(0, close(fd)); + + // Map the first 16 bytes of the file + fd = open(filename, O_RDONLY); + TEST_ASSERT_NOT_EQUAL(-1, fd); + void *map = mmap(NULL, 16, PROT_READ, MAP_PRIVATE, fd, 0); + TEST_ASSERT_NOT_EQUAL(MAP_FAILED, map); + + TEST_ASSERT_EQUAL(0, munmap(map, 16)); + TEST_ASSERT_EQUAL(0, close(fd)); +} + + +TEST_GROUP_RUNNER(test_mmap) +{ + RUN_TEST_CASE(test_mmap, len__not_page_aligned); + RUN_TEST_CASE(test_mmap, len__not_page_aligned_file); + RUN_TEST_CASE(test_mmap, len__zero); +} + + +static void runner(void) +{ + RUN_TEST_GROUP(test_mmap); +} + + +int main(int argc, char *argv[]) +{ + return (UnityMain(argc, (const char **)argv, runner) == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +}