Skip to content

Commit

Permalink
mmap: add test for unaligned len
Browse files Browse the repository at this point in the history
JIRA: RTOS-923
  • Loading branch information
badochov authored and Darchiv committed Sep 23, 2024
1 parent cf2f864 commit 518abe7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mem/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ LOCAL_SRCS := test_mprotect_fault.c
DEP_LIBS := unity

include $(binary.mk)

$(eval $(call add_unity_test, test_mmap_new))
5 changes: 4 additions & 1 deletion mem/test.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,4 +21,3 @@ test:
targets:
# mprotect: is noop on NOMMU architecture
exclude: [armv7m7-imxrt106x-evk, armv7m7-imxrt117x-evk, armv7m4-stm32l4x6-nucleo]

97 changes: 97 additions & 0 deletions mem/test_mmap_new.c
Original file line number Diff line number Diff line change
@@ -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 <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

#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;
}

0 comments on commit 518abe7

Please sign in to comment.