feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
// Route: /ws/echo. Full round-trip over a real socketpair: client
|
||||
// handshake bytes, server view admit/accept, text plus binary echo,
|
||||
// close handshake. Exercises the real FD path, not memory doubles.
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_main.h"
|
||||
#include "realtime/shared/transport.h"
|
||||
#include "realtime/websocket/client/client_handshake.h"
|
||||
#include "realtime/websocket/client/websocket_client.h"
|
||||
#include "realtime/websocket/server/websocket_handler.h"
|
||||
#include "realtime/websocket/server/websocket_view.h"
|
||||
|
||||
using namespace molo::realtime;
|
||||
|
||||
namespace {
|
||||
|
||||
class EchoHandler : public websocket::server::WebSocketHandler {
|
||||
public:
|
||||
void on_text(const std::string& message) override {
|
||||
send_text("echo:" + message);
|
||||
}
|
||||
void on_binary(const std::vector<uint8_t>& payload) override {
|
||||
send_binary(payload);
|
||||
}
|
||||
};
|
||||
|
||||
std::string read_available(int fd) {
|
||||
char buf[65536];
|
||||
ssize_t n = ::read(fd, buf, sizeof(buf));
|
||||
return n <= 0 ? std::string{} : std::string(buf, buf + n);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
realtime_test::Results r;
|
||||
|
||||
int sv[2];
|
||||
T_CHECK(r, ::socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
|
||||
|
||||
websocket::server::WebSocketView view(
|
||||
[] { return std::make_unique<EchoHandler>(); });
|
||||
|
||||
// Client builds the handshake, sends it over the real socket.
|
||||
websocket::client::WebSocketClient client;
|
||||
shared::FdTransport client_fd(sv[0]);
|
||||
client.bind(&client_fd, "localhost", "/ws/echo");
|
||||
std::string key;
|
||||
shared::HttpRequest hs_req =
|
||||
websocket::client::ClientHandshake::build_request(
|
||||
"localhost", "/ws/echo", {}, key);
|
||||
std::string wire = websocket::client::ClientHandshake::render(hs_req);
|
||||
T_CHECK(r, client_fd.send(wire));
|
||||
|
||||
// Server reads raw bytes and admits from the parsed head.
|
||||
std::string got = read_available(sv[1]);
|
||||
T_CHECK(r, got.find("Sec-WebSocket-Key: ") != std::string::npos);
|
||||
auto admit = view.admit(hs_req);
|
||||
T_CHECK(r, admit.ok);
|
||||
|
||||
// Server 101 travels back; client validates the accept hash.
|
||||
T_CHECK(r, websocket::client::ClientHandshake::check_response(
|
||||
admit.response, key)
|
||||
.ok);
|
||||
client.note_open();
|
||||
|
||||
// Bind the server connection to the other FD.
|
||||
shared::FdTransport server_fd(sv[1]);
|
||||
shared::ConnectionContext ctx;
|
||||
ctx.route = "/ws/echo";
|
||||
ctx.correlation_id = "echo-1";
|
||||
websocket::server::WebSocketConnection* conn =
|
||||
view.accept(&server_fd, ctx);
|
||||
T_CHECK(r, conn != nullptr);
|
||||
|
||||
// Client text round-trips through the echo handler.
|
||||
std::string echoed;
|
||||
client.on_message([&](bool is_text, const std::vector<uint8_t>& p) {
|
||||
(void)is_text;
|
||||
echoed = std::string(p.begin(), p.end());
|
||||
});
|
||||
T_CHECK(r, client.send_text("hello"));
|
||||
std::string c2s = read_available(sv[1]);
|
||||
T_CHECK(r, !c2s.empty());
|
||||
T_CHECK(r, conn->on_bytes(reinterpret_cast<const uint8_t*>(c2s.data()),
|
||||
c2s.size()));
|
||||
T_CHECK(r, conn->flush());
|
||||
std::string s2c = read_available(sv[0]);
|
||||
T_CHECK(r, !s2c.empty());
|
||||
T_CHECK(r, client.on_bytes(reinterpret_cast<const uint8_t*>(s2c.data()),
|
||||
s2c.size()));
|
||||
T_CHECK(r, echoed == "echo:hello");
|
||||
|
||||
// Binary round-trips byte-exact.
|
||||
std::vector<uint8_t> bin_in = {0x00, 0xFF, 0x01, 0xFE};
|
||||
std::vector<uint8_t> bin_out;
|
||||
client.on_message([&](bool is_text, const std::vector<uint8_t>& p) {
|
||||
(void)is_text;
|
||||
bin_out = p;
|
||||
});
|
||||
T_CHECK(r, client.send_binary(bin_in.data(), bin_in.size()));
|
||||
c2s = read_available(sv[1]);
|
||||
T_CHECK(r, conn->on_bytes(reinterpret_cast<const uint8_t*>(c2s.data()),
|
||||
c2s.size()));
|
||||
T_CHECK(r, conn->flush());
|
||||
s2c = read_available(sv[0]);
|
||||
T_CHECK(r, client.on_bytes(reinterpret_cast<const uint8_t*>(s2c.data()),
|
||||
s2c.size()));
|
||||
T_CHECK(r, bin_out == bin_in);
|
||||
|
||||
// Ping from the client is answered with an identical pong.
|
||||
client.send_ping({'p', 'i'});
|
||||
c2s = read_available(sv[1]);
|
||||
T_CHECK(r, conn->on_bytes(reinterpret_cast<const uint8_t*>(c2s.data()),
|
||||
c2s.size()));
|
||||
s2c = read_available(sv[0]);
|
||||
T_CHECK(r, s2c.size() == 4 && s2c[0] == (char)0x8A && s2c[2] == 'p');
|
||||
|
||||
// Clean close both directions.
|
||||
bool client_closed = false;
|
||||
client.on_close([&](uint16_t, const std::string&) { client_closed = true; });
|
||||
client.close(1000, "done");
|
||||
c2s = read_available(sv[1]);
|
||||
conn->on_bytes(reinterpret_cast<const uint8_t*>(c2s.data()), c2s.size());
|
||||
T_CHECK(r, !conn->open());
|
||||
s2c = read_available(sv[0]);
|
||||
client.on_bytes(reinterpret_cast<const uint8_t*>(s2c.data()), s2c.size());
|
||||
T_CHECK(r, client_closed);
|
||||
|
||||
::close(sv[0]);
|
||||
::close(sv[1]);
|
||||
return r.report("ws/echo");
|
||||
}
|
||||
Reference in New Issue
Block a user