From b169f56098c2a8b07f9d77832959b65b812ed18a Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Thu, 26 Oct 2023 15:13:02 +0200 Subject: [PATCH] Fix tests so they work both with GNU and BSD tools The GNU and BSD versions of `touch` and `truncate` allow slightly different arguments, change the tests so they work in both versions. (cherry picked from commit caaffd0324ac0557ba4c5ce0290d07a4d013a735) --- .../core.mmdb.temporary-error/reporter.log | 8 ++-- testing/btest/core/mmdb/reopen.zeek | 12 ++++-- testing/btest/core/mmdb/temporary-error.zeek | 37 +++++++++++++------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/testing/btest/Baseline/core.mmdb.temporary-error/reporter.log b/testing/btest/Baseline/core.mmdb.temporary-error/reporter.log index 5ec6a3e3ec..e511eebbdc 100644 --- a/testing/btest/Baseline/core.mmdb.temporary-error/reporter.log +++ b/testing/btest/Baseline/core.mmdb.temporary-error/reporter.log @@ -3,15 +3,15 @@ ts level message location 1299470395.000000 Reporter::INFO Modification time change detected for MaxMind DB [.<...>/GeoLite2-ASN.mmdb] , line 1 1299470395.000000 Reporter::INFO Closing stale MaxMind DB [.<...>/GeoLite2-ASN.mmdb] , line 1 1299470395.000000 Reporter::INFO Failed to open MaxMind DB: .<...>/GeoLite2-ASN.mmdb [The MaxMind DB file contains invalid metadata] , line 1 -1299470395.000000 Reporter::ERROR Failed to open GeoIP ASN database (lookup_autonomous_system(128.3.0.1)) <...>/temporary-error.zeek, line 83 +1299470395.000000 Reporter::ERROR Failed to open GeoIP ASN database (lookup_autonomous_system(128.3.0.1)) <...>/temporary-error.zeek, line 98 1299470395.000000 Reporter::INFO Modification time change detected for MaxMind DB [.<...>/GeoLite2-City.mmdb] , line 1 1299470395.000000 Reporter::INFO Closing stale MaxMind DB [.<...>/GeoLite2-City.mmdb] , line 1 1299470395.000000 Reporter::INFO Failed to open MaxMind DB: .<...>/GeoLite2-City.mmdb [The MaxMind DB file contains invalid metadata] , line 1 -1299470395.000000 Reporter::ERROR Failed to open GeoIP location database (lookup_location(128.3.0.1)) <...>/temporary-error.zeek, line 84 +1299470395.000000 Reporter::ERROR Failed to open GeoIP location database (lookup_location(128.3.0.1)) <...>/temporary-error.zeek, line 99 1299473995.000000 Reporter::INFO Closing stale MaxMind DB [.<...>/GeoLite2-ASN.mmdb] , line 1 -1299473995.000000 Reporter::ERROR Failed to open GeoIP ASN database (lookup_autonomous_system(128.3.0.1)) <...>/temporary-error.zeek, line 83 +1299473995.000000 Reporter::ERROR Failed to open GeoIP ASN database (lookup_autonomous_system(128.3.0.1)) <...>/temporary-error.zeek, line 98 1299473995.000000 Reporter::INFO Closing stale MaxMind DB [.<...>/GeoLite2-City.mmdb] , line 1 -1299473995.000000 Reporter::ERROR Failed to open GeoIP location database (lookup_location(128.3.0.1)) <...>/temporary-error.zeek, line 84 +1299473995.000000 Reporter::ERROR Failed to open GeoIP location database (lookup_location(128.3.0.1)) <...>/temporary-error.zeek, line 99 1299477595.000000 Reporter::INFO Inode change detected for MaxMind DB [.<...>/GeoLite2-ASN.mmdb] , line 1 1299477595.000000 Reporter::INFO Closing stale MaxMind DB [.<...>/GeoLite2-ASN.mmdb] , line 1 1299477595.000000 Reporter::INFO Inode change detected for MaxMind DB [.<...>/GeoLite2-City.mmdb] , line 1 diff --git a/testing/btest/core/mmdb/reopen.zeek b/testing/btest/core/mmdb/reopen.zeek index d9c120c8dc..265bb7b6c9 100644 --- a/testing/btest/core/mmdb/reopen.zeek +++ b/testing/btest/core/mmdb/reopen.zeek @@ -14,17 +14,23 @@ redef mmdb_dir = "./mmdb"; global pkt = 0; +function timestamp(n: count): string + { + return fmt("2020-01-01T00:%s:00", n); + } + event new_packet(c: connection, p: pkt_hdr) { ++pkt; - # Set MMDB's modification time to current network time. + + # Increment MMDB's modification time. local asn_fn = safe_shell_quote(mmdb_dir + "/GeoLite2-ASN.mmdb"); local city_fn = safe_shell_quote(mmdb_dir + "/GeoLite2-City.mmdb"); - if ( ! piped_exec(fmt("touch -d @%s %s", network_time(), asn_fn), "") ) + if ( ! piped_exec(fmt("touch -d %s %s", timestamp(pkt), asn_fn), "") ) exit(1); - if ( ! piped_exec(fmt("touch -d @%s %s", network_time(), city_fn), "") ) + if ( ! piped_exec(fmt("touch -d %s %s", timestamp(pkt), city_fn), "") ) exit(1); print network_time(), pkt, 128.3.0.1, "asn", lookup_autonomous_system(128.3.0.1); diff --git a/testing/btest/core/mmdb/temporary-error.zeek b/testing/btest/core/mmdb/temporary-error.zeek index 0a5102535c..74b7ac4a16 100644 --- a/testing/btest/core/mmdb/temporary-error.zeek +++ b/testing/btest/core/mmdb/temporary-error.zeek @@ -15,16 +15,31 @@ redef mmdb_dir = "./mmdb"; global pkt = 0; +global asn_fn = safe_shell_quote(mmdb_dir + "/GeoLite2-ASN.mmdb"); +global city_fn = safe_shell_quote(mmdb_dir + "/GeoLite2-City.mmdb"); + +global asn_fn_backup = safe_shell_quote(mmdb_dir + "-backup/GeoLite2-ASN.mmdb"); +global city_fn_backup = safe_shell_quote(mmdb_dir + "-backup/GeoLite2-City.mmdb"); + +function timestamp(n: count): string + { + return fmt("2020-01-01T00:%s:00", n); + } + +event zeek_init() + { + # Set the initial modification time for the MMDBs. + for ( db in vector(asn_fn, city_fn, asn_fn_backup, city_fn_backup) ) + { + if ( ! piped_exec(fmt("test -f %s && touch -d %s %s", db, timestamp(pkt), db), "") ) + exit(1); + } + } + event new_packet(c: connection, p: pkt_hdr) { ++pkt; - local asn_fn = safe_shell_quote(mmdb_dir + "/GeoLite2-ASN.mmdb"); - local city_fn = safe_shell_quote(mmdb_dir + "/GeoLite2-City.mmdb"); - - local asn_fn_backup = safe_shell_quote(mmdb_dir + "-backup/GeoLite2-ASN.mmdb"); - local city_fn_backup = safe_shell_quote(mmdb_dir + "-backup/GeoLite2-City.mmdb"); - if ( pkt == 1 ) { print "start"; @@ -32,10 +47,10 @@ event new_packet(c: connection, p: pkt_hdr) if ( pkt == 2 ) { print "corrupting db"; - if ( ! piped_exec(fmt("truncate --size=8 %s", asn_fn), "") ) + if ( ! piped_exec(fmt("truncate -s 8 %s", asn_fn), "") ) exit(1); - if ( ! piped_exec(fmt("truncate --size=8 %s", city_fn), "") ) + if ( ! piped_exec(fmt("truncate -s 8 %s", city_fn), "") ) exit(1); } else if ( pkt == 4 ) @@ -73,11 +88,11 @@ event new_packet(c: connection, p: pkt_hdr) exit(1); } - # Set MMDB's modification time to current network time for predictability. - if ( ! piped_exec(fmt("test -f %s && touch -d @%s %s", asn_fn, network_time(), asn_fn), "") ) + # Increment MMDB's modification time. + if ( ! piped_exec(fmt("test -f %s && touch -d %s %s", asn_fn, timestamp(pkt), asn_fn), "") ) exit(1); - if ( ! piped_exec(fmt("test -f %s && touch -d @%s %s", city_fn, network_time(), city_fn), "") ) + if ( ! piped_exec(fmt("test -f %s && touch -d %s %s", city_fn, timestamp(pkt), city_fn), "") ) exit(1); print network_time(), pkt, 128.3.0.1, "asn", lookup_autonomous_system(128.3.0.1);