From 89f993176b7e15bb09aa85280fe721e110a9daac Mon Sep 17 00:00:00 2001 From: Leonardo Alminana Date: Thu, 22 Aug 2024 23:09:52 +0200 Subject: [PATCH] tests: internal: added cpu byte order test cases Signed-off-by: Leonardo Alminana --- tests/internal/CMakeLists.txt | 1 + tests/internal/endianness.c | 32 ++++++++++++ tests/internal/log_event_encoder.c | 78 ++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 tests/internal/endianness.c diff --git a/tests/internal/CMakeLists.txt b/tests/internal/CMakeLists.txt index 7fe08ea0d8f..61009c1b544 100644 --- a/tests/internal/CMakeLists.txt +++ b/tests/internal/CMakeLists.txt @@ -43,6 +43,7 @@ set(UNIT_TESTS_FILES processor.c uri.c msgpack_append_message.c + endianness ) # Config format diff --git a/tests/internal/endianness.c b/tests/internal/endianness.c new file mode 100644 index 00000000000..8ab5f3de575 --- /dev/null +++ b/tests/internal/endianness.c @@ -0,0 +1,32 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include +#include + +#include "flb_tests_internal.h" + +/* This test case sets a specific value to a variable and compares + * the memory representation against the byte order detected by + * cmake. + */ +static void flb_test_endianness_detection() +{ + volatile uint64_t source_value; + volatile uint8_t *test_value; + + /* ~TEA, COFFEE */ + source_value = 0x08140C0FFEE; + test_value = (volatile uint8_t *) &source_value; + +#if FLB_BYTE_ORDER == FLB_LITTLE_ENDIAN + TEST_CHECK(test_value[0] == 0xEE); +#else + TEST_CHECK(test_value[0] != 0xEE); +#endif +} + +TEST_LIST = { + { "test_endianness_detection", flb_test_endianness_detection }, + + { 0 } +}; diff --git a/tests/internal/log_event_encoder.c b/tests/internal/log_event_encoder.c index 21c26408c5b..99f67eb2777 100644 --- a/tests/internal/log_event_encoder.c +++ b/tests/internal/log_event_encoder.c @@ -650,6 +650,83 @@ static void emit_raw_record() flb_log_event_encoder_destroy(&encoder); } +/* This test case encodes a log event with a specific timestamp + * value and then it checks the raw data to ensure that regardless + * of the host byte order the value is encoded in network order. + */ +static void timestamp_encoding() +{ + struct flb_time timestamp; + struct flb_log_event_encoder encoder; + int result; + size_t index; + + timestamp.tm.tv_sec = 0x00C0FFEE; + timestamp.tm.tv_nsec = 0; + + result = flb_log_event_encoder_init(&encoder, + FLB_LOG_EVENT_FORMAT_FLUENT_BIT_V2); + if (!TEST_CHECK(result == FLB_EVENT_ENCODER_SUCCESS)) { + TEST_MSG("flb_log_event_encoder_init failed"); + return; + } + + result = flb_log_event_encoder_begin_record(&encoder); + if (!TEST_CHECK(result == FLB_EVENT_ENCODER_SUCCESS)) { + TEST_MSG("flb_log_event_encoder_begin_record failed. result=%s", + flb_log_event_encoder_get_error_description(result)); + flb_log_event_encoder_destroy(&encoder); + return; + } + + result = flb_log_event_encoder_set_timestamp(&encoder, ×tamp); + if (!TEST_CHECK(result == FLB_EVENT_ENCODER_SUCCESS)) { + TEST_MSG("flb_log_event_encoder_set_current_timestamp failed. result=%s", + flb_log_event_encoder_get_error_description(result)); + flb_log_event_encoder_destroy(&encoder); + return; + } + + result = flb_log_event_encoder_append_body_values( + &encoder, + FLB_LOG_EVENT_CSTRING_VALUE("test"), + FLB_LOG_EVENT_CSTRING_VALUE("value")); + + if (!TEST_CHECK(result == FLB_EVENT_ENCODER_SUCCESS)) { + TEST_MSG("flb_log_event_encoder_append_body_values failed. result=%s", + flb_log_event_encoder_get_error_description(result)); + flb_log_event_encoder_destroy(&encoder); + return; + } + + result = flb_log_event_encoder_commit_record(&encoder); + if (!TEST_CHECK(result == FLB_EVENT_ENCODER_SUCCESS)) { + TEST_MSG("flb_log_event_encoder_commit_record failed. result=%s", + flb_log_event_encoder_get_error_description(result)); + flb_log_event_encoder_destroy(&encoder); + return; + } + + result = FLB_FALSE; + + for (index = 0 ; index < encoder.output_length - 4 ; index++) { + if (encoder.output_buffer[index + 0] == 0x00 && + encoder.output_buffer[index + 1] == 0xC0 && + encoder.output_buffer[index + 2] == 0xFF && + encoder.output_buffer[index + 3] == 0xEE) { + result = FLB_TRUE; + + break; + } + } + + if (!TEST_CHECK(result == FLB_TRUE)) { + TEST_MSG("timestamp value not encoded in network order"); + } + + flb_log_event_encoder_destroy(&encoder); +} + TEST_LIST = { { "basic_format_fluent_bit_v2", basic_format_fluent_bit_v2}, { "basic_format_fluent_bit_v1", basic_format_fluent_bit_v1}, @@ -659,5 +736,6 @@ TEST_LIST = { { "init_destroy", init_destroy}, { "init_unsupported_format", init_unsupported_format}, { "emit_raw_record", emit_raw_record}, + { "timestamp_encoding", timestamp_encoding}, { NULL, NULL } };