LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
tests.c
Go to the documentation of this file.
1
9#include <kernel/tests.h>
10#include <lib/kstring.h>
11#include <arch/x86_64/gdt.h>
12#include <kernel/kprintf.h>
13#include <kernel/tools.h>
14#include <limits.h>
15
16// STRING.H TESTS
17
18bool test_memcpy(uint64_t dest, uint64_t src) {
19 // COPY 8 bytes (64 bits) of src into dest
20 uint32_t *ret_value = (uint32_t *) memcpy((void *) (&dest), (const void *) (&src), 8);
21
22 if ((uint64_t) ret_value == (uint64_t) &dest && *ret_value == src)
23 return true;
24
25 return false;
26}
27
28bool test_memmove(uint64_t dest, uint64_t src) {
29 // MOVE 8 bytes (64 bits) of src into dest
30 uint32_t *ret_value = (uint32_t *) memmove((void *) (&dest), (const void *) (&src), 8);
31
32 if ((uint64_t) ret_value == (uint64_t) &dest && *ret_value == src)
33 return true;
34
35 return false;
36}
37
38bool test_memset(uint64_t dest, int value) {
39 // set 8 bytes of dest to value
40 uint8_t *ret_value = (uint8_t *) memset((void *) (&dest), value, 8);
41
42 unsigned char small_value = (unsigned char) value;
43
44 if (ret_value[0] == small_value &&
45 ret_value[1] == small_value &&
46 ret_value[2] == small_value &&
47 ret_value[3] == small_value &&
48 ret_value[4] == small_value &&
49 ret_value[5] == small_value &&
50 ret_value[6] == small_value &&
51 ret_value[7] == small_value)
52 return true;
53
54 return false;
55}
56
57bool test_memcmp(uint64_t a, uint64_t b, int expected_value) {
58 // CMP 8 bytes (64 bits) of b and a
59 int ret_value = (int) memcmp((void *) (&a), (void *) (&b), 8);
60
61 if (ret_value == expected_value)
62 return true;
63
64 return false;
65}
66
67bool test_strlen(const char *str, uint8_t expected_value) {
68 if (strlen(str) == expected_value)
69 return true;
70
71 return false;
72}
73
74bool test_access_byte(void) {
75 if (get_access_byte(true, true, true, 0) == 0b10011010)
76 return true;
77
78 return false;
79}
80
81bool test_flag_byte(void) {
82 if (get_flag_byte(true, false, true) == 0b10100000)
83 return true;
84
85 return false;
86}
87
88// KPRINTF.H TESTS
89
91 // Print all the values since that is the only way to test.
92
93 kprintf("%d (0)\n", 0);
94 kprintf("%d (-7)\n", -7);
95 kprintf("%d (-2147483648)\n", -2147483648);
96 kprintf("%d (2147483647)\n", 2147483647);
97 kprintf("%u (4294967295)\n", 4294967295u);
98 kprintf("%x (ff)\n", 255);
99 kprintf("%X (FF)\n", 255);
100 kprintf("%x (0)\n\n", 0);
101 kprintf("%lu (18446744073709551615)\n", 18446744073709551615UL);
102 kprintf("%ld (-9223372036854775808)\n", LONG_MIN);
103 kprintf("%lx (ffffffff80001000)\n", 0xFFFFFFFF80001000UL);
104 kprintf("%p (0xffffffff80001000)\n\n", (void *) 0xFFFFFFFF80001000UL);
105 kprintf("%c%c%c (ABC)\n", 'A', 'B', 'C');
106 kprintf("%s|%s (a|)\n", "a", "");
107 kprintf("100%% (100%%)\n");
108 kprintf("%s ((null))\n", (char *) 0);
109}
110
111
112// FAULT HANDLER TESTS
113
114void test_de_fault(void) {
115 volatile int zero = 0;
116 asm volatile ("idiv %0, %1" : "=r"(zero), "=r"(zero) : : "memory");
117}
118
120 int x = *(volatile int *) 0xdead0000;
121}
122
124 uintptr_t addr = 0x10;
125 __asm__ volatile ("movl %1, (%0)"
126 :
127 : "r" (addr), "r" (10)
128 : "memory");
129}
130
131void test_bp_fault(void) {
132 asm volatile ("int3");
133}
uint8_t get_access_byte(bool rw, bool exec, bool type, uint8_t dpl)
Generates the access byte for the GDT.
Definition gdt.c:120
uint8_t get_flag_byte(bool granularity, bool size, bool long_mode)
Generates the flag byte for the GDT.
Definition gdt.c:128
Initializes and holds the GDT for the kernel.
Tools for printing to the console and serial output.
void kprintf(const char *fmt,...) __attribute__((format(printf
Wrapper for a standard print.
Utilities for memory and string operations.
size_t strlen(const char *str)
Calculates the length of a string.
Definition kstring.c:92
void * memset(void *dest, int c, size_t n)
Fills n bytes of the destination with c.
Definition kstring.c:44
void * memmove(void *dest, const void *src, size_t n)
Copies memory from one address to the other with possible overlap.
Definition kstring.c:26
int memcmp(const void *a, const void *b, size_t n)
Compares 2 the value of 2 memory addresses.
Definition kstring.c:52
void * memcpy(void *dest, const void *src, size_t n)
Copies memory from one address to the other without overlapping.
Definition kstring.c:14
bool test_memcmp(uint64_t a, uint64_t b, int expected_value)
Tests the memcmp utility.
Definition tests.c:57
void test_pf_write_fault(void)
Causes a Page fault (write) for handler testing.
Definition tests.c:123
bool test_memmove(uint64_t dest, uint64_t src)
Tests the memmove utility.
Definition tests.c:28
void test_pf_read_fault(void)
Causes a Page fault (read) for handler testing.
Definition tests.c:119
bool test_flag_byte(void)
Tests the flag byte utility for the gdt.
Definition tests.c:81
bool test_memset(uint64_t dest, int value)
Tests the memset utility.
Definition tests.c:38
void test_bp_fault(void)
Triggers a Breakpoint for handler testing.
Definition tests.c:131
bool test_strlen(const char *str, uint8_t expected_value)
Tests the strlen utility.
Definition tests.c:67
void test_de_fault(void)
Causes a Division error for handler testing.
Definition tests.c:114
void test_kprintf_values(void)
Prints test data and the expected values for visual verification of kprintf.
Definition tests.c:90
bool test_access_byte(void)
Tests the access byte utility for the gdt.
Definition tests.c:74
bool test_memcpy(uint64_t dest, uint64_t src)
Tests the memcpy utility.
Definition tests.c:18
Tools for testing different parts of the kernel.
Extra kernel utilities.