Skip to content

Commit

Permalink
logproto: add testcases for LogProtoAutoServer
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed May 4, 2024
1 parent 8da2591 commit 0e695a0
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/logproto/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(TEST_LOGPROTO_SOURCES
test-text-server.c
test-dgram-server.c
test-framed-server.c
test-auto-server.c
test-indented-multiline-server.c
test-regexp-multiline-server.c
test-proxy-proto.c)
Expand Down
1 change: 1 addition & 0 deletions lib/logproto/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ lib_logproto_tests_test_logproto_SOURCES = \
lib/logproto/tests/test-text-server.c \
lib/logproto/tests/test-dgram-server.c \
lib/logproto/tests/test-framed-server.c \
lib/logproto/tests/test-auto-server.c \
lib/logproto/tests/test-indented-multiline-server.c \
lib/logproto/tests/test-regexp-multiline-server.c \
lib/logproto/tests/test-proxy-proto.c
Expand Down
139 changes: 139 additions & 0 deletions lib/logproto/tests/test-auto-server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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 <criterion/criterion.h>
#include "libtest/mock-transport.h"
#include "libtest/proto_lib.h"
#include "libtest/msg_parse_lib.h"

#include "logproto/logproto-auto-server.h"

#include <errno.h>

Test(log_proto, test_log_proto_initial_framing_too_long)
{
LogProtoServer *proto;

proto = log_proto_auto_server_new(
log_transport_mock_stream_new(
"100000000 too long\n", -1,
LTM_EOF),
get_inited_proto_server_options());

assert_proto_server_handshake_failure(proto, LPS_SUCCESS);
assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL);
log_proto_server_free(proto);
}

Test(log_proto, test_log_proto_error_in_initial_frame)
{
LogProtoServer *proto;

proto = log_proto_auto_server_new(
log_transport_mock_stream_new(
LTM_INJECT_ERROR(EIO)),
get_inited_proto_server_options());

assert_proto_server_handshake_failure(proto, LPS_ERROR);
log_proto_server_free(proto);
}

Test(log_proto, test_log_proto_auto_server_no_framing)
{
LogProtoServer *proto;

proto = log_proto_auto_server_new(
log_transport_mock_stream_new(
"abcdefghijklmnopqstuvwxyz\n", -1,
"01234567\n", -1,
"01234567\0", 9,
"abcdef", -1,
LTM_EOF),
get_inited_proto_server_options());

assert_proto_server_handshake(proto);
assert_proto_server_fetch(proto, "abcdefghijklmnopqstuvwxyz", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "01234567", 8);
assert_proto_server_fetch(proto, "abcdef", -1);
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}

Test(log_proto, test_log_proto_auto_server_opening_bracket)
{
LogProtoServer *proto;

proto = log_proto_auto_server_new(
log_transport_mock_stream_new(
"<55> abcdefghijklmnopqstuvwxyz\n", -1,
"01234567\n", -1,
"01234567\0", 9,
"abcdef", -1,
LTM_EOF),
get_inited_proto_server_options());

assert_proto_server_handshake(proto);
assert_proto_server_fetch(proto, "<55> abcdefghijklmnopqstuvwxyz", -1);
assert_proto_server_fetch(proto, "01234567", -1);
assert_proto_server_fetch(proto, "01234567", 8);
assert_proto_server_fetch(proto, "abcdef", -1);
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}

Test(log_proto, test_log_proto_auto_server_with_framing)
{
LogProtoServer *proto;

proto = log_proto_auto_server_new(
log_transport_mock_stream_new(
"32 0123456789ABCDEF0123456789ABCDEF", -1,
"10 01234567\n\n", -1,
"10 01234567\0\0", 13,
/* utf8 */
"30 árvíztűrőtükörfúrógép", -1,
/* iso-8859-2 */
"21 \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", -1, /* |rógép| */
/* ucs4 */
"32 \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", 35, /* |...z...t...ű...r| */
LTM_EOF),
get_inited_proto_server_options());

assert_proto_server_handshake(proto);
assert_proto_server_fetch(proto, "0123456789ABCDEF0123456789ABCDEF", -1);
assert_proto_server_fetch(proto, "01234567\n\n", -1);
assert_proto_server_fetch(proto, "01234567\0\0", 10);
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", -1); /* |r.g.p| */
assert_proto_server_fetch(proto,
"\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", 32); /* |...z...t...q...r| */
assert_proto_server_fetch_failure(proto, LPS_EOF, NULL);
log_proto_server_free(proto);
}
38 changes: 38 additions & 0 deletions libtest/proto_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ assert_proto_server_status(LogProtoServer *proto, LogProtoStatus status, LogProt
cr_assert_eq(status, expected_status, "LogProtoServer expected status mismatch");
}

LogProtoStatus
proto_server_handshake(LogProtoServer *proto)
{
gboolean handshake_finished = FALSE;
LogProtoStatus status;

start_grabbing_messages();
do
{
status = log_proto_server_handshake(proto, &handshake_finished);
if (status == LPS_AGAIN)
status = LPS_SUCCESS;
}
while (status == LPS_SUCCESS && handshake_finished == FALSE);
stop_grabbing_messages();
return status;
}

LogProtoStatus
proto_server_fetch(LogProtoServer *proto, const guchar **msg, gsize *msg_len)
{
Expand Down Expand Up @@ -80,6 +98,16 @@ construct_server_proto_plugin(const gchar *name, LogTransport *transport)
return log_proto_server_factory_construct(proto_factory, transport, &proto_server_options);
}

void
assert_proto_server_handshake(LogProtoServer *proto)
{
LogProtoStatus status;

status = proto_server_handshake(proto);

assert_proto_server_status(proto, status, LPS_SUCCESS);
}

void
assert_proto_server_fetch(LogProtoServer *proto, const gchar *expected_msg, gssize expected_msg_len)
{
Expand Down Expand Up @@ -146,6 +174,16 @@ assert_proto_server_fetch_failure(LogProtoServer *proto, LogProtoStatus expected
assert_grabbed_log_contains(error_message);
}

void
assert_proto_server_handshake_failure(LogProtoServer *proto, LogProtoStatus expected_status)
{
LogProtoStatus status;

status = proto_server_handshake(proto);

assert_proto_server_status(proto, status, expected_status);
}

void
assert_proto_server_fetch_ignored_eof(LogProtoServer *proto)
{
Expand Down
3 changes: 3 additions & 0 deletions libtest/proto_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

extern LogProtoServerOptions proto_server_options;


void assert_proto_server_handshake(LogProtoServer *proto);
void assert_proto_server_handshake_failure(LogProtoServer *proto, LogProtoStatus expected_status);
void assert_proto_server_status(LogProtoServer *proto, LogProtoStatus status, LogProtoStatus expected_status);
void assert_proto_server_fetch(LogProtoServer *proto, const gchar *expected_msg, gssize expected_msg_len);
void assert_proto_server_fetch_single_read(LogProtoServer *proto, const gchar *expected_msg, gssize expected_msg_len);
Expand Down

0 comments on commit 0e695a0

Please sign in to comment.