-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathhooks_connection_lifecycle.cpp
More file actions
215 lines (185 loc) · 7.71 KB
/
Copy pathhooks_connection_lifecycle.cpp
File metadata and controls
215 lines (185 loc) · 7.71 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
// Contract under test:
//
// "New integ test `hooks_connection_lifecycle`: opens a connection,
// registers one hook on each of the three phases, observes
// (connection_opened, accept_decision{accepted=true}, connection_closed)
// firing in order."
//
// We start a webserver, register a hook on each of the three lifecycle
// phases, drive one curl round-trip, stop the webserver (which tears
// down all keep-alive connections), and assert that:
// - all three phases fired at least once;
// - the accept_decision hook saw accepted=true and reason=nullopt;
// - peer.to_string() yields a non-empty IPv4-localhost string;
// - connection_closed is the LAST observed event (lifecycle invariant);
// - either connection_opened or accept_decision is the FIRST event
// (MHD's accept loop fires policy_callback before NOTIFY_STARTED,
// so the natural order is accept_decision first, but the test
// should not pin a specific MHD callback ordering — only the
// start/end bookends).
#include <curl/curl.h>
#include <atomic>
#include <cstddef>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
#include "./httpserver.hpp"
#include "./littletest.hpp"
#include "./server_ready.hpp"
using httpserver::create_webserver;
using httpserver::hook_phase;
using httpserver::http_request;
using httpserver::http_response;
using httpserver::webserver;
#define PORT 8199
namespace {
size_t writefunc(void* ptr, size_t size, size_t nmemb, std::string* s) {
s->append(reinterpret_cast<char*>(ptr), size * nmemb);
return size * nmemb;
}
class hello_resource : public httpserver::http_resource {
public:
http_response render_get(const http_request&) override {
return http_response::string("OK");
}
};
struct lifecycle_event {
std::string phase_name;
std::string peer_str;
bool accepted;
std::string reason;
bool reason_set;
};
} // namespace
LT_BEGIN_SUITE(hooks_connection_lifecycle_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(hooks_connection_lifecycle_suite)
LT_BEGIN_AUTO_TEST(hooks_connection_lifecycle_suite, all_three_phases_fire)
webserver ws{create_webserver(PORT)};
std::mutex log_mu;
std::vector<lifecycle_event> log;
auto record = [&log_mu, &log](lifecycle_event e) {
std::lock_guard<std::mutex> g(log_mu);
log.push_back(std::move(e));
};
auto h_open = ws.add_hook(hook_phase::connection_opened,
std::function<void(const httpserver::connection_open_ctx&)>(
[&](const httpserver::connection_open_ctx& ctx) {
record(lifecycle_event{"connection_opened",
ctx.peer.to_string(),
true, "", false});
}));
auto h_accept = ws.add_hook(hook_phase::accept_decision,
std::function<void(const httpserver::accept_ctx&)>(
[&](const httpserver::accept_ctx& ctx) {
record(lifecycle_event{"accept_decision",
ctx.peer.to_string(),
ctx.accepted,
ctx.reason.has_value()
? std::string(*ctx.reason)
: std::string{},
ctx.reason.has_value()});
}));
auto h_close = ws.add_hook(hook_phase::connection_closed,
std::function<void(const httpserver::connection_close_ctx&)>(
[&](const httpserver::connection_close_ctx& ctx) {
record(lifecycle_event{"connection_closed",
ctx.peer.to_string(),
true, "", false});
}));
auto resource = std::make_shared<hello_resource>();
ws.register_path("/hello", resource);
ws.start(false);
httpserver_test::wait_for_server_ready(PORT);
CURL* curl = curl_easy_init();
LT_ASSERT_NEQ(curl, static_cast<CURL*>(nullptr));
std::string url = "http://127.0.0.1:" + std::to_string(PORT) + "/hello";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
std::string body;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
LT_CHECK_EQ(res, CURLE_OK);
LT_CHECK_EQ(body, "OK");
// Stop the daemon: MHD tears down every live connection, so any
// pending connection_closed fires synchronously inside MHD_stop_daemon.
ws.stop();
// Take a stable snapshot of the log.
std::vector<lifecycle_event> snapshot;
{
std::lock_guard<std::mutex> g(log_mu);
snapshot = log;
}
// Count occurrences of each phase.
std::size_t opened_count = 0;
std::size_t accept_count = 0;
std::size_t closed_count = 0;
for (const auto& e : snapshot) {
if (e.phase_name == "connection_opened") ++opened_count;
else if (e.phase_name == "accept_decision") ++accept_count;
else if (e.phase_name == "connection_closed") ++closed_count;
}
LT_CHECK(opened_count >= 1);
LT_CHECK(accept_count >= 1);
LT_CHECK(closed_count >= 1);
// The accept hook should have observed accepted=true with no reason.
bool found_accept_decision = false;
for (const auto& e : snapshot) {
if (e.phase_name == "accept_decision") {
LT_CHECK_EQ(e.accepted, true);
LT_CHECK(!e.reason_set);
// The peer should be IPv4 loopback when the test client is
// local. (MHD may report 0.0.0.0 on some platforms when
// listening on an IPv6 dual stack; tolerate non-empty.)
LT_CHECK(!e.peer_str.empty());
found_accept_decision = true;
break;
}
}
LT_CHECK(found_accept_decision);
// Bookends:
// - connection_closed is the LAST event (lifecycle invariant)
// - the FIRST event is either connection_opened or accept_decision
// (MHD callback order is platform-dependent)
// - connection_opened must appear before connection_closed
// (the open-before-close invariant is always required)
LT_ASSERT(!snapshot.empty());
LT_CHECK_EQ(snapshot.back().phase_name, std::string("connection_closed"));
const std::string& first = snapshot.front().phase_name;
LT_CHECK(first == "connection_opened" || first == "accept_decision");
// Verify that connection_opened appears before connection_closed.
// Find the first index of each; connection_opened must come first.
std::size_t opened_idx = snapshot.size();
std::size_t closed_idx = snapshot.size();
for (std::size_t i = 0; i < snapshot.size(); ++i) {
if (snapshot[i].phase_name == "connection_opened" &&
opened_idx == snapshot.size()) {
opened_idx = i;
}
if (snapshot[i].phase_name == "connection_closed" &&
closed_idx == snapshot.size()) {
closed_idx = i;
}
}
// Both phases must have fired (already asserted above) and
// connection_opened must precede connection_closed.
LT_ASSERT(opened_idx < snapshot.size());
LT_ASSERT(closed_idx < snapshot.size());
LT_CHECK(opened_idx < closed_idx);
LT_END_AUTO_TEST(all_three_phases_fire)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()