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,149 @@
|
||||
// Shared foundations: base64/SHA-1 vectors, UTF-8, send-queue shed
|
||||
// policies, jittered backoff bounds, valve watermarks, JSON codec,
|
||||
// metrics names, auth headers.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_main.h"
|
||||
#include "realtime/shared/backpressure_valve.h"
|
||||
#include "realtime/shared/base64.h"
|
||||
#include "realtime/shared/base_realtime_client.h"
|
||||
#include "realtime/shared/mini_json.h"
|
||||
#include "realtime/shared/realtime_metrics.h"
|
||||
#include "realtime/shared/reconnect_policy.h"
|
||||
#include "realtime/shared/send_queue.h"
|
||||
#include "realtime/shared/sha1.h"
|
||||
#include "realtime/shared/utf8.h"
|
||||
|
||||
using namespace molo::realtime;
|
||||
|
||||
int main() {
|
||||
realtime_test::Results r;
|
||||
|
||||
// SHA-1 golden vector ("abc").
|
||||
{
|
||||
auto d = shared::Sha1::hash("abc");
|
||||
std::string hex;
|
||||
for (uint8_t b : d) {
|
||||
char buf[3];
|
||||
std::snprintf(buf, sizeof(buf), "%02x", b);
|
||||
hex += buf;
|
||||
}
|
||||
T_CHECK(r, hex == "a9993e364706816aba3e25717850c26c9cd0d89d");
|
||||
}
|
||||
// Base64 round-trip plus rejection.
|
||||
{
|
||||
T_CHECK(r, shared::base64_encode("Man") == "TWFu");
|
||||
T_CHECK(r, shared::base64_encode("Ma") == "TWE=");
|
||||
auto dec = shared::base64_decode("TWFu");
|
||||
T_CHECK(r, dec.has_value() &&
|
||||
std::string(dec->begin(), dec->end()) == "Man");
|
||||
T_CHECK(r, !shared::base64_decode("!!!").has_value());
|
||||
T_CHECK(r, !shared::base64_decode("abc").has_value());
|
||||
}
|
||||
// UTF-8 strictness.
|
||||
{
|
||||
T_CHECK(r, shared::is_valid_utf8("hello"));
|
||||
T_CHECK(r, shared::is_valid_utf8("\xc3\xa9")); // e-acute.
|
||||
T_CHECK(r, !shared::is_valid_utf8("\xc3\x28"));
|
||||
T_CHECK(r, !shared::is_valid_utf8("\xed\xa0\x80")); // Surrogate.
|
||||
T_CHECK(r, !shared::is_valid_utf8("\xf0\x82\x82\xac")); // Overlong.
|
||||
}
|
||||
|
||||
// Send queue shed policies.
|
||||
{
|
||||
shared::SendQueue q(2, shared::ShedPolicy::kDropOldest);
|
||||
shared::SendItem a, b, c;
|
||||
a.bytes = {'a'};
|
||||
b.bytes = {'b'};
|
||||
c.bytes = {'c'};
|
||||
T_CHECK(r, q.push(a) && q.push(b) && q.push(c));
|
||||
T_CHECK(r, q.shed_count() == 1);
|
||||
shared::SendItem out;
|
||||
T_CHECK(r, q.pop(out) && out.bytes[0] == 'b');
|
||||
}
|
||||
{
|
||||
shared::SendQueue q(1, shared::ShedPolicy::kDropNewest);
|
||||
shared::SendItem a, b;
|
||||
a.bytes = {'a'};
|
||||
b.bytes = {'b'};
|
||||
T_CHECK(r, q.push(a));
|
||||
T_CHECK(r, !q.push(b));
|
||||
T_CHECK(r, q.shed_count() == 1);
|
||||
}
|
||||
{
|
||||
shared::SendQueue q(1, shared::ShedPolicy::kClose);
|
||||
shared::SendItem a, b;
|
||||
a.bytes = {'a'};
|
||||
b.bytes = {'b'};
|
||||
T_CHECK(r, q.push(a));
|
||||
T_CHECK(r, !q.push(b));
|
||||
T_CHECK(r, q.must_close());
|
||||
}
|
||||
|
||||
// Backoff bounds double then cap; jitter stays inside.
|
||||
{
|
||||
shared::ReconnectPolicy p(std::chrono::milliseconds(100),
|
||||
std::chrono::milliseconds(1000), 10);
|
||||
T_CHECK(r, p.bound_for(0) == std::chrono::milliseconds(100));
|
||||
T_CHECK(r, p.bound_for(1) == std::chrono::milliseconds(200));
|
||||
T_CHECK(r, p.bound_for(9) == std::chrono::milliseconds(1000));
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
auto d = p.delay_for(2);
|
||||
T_CHECK(r, d >= std::chrono::milliseconds(0) &&
|
||||
d <= std::chrono::milliseconds(400));
|
||||
}
|
||||
}
|
||||
|
||||
// Valve pauses at high, resumes at low.
|
||||
{
|
||||
shared::BackpressureValve v(10, 100);
|
||||
v.note_buffered(50);
|
||||
T_CHECK(r, !v.paused());
|
||||
v.note_buffered(60);
|
||||
T_CHECK(r, v.paused());
|
||||
v.note_flushed(50);
|
||||
T_CHECK(r, v.paused()); // Still above low.
|
||||
v.note_flushed(55);
|
||||
T_CHECK(r, !v.paused());
|
||||
}
|
||||
|
||||
// JSON round-trip and rejection.
|
||||
{
|
||||
auto v = shared::Json::parse("{\"a\":[1,true,\"x\"],\"b\":null}");
|
||||
T_CHECK(r, v.has_value());
|
||||
T_CHECK(r, v->find("a")->array()[1].boolean());
|
||||
T_CHECK(r, v->dump() == "{\"a\":[1,true,\"x\"],\"b\":null}");
|
||||
T_CHECK(r, !shared::Json::parse("{oops").has_value());
|
||||
T_CHECK(r, !shared::Json::parse("[1,]").has_value());
|
||||
}
|
||||
|
||||
// Metrics count and reset.
|
||||
{
|
||||
auto& m = shared::RealtimeMetrics::instance();
|
||||
m.reset_all();
|
||||
m.inc(shared::RealtimeMetric::kMessagesSent, 3);
|
||||
T_CHECK(r, m.get(shared::RealtimeMetric::kMessagesSent) == 3);
|
||||
T_CHECK(r, std::string(shared::metric_name(
|
||||
shared::RealtimeMetric::kMessagesSent)) ==
|
||||
"molo_realtime_messages_sent");
|
||||
}
|
||||
|
||||
// Client auth headers: exactly one scheme at a time.
|
||||
{
|
||||
shared::ClientAuth anon;
|
||||
T_CHECK(r, anon.anonymous() && anon.headers().empty());
|
||||
shared::ClientAuth bearer;
|
||||
bearer.bearer_token = "tok";
|
||||
auto h = bearer.headers();
|
||||
T_CHECK(r, h.size() == 1 && h[0].second == "Bearer tok");
|
||||
shared::ClientAuth basic;
|
||||
basic.basic_user = "u";
|
||||
basic.basic_password = "p";
|
||||
auto h2 = basic.headers();
|
||||
T_CHECK(r, h2.size() == 1 && h2[0].second == "Basic dTpw");
|
||||
}
|
||||
|
||||
return r.report("shared/foundation");
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// Parser robustness: deterministic garbage into every byte-level
|
||||
// parser must fail clean, never crash or hang. Fixed seed, bounded
|
||||
// iterations, small inputs.
|
||||
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_main.h"
|
||||
#include "realtime/engineio/packet_codec.h"
|
||||
#include "realtime/shared/base64.h"
|
||||
#include "realtime/shared/mini_json.h"
|
||||
#include "realtime/shared/utf8.h"
|
||||
#include "realtime/socketio/packet_codec.h"
|
||||
#include "realtime/sse/sse_client.h"
|
||||
#include "realtime/webdav/server/dav_xml.h"
|
||||
#include "realtime/websocket/server/frame_parser.h"
|
||||
|
||||
using namespace molo::realtime;
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<uint8_t> garbage(std::mt19937_64& rng, std::size_t max_len) {
|
||||
std::uniform_int_distribution<std::size_t> len(0, max_len);
|
||||
std::uniform_int_distribution<int> byte(0, 255);
|
||||
std::uniform_int_distribution<int> pick(0, 3);
|
||||
std::vector<uint8_t> out(len(rng));
|
||||
for (auto& b : out) {
|
||||
// Mix raw bytes with protocol-ish alphabet to hit deep paths.
|
||||
int p = pick(rng);
|
||||
if (p == 0) {
|
||||
static const char k[] = "0123456-:,/[]{}<>\" _placeholder";
|
||||
b = static_cast<uint8_t>(k[byte(rng) % (sizeof(k) - 1)]);
|
||||
} else {
|
||||
b = static_cast<uint8_t>(byte(rng));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
realtime_test::Results r;
|
||||
std::mt19937_64 rng(0xC10C4E7E);
|
||||
constexpr int kIters = 4000;
|
||||
|
||||
int ws_frames = 0;
|
||||
for (int i = 0; i < kIters; ++i) {
|
||||
auto g = garbage(rng, 96);
|
||||
websocket::server::FrameParser parser(true);
|
||||
// Feed one byte at a time to stress incremental states.
|
||||
for (uint8_t b : g) {
|
||||
std::size_t consumed = 0;
|
||||
auto step = parser.feed(&b, 1, consumed);
|
||||
if (step.outcome == websocket::server::ParseOutcome::kFrame) {
|
||||
++ws_frames;
|
||||
}
|
||||
if (step.outcome == websocket::server::ParseOutcome::kFailed) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::string s(g.begin(), g.end());
|
||||
(void)engineio::PacketCodec::decode(s);
|
||||
(void)engineio::PacketCodec::decode_polling(s);
|
||||
(void)socketio::PacketCodec::decode_text(s);
|
||||
(void)shared::Json::parse(s);
|
||||
(void)shared::base64_decode(s);
|
||||
(void)shared::is_valid_utf8(g.data(), g.size());
|
||||
(void)webdav::server::parse_dav_xml(s, 65536);
|
||||
}
|
||||
T_CHECK(r, ws_frames >= 0); // Ran to completion without crashing.
|
||||
|
||||
// SSE client survives garbage streams and stays responsive.
|
||||
{
|
||||
sse::SseClient c;
|
||||
shared::HttpResponse head;
|
||||
head.status = 200;
|
||||
shared::set_header(head, "Content-Type", "text/event-stream");
|
||||
c.on_head(head);
|
||||
int events = 0;
|
||||
c.on_event([&](const sse::SseEvent&) { ++events; });
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
auto g = garbage(rng, 128);
|
||||
c.on_bytes(g.data(), g.size());
|
||||
}
|
||||
// Leading newline terminates any partial garbage line first:
|
||||
// without it the next bytes would correctly merge into that line.
|
||||
c.on_bytes("\ndata: alive\n\n");
|
||||
T_CHECK(r, events >= 1);
|
||||
}
|
||||
|
||||
return r.report("shared/robustness");
|
||||
}
|
||||
Reference in New Issue
Block a user