diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-31 09:50:47 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-31 09:50:47 +0100 |
| commit | 9f2369825584529ed2036588263aecf01bf89818 (patch) | |
| tree | e47b2f6ab27c3701eeb872be1a25cc9233455910 /test/kernel_test | |
| parent | af944082df5844cf3215dc9dd4960aeedd18bd75 (diff) | |
feat: test add more tests.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'test/kernel_test')
| -rw-r--r-- | test/kernel_test/error.test.cc | 67 | ||||
| -rw-r--r-- | test/kernel_test/event.test.cc | 103 | ||||
| -rw-r--r-- | test/kernel_test/kout.test.cc | 58 | ||||
| -rw-r--r-- | test/kernel_test/process.test.cc | 66 | ||||
| -rw-r--r-- | test/kernel_test/scheduler.test.cc | 108 |
5 files changed, 399 insertions, 3 deletions
diff --git a/test/kernel_test/error.test.cc b/test/kernel_test/error.test.cc new file mode 100644 index 00000000..084566b2 --- /dev/null +++ b/test/kernel_test/error.test.cc @@ -0,0 +1,67 @@ +/// \file error.test.cc +/// \brief Error handling API tests. +/// \author Amlal El Mahrouss (amlal at nekernel dot org) + +#include <libSystem/SystemKit/System.h> +#include <public/frameworks/KernelTest.fwrk/headers/TestCase.h> + +/// \note ErrGetLastError tests +KT_DECL_TEST(ErrGetLastErrorInitial, []() -> bool { + SInt32 error = ErrGetLastError(); + return error >= 0; +}); + +KT_DECL_TEST(ErrGetLastErrorAfterSuccess, []() -> bool { + auto heap = MmCreateHeap(1024, 0); + if (!heap) return NO; + + SInt32 error = ErrGetLastError(); + MmDestroyHeap(heap); + + return error == 0; +}); + +KT_DECL_TEST(ErrGetLastErrorAfterFailure, []() -> bool { + auto heap = MmCreateHeap(0, 0); + + SInt32 error = ErrGetLastError(); + + return error != 0; +}); + +KT_DECL_TEST(ErrGetLastErrorAfterInvalidFile, []() -> bool { + auto file = IoOpenFile("/invalid/path/that/does/not/exist", nullptr); + + SInt32 error = ErrGetLastError(); + + if (file) IoCloseFile(file); + + return error != 0; +}); + +KT_DECL_TEST(ErrGetLastErrorAfterNullOp, []() -> bool { + auto ptr = MmCopyMemory(nullptr, nullptr, 10); + + SInt32 error = ErrGetLastError(); + + return error != 0; +}); + +KT_DECL_TEST(ErrGetLastErrorMultipleCalls, []() -> bool { + SInt32 error1 = ErrGetLastError(); + SInt32 error2 = ErrGetLastError(); + + return error1 == error2; +}); + +/// \brief Run error tests. +SInt32 KT_TEST_MAIN() { + KT_RUN_TEST(ErrGetLastErrorInitial); + KT_RUN_TEST(ErrGetLastErrorAfterSuccess); + KT_RUN_TEST(ErrGetLastErrorAfterFailure); + KT_RUN_TEST(ErrGetLastErrorAfterInvalidFile); + KT_RUN_TEST(ErrGetLastErrorAfterNullOp); + KT_RUN_TEST(ErrGetLastErrorMultipleCalls); + + return KT_TEST_SUCCESS; +} diff --git a/test/kernel_test/event.test.cc b/test/kernel_test/event.test.cc new file mode 100644 index 00000000..6e342c6b --- /dev/null +++ b/test/kernel_test/event.test.cc @@ -0,0 +1,103 @@ +/// \file event.test.cc +/// \brief Event handling API tests. +/// \author Amlal El Mahrouss (amlal at nekernel dot org) + +#include <libSystem/SystemKit/System.h> +#include <public/frameworks/KernelTest.fwrk/headers/TestCase.h> + +/// \note Mock event listener +static Bool event_listener_called = NO; + +static Void mock_event_listener(VoidPtr data) { + event_listener_called = YES; +} + +/// \note EvtAddListener tests +KT_DECL_TEST(EvtAddListenerValid, []() -> bool { + REF_TYPE listener_ref; + listener_ref.__hash = 0x1234; + listener_ref.__self = (VoidPtr) mock_event_listener; + + EvtAddListener("test_event", (Ref) &listener_ref); + return YES; +}); + +KT_DECL_TEST(EvtAddListenerNull, []() -> bool { + EvtAddListener(nullptr, nullptr); + return YES; +}); + +/// \note EvtRemoveListener tests +KT_DECL_TEST(EvtRemoveListenerValid, []() -> bool { + REF_TYPE listener_ref; + listener_ref.__hash = 0x1234; + listener_ref.__self = (VoidPtr) mock_event_listener; + + EvtAddListener("remove_test", (Ref) &listener_ref); + EvtRemoveListener("remove_test", (Ref) &listener_ref); + return YES; +}); + +KT_DECL_TEST(EvtRemoveListenerNull, []() -> bool { + EvtRemoveListener(nullptr, nullptr); + return YES; +}); + +/// \note EvtDispatchEvent tests +KT_DECL_TEST(EvtDispatchEventValid, []() -> bool { + REF_TYPE listener_ref; + listener_ref.__hash = 0x5678; + listener_ref.__self = (VoidPtr) mock_event_listener; + + event_listener_called = NO; + + EvtAddListener("dispatch_test", (Ref) &listener_ref); + + char data[] = "event_data"; + EvtDispatchEvent("dispatch_test", (VoidPtr) data); + + return YES; +}); + +KT_DECL_TEST(EvtDispatchEventNull, []() -> bool { + VoidPtr result = EvtDispatchEvent(nullptr, nullptr); + return result == nullptr; +}); + +KT_DECL_TEST(EvtDispatchEventNotExist, []() -> bool { + VoidPtr result = EvtDispatchEvent("nonexistent_event", nullptr); + return YES; +}); + +/// \note Event lifecycle test +KT_DECL_TEST(EvtLifecycleTest, []() -> bool { + REF_TYPE listener_ref; + listener_ref.__hash = 0xABCD; + listener_ref.__self = (VoidPtr) mock_event_listener; + + EvtAddListener("lifecycle_event", (Ref) &listener_ref); + + char data[] = "test"; + EvtDispatchEvent("lifecycle_event", (VoidPtr) data); + + EvtRemoveListener("lifecycle_event", (Ref) &listener_ref); + + return YES; +}); + +/// \brief Run event tests. +SInt32 KT_TEST_MAIN() { + KT_RUN_TEST(EvtAddListenerValid); + KT_RUN_TEST(EvtAddListenerNull); + + KT_RUN_TEST(EvtRemoveListenerValid); + KT_RUN_TEST(EvtRemoveListenerNull); + + KT_RUN_TEST(EvtDispatchEventValid); + KT_RUN_TEST(EvtDispatchEventNull); + KT_RUN_TEST(EvtDispatchEventNotExist); + + KT_RUN_TEST(EvtLifecycleTest); + + return KT_TEST_SUCCESS; +} diff --git a/test/kernel_test/kout.test.cc b/test/kernel_test/kout.test.cc index 639ab94e..fb395b90 100644 --- a/test/kernel_test/kout.test.cc +++ b/test/kernel_test/kout.test.cc @@ -4,14 +4,70 @@ #include <libSystem/SystemKit/System.h> #include <public/frameworks/KernelTest.fwrk/headers/TestCase.h> -/// \note Declare tests +/// \note PrintGet tests KT_DECL_TEST(KOutIsNull, []() -> bool { return PrintGet("/null/") == nullptr; }); KT_DECL_TEST(KOutIsNotNull, []() -> bool { return PrintGet(nullptr) != nullptr; }); +/// \note PrintCreate/PrintRelease tests +KT_DECL_TEST(PrintCreateValid, []() -> bool { + auto handle = PrintCreate(); + if (!handle) return NO; + PrintRelease(handle); + return YES; +}); + +KT_DECL_TEST(PrintReleaseValid, []() -> bool { + auto handle = PrintCreate(); + if (!handle) return NO; + return PrintRelease(handle) == 0; +}); + +KT_DECL_TEST(PrintReleaseNull, []() -> bool { return PrintRelease(nullptr) != 0; }); + +/// \note PrintOut tests +KT_DECL_TEST(PrintOutValid, []() -> bool { + SInt32 result = PrintOut(nullptr, "Test output\n"); + return result >= 0; +}); + +KT_DECL_TEST(PrintOutWithHandle, []() -> bool { + auto handle = PrintCreate(); + SInt32 result = PrintOut(handle, "Test with handle\n"); + PrintRelease(handle); + return result >= 0; +}); + +KT_DECL_TEST(PrintOutNull, []() -> bool { + SInt32 result = PrintOut(nullptr, nullptr); + return result < 0; +}); + +KT_DECL_TEST(PrintOutFormatted, []() -> bool { + SInt32 result = PrintOut(nullptr, "Value: %d\n", 42); + return result >= 0; +}); + +/// \note PrintIn tests +KT_DECL_TEST(PrintInValid, []() -> bool { + SInt32 result = PrintIn(nullptr, "Input prompt: "); + return result >= 0; +}); + /// \brief Run 'kout' test. SInt32 KT_TEST_MAIN() { KT_RUN_TEST(KOutIsNull); KT_RUN_TEST(KOutIsNotNull); + KT_RUN_TEST(PrintCreateValid); + KT_RUN_TEST(PrintReleaseValid); + KT_RUN_TEST(PrintReleaseNull); + + KT_RUN_TEST(PrintOutValid); + KT_RUN_TEST(PrintOutWithHandle); + KT_RUN_TEST(PrintOutNull); + KT_RUN_TEST(PrintOutFormatted); + + KT_RUN_TEST(PrintInValid); + return KT_TEST_SUCCESS; }
\ No newline at end of file diff --git a/test/kernel_test/process.test.cc b/test/kernel_test/process.test.cc index 8a1a946e..1c7af22c 100644 --- a/test/kernel_test/process.test.cc +++ b/test/kernel_test/process.test.cc @@ -1,11 +1,11 @@ /// \file process.test.cc -/// \brief Process Out tests. +/// \brief Process management tests. /// \author Amlal El Mahrouss (amlal at nekernel dot org) #include <libSystem/SystemKit/System.h> #include <public/frameworks/KernelTest.fwrk/headers/TestCase.h> -/// \note Declare tests +/// \note RtlSpawnProcess tests KT_DECL_TEST(ProcessHasFailed, []() -> bool { /// \todo we return -1 here, should we document that or classify as common knowledge? return RtlSpawnProcess("/", 0, nullptr, nullptr, 0) == -1; @@ -17,10 +17,72 @@ KT_DECL_TEST(ProcessHasSucceeded, []() -> bool { return RtlSpawnProcess("/system/list", 0, nullptr, nullptr, 0) > 0; }); +KT_DECL_TEST(ProcessSpawnWithArgs, []() -> bool { + Char* argv[] = {(Char*) "arg1", (Char*) "arg2"}; + Char* envp[] = {(Char*) "VAR=value"}; + + UIntPtr pid = RtlSpawnProcess("/system/list", 2, argv, envp, 1); + return pid > 0; +}); + +KT_DECL_TEST(ProcessSpawnNullPath, []() -> bool { + return RtlSpawnProcess(nullptr, 0, nullptr, nullptr, 0) == -1; +}); + +KT_DECL_TEST(ProcessSpawnInvalidPath, []() -> bool { + return RtlSpawnProcess("/invalid/nonexistent", 0, nullptr, nullptr, 0) == -1; +}); + +/// \note RtlSpawnIB tests +KT_DECL_TEST(ProcessSpawnIBValid, []() -> bool { + UIntPtr pid = RtlSpawnProcess("/system/list", 0, nullptr, nullptr, 0); + if (pid <= 0) return NO; + + UInt32 result = RtlSpawnIB(pid); + return result == 0; +}); + +KT_DECL_TEST(ProcessSpawnIBInvalid, []() -> bool { + UInt32 result = RtlSpawnIB(0); + return result > 0; +}); + +/// \note RtlExitProcess tests +KT_DECL_TEST(ProcessExitValid, []() -> bool { + UIntPtr pid = RtlSpawnProcess("/system/list", 0, nullptr, nullptr, 0); + if (pid <= 0) return NO; + + Bool result = RtlExitProcess(pid, 0); + return result == YES; +}); + +KT_DECL_TEST(ProcessExitWithCode, []() -> bool { + UIntPtr pid = RtlSpawnProcess("/system/list", 0, nullptr, nullptr, 0); + if (pid <= 0) return NO; + + Bool result = RtlExitProcess(pid, 42); + return result == YES; +}); + +KT_DECL_TEST(ProcessExitInvalid, []() -> bool { + Bool result = RtlExitProcess(0, 0); + return result == NO; +}); + /// \brief Run 'process' test. SInt32 KT_TEST_MAIN() { KT_RUN_TEST(ProcessHasFailed); KT_RUN_TEST(ProcessHasSucceeded); + KT_RUN_TEST(ProcessSpawnWithArgs); + KT_RUN_TEST(ProcessSpawnNullPath); + KT_RUN_TEST(ProcessSpawnInvalidPath); + + KT_RUN_TEST(ProcessSpawnIBValid); + KT_RUN_TEST(ProcessSpawnIBInvalid); + + KT_RUN_TEST(ProcessExitValid); + KT_RUN_TEST(ProcessExitWithCode); + KT_RUN_TEST(ProcessExitInvalid); return KT_TEST_SUCCESS; } diff --git a/test/kernel_test/scheduler.test.cc b/test/kernel_test/scheduler.test.cc new file mode 100644 index 00000000..58cf6017 --- /dev/null +++ b/test/kernel_test/scheduler.test.cc @@ -0,0 +1,108 @@ +/// \file scheduler.test.cc +/// \brief Scheduler and debugging API tests. +/// \author Amlal El Mahrouss (amlal at nekernel dot org) + +#include <libSystem/SystemKit/System.h> +#include <public/frameworks/KernelTest.fwrk/headers/TestCase.h> + +/// \note SchedGetCurrentProcessID tests +KT_DECL_TEST(SchedGetCurrentPIDValid, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + return pid > 0; +}); + +/// \note SchedSetAffinity/SchedGetAffinity tests +KT_DECL_TEST(SchedSetAffinityValid, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + AffinityRef affinity; + + SInt32 result = SchedSetAffinity(pid, 1, &affinity); + return result >= 0; +}); + +KT_DECL_TEST(SchedGetAffinityValid, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + AffinityRef affinity; + + SInt32 result = SchedGetAffinity(pid, &affinity); + return result >= 0; +}); + +KT_DECL_TEST(SchedSetAffinityInvalid, []() -> bool { + AffinityRef affinity; + SInt32 result = SchedSetAffinity(0, 1, &affinity); + return result < 0; +}); + +KT_DECL_TEST(SchedGetAffinityInvalid, []() -> bool { + AffinityRef affinity; + SInt32 result = SchedGetAffinity(0, &affinity); + return result < 0; +}); + +KT_DECL_TEST(SchedAffinityRoundTrip, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + AffinityRef affinity_set; + AffinityRef affinity_get; + + SchedSetAffinity(pid, 2, &affinity_set); + SInt32 result = SchedGetAffinity(pid, &affinity_get); + + return result >= 0; +}); + +/// \note SchedFireSignal tests +KT_DECL_TEST(SchedFireSignalValid, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + SInt32 result = SchedFireSignal(pid, 1); + return result >= 0; +}); + +KT_DECL_TEST(SchedFireSignalInvalid, []() -> bool { + SInt32 result = SchedFireSignal(0, 1); + return result < 0; +}); + +/// \note SchedReadMemory/SchedWriteMemory tests +KT_DECL_TEST(SchedReadMemoryValid, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + SInt32 result = SchedReadMemory(pid, 0x1000, 64); + return result >= 0; +}); + +KT_DECL_TEST(SchedWriteMemoryValid, []() -> bool { + UIntPtr pid = SchedGetCurrentProcessID(); + SInt32 result = SchedWriteMemory(pid, 0x1000, 0x42); + return result >= 0; +}); + +KT_DECL_TEST(SchedReadMemoryInvalid, []() -> bool { + SInt32 result = SchedReadMemory(0, 0x1000, 64); + return result < 0; +}); + +KT_DECL_TEST(SchedWriteMemoryInvalid, []() -> bool { + SInt32 result = SchedWriteMemory(0, 0x1000, 0x42); + return result < 0; +}); + +/// \brief Run scheduler tests. +SInt32 KT_TEST_MAIN() { + KT_RUN_TEST(SchedGetCurrentPIDValid); + + KT_RUN_TEST(SchedSetAffinityValid); + KT_RUN_TEST(SchedGetAffinityValid); + KT_RUN_TEST(SchedSetAffinityInvalid); + KT_RUN_TEST(SchedGetAffinityInvalid); + KT_RUN_TEST(SchedAffinityRoundTrip); + + KT_RUN_TEST(SchedFireSignalValid); + KT_RUN_TEST(SchedFireSignalInvalid); + + KT_RUN_TEST(SchedReadMemoryValid); + KT_RUN_TEST(SchedWriteMemoryValid); + KT_RUN_TEST(SchedReadMemoryInvalid); + KT_RUN_TEST(SchedWriteMemoryInvalid); + + return KT_TEST_SUCCESS; +} |
