-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_multi.c
132 lines (94 loc) · 3.02 KB
/
test_multi.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "test_helper.h"
#include "tests.h"
int test_multi_simple (void) {
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s > 2);
assert(connect(s, NULL, 0) == 0);
stdin_file_create("AAAA---BBBB---");
char buf[256];
assert(read(s, buf, sizeof(buf)) == 4);
assert(memcmp(buf, "AAAA", 4) == 0);
assert(read(s, buf, sizeof(buf)) == 4);
assert(memcmp(buf, "BBBB", 4) == 0);
assert(read(s, buf, sizeof(buf)) == 0);
stdin_file_destroy();
close(s);
return TEST_SUCCESS;
}
int test_multi_simple_fucked_up (void) {
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s > 2);
assert(connect(s, NULL, 0) == 0);
stdin_file_create("AAAA---BBBB---");
char buf[256];
assert(read(s, buf, 4) == 4);
assert(memcmp(buf, "AAAA", 4) == 0);
assert(read(s, buf, sizeof(buf)) == 0);
assert(read(s, buf, 4) == 4);
assert(memcmp(buf, "BBBB", 4) == 0);
assert(read(s, buf, sizeof(buf)) == 0);
assert(read(s, buf, sizeof(buf)) == 0);
stdin_file_destroy();
close(s);
return TEST_SUCCESS;
}
int test_multi_partial (void) {
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s > 2);
assert(connect(s, NULL, 0) == 0);
stdin_file_create("AXYZ---B12---CD-----");
char buf[256];
assert(read(s, buf, 5) == 4);
assert(memcmp(buf, "AXYZ", 4) == 0);
assert(read(s, buf, 5) == 3);
assert(memcmp(buf, "B12", 3) == 0);
assert(read(s, buf, 2) == 2);
assert(memcmp(buf, "CD", 2) == 0);
assert(read(s, buf, sizeof(buf)) == 0);
assert(read(s, buf, sizeof(buf)) == 2);
assert(memcmp(buf, "--", 2) == 0);
assert(read(s, buf, sizeof(buf)) == 0);
stdin_file_destroy();
close(s);
return TEST_SUCCESS;
}
int test_multi_peek (void) {
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s > 2);
assert(connect(s, NULL, 0) == 0);
stdin_file_create("12345678---ABC---");
char buf[256];
// Peek only for the next packet, never beyond
for (size_t i = 1; i < 8; ++i) {
assert(recv(s, buf, i, MSG_PEEK) == (ssize_t) i);
assert(memcmp(buf, "12345678", i) == 0);
}
assert(recv(s, buf, 9, MSG_PEEK) == 8);
assert(recv(s, buf, 10, MSG_PEEK) == 8);
assert(recv(s, buf, 11, MSG_PEEK) == 8);
assert(recv(s, buf, sizeof(buf), MSG_PEEK) == 8);
// Release peekbuffer
assert(recv(s, buf, sizeof(buf), 0) == 8);
// Peek but without locking peekbuffer
assert(recv(s, buf, 2, MSG_PEEK) == 2);
assert(memcmp(buf, "AB", 2) == 0);
assert(recv(s, buf, 4, 0) == 3);
assert(memcmp(buf, "ABC", 2) == 0);
assert(recv(s, buf, sizeof(buf), 0) == 0);
stdin_file_destroy();
close(s);
return TEST_SUCCESS;
}
test_fn tests [] = {
test_multi_simple,
test_multi_simple_fucked_up,
test_multi_partial,
test_multi_peek,
NULL,
};