-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser_properties.cpp
More file actions
275 lines (242 loc) · 10.8 KB
/
Copy pathtest_parser_properties.cpp
File metadata and controls
275 lines (242 loc) · 10.8 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
#include "parser.hpp"
#include "parser/program_dispatch.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace {
void expect(bool condition, const std::string& message) {
if (!condition) {
throw std::runtime_error(message);
}
}
loglens::AuthLogParser make_syslog_parser() {
return loglens::AuthLogParser(loglens::ParserConfig{
loglens::InputMode::SyslogLegacy,
2026});
}
bool events_equal(const loglens::Event& left, const loglens::Event& right) {
return left.timestamp == right.timestamp
&& left.hostname == right.hostname
&& left.program == right.program
&& left.pid == right.pid
&& left.message == right.message
&& left.source_ip == right.source_ip
&& left.username == right.username
&& left.event_type == right.event_type
&& left.line_number == right.line_number;
}
bool results_equal(const loglens::parser_internal::HandlerResult& left,
const loglens::parser_internal::HandlerResult& right) {
if (left.matched != right.matched
|| left.failure_category != right.failure_category
|| left.reason != right.reason
|| left.event.has_value() != right.event.has_value()) {
return false;
}
return !left.event.has_value() || events_equal(*left.event, *right.event);
}
loglens::Event make_source_event(std::string program, std::string message) {
loglens::Event event;
event.program = std::move(program);
event.message = std::move(message);
event.hostname = "example-host";
event.line_number = 1;
return event;
}
std::array<loglens::Event, 7> representative_registry_events() {
return {
make_source_event(
"sshd",
"Failed password for user-a from 203.0.113.10 port 50101 ssh2"),
make_source_event(
"pam_unix(sshd:auth)",
"authentication failure; rhost=203.0.113.11 user=user-b"),
make_source_event(
"pam_faillock(sshd:auth)",
"Authentication failure for user user-c from 203.0.113.12"),
make_source_event(
"pam_sss(sshd:auth)",
"received for user user-d: 7 (Authentication failure)"),
make_source_event(
"sudo",
"user-e : TTY=pts/1 ; PWD=/home/user/project ; USER=root ; COMMAND=/usr/bin/id"),
make_source_event(
"su",
"FAILED SU (to root) user-f on pts/2"),
make_source_event(
"login",
"FAILED LOGIN 1 FROM tty1 FOR user-g, Authentication failure"),
};
}
void test_registry_dispatch_is_order_independent() {
const auto registry = loglens::parser_internal::program_handler_registry();
const auto sources = representative_registry_events();
expect(registry.size() == sources.size(), "expected one registry entry per supported program family");
for (const auto& source : sources) {
const auto match_count = std::count_if(
registry.begin(),
registry.end(),
[&source](const loglens::parser_internal::ProgramHandlerRegistration& registration) {
return registration.matches(source.program);
});
expect(match_count == 1, "expected each representative program to match exactly one handler");
}
std::vector<std::size_t> order(registry.size());
std::iota(order.begin(), order.end(), 0);
std::size_t permutation_count = 0;
do {
std::vector<loglens::parser_internal::ProgramHandlerRegistration> permuted;
permuted.reserve(order.size());
for (const auto index : order) {
permuted.push_back(registry[index]);
}
for (const auto& source : sources) {
const auto expected = loglens::parser_internal::dispatch_program(source);
const auto actual = loglens::parser_internal::dispatch_program(source, permuted);
expect(results_equal(actual, expected), "expected dispatch result to be registry-order independent");
}
++permutation_count;
} while (std::next_permutation(order.begin(), order.end()));
expect(permutation_count == 5040, "expected all seven-handler registry permutations to be checked");
}
std::uint32_t next_random(std::uint32_t& state) {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
return state;
}
std::string generated_malformed_ipv4(std::size_t index, std::uint32_t& state) {
const auto first = 1U + next_random(state) % 223U;
const auto second = next_random(state) % 256U;
const auto third = next_random(state) % 256U;
const auto fourth = next_random(state) % 256U;
switch (index % 4) {
case 0:
return std::to_string(first) + "." + std::to_string(second) + "."
+ std::to_string(third) + "." + std::to_string(256U + next_random(state) % 744U);
case 1:
return std::to_string(first) + "." + std::to_string(second) + "." + std::to_string(third);
case 2:
return std::to_string(first) + ".invalid." + std::to_string(third) + "." + std::to_string(fourth);
default:
return std::to_string(first) + "." + std::to_string(second) + "."
+ std::to_string(third) + "." + std::to_string(fourth) + ",";
}
}
void test_generated_malformed_source_tokens_keep_failure_taxonomy() {
const auto parser = make_syslog_parser();
std::uint32_t state = 0x6c6f676cU;
for (std::size_t index = 0; index < 256; ++index) {
const auto source_ip = generated_malformed_ipv4(index, state);
const auto line = "Mar 10 08:30:00 example-host sshd[4200]: Failed password for user-a from "
+ source_ip + " port 50101 ssh2";
std::string reason;
auto category = loglens::ParserFailureCategory::KnownProgramUnknownMessage;
const auto event = parser.parse_line(line, index + 1, &reason, &category);
expect(!event.has_value(), "expected malformed source token not to emit an event");
expect(category == loglens::ParserFailureCategory::MalformedSourceIp,
"expected malformed source token to keep malformed_source_ip category");
expect(reason == "malformed source IP", "expected stable malformed source failure reason");
}
}
void test_failure_classification_is_stable_across_envelope_variants() {
struct FailureCase {
std::string program;
std::string message;
loglens::ParserFailureCategory category;
std::string reason;
};
const std::array<FailureCase, 8> cases{{
{"sshd", "Connection closed by 203.0.113.50 port 50100 [preauth]",
loglens::ParserFailureCategory::KnownProgramUnknownMessage,
"unrecognized auth pattern: sshd_connection_closed_preauth"},
{"pam_unix(sshd:session)", "session closed for user user-a",
loglens::ParserFailureCategory::UnsupportedPamVariant,
"unrecognized auth pattern: pam_unix_session_closed"},
{"pam_faillock(sshd:auth)", "Account temporarily locked for user user-b",
loglens::ParserFailureCategory::UnsupportedPamVariant,
"unrecognized auth pattern: pam_faillock_account_locked"},
{"pam_sss(sshd:auth)", "User not known to the underlying authentication module",
loglens::ParserFailureCategory::UnsupportedPamVariant,
"unrecognized auth pattern: pam_sss_unknown_user"},
{"sudo", "user-c : TTY=pts/1 ; PWD=/home/user/project ; USER=root",
loglens::ParserFailureCategory::KnownProgramUnknownMessage,
"unrecognized auth pattern: sudo_other"},
{"su", "pam_authenticate: Authentication failure",
loglens::ParserFailureCategory::KnownProgramUnknownMessage,
"unrecognized auth pattern: su_other"},
{"login", "DIALUP AT ttyS0 BY user-d",
loglens::ParserFailureCategory::KnownProgramUnknownMessage,
"unrecognized auth pattern: login_other"},
{"cron", "job completed",
loglens::ParserFailureCategory::UnknownProgram,
"unrecognized auth pattern: program_cron"},
}};
const auto parser = make_syslog_parser();
for (const auto& test_case : cases) {
for (std::size_t variant = 0; variant < 16; ++variant) {
const auto tag = variant % 2 == 0
? test_case.program
: test_case.program + "[" + std::to_string(4300 + variant) + "]";
auto line = "Mar 10 08:31:00 example-host " + tag + ": " + test_case.message;
if (variant % 3 == 0) {
line.push_back('\r');
}
std::string reason;
auto category = loglens::ParserFailureCategory::MalformedSourceIp;
const auto event = parser.parse_line(line, variant + 1, &reason, &category);
expect(!event.has_value(), "expected unsupported pattern not to emit an event");
expect(category == test_case.category, "expected stable failure category across envelope variants");
expect(reason == test_case.reason, "expected stable failure reason across envelope variants");
}
}
}
void test_deterministic_byte_corpus_never_breaks_result_invariants() {
const std::array<loglens::AuthLogParser, 2> parsers{{
make_syslog_parser(),
loglens::AuthLogParser(loglens::ParserConfig{
loglens::InputMode::JournalctlShortFull,
std::nullopt}),
}};
std::uint32_t state = 0x70617273U;
for (std::size_t index = 0; index < 512; ++index) {
const auto length = static_cast<std::size_t>(next_random(state) % 257U);
std::string line(length, '\0');
for (auto& character : line) {
character = static_cast<char>(next_random(state) & 0xffU);
}
for (const auto& parser : parsers) {
std::string reason;
auto category = loglens::ParserFailureCategory::KnownProgramUnknownMessage;
const auto event = parser.parse_line(line, index + 1, &reason, &category);
if (event.has_value()) {
expect(event->event_type != loglens::EventType::Unknown,
"expected every emitted event to have a normalized type");
expect(!event->program.empty(), "expected every emitted event to have a program");
} else {
expect(!reason.empty(), "expected every rejected input to have a failure reason");
}
}
}
}
} // namespace
int main() {
try {
test_registry_dispatch_is_order_independent();
test_generated_malformed_source_tokens_keep_failure_taxonomy();
test_failure_classification_is_stable_across_envelope_variants();
test_deterministic_byte_corpus_never_breaks_result_invariants();
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}