From cb610bdea270ac5ae009cc839c3c45e59f5268ab Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Fri, 4 Apr 2025 09:52:46 +0200 Subject: [PATCH] btest/plugin: Add plugin testing enum identifiers This plugin has a generic name, but for now just tests the API around enum types and enum vals at InitPostScript() time. --- .../btest/Baseline/plugins.api-plugin/output | 2 + testing/btest/plugins/api-plugin.zeek | 26 ++++++ .../btest/plugins/api-plugin/.btest-ignore | 0 .../btest/plugins/api-plugin/src/Plugin.cc | 88 +++++++++++++++++++ testing/btest/plugins/api-plugin/src/Plugin.h | 18 ++++ 5 files changed, 134 insertions(+) create mode 100644 testing/btest/Baseline/plugins.api-plugin/output create mode 100644 testing/btest/plugins/api-plugin.zeek create mode 100644 testing/btest/plugins/api-plugin/.btest-ignore create mode 100644 testing/btest/plugins/api-plugin/src/Plugin.cc create mode 100644 testing/btest/plugins/api-plugin/src/Plugin.h diff --git a/testing/btest/Baseline/plugins.api-plugin/output b/testing/btest/Baseline/plugins.api-plugin/output new file mode 100644 index 0000000000..f21c6a7f90 --- /dev/null +++ b/testing/btest/Baseline/plugins.api-plugin/output @@ -0,0 +1,2 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +TestEnumIdentifiers successful diff --git a/testing/btest/plugins/api-plugin.zeek b/testing/btest/plugins/api-plugin.zeek new file mode 100644 index 0000000000..f318cab1d7 --- /dev/null +++ b/testing/btest/plugins/api-plugin.zeek @@ -0,0 +1,26 @@ +# @TEST-DOC: A plugin testing some parts Zeek's C++ API +# @TEST-EXEC: ${DIST}/auxil/zeek-aux/plugin-support/init-plugin -u . Demo API +# @TEST-EXEC: cp -r %DIR/api-plugin/* . +# +# @TEST-EXEC: ./configure --zeek-dist=${DIST} && make +# +# @TEST-EXEC: ZEEK_PLUGIN_PATH=`pwd` zeek %INPUT >output +# +# @TEST-EXEC: TEST_DIFF_CANONIFIER= btest-diff output + +module DemoAPI; + +export { + type Severity: enum { + CRITICAL = 1, + ERROR = 2, + WARNING = 3, + INFO = 4, + }; +} + +module User; + +redef enum DemoAPI::Severity += { + USER_DEBUG = 50, +}; diff --git a/testing/btest/plugins/api-plugin/.btest-ignore b/testing/btest/plugins/api-plugin/.btest-ignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testing/btest/plugins/api-plugin/src/Plugin.cc b/testing/btest/plugins/api-plugin/src/Plugin.cc new file mode 100644 index 0000000000..40ed229c66 --- /dev/null +++ b/testing/btest/plugins/api-plugin/src/Plugin.cc @@ -0,0 +1,88 @@ + +#include "Plugin.h" + +#include +#include +#include +#include +#include + +namespace btest::plugin::Demo_API { +Plugin plugin; +} + +using namespace btest::plugin::Demo_API; + +zeek::plugin::Configuration Plugin::Configure() { + zeek::plugin::Configuration config; + config.name = "Demo::API"; + config.description = "Use some of Zeek's API for testing"; + config.version.major = 1; + config.version.minor = 0; + config.version.patch = 0; + return config; +} + +namespace { + +// Test zeek::id::find for enums an their types. +void TestEnumIdentifiers() { + auto severity_type = zeek::id::find_type("DemoAPI::Severity"); + if ( ! severity_type ) + zeek::reporter->FatalError("DemoAPI::Severity not found"); + + // Expect 5 entries! + if ( severity_type->Names().size() != 5 ) + zeek::reporter->FatalError("Wrong number of severities %" PRId64, severity_type->Names().size()); + + // Ensure CRITICAL and USER_DEBUG identifiers have the same enum type as severity_type. + auto critical_id = zeek::id::find("DemoAPI::CRITICAL"); + auto critical_type = critical_id->GetType(); + auto user_debug_id = zeek::id::find("User::USER_DEBUG"); + auto user_debug_type = user_debug_id->GetType(); + + if ( critical_type != user_debug_type ) + zeek::reporter->FatalError("CRITICAL and USER_DEBUG have different types (%p and %p)", critical_type.get(), + user_debug_type.get()); + + // Ensure the critical_id and user_debug_type IDs have an EnumVal attached + // and that the value is the same as in script land. + auto critical_val = critical_id->GetVal(); + auto user_debug_val = user_debug_id->GetVal(); + + if ( ! critical_val || ! user_debug_val ) + zeek::reporter->FatalError("Missing values on enum value identifiers %p %p", critical_val.get(), + user_debug_val.get()); + + if ( critical_val->AsEnum() != 1 ) + zeek::reporter->FatalError("Wrong value for CRITICAL: %" PRId64, critical_val->AsEnum()); + + if ( user_debug_val->AsEnum() != 50 ) + zeek::reporter->FatalError("Wrong value for USER_DEBUG: %" PRId64, user_debug_val->AsEnum()); + + // Ensure all the types (identifiers and values) agree with severity_type. + if ( critical_type != severity_type ) + zeek::reporter->FatalError("CRITICAL identifier has the wrong enum type %p vs %p", critical_type.get(), + severity_type.get()); + + if ( user_debug_type != severity_type ) + zeek::reporter->FatalError("USER_DEBUG identifier has the wrong enum type %p vs %p", user_debug_type.get(), + severity_type.get()); + + if ( critical_val->GetType() != severity_type ) + zeek::reporter->FatalError("CRITICAL value has the wrong enum type %p vs %p", critical_val->GetType().get(), + severity_type.get()); + + if ( user_debug_val->GetType() != severity_type ) + zeek::reporter->FatalError("USER_DEBUG value has the wrong enum type %p vs %p", user_debug_val->GetType().get(), + severity_type.get()); + + std::cout << "TestEnumIdentifiers successful" << std::endl; +} + +} // namespace + +void Plugin::InitPostScript() { + // Other API tests if wanted. + TestEnumIdentifiers(); +} diff --git a/testing/btest/plugins/api-plugin/src/Plugin.h b/testing/btest/plugins/api-plugin/src/Plugin.h new file mode 100644 index 0000000000..2c2740e97a --- /dev/null +++ b/testing/btest/plugins/api-plugin/src/Plugin.h @@ -0,0 +1,18 @@ + +#pragma once + +#include + +namespace btest::plugin::Demo_API { + +class Plugin : public zeek::plugin::Plugin { +protected: + // Overridden from zeek::plugin::Plugin. + zeek::plugin::Configuration Configure() override; + + void InitPostScript() override; +}; + +extern Plugin plugin; + +} // namespace btest::plugin::Demo_API