From 08b4db4178d307460dd9c9cbd80cb060ef2e5f13 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 24 May 2023 12:43:49 -0400 Subject: [PATCH] tests: add bt_field_string_clear test Add a test for bt_field_string_clear, exercising the fix done in commit 0022a87819b0 ("Fix: clear_string_field(): set first character to 0"). The test sets the field to a non-empty value, clears it with bt_field_string_clear, and verifies that the value returned by bt_field_string_get_value is an empty string, and that the length returned by bt_field_string_get_length is 0. When reverting 0022a87819b0, the test gives: 1..2 not ok 1 - string field is empty # Failed test (/home/smarchi/src/babeltrace/tests/lib/test_fields.cpp:operator()() at line 36) ok 2 - string field length is 0 # Looks like you failed 1 test of 2. Change-Id: I46800a93a7290fe72618081efdc5cdcd944c8567 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/10068 Reviewed-by: Philippe Proulx Tested-by: jenkins --- tests/Makefile.am | 1 + tests/lib/Makefile.am | 8 +++- tests/lib/test_fields | 17 +++++++ tests/lib/test_fields_bin.cpp | 72 +++++++++++++++++++++++++++++ tests/lib/utils/run-in.cpp | 85 ++++++++++++++++++++++++++--------- 5 files changed, 161 insertions(+), 22 deletions(-) create mode 100755 tests/lib/test_fields create mode 100644 tests/lib/test_fields_bin.cpp diff --git a/tests/Makefile.am b/tests/Makefile.am index 1b44a9cc..687445a1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -82,6 +82,7 @@ TESTS_CLI = \ TESTS_LIB = \ lib/test_bt_uuid \ lib/test_bt_values \ + lib/test_fields \ lib/test_graph_topo \ lib/test_remove_destruction_listener_in_destruction_listener \ lib/test_simple_sink \ diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index cc91bb2b..488429fa 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -7,12 +7,16 @@ AM_CPPFLAGS += -I$(top_srcdir)/tests/utils COMMON_TEST_LDADD = \ $(top_builddir)/tests/utils/tap/libtap.la \ $(top_builddir)/tests/utils/libtestcommon.la \ + $(top_builddir)/tests/lib/utils/liblib-utils.la \ $(top_builddir)/src/common/libbabeltrace2-common.la \ $(top_builddir)/src/logging/libbabeltrace2-logging.la test_bt_values_LDADD = $(COMMON_TEST_LDADD) \ $(top_builddir)/src/lib/libbabeltrace2.la +test_fields_bin_LDADD = $(COMMON_TEST_LDADD) \ + $(top_builddir)/src/lib/libbabeltrace2.la + test_bt_uuid_LDADD = $(COMMON_TEST_LDADD) test_trace_ir_ref_LDADD = $(COMMON_TEST_LDADD) \ @@ -33,11 +37,13 @@ noinst_PROGRAMS = \ test_bt_uuid \ test_bt_values \ test_graph_topo \ + test_fields_bin \ test_remove_destruction_listener_in_destruction_listener \ test_simple_sink \ test_trace_ir_ref test_bt_values_SOURCES = test_bt_values.c +test_fields_bin_SOURCES = test_fields_bin.cpp test_simple_sink_SOURCES = test_simple_sink.c test_bt_uuid_SOURCES = test_bt_uuid.c test_trace_ir_ref_SOURCES = test_trace_ir_ref.c @@ -53,7 +59,7 @@ plugin_SOURCES = plugin.c SUBDIRS += test-plugin-plugins endif -dist_check_SCRIPTS = test_plugin +dist_check_SCRIPTS = test_plugin test_fields if HAVE_PYTHON if DEV_MODE diff --git a/tests/lib/test_fields b/tests/lib/test_fields new file mode 100755 index 00000000..577aa886 --- /dev/null +++ b/tests/lib/test_fields @@ -0,0 +1,17 @@ +#!/bin/bash +# +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2023 EfficiOS, Inc. +# + +if [ -n "${BT_TESTS_SRCDIR:-}" ]; then + UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh" +else + UTILSSH="$(dirname "$0")/../utils/utils.sh" +fi + +# shellcheck source=../utils/utils.sh +source "$UTILSSH" + +run_python_bt2 "${BT_TESTS_BUILDDIR}/lib/test_fields_bin" diff --git a/tests/lib/test_fields_bin.cpp b/tests/lib/test_fields_bin.cpp new file mode 100644 index 00000000..9899f86e --- /dev/null +++ b/tests/lib/test_fields_bin.cpp @@ -0,0 +1,72 @@ +/* + * SPDX-License-Identifier: GPL-2.0-only + * + * Copyright (C) 2023 EfficiOS Inc. + */ + +#include "utils/run-in.hpp" +#include "tap/tap.h" +#include "common/assert.h" +#include + +static const int NR_TESTS = 2; + +static void test_string_clear() +{ + runInMsgIterClsInit([](bt_self_message_iterator * const self) { + /* Boilerplate to get a string field */ + const auto traceCls = + bt_trace_class_create(bt_self_message_iterator_borrow_component(self)); + const auto streamCls = bt_stream_class_create(traceCls); + const auto eventCls = bt_event_class_create(streamCls); + const auto payloadCls = bt_field_class_structure_create(traceCls); + + { + const auto stringFieldCls = bt_field_class_string_create(traceCls); + const auto status = + bt_field_class_structure_append_member(payloadCls, "str", stringFieldCls); + BT_ASSERT(status == BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK); + bt_field_class_put_ref(stringFieldCls); + } + + { + const auto status = bt_event_class_set_payload_field_class(eventCls, payloadCls); + BT_ASSERT(status == BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK); + } + + const auto trace = bt_trace_create(traceCls); + const auto stream = bt_stream_create(streamCls, trace); + const auto msg = bt_message_event_create(self, eventCls, stream); + const auto field = bt_field_structure_borrow_member_field_by_name( + bt_event_borrow_payload_field(bt_message_event_borrow_event(msg)), "str"); + + /* Set the field to a known non-empty value */ + { + const auto status = bt_field_string_set_value(field, "pomme"); + BT_ASSERT(status == BT_FIELD_STRING_SET_VALUE_STATUS_OK); + BT_ASSERT(std::strcmp(bt_field_string_get_value(field), "pomme") == 0); + } + + /* Clear the field, verify its value and length */ + bt_field_string_clear(field); + ok(std::strcmp(bt_field_string_get_value(field), "") == 0, "string field is empty"); + ok(bt_field_string_get_length(field) == 0, "string field length is 0"); + + bt_message_put_ref(msg); + bt_stream_put_ref(stream); + bt_trace_put_ref(trace); + bt_field_class_put_ref(payloadCls); + bt_event_class_put_ref(eventCls); + bt_stream_class_put_ref(streamCls); + bt_trace_class_put_ref(traceCls); + }); +} + +int main() +{ + plan_tests(NR_TESTS); + + test_string_clear(); + + return exit_status(); +} diff --git a/tests/lib/utils/run-in.cpp b/tests/lib/utils/run-in.cpp index 3a3a0a26..2c2a53fc 100644 --- a/tests/lib/utils/run-in.cpp +++ b/tests/lib/utils/run-in.cpp @@ -74,6 +74,48 @@ msgIterClsNext(bt_self_message_iterator *, bt_message_array_const, uint64_t, uin return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; } +struct DummySinkData +{ + bt_message_iterator *msgIter; +}; + +static bt_component_class_initialize_method_status +dummySinkInit(bt_self_component_sink * const self, bt_self_component_sink_configuration * const, + const bt_value * const, void * const initMethodData) +{ + const auto status = bt_self_component_sink_add_input_port(self, "in", NULL, nullptr); + BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); + bt_self_component_set_data(bt_self_component_sink_as_self_component(self), initMethodData); + return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; +} + +static DummySinkData& dummySinkDataFromSelfCompSink(bt_self_component_sink * const self) +{ + return *static_cast( + bt_self_component_get_data(bt_self_component_sink_as_self_component(self))); +} + +static bt_component_class_sink_graph_is_configured_method_status +dummySinkGraphIsConfigured(bt_self_component_sink *const self) +{ + auto& data = dummySinkDataFromSelfCompSink(self); + const auto port = bt_self_component_sink_borrow_input_port_by_name(self, "in"); + BT_ASSERT(port); + const auto status = bt_message_iterator_create_from_sink_component(self, port, &data.msgIter); + BT_ASSERT(status == BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK); + return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; +} + +static bt_component_class_sink_consume_method_status dummySinkConsume(bt_self_component_sink *const self) +{ + const auto& data = dummySinkDataFromSelfCompSink(self); + bt_message_array_const msgs; + uint64_t msgCount; + const auto status = bt_message_iterator_next(data.msgIter, &msgs, &msgCount); + BT_ASSERT(status == BT_MESSAGE_ITERATOR_NEXT_STATUS_END); + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; +} + void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc, RunInMsgIterClsInitFunc msgIterCtxFunc) { @@ -124,6 +166,22 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun bt_query_executor_put_ref(queryExec); } + /* Create a dummy sink component */ + const auto sinkCompCls = bt_component_class_sink_create("dummy", dummySinkConsume); + BT_ASSERT(sinkCompCls); + + { + const auto status = + bt_component_class_sink_set_initialize_method(sinkCompCls, dummySinkInit); + BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); + } + + { + const auto status = bt_component_class_sink_set_graph_is_configured_method( + sinkCompCls, dummySinkGraphIsConfigured); + BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); + } + /* Create graph */ const auto graph = bt_graph_create(0); @@ -134,33 +192,18 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun { const auto status = bt_graph_add_source_component_with_initialize_method_data( - graph, srcCompCls, "whatever", NULL, &data, BT_LOGGING_LEVEL_NONE, &srcComp); + graph, srcCompCls, "the-source", NULL, &data, BT_LOGGING_LEVEL_NONE, &srcComp); BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); } - /* Find `sink.utils.dummy` component class */ - const bt_plugin *utilsPlugin = nullptr; - - { - const auto status = - bt_plugin_find("utils", BT_TRUE, BT_TRUE, BT_TRUE, BT_TRUE, BT_TRUE, &utilsPlugin); - - BT_ASSERT(status == BT_PLUGIN_FIND_STATUS_OK); - } - - /* Add `sink.utils.dummy` component */ + /* Add dummy sink component */ const bt_component_sink *sinkComp; + DummySinkData dummySinkData; { - const auto dummyCompCls = - bt_plugin_borrow_sink_component_class_by_name_const(utilsPlugin, "dummy"); - - BT_ASSERT(dummyCompCls); - - const auto status = bt_graph_add_sink_component(graph, dummyCompCls, "the-sink", nullptr, - BT_LOGGING_LEVEL_NONE, &sinkComp); - + const auto status = bt_graph_add_sink_component_with_initialize_method_data( + graph, sinkCompCls, "the-sink", NULL, &dummySinkData, BT_LOGGING_LEVEL_NONE, &sinkComp); BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); } @@ -185,9 +228,9 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun BT_ASSERT(status == BT_GRAPH_RUN_STATUS_OK); /* Discard plugin and graph */ - bt_plugin_put_ref(utilsPlugin); bt_graph_put_ref(graph); bt_component_class_source_put_ref(srcCompCls); + bt_component_class_sink_put_ref(sinkCompCls); bt_message_iterator_class_put_ref(msgIterCls); } -- 2.34.1