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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
/*
* Copyright (c) 2012-2019 Balabit
* Copyright (c) 2012-2013 Balázs Scheidler
*
* 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.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "mock-transport.h"
#include "proto_lib.h"
#include "msg_parse_lib.h"
#include "logproto/logproto-text-server.h"
#include <errno.h>
#include <criterion/criterion.h>
static gint accumulate_seq;
LogProtoServer *
construct_test_proto(LogTransport *transport)
{
proto_server_options.max_msg_size = 32;
return log_proto_text_server_new(transport, get_inited_proto_server_options());
}
LogProtoServer *
construct_test_proto_with_accumulator(gint (*accumulator)(LogProtoTextServer *, const guchar *, gsize, gssize),
LogTransport *transport)
{
LogProtoServer *proto = construct_test_proto(transport);
((LogProtoTextServer *) proto)->accumulate_line = accumulator;
return proto;
}
static gint
accumulator_delay_lines(LogProtoTextServer *self, const guchar *msg, gsize msg_len, gssize consumed_len)
{
accumulate_seq++;
if ((accumulate_seq % 2) == 0)
return LPT_REWIND_LINE | LPT_EXTRACTED;
else
return LPT_CONSUME_LINE | LPT_WAITING;
}
static void
test_log_proto_text_server_no_encoding(LogTransportMockConstructor log_transport_mock_new)
{
LogProtoServer *proto;
proto = construct_test_proto(
/* 32 bytes max line length */
log_transport_mock_new(
"01234567\n"
/* line too long */
"0123456789ABCDEF0123456789ABCDEF01234567\n"
/* utf8 */
"árvíztűrőtükörfúrógép\n"
/* iso-8859-2 */
"\xe1\x72\x76\xed\x7a\x74\xfb\x72\xf5\x74\xfc\x6b\xf6\x72\x66\xfa" /* |árvíztűrőtükörfú| */
"\x72\xf3\x67\xe9\x70\n", /* |rógép| */
-1,
/* NUL terminated line */
"01234567\0"
"01234567\0\n"
"01234567\n\0"
"01234567\r\n\0", 40,
"01234567\r\n"
/* no eol before EOF */
"01234567", -1,
LTM_EOF));
assert_proto_server_fetch(proto, "01234567", -1);
/* input split due to an oversized input line */
assert_proto_server_fetch(proto, "0123456789ABCDEF0123456789ABCDEF", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "árvíztűrőtükörfúrógép", -1);
assert_proto_server_fetch(proto,
"\xe1\x72\x76\xed\x7a\x74\xfb\x72\xf5\x74\xfc\x6b\xf6\x72\x66\xfa" /* |árvíztűrőtükörfú| */
"\x72\xf3\x67\xe9\x70", /* |rógép| */
-1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "01234567", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_no_encoding)
{
test_log_proto_text_server_no_encoding(log_transport_mock_stream_new);
test_log_proto_text_server_no_encoding(log_transport_mock_records_new);
}
Test(log_proto, test_log_proto_text_server_no_eol_before_eof)
{
LogProtoServer *proto;
proto = construct_test_proto(
log_transport_mock_stream_new(
/* no eol before EOF */
"01234567", -1,
LTM_EOF));
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_eol_before_eof)
{
LogProtoServer *proto;
proto = construct_test_proto(
log_transport_mock_records_new(
/* eol before EOF */
"01234\n567\n890\n", -1,
LTM_INJECT_ERROR(EIO),
LTM_EOF));
assert_proto_server_fetch(proto, "01234", -1);
assert_proto_server_fetch(proto, "567", -1);
assert_proto_server_fetch(proto, "890", -1);
assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_io_error_before_eof)
{
LogProtoServer *proto;
proto = construct_test_proto(
log_transport_mock_stream_new(
"01234567", -1,
LTM_INJECT_ERROR(EIO),
LTM_EOF));
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_partial_chars_before_eof)
{
LogProtoServer *proto;
log_proto_server_options_set_encoding(&proto_server_options, "utf-8");
proto = construct_test_proto(
log_transport_mock_stream_new(
/* utf8 */
"\xc3", -1,
LTM_EOF));
cr_assert(log_proto_server_validate_options(proto),
"validate_options() returned failure but it should have succeeded");
assert_proto_server_fetch_failure(proto, LPS_EOF,
"EOF read on a channel with leftovers from previous character conversion, dropping input");
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_not_fixed_encoding)
{
LogProtoServer *proto;
log_proto_server_options_set_encoding(&proto_server_options, "utf-8");
/* to test whether a non-easily-reversable charset works too */
proto = construct_test_proto(
log_transport_mock_stream_new(
/* utf8 */
"árvíztűrőtükörfúrógép\n", -1,
LTM_EOF));
cr_assert(log_proto_server_validate_options(proto),
"validate_options() returned failure but it should have succeeded");
assert_proto_server_fetch(proto, "árvíztűrőtükörfúrógép", -1);
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_ucs4)
{
LogProtoServer *proto;
log_proto_server_options_set_encoding(&proto_server_options, "ucs-4");
proto = construct_test_proto(
log_transport_mock_stream_new(
/* ucs4 */
"\x00\x00\x00\xe1\x00\x00\x00\x72\x00\x00\x00\x76\x00\x00\x00\xed" /* |...á...r...v...í| */
"\x00\x00\x00\x7a\x00\x00\x00\x74\x00\x00\x01\x71\x00\x00\x00\x72" /* |...z...t...ű...r| */
"\x00\x00\x01\x51\x00\x00\x00\x74\x00\x00\x00\xfc\x00\x00\x00\x6b" /* |...Q...t.......k| */
"\x00\x00\x00\xf6\x00\x00\x00\x72\x00\x00\x00\x66\x00\x00\x00\xfa" /* |.......r...f....| */
"\x00\x00\x00\x72\x00\x00\x00\xf3\x00\x00\x00\x67\x00\x00\x00\xe9" /* |...r.......g....| */
"\x00\x00\x00\x70\x00\x00\x00\x0a", 88, /* |...p....| */
LTM_EOF));
cr_assert(log_proto_server_validate_options(proto),
"validate_options() returned failure but it should have succeeded");
assert_proto_server_fetch(proto, "árvíztűrőtükörfúrógép", -1);
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_iso8859_2)
{
LogProtoServer *proto;
log_proto_server_options_set_encoding(&proto_server_options, "iso-8859-2");
proto = construct_test_proto(
log_transport_mock_stream_new(
/* iso-8859-2 */
"\xe1\x72\x76\xed\x7a\x74\xfb\x72\xf5\x74\xfc\x6b\xf6\x72\x66\xfa" /* |árvíztűrőtükörfú| */
"\x72\xf3\x67\xe9\x70\n", -1, /* |rógép| */
LTM_EOF));
cr_assert(log_proto_server_validate_options(proto),
"validate_options() returned failure but it should have succeeded");
assert_proto_server_fetch(proto, "árvíztűrőtükörfúrógép", -1);
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_invalid_encoding)
{
LogProtoServer *proto;
gboolean success;
log_proto_server_options_set_encoding(&proto_server_options, "never-ever-is-going-to-be-such-an-encoding");
proto = construct_test_proto(
log_transport_mock_stream_new(
"", -1,
LTM_EOF));
start_grabbing_messages();
success = log_proto_server_validate_options(proto);
assert_grabbed_messages_contain("Unknown character set name specified; encoding='never-ever-is-going-to-be-such-an-encoding'",
"message about unknown charset missing");
cr_assert_not(success, "validate_options() returned success but it should have failed");
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_multi_read)
{
LogProtoServer *proto;
proto = construct_test_proto(
log_transport_mock_records_new(
"foobar\n", -1,
/* no EOL, proto implementation would read another chunk */
"foobaz", -1,
LTM_INJECT_ERROR(EIO),
LTM_EOF));
assert_proto_server_fetch(proto, "foobar", -1);
assert_proto_server_fetch(proto, "foobaz", -1);
assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_multi_read_not_allowed, .disabled = true)
{
/* FIXME: */
LogProtoServer *proto;
proto = construct_test_proto(
log_transport_mock_records_new(
"foobar\n", -1,
/* no EOL, proto implementation would read another chunk */
"foobaz", -1,
LTM_INJECT_ERROR(EIO),
LTM_EOF));
((LogProtoBufferedServer *) proto)->no_multi_read = TRUE;
assert_proto_server_fetch_single_read(proto, "foobar", -1);
/* because of EAGAIN */
assert_proto_server_fetch_single_read(proto, NULL, -1);
/* because of NOMREAD, partial lines are returned as empty */
assert_proto_server_fetch_single_read(proto, NULL, -1);
/* because of EAGAIN */
assert_proto_server_fetch_single_read(proto, NULL, -1);
/* error was detected by this time, partial line is returned before the error */
assert_proto_server_fetch_single_read(proto, "foobaz", -1);
/* finally the error is returned too */
assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_is_not_fetching_input_as_long_as_there_is_an_eol_in_buffer)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_delay_lines,
log_transport_mock_records_new(
/* should the LogProto instance read ahead, it gets an
* EIO error */
"foo\n"
"bar\n"
"baz\n"
"booz\n", -1,
LTM_INJECT_ERROR(EIO),
LTM_EOF));
assert_proto_server_fetch(proto, "foo", -1);
assert_proto_server_fetch(proto, "bar", -1);
assert_proto_server_fetch(proto, "baz", -1);
assert_proto_server_fetch(proto, "booz", -1);
assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL);
log_proto_server_free(proto);
}
static gint
accumulator_assert_that_lines_are_starting_with_sequence_number(LogProtoTextServer *self, const guchar *msg,
gsize msg_len, gssize consumed_len)
{
cr_assert_eq((msg[0] - '0'), accumulate_seq,
"accumulate_line: Message doesn't start with sequence number, msg=%.*s, seq=%d",
(int)msg_len, msg, accumulate_seq);
cr_assert_eq(consumed_len, -1, "Initial invocation of the accumulator expects -1 as consumed_len");
accumulate_seq++;
return LPT_CONSUME_LINE | LPT_EXTRACTED;
}
static void
test_log_proto_text_server_accumulate_line_is_called_for_each_line(LogTransportMockConstructor log_transport_mock_new)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_assert_that_lines_are_starting_with_sequence_number,
/* 32 bytes max line length */
log_transport_mock_new(
"0 line\n"
"1 line\n"
"2 line\n"
"3 line\n", -1,
LTM_PADDING,
LTM_EOF));
assert_proto_server_fetch(proto, "0 line", -1);
assert_proto_server_fetch(proto, "1 line", -1);
assert_proto_server_fetch(proto, "2 line", -1);
assert_proto_server_fetch(proto, "3 line", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_accumulate_line_is_called_for_each_line)
{
test_log_proto_text_server_accumulate_line_is_called_for_each_line(log_transport_mock_stream_new);
test_log_proto_text_server_accumulate_line_is_called_for_each_line(log_transport_mock_records_new);
}
static gint
accumulator_extract_pairs(LogProtoTextServer *self, const guchar *msg, gsize msg_len, gssize consumed_len)
{
accumulate_seq++;
if ((accumulate_seq % 2) == 0)
return LPT_CONSUME_LINE | LPT_EXTRACTED;
else
return LPT_CONSUME_LINE | LPT_WAITING;
}
static void
test_log_proto_text_server_accumulate_line_can_consume_lines_without_returning_them(
LogTransportMockConstructor log_transport_mock_new)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_extract_pairs,
log_transport_mock_new(
"0 line\n"
"1 line\n"
"2 line\n"
"3 line\n", -1,
LTM_PADDING,
LTM_EOF));
assert_proto_server_fetch(proto, "0 line\n1 line", -1);
assert_proto_server_fetch(proto, "2 line\n3 line", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_accumulate_line_can_consume_lines_without_returning_them)
{
test_log_proto_text_server_accumulate_line_can_consume_lines_without_returning_them(log_transport_mock_stream_new);
test_log_proto_text_server_accumulate_line_can_consume_lines_without_returning_them(log_transport_mock_records_new);
}
static gint
accumulator_join_continuation_lines(LogProtoTextServer *self, const guchar *msg, gsize msg_len, gssize consumed_len)
{
if (consumed_len >= 0 && msg_len > consumed_len + 1)
{
gchar first_char = msg[consumed_len + 1];
if (first_char == ' ')
return LPT_CONSUME_LINE | LPT_WAITING;
else
return LPT_REWIND_LINE | LPT_EXTRACTED;
}
else
{
return LPT_CONSUME_LINE | LPT_WAITING;
}
}
static void
test_log_proto_text_server_accumulate_line_can_rewind_lines_if_uninteresting(LogTransportMockConstructor
log_transport_mock_new)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_join_continuation_lines,
log_transport_mock_new(
"0 line\n"
" line\n"
"2 line\n"
" line\n"
"3 end\n", -1,
LTM_PADDING,
LTM_EOF));
assert_proto_server_fetch(proto, "0 line\n line", -1);
assert_proto_server_fetch(proto, "2 line\n line", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_accumulate_line_can_rewind_lines_if_uninteresting)
{
test_log_proto_text_server_accumulate_line_can_rewind_lines_if_uninteresting(log_transport_mock_stream_new);
test_log_proto_text_server_accumulate_line_can_rewind_lines_if_uninteresting(log_transport_mock_records_new);
}
static void
test_log_proto_text_server_accumulation_terminated_if_input_is_closed(LogTransportMockConstructor
log_transport_mock_new)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_join_continuation_lines,
log_transport_mock_new(
"0 line\n"
" line\n"
"1 line\n", -1,
LTM_EOF));
assert_proto_server_fetch(proto, "0 line\n line", -1);
assert_proto_server_fetch(proto, "1 line", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_accumulation_terminated_if_input_is_closed)
{
test_log_proto_text_server_accumulation_terminated_if_input_is_closed(log_transport_mock_stream_new);
test_log_proto_text_server_accumulation_terminated_if_input_is_closed(log_transport_mock_records_new);
}
static void
test_log_proto_text_server_accumulation_terminated_if_buffer_full(LogTransportMockConstructor log_transport_mock_new)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_join_continuation_lines,
log_transport_mock_new(
"0123456789abcdef\n"
" 0123456789abcdef\n"
" continuation\n", -1,
LTM_EOF));
assert_proto_server_fetch(proto, "0123456789abcdef\n 0123456789abcd", -1);
assert_proto_server_fetch(proto, "ef\n continuation", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_accumulation_terminated_if_buffer_full)
{
test_log_proto_text_server_accumulation_terminated_if_buffer_full(log_transport_mock_stream_new);
test_log_proto_text_server_accumulation_terminated_if_buffer_full(log_transport_mock_records_new);
}
static gint
accumulator_rewind_initial(LogProtoTextServer *self, const guchar *msg, gsize msg_len, gssize consumed_len)
{
accumulate_seq++;
if (accumulate_seq == 1)
return LPT_REWIND_LINE | LPT_EXTRACTED;
return LPT_CONSUME_LINE | LPT_EXTRACTED;
}
static void
test_log_proto_text_server_rewinding_the_initial_line_results_in_an_empty_message(LogTransportMockConstructor
log_transport_mock_new)
{
LogProtoServer *proto;
accumulate_seq = 0;
proto = construct_test_proto_with_accumulator(
accumulator_rewind_initial,
log_transport_mock_new(
"0 line\n"
"1 line\n"
"2 line\n", -1,
LTM_PADDING,
LTM_EOF));
assert_proto_server_fetch(proto, "", -1);
assert_proto_server_fetch(proto, "0 line", -1);
assert_proto_server_fetch(proto, "1 line", -1);
assert_proto_server_fetch(proto, "2 line", -1);
log_proto_server_free(proto);
}
Test(log_proto, test_log_proto_text_server_rewinding_the_initial_line_results_in_an_empty_message)
{
test_log_proto_text_server_rewinding_the_initial_line_results_in_an_empty_message(log_transport_mock_stream_new);
test_log_proto_text_server_rewinding_the_initial_line_results_in_an_empty_message(log_transport_mock_records_new);
}
|