diff --git a/testing/btest/Baseline/plugins.publish-event-hook/output b/testing/btest/Baseline/plugins.publish-event-hook/output new file mode 100644 index 0000000000..2a41f600e0 --- /dev/null +++ b/testing/btest/Baseline/plugins.publish-event-hook/output @@ -0,0 +1,9 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +0.000000 InitPostScript +0.000000 MetaHookPre PublishEvent(, /test/topic, ) +0.000000 HookPublishEvent backend=Broker topic=/test/topic event=App::test_event +0.000000 HookPublishEvent /test/topic(App::test_event) +0.000000 MetaHookPost PublishEvent(, /test/topic, ) -> true +0.000000 MetaHookPre PublishEvent(, /do/not/publish, ) +0.000000 HookPublishEvent backend=Broker topic=/do/not/publish event=App::test_event +0.000000 MetaHookPost PublishEvent(, /do/not/publish, ) -> false diff --git a/testing/btest/plugins/publish-event-hook-plugin/.btest-ignore b/testing/btest/plugins/publish-event-hook-plugin/.btest-ignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testing/btest/plugins/publish-event-hook-plugin/src/Plugin.cc b/testing/btest/plugins/publish-event-hook-plugin/src/Plugin.cc new file mode 100644 index 0000000000..ed88976120 --- /dev/null +++ b/testing/btest/plugins/publish-event-hook-plugin/src/Plugin.cc @@ -0,0 +1,86 @@ + +#include "Plugin.h" + +#include +#include +#include +#include + +namespace btest::plugin::Demo_PublishEvent { +Plugin plugin; +} + +using namespace btest::plugin::Demo_PublishEvent; + +zeek::plugin::Configuration Plugin::Configure() { + EnableHook(zeek::plugin::HOOK_PUBLISH_EVENT); + EnableHook(zeek::plugin::META_HOOK_PRE); + EnableHook(zeek::plugin::META_HOOK_POST); + + zeek::plugin::Configuration config; + config.name = "Demo::PublishEvent"; + config.description = "Exercises hook for publishing events"; + config.version.major = 1; + config.version.minor = 0; + config.version.patch = 0; + return config; +} + +void Plugin::InitPostScript() { + std::fprintf(stdout, "%.6f %-15s\n", zeek::run_state::network_time, " InitPostScript"); +} + +static void describe_hook_args(const zeek::plugin::HookArgumentList& args, zeek::ODesc* d) { + bool first = true; + + for ( const auto& arg : args ) { + if ( ! first ) + d->Add(", "); + + arg.Describe(d); + first = false; + } +} + +bool Plugin::HookPublishEvent(zeek::cluster::Backend& backend, const std::string& topic, + zeek::cluster::detail::Event& event) { + std::fprintf(stdout, "%.6f %-15s backend=%s topic=%s event=%s\n", zeek::run_state::network_time, + " HookPublishEvent", backend.Name().c_str(), topic.c_str(), std::string(event.HandlerName()).c_str()); + + if ( topic == "/do/not/publish" ) + return false; + + std::fprintf(stdout, "%.6f %-15s %s(%s)\n", zeek::run_state::network_time, " HookPublishEvent", topic.c_str(), + std::string(event.HandlerName()).c_str()); + + return true; +} + +void Plugin::MetaHookPre(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args) { + if ( hook != zeek::plugin::HOOK_PUBLISH_EVENT ) + return; + + zeek::ODesc d; + d.SetShort(); + describe_hook_args(args, &d); + + std::fprintf(stdout, "%.6f %-15s %s(%s)\n", zeek::run_state::network_time, " MetaHookPre", hook_name(hook), + d.Description()); +} + +void Plugin::MetaHookPost(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args, + zeek::plugin::HookArgument result) { + if ( hook != zeek::plugin::HOOK_PUBLISH_EVENT ) + return; + + zeek::ODesc d1; + d1.SetShort(); + describe_hook_args(args, &d1); + + zeek::ODesc d2; + d2.SetShort(); + result.Describe(&d2); + + std::fprintf(stdout, "%.6f %-15s %s(%s) -> %s\n", zeek::run_state::network_time, " MetaHookPost", hook_name(hook), + d1.Description(), d2.Description()); +} diff --git a/testing/btest/plugins/publish-event-hook-plugin/src/Plugin.h b/testing/btest/plugins/publish-event-hook-plugin/src/Plugin.h new file mode 100644 index 0000000000..82b7c68b56 --- /dev/null +++ b/testing/btest/plugins/publish-event-hook-plugin/src/Plugin.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +namespace btest::plugin::Demo_PublishEvent { + +class Plugin : public zeek::plugin::Plugin { +protected: + zeek::plugin::Configuration Configure() override; + void InitPostScript() override; + + bool HookPublishEvent(zeek::cluster::Backend& backend, const std::string& topic, + zeek::cluster::detail::Event& event) override; + + void MetaHookPre(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args) override; + void MetaHookPost(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args, + zeek::plugin::HookArgument result) override; +}; + +extern Plugin plugin; + +} // namespace btest::plugin::Demo_PublishEvent diff --git a/testing/btest/plugins/publish-event-hook.zeek b/testing/btest/plugins/publish-event-hook.zeek new file mode 100644 index 0000000000..704a1ccc59 --- /dev/null +++ b/testing/btest/plugins/publish-event-hook.zeek @@ -0,0 +1,24 @@ +# @TEST-EXEC: ${DIST}/auxil/zeek-aux/plugin-support/init-plugin -u . Demo PublishEvent +# @TEST-EXEC: cp -r %DIR/publish-event-hook-plugin/* . +# @TEST-EXEC: ./configure --zeek-dist=${DIST} && make +# @TEST-EXEC: ZEEK_PLUGIN_PATH=`pwd` zeek -b Demo::PublishEvent %INPUT > output +# @TEST-EXEC: btest-diff output + +redef allow_network_time_forward = F; + + +module App; + +export { + global test_event: event(c: count); + + global topic: string = "/test/topic"; + + global do_not_publish_topic: string = "/do/not/publish"; +} + +event zeek_init() &priority=10 + { + Cluster::publish(topic, test_event, 42); + Cluster::publish(do_not_publish_topic, test_event, 9999); + }