diff --git a/src/dom.cpp b/src/dom.cpp index 20e5103c..24a5ce9f 100644 --- a/src/dom.cpp +++ b/src/dom.cpp @@ -108,6 +108,29 @@ bool domGet ( auto tracked = getTracked (database, rules, filter); int count = static_cast (tracked.size ()); + // dom.tracked.tags + if (pig.skipLiteral ("tags")) + { + std::set tags; + for (const auto& interval : tracked) + { + for (const auto &tag : interval.tags ()) + { + tags.insert (tag); + } + } + + std::stringstream s; + + for (const auto& tag : tags) + { + s << format ( "{1} ", tag ); + } + + value = s.str(); + return true; + } + // dom.tracked.count if (pig.skipLiteral ("count")) { diff --git a/test/dom.t b/test/dom.t index 878fd8ef..f31466d5 100755 --- a/test/dom.t +++ b/test/dom.t @@ -27,9 +27,10 @@ ############################################################################### import os +import sys import unittest -import sys +from datetime import datetime, timedelta # Ensure python finds the local simpletap module sys.path.append(os.path.dirname(os.path.abspath(__file__))) @@ -166,6 +167,36 @@ class TestDOMTracked(TestCase): code, out, err = self.t("get dom.tracked.count") self.assertEqual('2\n', out) + def test_dom_tracked_tags_with_emtpy_database(self): + """Test dom.tracked.tags with empty database""" + code, out, err = self.t("get dom.tracked.tags") + self.assertEqual("\n", out) + + def test_dom_tracked_tags_with_no_tags(self): + """Test dom.tracked.tags with no tags""" + now_utc = datetime.now().utcnow() + four_hours_before_utc = now_utc - timedelta(hours=4) + five_hours_before_utc = now_utc - timedelta(hours=5) + + self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z".format(five_hours_before_utc, four_hours_before_utc)) + + code, out, err = self.t("get dom.tracked.tags") + self.assertEqual("\n", out) + + def test_dom_tracked_tags_with_tags(self): + """Test dom.tracked.tags with tags""" + now_utc = datetime.now().utcnow() + two_hours_before_utc = now_utc - timedelta(hours=2) + three_hours_before_utc = now_utc - timedelta(hours=3) + four_hours_before_utc = now_utc - timedelta(hours=4) + five_hours_before_utc = now_utc - timedelta(hours=5) + + self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z foo".format(five_hours_before_utc, four_hours_before_utc)) + self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z bar".format(three_hours_before_utc, two_hours_before_utc)) + + code, out, err = self.t("get dom.tracked.tags") + self.assertEqual("bar foo \n", out) + def test_dom_tracked_N_tag_count_zero(self): """Test dom.tracked.N.tag.count with zero tags""" self.t("track :yesterday one two")