utils: Introduce packages.zeek with can_load() helper

This commit is contained in:
Arne Welzel 2024-02-22 09:53:12 +01:00
parent 704f75a214
commit 947294efab
5 changed files with 54 additions and 0 deletions

View file

@ -17,6 +17,7 @@
@load base/utils/geoip-distance
@load base/utils/hash_hrw
@load base/utils/numbers
@load base/utils/packages
@load base/utils/paths
@load base/utils/patterns
@load base/utils/queue

View file

@ -0,0 +1,16 @@
##! Rudimentary functions for helping with Zeek packages.
## Checks whether @load of a given package name could
## be successful.
##
## This tests for the existence of corresponding script files
## in ZEEKPATH. It does not attempt to parse and validate
## any actual Zeek script code.
##
## path: The filename, package or path to test.
##
## Returns: T if the given filename, package or path may load.
function can_load(p: string): bool
{
return find_in_zeekpath(p) != "";
}

View file

@ -290,6 +290,7 @@ scripts/base/init-default.zeek
scripts/base/utils/files.zeek
scripts/base/utils/geoip-distance.zeek
scripts/base/utils/numbers.zeek
scripts/base/utils/packages.zeek
scripts/base/utils/queue.zeek
scripts/base/utils/strings.zeek
scripts/base/utils/thresholds.zeek

View file

@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
no pkg3
pkg1!
pkg2!

View file

@ -0,0 +1,32 @@
# @TEST-EXEC: zeek -b %INPUT >output
# @TEST-EXEC: btest-diff output
@load base/utils/packages
@if ( can_load("pkg1") )
@load pkg1
@endif
@if ( can_load("pkg2") )
@load pkg2
@endif
@if ( can_load("pkg3") )
@load pkg3
@else
print "no pkg3";
@endif
@TEST-START-FILE pkg1.zeek
event zeek_init()
{
print "pkg1!";
}
@TEST-END-FILE
@TEST-START-FILE pkg2/__load__.zeek
event zeek_init()
{
print "pkg2!";
}
@TEST-END-FILE