From 9370db8980e64b1b9e7b679ccd3ed676ea197abb Mon Sep 17 00:00:00 2001 From: Daniel Thayer Date: Thu, 2 Feb 2017 11:58:38 -0600 Subject: [PATCH 1/3] Add tests for the to_json() function --- .../Baseline/scripts.base.utils.json/output | 39 ++++++ testing/btest/scripts/base/utils/json.test | 122 ++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 testing/btest/Baseline/scripts.base.utils.json/output create mode 100644 testing/btest/scripts/base/utils/json.test diff --git a/testing/btest/Baseline/scripts.base.utils.json/output b/testing/btest/Baseline/scripts.base.utils.json/output new file mode 100644 index 0000000000..a1f6875417 --- /dev/null +++ b/testing/btest/Baseline/scripts.base.utils.json/output @@ -0,0 +1,39 @@ +T +123 +-999 +1.123457 +-1.123456e+308 +0 +1.0 +"" +"hello" +"" +65535 +1 +123 +0 +"1.2.3.4" +"ffff:1234::1" +"123.123.123.123" +"192.0.0.0/8" +"fe80::/64" +Red +{"s": "test", "c": 100} +{"s": "test"} +{"s": "test"} +{"m": {"s": "test"}} +[] +[2, 1] +["1.2.3.4"] +[[T, F]] +[{"s": "test"}] +[] +[2, 1] +["1.2.3.4"] +[{"s": "test"}] +[{"s": "test"}] +{} +{"2": "10.2.2.2", "1": "10.1.1.1"} +{"10.1.1.1": {"a": 1}, "10.2.2.2": {"b": 2}} +{"10.1.1.1": [1, 2], "10.2.2.2": [3, 5]} +{"1": {"s": "test"}} diff --git a/testing/btest/scripts/base/utils/json.test b/testing/btest/scripts/base/utils/json.test new file mode 100644 index 0000000000..369ba0ebb7 --- /dev/null +++ b/testing/btest/scripts/base/utils/json.test @@ -0,0 +1,122 @@ +# Test the to_json() function on every basic data type. For container types, +# test with no elements, with one element, and with more than one element. +# Test that the "only_loggable" option works (output only record fields with +# the &log attribute). +# @TEST-EXEC: bro %INPUT >output +# @TEST-EXEC: btest-diff output + +type color: enum { Red, White, Blue }; + +type myrec1: record { + c: count &optional; + s: string &log; +}; + +type myrec2: record { + m: myrec1 &log; +}; + +event bro_init() + { + # ##################################### + # Test the basic (non-container) types: + + local b: bool = T; + print to_json(b); + + local c: count = 123; + print to_json(c); + + local i: int = -999; + print to_json(i); + + local d1: double = 1.123456789; + local d2: double = -1.123456e308; + local d3: double = 9.876543e-308; + print to_json(d1); + print to_json(d2); + print to_json(d3); + + local t: time = double_to_time(1.0); + print to_json(t); + + local ti: interval = -12hr; + print to_json(ti); + + local s1: string = "hello"; + local s2: string = ""; + print to_json(s1); + print to_json(s2); + + local p1: port = 65535/tcp; + local p2: port = 1/udp; + local p3: port = 123/icmp; + local p4: port = 0/unknown; + print to_json(p1); + print to_json(p2); + print to_json(p3); + print to_json(p4); + + local a1: addr = 1.2.3.4; + local a2: addr = [ffff:1234::1]; + local a3: addr = [::ffff:123.123.123.123]; + print to_json(a1); + print to_json(a2); + print to_json(a3); + + local su1: subnet = 192.0.0.0/8; + local su2: subnet = [fe80::]/64; + print to_json(su1); + print to_json(su2); + + local e: color = Red; + print to_json(e); + + # ######################### + # Test the container types: + + # Records + local re1 = myrec1($c=100, $s="test"); + local re2 = myrec1($s="test"); + local re3 = myrec2($m=myrec1($c=15, $s="test")); + print to_json(re1); + print to_json(re1, T); + print to_json(re2); + print to_json(re3, T); + + # Vectors + local ve1: vector of count = vector(); + local ve2: vector of count = vector(2, 1); + local ve3: vector of addr = vector(1.2.3.4); + local ve4: vector of set[bool] = vector(set(T, F)); + local ve5: vector of myrec1 = vector(myrec1($s="test", $c=2)); + print to_json(ve1); + print to_json(ve2); + print to_json(ve3); + print to_json(ve4); + print to_json(ve5, T); + + # Sets + local st1: set[count] = set(); + local st2: set[count] = set(2, 1); + local st3: set[addr] = set(1.2.3.4); + local st4: set[myrec1] = set(myrec1($s="test")); + local st5: set[myrec1] = set(myrec1($s="test", $c=2)); + print to_json(st1); + print to_json(st2); + print to_json(st3); + print to_json(st4); + print to_json(st5, T); + + # Tables + local ta1: table[count] of addr = table(); + local ta2: table[count] of addr = {[1] = 10.1.1.1, [2] = 10.2.2.2}; + local ta3: table[addr] of table[string] of count = {[10.1.1.1] = table(["a"] = 1), [10.2.2.2] = table(["b"] = 2)}; + local ta4: table[addr] of vector of count = {[10.1.1.1] = vector(1, 2), [10.2.2.2] = vector(3, 5)}; + local ta5: table[count] of myrec1 = {[1] = myrec1($s="test", $c=2)}; + print to_json(ta1); + print to_json(ta2); + print to_json(ta3); + print to_json(ta4); + print to_json(ta5, T); + } From 65d6e5a4f724842b7f5357435ec14b5c9de9d654 Mon Sep 17 00:00:00 2001 From: Daniel Thayer Date: Thu, 2 Feb 2017 12:09:40 -0600 Subject: [PATCH 2/3] Fix the to_json() function for bool, enum, and interval types --- scripts/base/utils/json.bro | 12 ++++++++---- .../btest/Baseline/scripts.base.utils.json/output | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/scripts/base/utils/json.bro b/scripts/base/utils/json.bro index b6d0093b58..5b66f3ad02 100644 --- a/scripts/base/utils/json.bro +++ b/scripts/base/utils/json.bro @@ -25,6 +25,10 @@ function to_json(v: any, only_loggable: bool &default=F, field_escape_pattern: p case "port": return cat(port_to_count(to_port(cat(v)))); + case "enum": + fallthrough; + case "interval": + fallthrough; case "addr": fallthrough; case "subnet": @@ -37,12 +41,12 @@ function to_json(v: any, only_loggable: bool &default=F, field_escape_pattern: p case "time": fallthrough; case "double": - fallthrough; - case "bool": - fallthrough; - case "enum": return cat(v); + case "bool": + local bval: bool = v; + return bval ? "true" : "false"; + default: break; } diff --git a/testing/btest/Baseline/scripts.base.utils.json/output b/testing/btest/Baseline/scripts.base.utils.json/output index a1f6875417..f7a16cb2bb 100644 --- a/testing/btest/Baseline/scripts.base.utils.json/output +++ b/testing/btest/Baseline/scripts.base.utils.json/output @@ -1,11 +1,11 @@ -T +true 123 -999 1.123457 -1.123456e+308 0 1.0 -"" +"-12.0 hrs" "hello" "" 65535 @@ -17,7 +17,7 @@ T "123.123.123.123" "192.0.0.0/8" "fe80::/64" -Red +"Red" {"s": "test", "c": 100} {"s": "test"} {"s": "test"} @@ -25,7 +25,7 @@ Red [] [2, 1] ["1.2.3.4"] -[[T, F]] +[[true, false]] [{"s": "test"}] [] [2, 1] From 6812a7febeb019277dc889792f54fe6a821f7f7d Mon Sep 17 00:00:00 2001 From: Daniel Thayer Date: Thu, 2 Feb 2017 13:03:05 -0600 Subject: [PATCH 3/3] Fix to_json() to not lose precision for values of type double Also changed a few values in the tests for better numerical diversity. --- scripts/base/utils/json.bro | 5 +++-- testing/btest/Baseline/scripts.base.utils.json/output | 8 ++++---- testing/btest/scripts/base/utils/json.test | 8 ++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/scripts/base/utils/json.bro b/scripts/base/utils/json.bro index 5b66f3ad02..05dcff1e27 100644 --- a/scripts/base/utils/json.bro +++ b/scripts/base/utils/json.bro @@ -39,10 +39,11 @@ function to_json(v: any, only_loggable: bool &default=F, field_escape_pattern: p case "count": fallthrough; case "time": - fallthrough; - case "double": return cat(v); + case "double": + return fmt("%.16g", v); + case "bool": local bval: bool = v; return bval ? "true" : "false"; diff --git a/testing/btest/Baseline/scripts.base.utils.json/output b/testing/btest/Baseline/scripts.base.utils.json/output index f7a16cb2bb..43513e43f9 100644 --- a/testing/btest/Baseline/scripts.base.utils.json/output +++ b/testing/btest/Baseline/scripts.base.utils.json/output @@ -1,10 +1,10 @@ true 123 -999 -1.123457 --1.123456e+308 -0 -1.0 +3.14 +-1.23456789e+308 +9e-308 +1480788576.868945 "-12.0 hrs" "hello" "" diff --git a/testing/btest/scripts/base/utils/json.test b/testing/btest/scripts/base/utils/json.test index 369ba0ebb7..264151136a 100644 --- a/testing/btest/scripts/base/utils/json.test +++ b/testing/btest/scripts/base/utils/json.test @@ -30,14 +30,14 @@ event bro_init() local i: int = -999; print to_json(i); - local d1: double = 1.123456789; - local d2: double = -1.123456e308; - local d3: double = 9.876543e-308; + local d1: double = 3.14; + local d2: double = -1.23456789e308; + local d3: double = 9e-308; print to_json(d1); print to_json(d2); print to_json(d3); - local t: time = double_to_time(1.0); + local t: time = double_to_time(1480788576.868945); print to_json(t); local ti: interval = -12hr;