-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_select.c
81 lines (62 loc) · 1.61 KB
/
test_select.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
#include <stdlib.h>
#include <sys/socket.h>
#include <assert.h>
#include <unistd.h>
#include <sys/select.h>
#include "test_helper.h"
#include "tests.h"
int test_select (void) {
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s > 2);
assert(bind(s, NULL, 0) == 0);
assert(listen(s, 1) == 0);
fd_set rfds, wfds;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(s, &rfds);
FD_SET(s, &wfds);
FD_SET(1, &wfds);
assert(select(s + 1, &rfds, &wfds, NULL, NULL) == 1);
assert(FD_ISSET(s, &rfds));
assert(!FD_ISSET(s, &wfds));
assert(!FD_ISSET(1, &wfds));
int c = accept(s, NULL, NULL);
assert(c > s);
FD_SET(s, &rfds);
FD_SET(s, &wfds);
FD_SET(c, &rfds);
FD_SET(c, &wfds);
assert(select(c + 1, &rfds, &wfds, NULL, NULL) == 2);
assert(FD_ISSET(c, &rfds));
assert(FD_ISSET(c, &wfds));
assert(!FD_ISSET(s, &rfds));
assert(!FD_ISSET(s, &wfds));
close(c);
FD_SET(s, &rfds);
FD_SET(s, &wfds);
FD_SET(c, &rfds);
FD_SET(c, &wfds);
assert(select(c + 1, &rfds, &wfds, NULL, NULL) == 1);
assert(FD_ISSET(s, &rfds));
assert(!FD_ISSET(s, &wfds));
assert(!FD_ISSET(c, &rfds));
assert(!FD_ISSET(c, &wfds));
close(s);
return TEST_SUCCESS;
}
int test_passthrough (void) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
struct timeval tv = {
.tv_sec = 1,
.tv_usec = 0,
};
assert(select(2, &rfds, NULL, NULL, &tv) == 0);
return TEST_SUCCESS;
}
test_fn tests [] = {
test_select,
test_passthrough,
NULL,
};