Skip to content

Commit

Permalink
tests: internal: added cpu byte order test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Alminana <[email protected]>
  • Loading branch information
leonardo-albertovich authored and edsiper committed Aug 27, 2024
1 parent 7e2f3f0 commit 89f9931
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(UNIT_TESTS_FILES
processor.c
uri.c
msgpack_append_message.c
endianness
)

# Config format
Expand Down
32 changes: 32 additions & 0 deletions tests/internal/endianness.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_endian.h>

#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 }
};
78 changes: 78 additions & 0 deletions tests/internal/log_event_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &timestamp);
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},
Expand All @@ -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 }
};

0 comments on commit 89f9931

Please sign in to comment.