forked from mit-pdos/xv6-riscv
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dump2tests.c
208 lines (167 loc) · 4.04 KB
/
dump2tests.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "kernel/types.h"
#include "kernel/syscall.h"
#include "user/user.h"
#ifdef SYS_dump2
int success;
void test1();
void test2();
void test3();
void test4();
#endif
int main(void)
{
printf("dump2 tests started\n");
#ifndef SYS_dump2
printf("no dump2 syscall found. Stop testing\n");
goto no_dump2;
#endif
#ifdef SYS_dump2
printf("dump2 syscall found. Start testing\n");
success = 0;
test1();
test2();
test3();
test4();
printf("4 tests were run. %d tests passed\n", success);
#endif
#ifndef SYS_dump2
no_dump2:
#endif
exit(0);
}
#ifdef SYS_dump2
int dump2_test1_asm(int pipefd, char *str, int len);
void test1()
{
printf("test 1 started\n");
int pipefd[2];
pipe(pipefd);
int child_proc = fork();
if (child_proc == 0) {
uint64 a = 34381;
dump2_test1_asm(pipefd[1], (char *)(&a), 8);
} else {
uint64 a;
read(pipefd[0], &a, 8);
for (int i = 2; i < 12; i++) {
uint64 value;
int error = dump2(child_proc, i, &value);
if (error != 0) {
printf("[ERROR] dump2 returned unexpected error %d\n", error);
goto failed;
}
if ((int)value != i) {
printf("[ERROR] expected: %d, found: %ld\n", i, value);
goto failed;
}
}
}
printf("[SUCCESS] test 1 passed\n");
success++;
failed:
if (child_proc > 0) {
kill(child_proc);
}
printf("test 1 finished\n");
}
int dump2_test2_asm(int pipefd, char *str, int len);
void test2()
{
printf("test 2 started\n");
int pipefd[2];
pipe(pipefd);
int child_proc = fork();
if (child_proc == 0) {
uint64 a = 34381;
dump2_test2_asm(pipefd[1], (char *)(&a), 8);
} else {
uint64 a;
read(pipefd[0], &a, 8);
for (int i = 2; i < 12; i++) {
uint64 value;
int error = dump2(child_proc, i, &value);
if (error != 0) {
printf("[ERROR] dump2 returned unexpected error %d\n", error);
goto failed;
}
if (value != i * i) {
printf("[ERROR] expected: %d, found %ld\n", i * i, value);
goto failed;
}
}
}
printf("[SUCCESS] test 2 passed\n");
success++;
failed:
if (child_proc > 0) {
kill(child_proc);
}
printf("test 2 finished\n");
}
int dump2_test3_asm(int pid, uint64 *ptr);
void test3() {
printf("test 3 started\n");
uint64 value;
int result = dump2_test3_asm(getpid(), &value);
if (result != 0) {
printf("[ERROR] dump2 returned unexpected error %d\n", result);
goto failed;
}
if (value != 1337) {
printf("[ERROR] expected: 1337, found: %ld", value);
goto failed;
}
printf("[SUCCESS] test 3 passed\n");
success++;
failed:
printf("test 3 finished\n");
}
void test4()
{
printf("test 4 started\n");
uint64 a;
printf("[INFO] testing nonexisting proccess\n");
int error = dump2(2147483647, 10, &a);
if (error != -2) {
printf("[ERROR] dump2 returned unexpected value %d, expected -2\n", error);
goto failed;
}
printf("[OK] nonexisting proccess\n");
printf("[INFO] testing illegal access to registers\n");
int pipefd[2];
pipe(pipefd);
int parent_pid = getpid();
int child_proc = fork();
if (child_proc == 0) {
error = dump2(parent_pid, 10, &a);
write(pipefd[1], &error, 4);
exit(0);
} else {
read(pipefd[0], &error, 4);
if (error != -1) {
printf("[ERROR] dump2 returned unexpected value %d, expected -1\n",
error);
goto failed;
}
}
printf("[OK] illegal access to registers\n");
printf("[INFO] testing incorrect number of register\n");
error = dump2(parent_pid, 1337, &a);
if (error != -3) {
printf("[ERROR] dump2 returned unexpected value %d, expected -3\n", error);
goto failed;
}
printf("[OK] incorrect number of register\n");
printf("[INFO] testing invalid memory address\n");
error = dump2(parent_pid, 10, &a + 123456789);
if (error != -4) {
printf("[ERROR] dump2 returned unexpected value %d, expected -4\n", error);
goto failed;
}
printf("[OK] invalid memory address\n");
printf("[SUCCESS] test 4 passed\n");
success++;
failed:
printf("test 4 finished\n");
}
#endif