From 4c305d6b92c706d6a14c9c6c34e0766e7162a172 Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Mon, 20 Oct 2014 15:59:58 -0700 Subject: [PATCH 1/5] [FIX] Add files to result table even if the files are empty --- scripts/base/utils/active-http.bro | 4 +++- scripts/base/utils/exec.bro | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/base/utils/active-http.bro b/scripts/base/utils/active-http.bro index 9f62e7bbaa..1bc390d6d6 100644 --- a/scripts/base/utils/active-http.bro +++ b/scripts/base/utils/active-http.bro @@ -65,12 +65,14 @@ function request2curl(r: Request, bodyfile: string, headersfile: string): string cmd = fmt("%s -m %.0f", cmd, r$max_time); if ( r?$client_data ) - cmd = fmt("%s -d -", cmd); + cmd = fmt("%s -d @-", cmd); if ( r?$addl_curl_args ) cmd = fmt("%s %s", cmd, r$addl_curl_args); cmd = fmt("%s \"%s\"", cmd, str_shell_escape(r$url)); + # hack so the bodyfile will exsist even if curl did not write one. + cmd = fmt("%s && touch %s", cmd, str_shell_escape(bodyfile)); return cmd; } diff --git a/scripts/base/utils/exec.bro b/scripts/base/utils/exec.bro index 12f5a0087b..dd992a63cc 100644 --- a/scripts/base/utils/exec.bro +++ b/scripts/base/utils/exec.bro @@ -106,6 +106,16 @@ event Input::end_of_data(name: string, source:string) local track_file = parts[2]; + # If the file is empty, add it to the result$files table + # this is needed because it is expected that the file was read + # even if it was empty + local result = results[name]; + if ( ! result?$files ) + result$files = table(); + + if ( track_file !in result$files ) + result$files[track_file] = vector(source); + Input::remove(name); if ( name !in pending_files ) From 04746c7ffc382cd4a870ed279d78544bc1536b1a Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Wed, 22 Oct 2014 11:57:03 -0700 Subject: [PATCH 2/5] [FIX] exec should write an empty string when file is empty instead of the filename --- scripts/base/utils/exec.bro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/base/utils/exec.bro b/scripts/base/utils/exec.bro index dd992a63cc..6111608eca 100644 --- a/scripts/base/utils/exec.bro +++ b/scripts/base/utils/exec.bro @@ -114,7 +114,7 @@ event Input::end_of_data(name: string, source:string) result$files = table(); if ( track_file !in result$files ) - result$files[track_file] = vector(source); + result$files[track_file] = vector(); Input::remove(name); From 0a597720434fa893623d5c97e081ac5002b896c6 Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Wed, 22 Oct 2014 16:02:19 -0700 Subject: [PATCH 3/5] [ADD] added baseline for the new exec test and added a test to check for the empty files fix. --- .../btest/Baseline/scripts.base.utils.exec/bro..stdout | 6 +++++- testing/btest/scripts/base/utils/exec.test | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/testing/btest/Baseline/scripts.base.utils.exec/bro..stdout b/testing/btest/Baseline/scripts.base.utils.exec/bro..stdout index 3cfdaafb4c..043e1d4baa 100644 --- a/testing/btest/Baseline/scripts.base.utils.exec/bro..stdout +++ b/testing/btest/Baseline/scripts.base.utils.exec/bro..stdout @@ -1,6 +1,10 @@ +test2, [exit_code=1, signal_exit=F, stdout=[here's something on stdout, some more stdout, last stdout], stderr=[and some stderr, more stderr, last stderr], files=] test1, [exit_code=0, signal_exit=F, stdout=[done, exit, stop], stderr=, files={ [out1] = [insert text here, and here], [out2] = [insert more text here, and there] }] -test2, [exit_code=1, signal_exit=F, stdout=[here's something on stdout, some more stdout, last stdout], stderr=[and some stderr, more stderr, last stderr], files=] test4, [exit_code=0, signal_exit=F, stdout=[hibye], stderr=, files=] +test5, [exit_code=0, signal_exit=F, stdout=, stderr=, files={ +[out4] = [test], +[out3] = [] +}] diff --git a/testing/btest/scripts/base/utils/exec.test b/testing/btest/scripts/base/utils/exec.test index 33ba10f97a..389527bcfc 100644 --- a/testing/btest/scripts/base/utils/exec.test +++ b/testing/btest/scripts/base/utils/exec.test @@ -14,7 +14,7 @@ function check_exit_condition() { c += 1; - if ( c == 3 ) + if ( c == 4 ) terminate(); } @@ -35,6 +35,8 @@ event bro_init() # Not sure of a portable way to test signals yet. #test_cmd("test3", [$cmd="bash ../suicide.sh"]); test_cmd("test4", [$cmd="bash ../stdin.sh", $stdin="hibye"]); + test_cmd("test5", [$cmd="bash ../empty_file.sh", + $read_files=set("out3", "out4")]); } @TEST-END-FILE @@ -73,3 +75,9 @@ echo "nope" read -r line echo "$line" @TEST-END-FILE + +@TEST-START-FILE empty_file.sh +#! /usr/bin/env bash +touch out3 +echo "test" > out4 +@TEST-END-FILE From d17b3746cf400c430ff2bbd733aacb9c1b5c431b Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Wed, 22 Oct 2014 16:04:04 -0700 Subject: [PATCH 4/5] [ADD] added baseline for the new active-http test and added a test to check for the content-length 0 fix. --- .../bro..stdout | 7 ++++- .../btest/scripts/base/utils/active-http.test | 30 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/testing/btest/Baseline/scripts.base.utils.active-http/bro..stdout b/testing/btest/Baseline/scripts.base.utils.active-http/bro..stdout index 0284eb19b3..eac07da9a8 100644 --- a/testing/btest/Baseline/scripts.base.utils.active-http/bro..stdout +++ b/testing/btest/Baseline/scripts.base.utils.active-http/bro..stdout @@ -1,4 +1,9 @@ -[code=200, msg=OK^M, body=It works!, headers={ +test1, [code=200, msg=OK^M, body=It works!, headers={ +[Server] = 1.0, +[Content-type] = text/plain, +[Date] = July 22, 2013 +}] +test2, [code=200, msg=OK^M, body=, headers={ [Server] = 1.0, [Content-type] = text/plain, [Date] = July 22, 2013 diff --git a/testing/btest/scripts/base/utils/active-http.test b/testing/btest/scripts/base/utils/active-http.test index 442d5b9e06..b910ea1c7e 100644 --- a/testing/btest/scripts/base/utils/active-http.test +++ b/testing/btest/scripts/base/utils/active-http.test @@ -1,7 +1,7 @@ # @TEST-REQUIRES: which python # @TEST-REQUIRES: which curl # -# @TEST-EXEC: btest-bg-run httpd python $SCRIPTS/httpd.py --max 1 --addr=127.0.0.1 +# @TEST-EXEC: btest-bg-run httpd python $SCRIPTS/httpd.py --max 2 --addr=127.0.0.1 # @TEST-EXEC: sleep 3 # @TEST-EXEC: btest-bg-run bro bro -b %INPUT # @TEST-EXEC: btest-bg-wait 15 @@ -11,18 +11,32 @@ @load base/frameworks/communication # let network-time run. otherwise there are no heartbeats... redef exit_only_after_terminate = T; -event bro_init() - { - local req = ActiveHTTP::Request($url="127.0.0.1:32123"); +global c: count = 0; - when ( local resp = ActiveHTTP::request(req) ) - { - print resp; +function check_exit_condition() + { + c += 1; + + if ( c == 2 ) terminate(); + } + +function test_request(label: string, req: ActiveHTTP::Request) + { + when ( local response = ActiveHTTP::request(req) ) + { + print label, response; + check_exit_condition(); } timeout 1min { print "HTTP request timeout"; - terminate(); + check_exit_condition(); } } + +event bro_init() + { + test_request("test1", [$url="127.0.0.1:32123"]); + test_request("test2", [$url="127.0.0.1:32123/empty", $method="POST"]); + } From de334905866806728a3730bb137f5fefb99f5db2 Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Wed, 22 Oct 2014 16:05:06 -0700 Subject: [PATCH 5/5] [ADD] Added the feature to return 0 content to the python http test server and added functionality for post requests --- testing/scripts/httpd.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/testing/scripts/httpd.py b/testing/scripts/httpd.py index 0732614bc2..e00eb3a6bc 100755 --- a/testing/scripts/httpd.py +++ b/testing/scripts/httpd.py @@ -2,13 +2,26 @@ import BaseHTTPServer + class MyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() - self.wfile.write("It works!") + if "/empty" in self.path: + self.wfile.write("") + else: + self.wfile.write("It works!") + + def do_POST(self): + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + if "/empty" in self.path: + self.wfile.write("") + else: + self.wfile.write("It works!") def version_string(self): return "1.0"