btest/plugin: Test for PublishEventHook()

This commit is contained in:
Arne Welzel 2025-04-08 11:08:55 +02:00
parent 53236a184a
commit 0bf3417d4c
5 changed files with 142 additions and 0 deletions

View file

@ -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(<cluster backend Broker>, /test/topic, <cluster event App::test_event, 1 argument>)
0.000000 HookPublishEvent backend=Broker topic=/test/topic event=App::test_event
0.000000 HookPublishEvent /test/topic(App::test_event)
0.000000 MetaHookPost PublishEvent(<cluster backend Broker>, /test/topic, <cluster event App::test_event, 1 argument>) -> true
0.000000 MetaHookPre PublishEvent(<cluster backend Broker>, /do/not/publish, <cluster event App::test_event, 1 argument>)
0.000000 HookPublishEvent backend=Broker topic=/do/not/publish event=App::test_event
0.000000 MetaHookPost PublishEvent(<cluster backend Broker>, /do/not/publish, <cluster event App::test_event, 1 argument>) -> false

View file

@ -0,0 +1,86 @@
#include "Plugin.h"
#include <zeek/Desc.h>
#include <zeek/cluster/Backend.h>
#include <cstdio>
#include <string>
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());
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <zeek/plugin/Plugin.h>
#include <string>
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

View file

@ -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);
}