mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
zeek.bif: Add find_in_zeekpath() helper
Relates to #3594. This helper can be used to determine the path that will be used by @load, if at all.
This commit is contained in:
parent
4a9b580dab
commit
704f75a214
6 changed files with 108 additions and 0 deletions
1
testing/btest/Baseline/bifs.find_in_zeekpath/.stderr
Normal file
1
testing/btest/Baseline/bifs.find_in_zeekpath/.stderr
Normal file
|
@ -0,0 +1 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
|
@ -0,0 +1,3 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
error: find_in_zeek_path: path must be relative or absolute
|
||||
error: find_in_zeek_path: path must be relative or absolute
|
|
@ -0,0 +1,3 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
relative,
|
||||
absolute,
|
10
testing/btest/Baseline/bifs.find_in_zeekpath/out
Normal file
10
testing/btest/Baseline/bifs.find_in_zeekpath/out
Normal file
|
@ -0,0 +1,10 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
find_in_zeekpath base/protocols/conn, base/protocols/conn/__load__.zeek
|
||||
find_in_zeekpath protocols/conn (empty expected, no __load__.zeek),
|
||||
find_in_zeekpath protocols/conn/vlan-logging, policy/protocols/conn/vlan-logging.zeek
|
||||
find_in_zeekpath pkg1, ./pkg1.zeek
|
||||
find_in_zeekpath pkg1.zeek, ./pkg1.zeek
|
||||
find_in_zeekpath pkg2, ./pkg2/__load__.zeek
|
||||
find_in_zeekpath pkg3,
|
||||
pkg1!
|
||||
pkg2!
|
59
testing/btest/bifs/find_in_zeekpath.zeek
Normal file
59
testing/btest/bifs/find_in_zeekpath.zeek
Normal file
|
@ -0,0 +1,59 @@
|
|||
# @TEST-DOC: Test find_in_zeekpath() and demo conditional @load'ing.
|
||||
#
|
||||
# @TEST-EXEC: zeek -b %INPUT >out
|
||||
# @TEST-EXEC: btest-diff out
|
||||
# @TEST-EXEC: btest-diff .stderr
|
||||
#
|
||||
# @TEST-EXEC: zeek -b errors.zeek >errors.stdout 2>errors.stderr
|
||||
# @TEST-EXEC: btest-diff errors.stdout
|
||||
# @TEST-EXEC: btest-diff errors.stderr
|
||||
|
||||
|
||||
@if ( find_in_zeekpath("pkg1") != "" )
|
||||
@load pkg1
|
||||
@endif
|
||||
|
||||
@if ( find_in_zeekpath("pkg2") != "" )
|
||||
@load pkg2
|
||||
@endif
|
||||
|
||||
@if ( find_in_zeekpath("pkg3") != "" )
|
||||
@load pkg3
|
||||
@endif
|
||||
|
||||
function path_tail(r: string): string
|
||||
{
|
||||
if ( |r| == 0 )
|
||||
return r;
|
||||
local parts = split_string(r, /\//);
|
||||
return join_string_vec(parts[-4:], "/");
|
||||
}
|
||||
|
||||
print "find_in_zeekpath base/protocols/conn", path_tail(find_in_zeekpath("base/protocols/conn"));
|
||||
print "find_in_zeekpath protocols/conn (empty expected, no __load__.zeek)", find_in_zeekpath("protocols/conn");
|
||||
print "find_in_zeekpath protocols/conn/vlan-logging", path_tail(find_in_zeekpath("protocols/conn/vlan-logging"));
|
||||
|
||||
print "find_in_zeekpath pkg1", find_in_zeekpath("pkg1");
|
||||
print "find_in_zeekpath pkg1.zeek", find_in_zeekpath("pkg1.zeek");
|
||||
print "find_in_zeekpath pkg2", find_in_zeekpath("pkg2");
|
||||
print "find_in_zeekpath pkg3", find_in_zeekpath("pkg3");
|
||||
|
||||
@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
|
||||
|
||||
@TEST-START-FILE errors.zeek
|
||||
# Using relative and absolute paths is an error (empty string)
|
||||
print "relative", find_in_zeekpath("./pkg1.zeek");
|
||||
print "absolute", find_in_zeekpath("/pkg1");
|
||||
@TEST-END-FILE
|
Loading…
Add table
Add a link
Reference in a new issue