Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: internal: added cpu byte order test cases #9270

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
};
81 changes: 81 additions & 0 deletions tests/internal/log_event_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,86 @@ 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()
{
uint8_t *encoder_buffer;
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;
}

encoder_buffer = (uint8_t *) encoder.output_buffer;

result = FLB_FALSE;

for (index = 0 ; index < encoder.output_length - 4 ; index++) {
if (encoder_buffer[index + 0] == 0x00 &&
encoder_buffer[index + 1] == 0xC0 &&
encoder_buffer[index + 2] == 0xFF &&
encoder_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 +739,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 }
};
Loading