-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathh2_test.cpp
More file actions
146 lines (126 loc) · 4.04 KB
/
h2_test.cpp
File metadata and controls
146 lines (126 loc) · 4.04 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
#include "rpc.h"
#include <algorithm>
#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) {
std::cerr << message << std::endl;
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(), 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");
}
} // namespace
int main() {
test_client_to_server();
test_server_to_client_after_request_headers();
test_fragmented_iovec();
test_large_payload();
std::cout << "h2_test: PASS" << std::endl;
return 0;
}