CeDOS - Commit e12ad3bf

tests: Add first test Signed-off-by: Celina Sophie Kalus <hello@celinakalus.de>
Celina Sophie Kalus
Thu, 24 Jul 2025 00:01:13 +0200
5 files changed, 114 insertions(+), 2 deletions(-)
M CMakeLists.txtCMakeLists.txt

@@ -41,8 +41,13 @@ )

add_compile_options(-DVERSION="${GIT_VERSION}") add_compile_options(-Wall -Wextra -Werror) +if (DEFINED CEDOS_TEST) +set(CEDOS_TTY_CON serial_con) +set(CEDOS_INIT_EXE "test") +else() set(CEDOS_TTY_CON vga_con) set(CEDOS_INIT_EXE "shelf") +endif() list(APPEND COMMON_SRC common/memory.c

@@ -119,6 +124,10 @@ cat

ce3d ) +list(APPEND TEST_APPS + test +) + if(DEFINED DEBUG) add_compile_options(-DDEBUG) endif()

@@ -176,11 +185,27 @@ common/

) # shell +list(APPEND IMAGE_APPS ${SHELL_APPS}) foreach(SHELL_APP IN LISTS SHELL_APPS) add_executable(${SHELL_APP} shell/${SHELL_APP}.c ) +endforeach() +if (DEFINED CEDOS_TEST) + list(APPEND IMAGE_APPS ${TEST_APPS}) + foreach(TEST_APP IN LISTS TEST_APPS) + add_executable(${TEST_APP} + tests/${TEST_APP}.c + ) + target_include_directories(${TEST_APP} PRIVATE + tests/ + ) + endforeach() + list(APPEND IMAGE_FILES ${CMAKE_SOURCE_DIR}/tests/testfile.txt) +endif() + +foreach(SHELL_APP IN LISTS IMAGE_APPS) target_link_libraries(${SHELL_APP} cedos) target_include_directories(${SHELL_APP} PRIVATE libcedos/

@@ -191,6 +216,8 @@

target_link_options(${SHELL_APP} PRIVATE LINKER:-Map=${CMAKE_BINARY_DIR}/${SHELL_APP}.map ) + + list(APPEND IMAGE_FILES ${SHELL_APP}) endforeach() target_compile_options(ce3d PRIVATE -mhard-float)

@@ -198,9 +225,9 @@

# image add_custom_target( cedos.img ALL - DEPENDS kernel.bin boot.bin ${SHELL_APPS} + DEPENDS kernel.bin boot.bin ${IMAGE_APPS} COMMAND mkdir -p ${CMAKE_BINARY_DIR}/root ${CMAKE_BINARY_DIR}/input - COMMAND cp kernel.bin ${SHELL_APPS} ${CMAKE_BINARY_DIR}/root + COMMAND cp kernel.bin ${IMAGE_FILES} ${CMAKE_BINARY_DIR}/root COMMAND cp boot.bin ${CMAKE_BINARY_DIR}/input COMMAND genimage --config ${CMAKE_SOURCE_DIR}/image.conf )
A test.sh

@@ -0,0 +1,19 @@

+#!/usr/bin/env bash + +SCRIPT_DIR=$(dirname $0) +GIT_ROOT=$(git rev-parse --show-toplevel) + +if [[ "$GIT_ROOT" -ef "$PWD" ]]; then + echo "Cannot run in repository root." + exit -1 +fi + +cmake -GNinja -DCEDOS_TEST=y .. +ninja + +mkdir -p logs/ +QEMU_NOGRAPHIC=1 "${GIT_ROOT}/run.sh" "$PWD/images/cedos.img" + +awk '/^##/ {close(out); out="./logs/test" ++n ".log"} out {print > out}' "$PWD/logs/serial.log" + +prove -e cat logs/test*.log
A tests/tap.h

@@ -0,0 +1,37 @@

+#ifndef INCLUDE_TAP_H +#define INCLUDE_TAP_H + +#include <stdbool.h> +#include <stdio.h> +#include <cedos.h> + +static int test_id; + +static inline void plan(const char *test_name, int num_tests) { + printf("## STARTING TEST %s\n", test_name); + printf("1..%i\n", num_tests); + test_id = 1; +} +static inline void test_true(bool value, const char *comment) { + if (value) { + printf("ok %i", test_id++); + } else { + printf("not ok %i", test_id++); + } + + if (comment != NULL) { + printf(" # %s\n", comment); + } else { + printf("\n"); + } +} + +static inline void test_zero(int value, const char *comment) { + test_true(value == 0, comment); +} + +static inline void test_nonzero(int value, const char *comment) { + test_true(value != 0, comment); +} + +#endif
A tests/test.c

@@ -0,0 +1,27 @@

+#include <stdio.h> +#include <stdint.h> + +#include "cedos.h" + +#include "tap.h" + +void main(char *args) { + (void)args; + + plan("basic_test", 5); + + int pid = 0; + int ret = 0; + + test_true(true, "sanity check"); + test_zero(0, "sanity check"); + test_nonzero(1, "sanity check"); + + pid = process_spawn("cat", "testfile.txt"); + test_nonzero(pid, "successfully spawned process"); + + ret = process_wait(pid); + test_zero(ret, "process successfully terminated"); + + hard_reset(); +}
A tests/testfile.txt

@@ -0,0 +1,2 @@

+# this is a test output file. +