1 #include <test.h>
2
3 #include <compiler.h>
4 #include <process_lib.h>
5 #include <process_unix_priv.h>
6
7 /* Stubs for /proc/<pid>/stat. */
8
9 static const char *filecontents[2] = {
10 "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",
11 "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",
12 };
13
14 static int filepos[2];
15
open(const char * filename,ARG_UNUSED int flags,...)16 int open(const char *filename, ARG_UNUSED int flags, ...)
17 {
18 if (!strcmp(filename, "/proc/1/stat"))
19 {
20 filepos[0] = 0;
21 return 0;
22 }
23 else if (!strcmp(filename, "/proc/2/stat"))
24 {
25 filepos[1] = 0;
26 static int got_intr = false;
27 if (!got_intr)
28 {
29 got_intr = true;
30 errno = EINTR;
31 return -1;
32 }
33
34 return 1;
35 }
36 else if (!strcmp(filename, "/proc/666/stat"))
37 {
38 errno = EACCES;
39 return -1;
40 }
41 else
42 {
43 errno = ENOENT;
44 return -1;
45 }
46 }
47
read(int fd,void * buffer,ARG_UNUSED size_t buf_size)48 ssize_t read(int fd, void *buffer, ARG_UNUSED size_t buf_size)
49 {
50 if (fd == 0)
51 {
52 if (filepos[0] < strlen(filecontents[0]))
53 {
54 memcpy(buffer, filecontents[0], strlen(filecontents[0]));
55 filepos[0] = strlen(filecontents[0]);
56 return strlen(filecontents[0]);
57 }
58 else
59 {
60 return 0;
61 }
62 }
63
64 if (fd == 1)
65 {
66 static bool got_eintr = false;
67
68 if (!got_eintr)
69 {
70 got_eintr = true;
71 errno = EINTR;
72 return -1;
73 }
74 else
75 {
76 got_eintr = false;
77 }
78
79 if (filepos[1] < strlen(filecontents[1]))
80 {
81 memcpy(buffer, filecontents[1] + filepos[1], 1);
82 filepos[1]++;
83 return 1;
84 }
85 else
86 {
87 return 0;
88 }
89 }
90
91 errno = EIO;
92 return -1;
93 }
94
close(ARG_UNUSED int fd)95 int close(ARG_UNUSED int fd)
96 {
97 return 0;
98 }
99
test_get_start_time_process1(void)100 static void test_get_start_time_process1(void)
101 {
102 time_t t = GetProcessStartTime(1);
103 assert_int_equal(t, 65535 / sysconf(_SC_CLK_TCK));
104 }
105
106
test_get_start_time_process2(void)107 static void test_get_start_time_process2(void)
108 {
109 time_t t2 = GetProcessStartTime(2);
110 assert_int_equal(t2, 100000 / sysconf(_SC_CLK_TCK));
111 }
112
test_get_start_time_process3(void)113 static void test_get_start_time_process3(void)
114 {
115 time_t t3 = GetProcessStartTime(3);
116 assert_int_equal(t3, PROCESS_START_TIME_UNKNOWN);
117 }
118
test_get_start_time_process666(void)119 static void test_get_start_time_process666(void)
120 {
121 time_t t4 = GetProcessStartTime(666);
122 assert_int_equal(t4, PROCESS_START_TIME_UNKNOWN);
123 }
124
125
test_get_state_process1(void)126 static void test_get_state_process1(void)
127 {
128 ProcessState s = GetProcessState(1);
129 assert_int_equal(s, PROCESS_STATE_RUNNING);
130 }
131
test_get_state_process2(void)132 static void test_get_state_process2(void)
133 {
134 ProcessState s = GetProcessState(2);
135 assert_int_equal(s, PROCESS_STATE_STOPPED);
136 }
137
test_get_state_process3(void)138 static void test_get_state_process3(void)
139 {
140 ProcessState s = GetProcessState(3);
141 assert_int_equal(s, PROCESS_STATE_DOES_NOT_EXIST);
142 }
143
test_get_state_process666(void)144 static void test_get_state_process666(void)
145 {
146 ProcessState s = GetProcessState(666);
147 assert_int_equal(s, PROCESS_STATE_DOES_NOT_EXIST);
148 }
149
150
main()151 int main()
152 {
153 PRINT_TEST_BANNER();
154
155 const UnitTest tests[] =
156 {
157 unit_test(test_get_start_time_process1),
158 unit_test(test_get_start_time_process2),
159 unit_test(test_get_start_time_process3),
160 unit_test(test_get_start_time_process666),
161 unit_test(test_get_state_process1),
162 unit_test(test_get_state_process2),
163 unit_test(test_get_state_process3),
164 unit_test(test_get_state_process666),
165 };
166
167 return run_tests(tests);
168 }
169