File: linux_process_test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (168 lines) | stat: -rw-r--r-- 3,762 bytes parent folder | download | duplicates (7)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <test.h>

#include <compiler.h>
#include <process_lib.h>
#include <process_unix_priv.h>

/* Stubs for /proc/<pid>/stat. */

static const char *filecontents[2] = {
    "1 (i()))))))-:!#!#@)) S 1 3927 3927 1025 3927 4202752 359 0 2 0 0 0 0 0 20 0 1 0 65535 19234816 226 18446744073709551615 1 1 0 0 0 0 0 6 0 18446744073709551615 0 0 17 2 0 0 40 0 0",
    "3929 (getty) T 1 3929 3929 1027 3929 4202752 359 0 1 0 0 0 0 0 20 0 1 0 100000 19234816 225 18446744073709551615 1 1 0 0 0 0 0 6 0 18446744073709551615 0 0 17 0 0 0 42 0 0",
};

static int filepos[2];

int open(const char *filename, ARG_UNUSED int flags, ...)
{
    if (!strcmp(filename, "/proc/1/stat"))
    {
        filepos[0] = 0;
        return 0;
    }
    else if (!strcmp(filename, "/proc/2/stat"))
    {
        filepos[1] = 0;
        static int got_intr = false;
        if (!got_intr)
        {
            got_intr = true;
            errno = EINTR;
            return -1;
        }

        return 1;
    }
    else if (!strcmp(filename, "/proc/666/stat"))
    {
        errno = EACCES;
        return -1;
    }
    else
    {
        errno = ENOENT;
        return -1;
    }
}

ssize_t read(int fd, void *buffer, ARG_UNUSED size_t buf_size)
{
    if (fd == 0)
    {
        if (filepos[0] < strlen(filecontents[0]))
        {
            memcpy(buffer, filecontents[0], strlen(filecontents[0]));
            filepos[0] = strlen(filecontents[0]);
            return strlen(filecontents[0]);
        }
        else
        {
            return 0;
        }
    }

    if (fd == 1)
    {
        static bool got_eintr = false;

        if (!got_eintr)
        {
            got_eintr = true;
            errno = EINTR;
            return -1;
        }
        else
        {
            got_eintr = false;
        }

        if (filepos[1] < strlen(filecontents[1]))
        {
            memcpy(buffer, filecontents[1] + filepos[1], 1);
            filepos[1]++;
            return 1;
        }
        else
        {
            return 0;
        }
    }

    errno = EIO;
    return -1;
}

int close(ARG_UNUSED int fd)
{
    return 0;
}

static void test_get_start_time_process1(void)
{
    time_t t = GetProcessStartTime(1);
    assert_int_equal(t, 65535 / sysconf(_SC_CLK_TCK));
}


static void test_get_start_time_process2(void)
{
    time_t t2 = GetProcessStartTime(2);
    assert_int_equal(t2, 100000 / sysconf(_SC_CLK_TCK));
}

static void test_get_start_time_process3(void)
{
    time_t t3 = GetProcessStartTime(3);
    assert_int_equal(t3, PROCESS_START_TIME_UNKNOWN);
}

static void test_get_start_time_process666(void)
{
    time_t t4 = GetProcessStartTime(666);
    assert_int_equal(t4, PROCESS_START_TIME_UNKNOWN);
}


static void test_get_state_process1(void)
{
    ProcessState s = GetProcessState(1);
    assert_int_equal(s, PROCESS_STATE_RUNNING);
}

static void test_get_state_process2(void)
{
    ProcessState s = GetProcessState(2);
    assert_int_equal(s, PROCESS_STATE_STOPPED);
}

static void test_get_state_process3(void)
{
    ProcessState s = GetProcessState(3);
    assert_int_equal(s, PROCESS_STATE_DOES_NOT_EXIST);
}

static void test_get_state_process666(void)
{
    ProcessState s = GetProcessState(666);
    assert_int_equal(s, PROCESS_STATE_DOES_NOT_EXIST);
}


int main()
{
    PRINT_TEST_BANNER();

    const UnitTest tests[] =
    {
        unit_test(test_get_start_time_process1),
        unit_test(test_get_start_time_process2),
        unit_test(test_get_start_time_process3),
        unit_test(test_get_start_time_process666),
        unit_test(test_get_state_process1),
        unit_test(test_get_state_process2),
        unit_test(test_get_state_process3),
        unit_test(test_get_state_process666),
    };

    return run_tests(tests);
}