-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathh2_test.cpp
More file actions
201 lines (177 loc) · 6.23 KB
/
Copy pathh2_test.cpp
File metadata and controls
201 lines (177 loc) · 6.23 KB
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
#include "lupine_log.h"
#include "rpc.h"
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <vector>
namespace {
struct h2_pair {
conn_t client = {};
conn_t server = {};
~h2_pair() {
if (client.connfd >= 0) {
close(client.connfd);
}
if (server.connfd >= 0) {
close(server.connfd);
}
}
};
void require(bool condition, const char *message) {
if (!condition) {
LUPINE_LOG_ERROR(message);
std::exit(1);
}
}
h2_pair make_pair() {
int fds[2] = {-1, -1};
require(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0, "socketpair failed");
h2_pair pair;
pair.client.connfd = fds[0];
pair.server.connfd = fds[1];
require(rpc_http2_client_init(&pair.client) == 0, "client h2 init failed");
require(rpc_http2_server_init(&pair.server) == 0, "server h2 init failed");
return pair;
}
void write_all(conn_t *conn, const std::vector<std::string> &chunks) {
std::vector<struct iovec> iov;
iov.reserve(chunks.size());
for (const std::string &chunk : chunks) {
iov.push_back({const_cast<char *>(chunk.data()), chunk.size()});
}
require(rpc_http2_writev(conn, iov.data(), nullptr,
static_cast<int>(iov.size())) == 0,
"h2 write failed");
}
std::string read_string(conn_t *conn, size_t size) {
std::string output(size, '\0');
require(rpc_http2_read(conn, output.data(), output.size()) ==
static_cast<int>(output.size()),
"h2 read failed");
return output;
}
void test_client_to_server() {
h2_pair pair = make_pair();
std::string message = "hello over h2";
std::string received;
std::thread reader(
[&] { received = read_string(&pair.server, message.size()); });
write_all(&pair.client, {message});
reader.join();
require(received == message, "client-to-server payload mismatch");
}
void test_server_to_client_after_request_headers() {
h2_pair pair = make_pair();
std::string request = "request";
std::string response = "response";
std::string received_request;
std::thread reader(
[&] { received_request = read_string(&pair.server, request.size()); });
write_all(&pair.client, {request});
reader.join();
require(received_request == request, "server did not receive request");
write_all(&pair.server, {response});
require(read_string(&pair.client, response.size()) == response,
"server-to-client payload mismatch");
}
void test_fragmented_iovec() {
h2_pair pair = make_pair();
std::vector<std::string> chunks = {"alpha", "", ":", "beta", ":gamma"};
std::string received;
std::thread reader([&] { received = read_string(&pair.server, 16); });
write_all(&pair.client, chunks);
reader.join();
require(received == "alpha:beta:gamma", "fragmented payload mismatch");
}
void exchange_settings(h2_pair *pair) {
std::string request = "x";
std::string response = "y";
std::string received_request;
std::thread reader(
[&] { received_request = read_string(&pair->server, request.size()); });
write_all(&pair->client, {request});
reader.join();
require(received_request == request, "settings exchange request mismatch");
write_all(&pair->server, {response});
require(read_string(&pair->client, response.size()) == response,
"settings exchange response mismatch");
}
void test_large_payload() {
h2_pair pair = make_pair();
exchange_settings(&pair);
std::string payload(2 * 1024 * 1024, '\0');
for (size_t i = 0; i < payload.size(); ++i) {
payload[i] = static_cast<char>('a' + (i % 26));
}
size_t midpoint = payload.size() / 2;
std::string received;
std::thread reader(
[&] { received = read_string(&pair.server, payload.size()); });
write_all(&pair.client,
{payload.substr(0, midpoint), payload.substr(midpoint)});
reader.join();
require(received == payload, "large payload mismatch");
}
// Round-trips a multi-block LZ4-framed payload: the transport compresses it
// lazily block by block (h2.cpp) and rpc_read_payload_part decodes it with
// chunked, block-aligned reads (compress.cpp). The payload mixes
// compressible and random data so both compressed and raw block tokens are
// exercised, and plain iovecs surround the framed one as in a real message.
void test_framed_payload_round_trip() {
h2_pair pair = make_pair();
exchange_settings(&pair);
std::string prefix = "head";
std::string suffix = "tail";
std::vector<char> payload(2 * LUPINE_COMPRESS_BLOCK_BYTES + 123457);
unsigned int seed = 42;
for (size_t i = 0; i < payload.size() / 2; ++i) {
payload[i] = static_cast<char>(i % 7);
}
for (size_t i = payload.size() / 2; i < payload.size(); ++i) {
seed = seed * 1664525u + 1013904223u;
payload[i] = static_cast<char>(seed >> 24);
}
std::string received_prefix;
std::string received_suffix;
std::vector<char> received(payload.size());
std::thread reader([&] {
received_prefix = read_string(&pair.server, prefix.size());
size_t first = LUPINE_COMPRESS_BLOCK_BYTES;
require(rpc_read_payload_part(&pair.server, 1, received.data(), first) ==
static_cast<int>(first),
"framed read part 1 failed");
require(rpc_read_payload_part(&pair.server, 1, received.data() + first,
received.size() - first) ==
static_cast<int>(received.size() - first),
"framed read part 2 failed");
received_suffix = read_string(&pair.server, suffix.size());
});
struct iovec iov[3] = {
{const_cast<char *>(prefix.data()), prefix.size()},
{payload.data(), payload.size()},
{const_cast<char *>(suffix.data()), suffix.size()},
};
unsigned char framed[3] = {0, 1, 0};
require(rpc_http2_writev(&pair.client, iov, framed, 3) == 0,
"framed write failed");
reader.join();
require(received_prefix == prefix, "framed prefix mismatch");
require(received == payload, "framed payload mismatch");
require(received_suffix == suffix, "framed suffix mismatch");
}
} // namespace
int main() {
test_client_to_server();
test_server_to_client_after_request_headers();
test_fragmented_iovec();
test_large_payload();
test_framed_payload_round_trip();
std::cout << "h2_test: PASS" << std::endl;
return 0;
}