From 2b5db4327058554733305a431dfff918c3211478 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Fri, 17 Feb 2023 15:57:36 +0100 Subject: [PATCH 1/2] testing/btest: Add test showing &expire_func/&create_expire is copied Does not seem there is a test, but current implementation is that &expire_func and &create_expire are propagated through copy(). --- .../Baseline/language.expire_func-copy/output | 54 +++++++++++++++++++ testing/btest/language/expire_func-copy.zeek | 27 ++++++++++ 2 files changed, 81 insertions(+) create mode 100644 testing/btest/Baseline/language.expire_func-copy/output create mode 100644 testing/btest/language/expire_func-copy.zeek diff --git a/testing/btest/Baseline/language.expire_func-copy/output b/testing/btest/Baseline/language.expire_func-copy/output new file mode 100644 index 0000000000..f971c3d005 --- /dev/null +++ b/testing/btest/Baseline/language.expire_func-copy/output @@ -0,0 +1,54 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +@XXXXXXXXXX.XXXXXX expired a +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.1, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=fe80::20c:29ff:febd:6f01, orig_p=5353/udp, resp_h=ff02::fb, resp_p=5353/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.1, orig_p=49658/tcp, resp_h=172.16.238.131, resp_p=80/tcp] +@XXXXXXXXXX.XXXXXX expired b +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.1, orig_p=17500/udp, resp_h=172.16.238.255, resp_p=17500/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.1, orig_p=49656/tcp, resp_h=172.16.238.131, resp_p=22/tcp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=37975/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.1, orig_p=49657/tcp, resp_h=172.16.238.131, resp_p=80/tcp] +@XXXXXXXXXX.XXXXXX expired a +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.1, orig_p=49658/tcp, resp_h=172.16.238.131, resp_p=80/tcp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.1, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=37975/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.1, orig_p=17500/udp, resp_h=172.16.238.255, resp_p=17500/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=fe80::20c:29ff:febd:6f01, orig_p=5353/udp, resp_h=ff02::fb, resp_p=5353/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.1, orig_p=49657/tcp, resp_h=172.16.238.131, resp_p=80/tcp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.1, orig_p=49656/tcp, resp_h=172.16.238.131, resp_p=22/tcp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=45126/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.1, orig_p=49659/tcp, resp_h=172.16.238.131, resp_p=21/tcp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=45126/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.1, orig_p=49659/tcp, resp_h=172.16.238.131, resp_p=21/tcp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=53102/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=48621/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=33109/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=57272/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=50205/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=54304/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=44555/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=51970/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=59573/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=55515/tcp, resp_h=74.125.225.81, resp_p=80/tcp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=33818/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=55368/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=37846/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=45140/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired [orig_h=172.16.238.131, orig_p=52952/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=53102/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=48621/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=51970/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=52952/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=33818/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=37846/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=50205/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=59573/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=55368/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=55515/tcp, resp_h=74.125.225.81, resp_p=80/tcp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=57272/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=44555/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=33109/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=45140/udp, resp_h=172.16.238.2, resp_p=53/udp] +@XXXXXXXXXX.XXXXXX expired copy [orig_h=172.16.238.131, orig_p=54304/udp, resp_h=172.16.238.2, resp_p=53/udp] diff --git a/testing/btest/language/expire_func-copy.zeek b/testing/btest/language/expire_func-copy.zeek new file mode 100644 index 0000000000..e6e1ee31c7 --- /dev/null +++ b/testing/btest/language/expire_func-copy.zeek @@ -0,0 +1,27 @@ +# @TEST-DOC: Test that expire_func is copied (the expiration behavior of existing elements +# +# @TEST-EXEC: zeek -b -C -r $TRACES/var-services-std-ports.trace %INPUT >output +# @TEST-EXEC: btest-diff output + +function inform_me(s: table[string] of count, idx: string): interval + { + print fmt("@%.2f expired %s", time_to_double(network_time()), idx); + return 0secs; + } + +global s: table[string] of count &create_expire=1secs &expire_func=inform_me; +global scopy: table[string] of count; + +event new_connection(c: connection) + { + s[fmt("%s", c$id)] = 1; + scopy[fmt("copy %s", c$id)] = 1; + } + +event zeek_init() + { + # This copies &expire_func and &create_expire from the value s; + s["a"] = 5; + scopy = copy(s); + scopy["b"] = 5; + } From b2c4f8fd92f06a3c2d07692a065af72796652a44 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Fri, 17 Feb 2023 16:15:20 +0100 Subject: [PATCH 2/2] TableVal: Propagate &on_change attribute through copy() Mostly for consistency with &default, &expire_func and other attributes being propagated through a copy(). Seems this was just missed during the implementation and/or was never tested for. --- NEWS | 3 ++ src/Val.cc | 3 ++ .../Baseline/language.on_change-copy/output | 9 ++++++ testing/btest/language/on_change-copy.test | 31 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 testing/btest/Baseline/language.on_change-copy/output create mode 100644 testing/btest/language/on_change-copy.test diff --git a/NEWS b/NEWS index 6f5acde047..0104e6a89f 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,9 @@ Changed Functionality an appropriate callback function, log a warning and return a generic handle value based on the analyzer and connection information. +- The ``&on_change`` attribute of set and tables is propagated through ``copy()``. + + Zeek 5.2.0 ========== diff --git a/src/Val.cc b/src/Val.cc index 3890e63361..387376a3ee 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -2649,6 +2649,9 @@ ValPtr TableVal::DoClone(CloneState* state) detail::timer_mgr->Add(tv->timer); } + if ( change_func ) + tv->change_func = change_func; + if ( expire_func ) tv->expire_func = expire_func; diff --git a/testing/btest/Baseline/language.on_change-copy/output b/testing/btest/Baseline/language.on_change-copy/output new file mode 100644 index 0000000000..ec1bc1a6f9 --- /dev/null +++ b/testing/btest/Baseline/language.on_change-copy/output @@ -0,0 +1,9 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +inserting +tbl_change, a, 1, 5, TABLE_ELEMENT_NEW +set_change, hi, TABLE_ELEMENT_NEW +changing +tbl_change, a, 1, 5, TABLE_ELEMENT_CHANGED +deleting +tbl_change, a, 1, 2, TABLE_ELEMENT_REMOVED +set_change, hi, TABLE_ELEMENT_REMOVED diff --git a/testing/btest/language/on_change-copy.test b/testing/btest/language/on_change-copy.test new file mode 100644 index 0000000000..5eecfd6c6a --- /dev/null +++ b/testing/btest/language/on_change-copy.test @@ -0,0 +1,31 @@ +# @TEST-EXEC: zeek -b %INPUT >output +# @TEST-EXEC: btest-diff output + +module TestModule; + +function tbl_change(t: table[string, int] of count, tpe: TableChange, idxa: string, idxb: int, val: count) + { + print "tbl_change", idxa, idxb, val, tpe; + } + +function set_change(t: set[string], tpe: TableChange, idx: string) + { + print "set_change", idx, tpe; + } + +global t: table[string, int] of count &on_change=tbl_change; +global s: set[string] &on_change=set_change; + +event zeek_init() + { + local tc = copy(t); + local sc = copy(s); + print "inserting"; + tc["a", 1] = 5; + add sc["hi"]; + print "changing"; + tc["a", 1] = 2; + print "deleting"; + delete tc["a", 1]; + delete sc["hi"]; + }