aboutsummaryrefslogtreecommitdiff
path: root/kernel/test/proctest.c
blob: eb70c81dd3b64d57f51f1dead0fa8db215c88f58 (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
#include "errno.h"
#include "globals.h"

#include "test/proctest.h"
#include "test/usertest.h"

#include "util/debug.h"
#include "util/printf.h"
#include "util/string.h"

#include "proc/kthread.h"
#include "proc/proc.h"
#include "proc/sched.h"

/*
 * Set up a testing function for the process to execute. 
*/
void *test_func(long arg1, void *arg2)
{
    proc_t *proc_as_arg = (proc_t *)arg2;
    test_assert(arg1 == proc_as_arg->p_pid, "Arguments are not set up correctly");
    test_assert(proc_as_arg->p_state == PROC_RUNNING, "Process state is not running");
    test_assert(list_empty(&proc_as_arg->p_children), "There should be no child processes");
    dbg(DBG_TEST, "Process %s is running\n", proc_as_arg->p_name);
    return NULL;
}

void test_termination()
{
    int num_procs_created = 0;
    proc_t *new_proc1 = proc_create("proc test 1");
    kthread_t *new_kthread1 = kthread_create(new_proc1, test_func, 2, new_proc1);
    num_procs_created++;
    sched_make_runnable(new_kthread1);

    int count = 0;
    int status;
    while (do_waitpid(-1, &status, 0) != -ECHILD)
    {
        dbg(DBG_TEST, "Waiting for child process to terminate\n");
        test_assert(status == 0, "Returned status not set correctly");
        count++;
    }
    test_assert(count == num_procs_created,
                "Expected: %d, Actual: %d number of processes have been cleaned up\n", num_procs_created, count);
}

long proctest_main(long arg1, void *arg2)
{
    dbg(DBG_TEST, "\nStarting Procs tests\n");
    test_init();
    test_termination();

    // Add more tests here!
    // We highly recommend looking at section 3.8 on the handout for help!

    test_fini();
    return 0;
}