-
Notifications
You must be signed in to change notification settings - Fork 5
/
ioctltests.c
246 lines (204 loc) · 5.97 KB
/
ioctltests.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "syscall.h"
#include "types.h"
#include "fcntl.h"
#include "ns_types.h"
#include "user.h"
/* Verify init
- verify the creation of tty devices
- on the init we are able to open all tty devices
- make sure no tty devices in use, e.g. make sure pouch containers occupy no tty device.
- no re-run tests afterwards
*/
int init_tests() {
int i;
int tty_fd;
char tty[] = "/ttyX";
for(i = 0; i<3; i++){
tty[4] = '0' + i;
if((tty_fd = open(tty, O_RDWR))<0){
printf(stderr, "failed to open %s\n",tty);
return -1;
}
close(tty_fd);
}
return 0;
}
/* Verify ioctl syscall attach/detatch
verify TTYGETS/TTY_SETS operations on attaching and detatching a tty device
EXAMPLE: if given tty is not attached, TTY_GET/DEV_ATTACH will return "0"
but TTY_GET/DEV_DETATCH will return "1", when we attach the given tty
using TTY_SET/DEV_ATTACH : TTY_GET/DEV_ATTACH will return "1" and TTY_GET/DEV_DETATCH
will return "0".
TTYSETS return "0" on operation success.
*/
int ioctl_attach_detach_test() {
int tty_fd;
char * tty_name = "tty0";
if((tty_fd = open(tty_name, O_RDWR)) < 0){
printf(stderr, "failed to open %s\n",tty_name);
return -1;
}
// pre condition, verify tty is not attached
if(ioctl(tty_fd, TTYGETS, DEV_ATTACH) != 0){
printf(stderr, "step 1. %s atach precondition failed \n",tty_name);
goto fail;
}
// test - attach tty
if(ioctl(tty_fd, TTYSETS, DEV_ATTACH) < 0){
printf(stderr, "step 2. %s failed TTYSETS & DEV_ATTACH \n",tty_name);
goto fail;
}
int status = ioctl(tty_fd, TTYGETS, DEV_ATTACH);
if(status == 0){
printf(stderr, "step 3. %s failed TTYGETS & DEV_ATTACH, expected: >0 \n",tty_name);
goto fail;
}
else if(status == -1){
printf(stderr, "step 3. ioctl failed");
goto fail;
}
else if(status == 1){
if(ioctl(tty_fd, TTYSETS, DEV_DETACH) == 0){
printf(stderr, "%s detached \n",tty_name);
}else{
printf(stderr, "step 3. %s detach failed \n",tty_name);
goto fail;
}
status = ioctl(tty_fd, TTYGETS, DEV_DETACH);
if(status == 0){
close(tty_fd);
return 0;
}
else if(status == -1){
printf(stderr, "step 3. ioctl GETS & DETACH failed");
goto fail;
}
}
fail:
close(tty_fd);
return -1;
}
/* Verify ioctl syscall connect/disconnect
verify TTYGETS/TTY_SETS operations on connecting and disconnecting a tty device
EXAMPLE: if given tty is not connected, TTY_GET/DEV_CONNECT will return "0"
but TTY_GET/DEV_DISCONNECT will return "1", when we connect the given tty
using TTY_SET/DEV_CONNECT : TTY_GET/DEV_CONNECT will return "1" and TTY_GET/DEV_DISCONNECT
will return "0".
TTYSETS return "0" on operation success.
*/
int ioctl_connect_disconnect_test() {
int tty_fd;
char * tty_name = "tty0";
if((tty_fd = open(tty_name, O_RDWR)) < 0){
printf(stderr, "failed to open %s\n",tty_name);
}
// pre condition, verify tty is not connected
if(ioctl(tty_fd, TTYGETS, DEV_CONNECT) != 0){
printf(stderr, "step 1. %s connect precondition failed \n",tty_name);
goto fail;
}
// test connect
if(ioctl(tty_fd, TTYSETS, DEV_CONNECT) < 0){
printf(stderr, "step 2. %s failed TTYSETS & DEV_CONNECT \n",tty_name);
goto fail;
}
int status = ioctl(tty_fd, TTYGETS, DEV_CONNECT);
if(ioctl(tty_fd, TTYSETS, DEV_DISCONNECT) == 0){
printf(stderr, "%s disconnected \n",tty_name);
if(ioctl(tty_fd, TTYSETS, DEV_DETACH) == 0){
printf(stderr, "%s detatched \n",tty_name);
}else{
goto fail;
}
}else{
goto fail;
}
if(status == 0){
printf(stderr, "step 3. %s failed TTYGETS & DEV_CONNECT, expected: > 0 \n",tty_name);
goto fail;
}
else if(status == -1){
printf(stderr, "step 3. ioctl failed");
goto fail;
}
else if(status == 1){
close(tty_fd);
return 0;
}
fail:
close(tty_fd);
return -1;
}
/* Verify that any attatch/connect operations to the console device fails
- console should not be used as a tty for a container.
*/
int ioctl_console_test() {
int tty_fd = 1; //CONSOLE
char * tty_name = "console";
if( ioctl(tty_fd, TTYGETS, DEV_CONNECT) == 0){
printf(stderr, " %s connected TTYGETS / DEV_CONNECT, expect: disconnected \n",tty_name);
return -1;
}
if(ioctl(tty_fd, TTYGETS, DEV_ATTACH) == 0){
printf(stderr, " %s attached TTYGETS / DEV_ATTACH, expect: detached \n",tty_name);
return -1;
}
if(ioctl(tty_fd, TTYSETS, DEV_ATTACH) == 0){
printf(stderr, " %s attached TTYSETS / DEV_ATTACH, expect: -1 \n",tty_name);
return -1;
}
if( ioctl(tty_fd, TTYSETS, DEV_CONNECT) == 0){
printf(stderr, " %s connected TTYSETS / DEV_CONNECT, expect: -1 \n",tty_name);
return -1;
}
return 0;
}
/* Verify wrong device usage
- only specified devices created by init (tty0-2) can be used as ttys
- mknod()is xv6 syscall to create a device, user should specify minor and major numbers
*/
int ioctl_wrong_device_test() {
int tty_fd;
char * tty_name = "tty_test";
if(mknod(tty_name,1,5) < 0){
printf(stderr, "failed to create test device %s\n",tty_name);
return -1;
}
if((tty_fd = open(tty_name, O_RDWR)) < 0){
printf(stderr, "failed to open %s\n",tty_name);
return -1;
}
int status = ioctl(tty_fd, TTYGETS, DEV_ATTACH);
unlink(tty_name);
if (status == 0){
printf(stderr, " %s device passed verification? - wrong! Expected: -1 \n",tty_name);
goto fail;
}
else if(status == 1){
printf(stderr, " %s return status: %d - wrong! Expected: -1 \n",tty_name, status);
goto fail;
}else if(status == -1){
close(tty_fd);
return 0;
}
fail:
close(tty_fd);
return -1;
}
int main() {
//TTY INIT TESTS
if(init_tests() < 0)
exit(1);
//ioctl TESTS
if(ioctl_wrong_device_test() < 0)
exit(1);
if(ioctl_console_test() < 0)
exit(1);
//ioctl SCENARIO TESTS
if(ioctl_attach_detach_test() < 0)
exit(1);
if(ioctl_connect_disconnect_test() < 0)
exit(1);
printf(stderr, "ioctl TESTS PASS:\n");
exit(0);
}