Repository navigation
Expand file tree
/
Copy pathcontext_logging_tests.cpp
More file actions
304 lines (291 loc) · 12.1 KB
/
Copy pathcontext_logging_tests.cpp
File metadata and controls
304 lines (291 loc) · 12.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "../../tests/support/runtime_logging_fixture.hpp"
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/log_manager.hpp>
#include <teaql/core/detail/debug.hpp>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <filesystem>
#include <iostream>
#include <limits>
#include <unistd.h>
using namespace teaql;
static void check(bool ok, const char* message) {
if (!ok) {
throw std::runtime_error(message);
}
}
template <class F>
void rejected(F&& f) {
bool fail = false;
try {
f();
} catch (const std::logic_error&) {
fail = true;
}
check(fail, "closed Context rejects logging API");
}
static void quiet() {
setenv("TEAQL_LOG_ENDPOINT", "off", 1);
unsetenv("TEAQL_ALLOW_SENSITIVE_PLAINTEXT_LOGS");
unsetenv("TEAQL_SQL_LOG_TABLES");
unsetenv("TEAQL_SQL_LOG");
setenv("TEAQL_LOG_FORMAT", "debug", 1);
}
static ExecutionMetadata metadata(Memory r) {
ExecutionMetadata m(std::allocator_arg, Allocator(r), DataServiceOperation::Query);
m.backend = "sqlite";
m.parameterized_query.emplace("SELECT ?, ?", r);
m.debug_query.emplace("RAW private-customer private-password", r);
m.params.emplace_back(Value(r, "private-customer"));
m.params.emplace_back(Value(r, "private-password"));
m.sql_log.generated_sql = true;
m.sql_log.parameter_policies = {SqlParameterLogPolicy::Masked,
SqlParameterLogPolicy::Credential};
m.result_count = 0;
m.trace_chain.emplace_back(TraceKind::Request, "Order", "private-customer private-password", 7);
m.started_at = ExecutionMetadata::SystemTime(std::chrono::nanoseconds(-1));
m.ended_at = ExecutionMetadata::SystemTime(std::chrono::nanoseconds(1500));
return m;
}
static void lifetime_and_options() {
quiet();
GlobalContext global;
test::RuntimeLoggingFixture logging_fixture(global);
auto& retained = global.make<UnifiedLogBuffer>();
auto user = logging_fixture.make_user_context();
user->insert_resource(global, retained);
auto r = user->memory_resource();
user->with_user_identifier("operator whose identity lives in the User arena");
{
auto m = metadata(r);
user->record_metadata_log(m);
}
check(retained.entries.size() == 1, "unified buffer receives safe entry");
check(retained.entries[0].memory_resource() == global.memory_resource(),
"registered Global buffer deep copies into its explicit domain");
{
auto copy = user->sql_logs();
copy[0].sql = "altered snapshot";
check(user->sql_logs()[0].sql == "SELECT ?, ?", "sql_logs returns a deep snapshot");
}
user->set_sql_log_options(SqlLogOptions::disabled());
check(user->sql_logs().size() == 1, "setting disabled options does not clear history");
user->disable_sql_log();
check(user->sql_logs().empty() && retained.entries.size() == 1,
"disable clears SQL history without clearing unified history");
user->enable_select_sql_log();
{
auto m = metadata(r);
user->record_metadata_log(m);
}
user->clear_sql_logs();
check(retained.entries.size() == 2, "clear leaves separately registered buffer unchanged");
auto& snapshot = global.make<Vector<SqlLogEntry>>(user->sql_logs());
check(snapshot.empty(), "explicit cross-domain snapshot");
user->close();
check(user->allocation_stats().live_blocks == 0, "log and identity blocks returned at close");
auto& item = retained.entries[0];
auto& sql = std::get<SqlLogEntry>(item.payload);
check(sql.params.size() == 2 && sql.debug_sql.find("private-password") == String::npos &&
sql.debug_sql.find("private-customer") == String::npos,
"Global buffer retains only projected values after source destruction");
check(item.user_identifier == "operator whose identity lives in the User arena" &&
!item.trace_chain.empty(),
"nested identity and trace survive through explicit deep copy");
rejected([&] {
user->sql_logs();
});
rejected([&] {
user->clear_sql_logs();
});
rejected([&] {
user->user_identifier();
});
rejected([&] {
user->set_user_identifier("closed");
});
rejected([&] {
user->enable_all_sql_log();
});
rejected([&] {
user->record_metadata_log(metadata(global.memory_resource()));
});
}
static void identity_and_quiescence() {
quiet();
setenv("USER", "captured-edge-user", 1);
GlobalContext global;
test::RuntimeLoggingFixture logging_fixture(global);
auto user = logging_fixture.make_user_context();
auto id = std::string(*user->user_identifier());
setenv("USER", "later-user", 1);
check(user->user_identifier() == id && id.starts_with("captured-edge-user@pid-"),
"default identity captures environment at construction");
user->with_user_identifier_option(std::string_view(""));
check(user->user_identifier() == "", "empty identity remains Some");
user->with_user_identifier_option({});
check(!user->user_identifier(), "None remains distinct");
setenv("USER", "\xff", 1);
setenv("USERNAME", "fallback-name", 1);
{
auto other = logging_fixture.make_user_context();
check(other->user_identifier()->starts_with("fallback-name@pid-"),
"invalid UTF8 USER falls back to USERNAME");
}
unsetenv("USER");
unsetenv("USERNAME");
{
auto other = logging_fixture.make_user_context();
check(other->user_identifier()->starts_with("main@pid-"), "missing environment uses main");
}
auto& buffer = global.make<UnifiedLogBuffer>();
user->insert_resource(global, buffer);
Arena::Quiescence stop;
stop.attach(*dynamic_cast<Arena*>(user->memory_resource()), user.get(), [](void* p) noexcept {
try {
auto& c = *static_cast<UserContext*>(p);
auto m = metadata(c.memory_resource());
c.record_metadata_log(m);
} catch (...) {
std::terminate();
}
});
user.reset();
check(buffer.entries.size() == 1,
"quiescence records final diagnostics before releasing Context");
}
static std::string file_text(const char* path) {
std::ifstream in(path);
return std::string(std::istreambuf_iterator<char>(in), {});
}
static void output_and_failure_order() {
quiet();
char path[] = "/tmp/teaql-context-log-XXXXXX";
int fd = mkstemp(path);
check(fd >= 0, "temporary log file");
close(fd);
struct Remove {
const char* path;
~Remove() {
std::remove(path);
}
} remove{path};
setenv("TEAQL_LOG_ENDPOINT", path, 1);
setenv("TEAQL_SQL_DEBUG_ENDPOINT", path, 1);
setenv("TEAQL_ALLOW_SENSITIVE_PLAINTEXT_LOGS",
"I_UNDERSTAND_SENSITIVE_DATA_MAY_BE_WRITTEN_TO_DISK",
1);
{
GlobalContext global;
test::RuntimeLoggingFixture logging_fixture(global);
auto user = logging_fixture.make_user_context();
{
auto m = metadata(user->memory_resource());
user->record_metadata_log(m);
}
auto logs = user->sql_logs();
check(logs.size() == 1 && logs[0].debug_sql.find("private-customer") == String::npos,
"plaintext endpoint does not upgrade retained SQL");
auto ordinary = file_text(user->runtime_logger().path->c_str());
check(ordinary.find("sql.") != ordinary.npos &&
ordinary.find("private-customer") == ordinary.npos &&
ordinary.find("private-password") == ordinary.npos,
"the Context-owned ordinary destination remains independently safe");
}
auto text = file_text(path);
auto first = text.find("SqlLogEntry {");
auto second = text.find("SqlLogEntry {", first + 1);
check(first != text.npos && second == text.npos && text.find("private-customer") > first &&
text.find("private-customer") != text.npos,
"only the explicit sensitive projection uses the legacy debug destination");
check(text.find("private-password") == text.npos && text.find("RAW private") == text.npos,
"credentials and untrusted diagnostic SQL never leave projection");
quiet();
setenv("TEAQL_LOG_ENDPOINT", "stdout", 1);
GlobalContext global;
test::RuntimeLoggingFixture logging_fixture(global);
auto user = logging_fixture.make_user_context();
auto& buffer = user->make<UnifiedLogBuffer>();
user->insert_resource(*user, buffer);
auto m = metadata(user->memory_resource());
auto ordinary_path = std::filesystem::path(*user->runtime_logger().path);
std::filesystem::remove(ordinary_path);
std::filesystem::create_directory(ordinary_path);
int saved = dup(fileno(stdout));
auto* full = std::fopen("/dev/full", "wb");
check(saved >= 0 && full, "stdout failure setup");
std::fflush(stdout);
dup2(fileno(full), fileno(stdout));
bool failed = false;
try {
user->record_metadata_log(m);
} catch (const std::runtime_error&) {
failed = true;
}
std::fflush(stdout);
dup2(saved, fileno(stdout));
close(saved);
std::fclose(full);
std::clearerr(stdout);
check(!failed && user->sql_logs().size() == 1 && buffer.entries.size() == 1,
"ordinary output failure preserves retention and the original execution outcome");
}
static void request_owned_audit_provenance() {
quiet();
GlobalContext global;
test::RuntimeLoggingFixture logging_fixture(global);
auto user_context = logging_fixture.make_user_context();
auto r = user_context->memory_resource();
auto make_entry = [&](DataServiceOperation operation,
std::optional<std::string_view> owned_reason,
std::optional<std::string_view> local_reason) {
auto input = metadata(r);
input.operation = operation;
if (owned_reason) {
input.comment.emplace(*owned_reason, r);
}
if (local_reason) {
input.trace_chain.emplace_back(TraceKind::AuditReason, "OrderLine", *local_reason);
}
user_context->clear_sql_logs();
user_context->record_metadata_log(input);
return user_context->sql_logs();
};
auto query = make_entry(DataServiceOperation::Query, "root audit", std::nullopt);
check(query.size() == 1 && !query[0].audit_reason,
"ordinary query comments do not become mutation audit reasons");
auto readback = make_entry(DataServiceOperation::Query,
"root private-customer audit",
"descendant private-password lineage");
check(readback[0].audit_reason == "root [REDACTED] audit",
"audited readback keeps the masked request-owned reason over descendant lineage");
auto mutation = make_entry(DataServiceOperation::Update, "root audit", "local lineage");
check(mutation[0].audit_reason == "root audit",
"mutation audit reason comes from executed request metadata");
auto legacy = make_entry(DataServiceOperation::Update, std::nullopt, "legacy lineage");
check(legacy[0].audit_reason == "legacy lineage",
"standalone metadata without owned intent retains its local audit fold");
auto recover = make_entry(DataServiceOperation::Recover, "recover audit", std::nullopt);
check(recover[0].operation == SqlLogOperation::Recover &&
recover[0].audit_reason == "recover audit" &&
recover[0].trace_path.back().kind == TraceKind::Sql &&
recover[0].trace_path.back().comment == "",
"recovery retains its distinct operation and request audit reason");
check(recover[0].trace_path.back().entity_type == "recover",
"recovery canonical SQL lineage names the actual operation");
}
int main() {
try {
auto* prior = std::pmr::set_default_resource(std::pmr::null_memory_resource());
lifetime_and_options();
identity_and_quiescence();
output_and_failure_order();
request_owned_audit_provenance();
std::pmr::set_default_resource(prior);
std::cout << "Context logging contracts passed\n";
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}