test(kernel): cover errno return contracts

This commit is contained in:
Microindole 2026-05-12 23:06:49 +08:00
parent 9cab2673af
commit 622ec19c14
No known key found for this signature in database
GPG Key ID: 22FB34CD2133ACA1
2 changed files with 43 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <tianole/early_log.h> #include <tianole/early_log.h>
#include <tianole/errno.h>
#include <tianole/mm.h> #include <tianole/mm.h>
#include "arch/x86/mm/page_table.h" #include "arch/x86/mm/page_table.h"
@ -20,10 +21,26 @@ void page_table_selftest(void)
page_tables_init(); page_tables_init();
if (map_page(TEST_VIRTUAL_PAGE + 1, page, PAGE_WRITABLE) != -EINVAL) {
panic("page table selftest unaligned map errno failed");
}
if (virt_to_phys(TEST_VIRTUAL_PAGE, 0) != -EINVAL) {
panic("page table selftest null output errno failed");
}
if (virt_to_phys(TEST_VIRTUAL_PAGE, &resolved) != -ENOENT) {
panic("page table selftest missing resolve errno failed");
}
if (map_page(TEST_VIRTUAL_PAGE, page, PAGE_WRITABLE) != 0) { if (map_page(TEST_VIRTUAL_PAGE, page, PAGE_WRITABLE) != 0) {
panic("page table selftest map failed"); panic("page table selftest map failed");
} }
if (map_page(TEST_VIRTUAL_PAGE, page, PAGE_WRITABLE) != -EEXIST) {
panic("page table selftest duplicate map errno failed");
}
if (virt_to_phys(TEST_VIRTUAL_PAGE, &resolved) != 0 || if (virt_to_phys(TEST_VIRTUAL_PAGE, &resolved) != 0 ||
resolved != page) { resolved != page) {
panic("page table selftest resolve failed"); panic("page table selftest resolve failed");
@ -38,6 +55,10 @@ void page_table_selftest(void)
panic("page table selftest unmap failed"); panic("page table selftest unmap failed");
} }
if (unmap_page(TEST_VIRTUAL_PAGE) != -ENOENT) {
panic("page table selftest missing unmap errno failed");
}
free_page(page); free_page(page);
early_log_puts("page table selftest ok\n"); early_log_puts("page table selftest ok\n");
} }

View File

@ -14,9 +14,16 @@ static void thread_selftest_entry(void *arg)
(void)arg; (void)arg;
} }
static int selftest_condition_false(void *arg)
{
(void)arg;
return 0;
}
void sched_selftest(void) void sched_selftest(void)
{ {
struct spinlock test_lock; struct spinlock test_lock;
struct wait_queue test_wait_queue;
struct thread *first = struct thread *first =
kernel_thread_create("worker-a", thread_selftest_entry, 0); kernel_thread_create("worker-a", thread_selftest_entry, 0);
struct thread *second = struct thread *second =
@ -47,6 +54,15 @@ void sched_selftest(void)
spin_lock_irqsave(&test_lock, &flags); spin_lock_irqsave(&test_lock, &flags);
spin_unlock_irqrestore(&test_lock, flags); spin_unlock_irqrestore(&test_lock, flags);
wait_queue_init(&test_wait_queue);
if (wait_queue_wait(0, selftest_condition_false, 0) != -EINVAL) {
panic("wait queue selftest null queue errno failed");
}
if (wait_queue_wait(&test_wait_queue, 0, 0) != -EINVAL) {
panic("wait queue selftest null condition errno failed");
}
early_log_puts("kernel thread selftest ok\n"); early_log_puts("kernel thread selftest ok\n");
} }
@ -127,6 +143,12 @@ static void timeout_wait_demo_waiter(void *arg)
(void)arg; (void)arg;
early_log_puts("timeout waiter sleeping\n"); early_log_puts("timeout waiter sleeping\n");
if (wait_queue_wait_timeout(
&timeout_wait_queue, condition_is_ready, &never_ready, 0) !=
-ETIMEDOUT) {
panic("timeout wait zero tick errno failed");
}
if (wait_queue_wait_timeout( if (wait_queue_wait_timeout(
&timeout_wait_queue, condition_is_ready, &never_ready, 3) != &timeout_wait_queue, condition_is_ready, &never_ready, 3) !=
-ETIMEDOUT) { -ETIMEDOUT) {