summaryrefslogtreecommitdiff
path: root/src/user/init.cc
blob: 85252be15ae64060b7ede75a5dc416d9e6fd7a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "user/user.h"

int strlen(const char *s)
{
    int n = 0;
    while (s[n])
        n++;
    return n;
}

void print(const char *s)
{
    write(1, s, strlen(s));
}

void print_int(int val)
{
    char buf[16];
    int i = 0;
    if (val == 0)
    {
        print("0");
        return;
    }
    if (val < 0)
    {
        print("-");
        val = -val;
    }
    while (val > 0)
    {
        buf[i++] = (val % 10) + '0';
        val /= 10;
    }
    while (i > 0)
    {
        char c = buf[--i];
        write(1, &c, 1);
    }
}

void pass() { print(" -> [PASS]\n"); }
void fail()
{
    print(" -> [FAIL]\n");
    exit(1);
}

int main()
{
    print("\n=== Lume OS Full Validation ===\n");

    print("[TEST 1] Write\n");
    print("  System call write is working.");
    pass();

    print("[TEST 2] GetPID\n");
    int parent_pid = getpid();
    print("  Parent PID: ");
    print_int(parent_pid);
    if (parent_pid > 0)
        pass();
    else
        fail();

    print("[TEST 3] Fork, Wait & COW\n");
    int val = 100;
    int pid = fork();

    if (pid < 0)
    {
        print("  Fork Failed\n");
        fail();
    }

    if (pid == 0)
    {
        print("  [Child] PID: ");
        print_int(getpid());
        print("\n");

        if (val != 100)
        {
            print("  [Child] Data corruption!\n");
            exit(1);
        }

        val = 200;
        print("  [Child] Modified val to 200. Exiting.\n");
        exit(0);
    }
    else
    {
        print("  [Parent] Waiting for child...\n");
        int child_pid = wait(0);

        print("  [Parent] Child ");
        print_int(child_pid);
        print(" exited.\n");

        print("  [Parent] Check val (Expect 100): ");
        print_int(val);

        if (val == 100)
            pass();
        else
        {
            print("  [Parent] Memory Isolation Failed! Val=");
            print_int(val);
            fail();
        }
    }

    print("\n=== All Tests Passed ===\n");
    while (1)
        ;
    return 0;
}