Improve an input framework unit test

This commit is contained in:
Jon Siwek 2018-07-17 17:51:13 -05:00
parent 1d1a63c16c
commit bf67076cdc
4 changed files with 38 additions and 56 deletions

View file

@ -1,4 +1,8 @@
2.5-746 | 2018-07-17 17:51:13 -0500
* Improve an input framework unit test (Jon Siwek, Corelight)
2.5-745 | 2018-07-17 16:46:16 -0500 2.5-745 | 2018-07-17 16:46:16 -0500
* Add explicit key in Travis known_hosts (Jon Siwek, Corelight) * Add explicit key in Travis known_hosts (Jon Siwek, Corelight)

View file

@ -1 +1 @@
2.5-745 2.5-746

View file

@ -1,27 +1,9 @@
Input::EVENT_NEW Input::EVENT_NEW line output (stderr=F): ../mydir:
..: Input::EVENT_NEW line output (stderr=F): a
F Input::EVENT_NEW line output (stderr=F): b
Input::EVENT_NEW Input::EVENT_NEW line output (stderr=F): c
bro Input::EVENT_NEW line output (stderr=T): <stderr output contained nonexistant>
F Input::EVENT_NEW line output (stderr=T): <stderr output contained nonexistant>
Input::EVENT_NEW Input::EVENT_NEW line output (stderr=T): <stderr output contained nonexistant>
out End of Data event, input
F Process finished event, input, T
Input::EVENT_NEW
stderr.bro
F
Input::EVENT_NEW
stderr output contained nonexistant
T
Input::EVENT_NEW
stderr output contained nonexistant
T
Input::EVENT_NEW
stderr output contained nonexistant
T
done
End of Data event
input
Process finished event
input
Exit code != 0

View file

@ -1,3 +1,4 @@
# @TEST-EXEC: mkdir mydir && touch mydir/a && touch mydir/b && touch mydir/c
# @TEST-EXEC: btest-bg-run bro bro -b %INPUT # @TEST-EXEC: btest-bg-run bro bro -b %INPUT
# @TEST-EXEC: btest-bg-wait 10 # @TEST-EXEC: btest-bg-wait 10
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
@ -9,64 +10,59 @@ type Val: record {
is_stderr: bool; is_stderr: bool;
}; };
global try: count; global try = 0;
global n = 0;
global outfile: file; global outfile: file;
event line(description: Input::EventDescription, tpe: Input::Event, s: string, is_stderr: bool) event line(description: Input::EventDescription, tpe: Input::Event, s: string, is_stderr: bool)
{ {
print outfile, tpe; local line_output = fmt("%s line output (stderr=%s): ", tpe, is_stderr);
if ( is_stderr ) if ( is_stderr )
{ {
# work around localized error messages. and if some localization does not include the filename... well... that would be bad :) # work around localized error messages. and if some localization does not include the filename... well... that would be bad :)
if ( strstr(s, "nonexistant") > 0 ) if ( strstr(s, "nonexistant") > 0 )
{ line_output += "<stderr output contained nonexistant>";
print outfile, "stderr output contained nonexistant"; else
} line_output += "<unexpected/weird error localization>";
} }
else else
{ line_output += s;
print outfile, s;
}
print outfile, is_stderr;
try = try + 1; print outfile, line_output;
if ( try == 7 ) ++try;
{
print outfile, "done"; if ( n == 2 && try == 7 )
Input::remove("input"); terminate();
}
} }
global n = 0;
event Input::end_of_data(name: string, source:string) event Input::end_of_data(name: string, source:string)
{ {
print outfile, "End of Data event"; print outfile, "End of Data event", name;
print outfile, name;
++n; ++n;
if ( n == 2 )
if ( n == 2 && try == 7 )
terminate(); terminate();
} }
event InputRaw::process_finished(name: string, source:string, exit_code:count, signal_exit:bool) event InputRaw::process_finished(name: string, source:string, exit_code:count, signal_exit:bool)
{ {
print outfile, "Process finished event"; print outfile, "Process finished event", name, exit_code != 0;
print outfile, name;
if ( exit_code != 0 )
print outfile, "Exit code != 0";
++n; ++n;
if ( n == 2 )
if ( n == 2 && try == 7 )
terminate(); terminate();
} }
event bro_init() event bro_init()
{ {
local config_strings: table[string] of string = { local config_strings: table[string] of string = {
["read_stderr"] = "1" ["read_stderr"] = "1"
}; };
outfile = open("../out"); outfile = open("../out");
try = 0; Input::add_event([$source="ls ../mydir ../nonexistant ../nonexistant2 ../nonexistant3 |",
Input::add_event([$source="ls .. ../nonexistant ../nonexistant2 ../nonexistant3 |", $reader=Input::READER_RAW, $name="input", $fields=Val, $ev=line, $want_record=F, $config=config_strings, $mode=Input::STREAM]); $reader=Input::READER_RAW, $name="input",
$fields=Val, $ev=line, $want_record=F,
$config=config_strings, $mode=Input::STREAM]);
} }