def test26get_match_element_match_context_input_validation(self):
        """Check if an exception is raised, when other classes than MatchContext are used in get_match_element."""
        model_element = DateTimeModelElement(self.id_, b"%d.%m.%Y %H:%M:%S")
        data = b"07.02.2019 11:40:00: it still works"
        model_element.get_match_element(self.path, DummyMatchContext(data))
        model_element.get_match_element(self.path, MatchContext(data))

        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, MatchElement(self.path, data, None, None))
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, data)
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, data.decode())
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, 123)
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, 123.22)
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, True)
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, None)
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, [])
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, {"key": MatchContext(data)})
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, set())
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, ())
        self.assertRaises(AttributeError, model_element.get_match_element,
                          self.path, model_element)
 def test16max_time_jump_seconds_exceeded(self):
     """
     Test if the start_year is not updated, when the next date exceeds the max_time_jump_seconds.
     A time inconsistency warning must occur.
     """
     log_stream = StringIO()
     logging.basicConfig(stream=log_stream, level=logging.INFO)
     max_time_jump_seconds = 86400
     start_year = 2020
     match_context = DummyMatchContext(b"31.12 23:59:00: it still works")
     date_time_model_element = DateTimeModelElement(
         "path",
         b"%d.%m %H:%M:%S",
         timezone.utc,
         start_year=start_year,
         max_time_jump_seconds=max_time_jump_seconds)
     self.assertEqual(
         date_time_model_element.get_match_element(
             "match1", match_context).get_match_object(), 1609459140)
     self.assertEqual(match_context.match_string, b"31.12 23:59:00")
     self.assertEqual(date_time_model_element.start_year, 2020)
     match_context = DummyMatchContext(b"01.01 23:59:01: it still works")
     self.assertEqual(
         date_time_model_element.get_match_element(
             "match1", match_context).get_match_object(), 1577923141)
     self.assertEqual(match_context.match_string, b"01.01 23:59:01")
     self.assertEqual(date_time_model_element.start_year, 2020)
     self.assertIn(
         "WARNING:DEBUG:DateTimeModelElement time inconsistencies parsing b'01.01 23:59:01', expecting value around "
         "1609459140. Check your settings!", log_stream.getvalue())
     for handler in logging.root.handlers[:]:
         logging.root.removeHandler(handler)
     initialize_loggers(self.aminer_config,
                        getpwnam("aminer").pw_uid,
                        getgrnam("aminer").gr_gid)
Exemple #3
0
    def test8modulo_time_match_rule(self):
        """This case unit the ModuloTimeMatchRule."""
        description = "Test8Rules"
        modulo_time_match_rule = ModuloTimeMatchRule(self.model_syslog_time, 86400, 43200, 86400, None)
        self.analysis_context.register_component(modulo_time_match_rule, description)
        date_time_model_element = DateTimeModelElement('time', b'%d.%m.%Y %H:%M:%S')

        match_context = MatchContext(b'14.02.2019 13:00:00')
        match_element = date_time_model_element.get_match_element(self.model_syslog, match_context)
        log_atom = LogAtom(match_context.match_data, ParserMatch(match_element), time(), date_time_model_element)
        self.assertTrue(modulo_time_match_rule.match(log_atom))

        match_context = MatchContext(b'15.02.2019 00:00:00')
        match_element = date_time_model_element.get_match_element(self.model_syslog, match_context)
        log_atom = LogAtom(match_context.match_data, ParserMatch(match_element), time(), date_time_model_element)
        self.assertTrue(not modulo_time_match_rule.match(log_atom))

        match_context = MatchContext(b'14.02.2019 12:00:00')
        match_element = date_time_model_element.get_match_element(self.model_syslog, match_context)
        log_atom = LogAtom(match_context.match_data, ParserMatch(match_element), time(), date_time_model_element)
        self.assertTrue(modulo_time_match_rule.match(log_atom))

        match_context = MatchContext(b'15.02.2019 01:00:00')
        match_element = date_time_model_element.get_match_element(self.model_syslog, match_context)
        log_atom = LogAtom(match_context.match_data, ParserMatch(match_element), time(), date_time_model_element)
        self.assertTrue(not modulo_time_match_rule.match(log_atom))
Exemple #4
0
    def test4_default_timezone(self):
        """This test case checks if the default Timezone is utc."""
        match_context = MatchContext(b'07.02.2018 11:40:00')
        date_time_model_element = DateTimeModelElement(
            'path', b'%d.%m.%Y %H:%M:%S',
            datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo)
        date1 = date_time_model_element.get_match_element(
            'match1', match_context).get_match_object()

        match_context = MatchContext(b'07.02.2018 11:40:00')
        date_time_model_element = DateTimeModelElement('path',
                                                       b'%d.%m.%Y %H:%M:%S')
        date2 = date_time_model_element.get_match_element(
            'match1', match_context).get_match_object()
        self.assertEqual(date1 - date2, 0)
    def test14learn_new_start_year_without_start_year_set(self):
        """Test if a new year is learned successfully with the start year being None."""
        match_context = DummyMatchContext(b"31.12 23:59:00: it still works")
        date_time_model_element = DateTimeModelElement("path",
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc)
        self.assertIsNotNone(
            date_time_model_element.get_match_element("match1", match_context))
        self.assertEqual(match_context.match_string, b"31.12 23:59:00")

        start_year = date_time_model_element.start_year
        match_context = DummyMatchContext(b"01.01 11:20:00: it still works")
        self.assertIsNotNone(
            date_time_model_element.get_match_element("match1", match_context))
        self.assertEqual(match_context.match_string, b"01.01 11:20:00")
        self.assertEqual(date_time_model_element.start_year, start_year + 1)
Exemple #6
0
    def test3_new_year_with_start_year_value(self):
        startYear = 2017
        match_context = MatchContext(b'07.02.2018 11:40:00: it still works')
        date_time_model_element = DateTimeModelElement('path',
                                                       b'%d.%m.%Y %H:%M:%S',
                                                       None, None, startYear)
        self.assertEqual(
            date_time_model_element.get_match_element(
                'match1', match_context).get_match_object(), 1518003600)

        match_context = MatchContext(b'07.02 11:40:00: it still works')
        date_time_model_element = DateTimeModelElement('path',
                                                       b'%d.%m %H:%M:%S', None,
                                                       None, startYear)
        self.assertEqual(
            date_time_model_element.get_match_element(
                'match1', match_context).get_match_object(), 1486467600)
    def test6get_match_element_with_different_time_zones(self):
        """Test if different time_zones work with the DateTimeModelElement."""
        date_time_model_element = DateTimeModelElement("path",
                                                       b"%d.%m.%Y %H:%M:%S%z",
                                                       timezone.utc)
        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC-1200: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1518046800)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC-1200")

        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC-12: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1518046800)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC-12")

        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC-5: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1518021600)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC-5")

        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC-0500: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1518021600)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC-0500")

        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC+0000: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1518003600)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC+0000")

        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC+0100: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1518000000)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC+0100")

        match_context = DummyMatchContext(
            b"07.02.2018 11:40:00 UTC+1400: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1517953200)
        self.assertEqual(match_context.match_string,
                         b"07.02.2018 11:40:00 UTC+1400")
 def test12get_match_element_without_leap_start_year(self):
     """Check if normal start_years can not parse the 29th February."""
     match_context = DummyMatchContext(b"29.02 11:40:00: it still works")
     date_time_model_element = DateTimeModelElement("path",
                                                    b"%d.%m %H:%M:%S",
                                                    timezone.utc,
                                                    start_year=2019)
     self.assertIsNone(
         date_time_model_element.get_match_element("match1", match_context))
Exemple #9
0
 def test2start_year_value(self):
     """This test checks if they class is parsing dates without year values correctly."""
     match_context = MatchContext(b'07.02 11:40:00: it still works')
     date_time_model_element = DateTimeModelElement('path',
                                                    b'%d.%m %H:%M:%S', None,
                                                    None, 2017)
     self.assertEqual(
         date_time_model_element.get_match_element(
             'match1', match_context).get_match_object(), 1486467600)
 def test10get_match_element_without_start_year_defined(self):
     """Test if dates without year can still be parsed, even without defining the start_year."""
     match_context = DummyMatchContext(b"07.02 11:40:00: it still works")
     date_time_model_element = DateTimeModelElement("path",
                                                    b"%d.%m %H:%M:%S",
                                                    timezone.utc)
     self.assertIsNotNone(
         date_time_model_element.get_match_element("match1", match_context))
     self.assertEqual(match_context.match_string, b"07.02 11:40:00")
 def test5_unclean_format_string(self):
     """This test case checks if unclean format_strings can be used."""
     match_context = MatchContext(
         b'Test 07.02.2018 11:40:00 UTC+0000: it still works')
     date_time_model_element = DateTimeModelElement(
         'path', b'Test %d.%m.%Y %H:%M:%S %z', datetime.timezone.utc, None,
         2017)
     self.assertEqual(
         date_time_model_element.get_match_element(
             'match1', match_context).get_match_object(), 1518003600)
     self.assertEqual(match_context.match_data,
                      self.__expected_match_context)
     match_context = MatchContext(
         b'Test 07.02.2018 11:40:00 UTC-0001: it still works')
     self.assertEqual(
         date_time_model_element.get_match_element(
             'match1', match_context).get_match_object(), 1518000000)
     self.assertEqual(match_context.match_data,
                      self.__expected_match_context)
 def test19date_before_unix_timestamps(self):
     """Check if timestamps before the unix timestamp are processed properly."""
     match_context = DummyMatchContext(
         b"01.01.1900 11:40:00: it still works")
     date_time_model_element = DateTimeModelElement("path",
                                                    b"%d.%m.%Y %H:%M:%S",
                                                    timezone.utc)
     match_element = date_time_model_element.get_match_element(
         "match1", match_context)
     self.assertEqual(match_element.match_string, b"01.01.1900 11:40:00")
     self.assertEqual(match_element.match_object, -2208946800)
 def test5get_match_element_with_unclean_format_string(self):
     """This test case checks if unclean format_strings can be used."""
     match_context = DummyMatchContext(
         b"Date %d: 07.02.2018 11:40:00 UTC+0000: it still works")
     date_time_model_element = DateTimeModelElement(
         "path", b"Date %%d: %d.%m.%Y %H:%M:%S%z", timezone.utc)
     self.assertEqual(
         date_time_model_element.get_match_element(
             "match1", match_context).get_match_object(), 1518003600)
     self.assertEqual(match_context.match_string,
                      b"Date %d: 07.02.2018 11:40:00 UTC+0000")
 def test5get_match_element_with_unclean_format_string(self):
     """This test case checks if unclean format_strings can be used."""
     data = b"Date %d: 07.02.2018 11:40:00 UTC+0000: it still works"
     date = b"Date %d: 07.02.2018 11:40:00 UTC+0000"
     match_context = DummyMatchContext(data)
     date_time_model_element = DateTimeModelElement(
         self.id_, b"Date %%d: %d.%m.%Y %H:%M:%S%z", timezone.utc)
     match_element = date_time_model_element.get_match_element(
         self.path, match_context)
     self.compare_match_results(data, match_element, match_context,
                                self.id_, self.path, date, 1518003600, None)
    def test18same_timestamp_multiple_times(self):
        """Test if the DateTimeModelElement can handle multiple same timestamps."""
        data = b"07.02.2019 11:40:00: it still works"
        date = b"07.02.2019 11:40:00"
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m.%Y %H:%M:%S",
                                                       timezone.utc)
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1549539600, None)

        data = b"07.02.2019 11:40:00: it still works"
        date = b"07.02.2019 11:40:00"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1549539600, None)
 def test11get_match_element_with_leap_start_year(self):
     """Check if leap start_years can parse the 29th February."""
     match_context = DummyMatchContext(b"29.02 11:40:00: it still works")
     date_time_model_element = DateTimeModelElement("path",
                                                    b"%d.%m %H:%M:%S",
                                                    timezone.utc,
                                                    start_year=2020)
     self.assertEqual(
         date_time_model_element.get_match_element(
             "match1", match_context).get_match_object(), 1582976400)
     self.assertEqual(match_context.match_string, b"29.02 11:40:00")
    def test18same_timestamp_multiple_times(self):
        """Test if the DateTimeModelElement can handle multiple same timestamps."""
        match_context = DummyMatchContext(
            b"07.02.2019 11:40:00: it still works")
        date_time_model_element = DateTimeModelElement("path",
                                                       b"%d.%m.%Y %H:%M:%S",
                                                       timezone.utc)
        match_element = date_time_model_element.get_match_element(
            "match1", match_context)
        self.assertEqual(match_element.match_string, b"07.02.2019 11:40:00")
        self.assertEqual(match_element.match_object, 1549539600)
        self.assertEqual(match_context.match_string, b"07.02.2019 11:40:00")

        match_context = DummyMatchContext(
            b"07.02.2019 11:40:00: it still works")
        match_element = date_time_model_element.get_match_element(
            "match1", match_context)
        self.assertEqual(match_element.match_string, b"07.02.2019 11:40:00")
        self.assertEqual(match_element.match_object, 1549539600)
        self.assertEqual(match_context.match_string, b"07.02.2019 11:40:00")
    def test13learn_new_start_year_with_start_year_set(self):
        """Test if a new year is learned successfully with the start year being set."""
        start_year = 2020
        match_context = DummyMatchContext(b"31.12 23:59:00: it still works")
        date_time_model_element = DateTimeModelElement("path",
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc,
                                                       start_year=start_year)
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1609459140)
        self.assertEqual(match_context.match_string, b"31.12 23:59:00")
        self.assertEqual(date_time_model_element.start_year, start_year)

        match_context = DummyMatchContext(b"01.01 11:20:00: it still works")
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1609500000)
        self.assertEqual(match_context.match_string, b"01.01 11:20:00")
        self.assertEqual(date_time_model_element.start_year, start_year + 1)
 def test12get_match_element_without_leap_start_year(self):
     """Check if normal start_years can not parse the 29th February."""
     data = b"29.02 11:40:00: it still works"
     date_time_model_element = DateTimeModelElement(self.id_,
                                                    b"%d.%m %H:%M:%S",
                                                    timezone.utc,
                                                    start_year=2019)
     match_context = DummyMatchContext(data)
     match_element = date_time_model_element.get_match_element(
         self.path, match_context)
     self.compare_no_match_results(data, match_element, match_context)
    def test9get_match_element_with_start_year(self):
        """Test if dates without year can be parsed, when the start_year is defined."""
        match_context = DummyMatchContext(b"07.02 11:40:00: it still works")
        date_time_model_element = DateTimeModelElement("path",
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc,
                                                       start_year=2017)
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1486467600)
        self.assertEqual(match_context.match_string, b"07.02 11:40:00")

        match_context = DummyMatchContext(b"07.02 11:40:00: it still works")
        date_time_model_element = DateTimeModelElement("path",
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc,
                                                       start_year=2019)
        self.assertEqual(
            date_time_model_element.get_match_element(
                "match1", match_context).get_match_object(), 1549539600)
        self.assertEqual(match_context.match_string, b"07.02 11:40:00")
Exemple #21
0
    def test9value_dependent_modulo_time_match_rule(self):
        """This case unit the ValueDependentModuloTimeMatchRule. Limit look up not working with tuples."""
        description = "Test9Rules"
        value_dependent_modulo_time_match_rule = ValueDependentModuloTimeMatchRule(self.model_syslog_time, 86400, [self.model_syslog_time],
                                                                                   {1550145600: [43200, 86400]})
        self.analysis_context.register_component(value_dependent_modulo_time_match_rule, description)
        date_time_model_element = DateTimeModelElement('time', b'%d.%m.%Y %H:%M:%S')

        match_context = MatchContext(b'14.02.2019 12:00:00')
        match_element = date_time_model_element.get_match_element(self.model_syslog, match_context)
        log_atom = LogAtom(match_context.match_data, ParserMatch(match_element), 1550138400, date_time_model_element)
        self.assertTrue(value_dependent_modulo_time_match_rule.match(log_atom))
    def test14learn_new_start_year_without_start_year_set(self):
        """Test if a new year is learned successfully with the start year being None."""
        data = b"31.12 23:59:00: it still works"
        date = b"31.12 23:59:00"
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc)
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        dtm = datetime(datetime.now().year,
                       12,
                       31,
                       23,
                       59,
                       tzinfo=timezone.utc)
        total_seconds = (
            dtm - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, total_seconds,
                                   None)

        data = b"01.01 11:20:00: it still works"
        date = b"01.01 11:20:00"
        start_year = date_time_model_element.start_year
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        dtm = datetime(datetime.now().year + 1,
                       1,
                       1,
                       11,
                       20,
                       tzinfo=timezone.utc)
        total_seconds = (
            dtm - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, total_seconds,
                                   None)
        self.assertEqual(date_time_model_element.start_year, start_year + 1)
    def test15max_time_jump_seconds_in_time(self):
        """
        Test if the max_time_jump_seconds parameter works if the next date is in time.
        Warnings with unqualified timestamp year wraparound.
        """
        log_stream = StringIO()
        logging.basicConfig(stream=log_stream, level=logging.INFO)
        max_time_jump_seconds = 86400
        start_year = 2020
        date_time_model_element = DateTimeModelElement(
            self.id_,
            b"%d.%m %H:%M:%S",
            timezone.utc,
            start_year=start_year,
            max_time_jump_seconds=max_time_jump_seconds)
        data = b"31.12 23:59:00: it still works"
        date = b"31.12 23:59:00"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1609459140, None)
        self.assertEqual(date_time_model_element.start_year, start_year)

        data = b"01.01 23:59:00: it still works"
        date = b"01.01 23:59:00"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1609545540, None)
        self.assertEqual(date_time_model_element.start_year, start_year + 1)
        self.assertIn(
            "WARNING:DEBUG:DateTimeModelElement unqualified timestamp year wraparound detected from 2021-01-01T23:59:00+00:00 to "
            "2021-01-01T23:59:00+00:00", log_stream.getvalue())
        for handler in logging.root.handlers[:]:
            logging.root.removeHandler(handler)
        initialize_loggers(self.aminer_config,
                           getpwnam("aminer").pw_uid,
                           getgrnam("aminer").gr_gid)
 def test19date_before_unix_timestamps(self):
     """Check if timestamps before the unix timestamp are processed properly."""
     data = b"01.01.1900 11:40:00: it still works"
     date = b"01.01.1900 11:40:00"
     date_time_model_element = DateTimeModelElement(self.id_,
                                                    b"%d.%m.%Y %H:%M:%S",
                                                    timezone.utc)
     match_context = DummyMatchContext(data)
     match_element = date_time_model_element.get_match_element(
         self.path, match_context)
     self.compare_match_results(data, match_element, match_context,
                                self.id_, self.path, date, -2208946800,
                                None)
Exemple #25
0
    def test1date_formats_exceptions(self):
        """This test case verifies, if all date_format qualifiers are valid and exceptions are raised, if they are invalid."""
        match_context = MatchContext(b'07.02.2019 11:40:00: it still works')
        date_time_model_element = DateTimeModelElement('path',
                                                       b'%d.%m.%Y %H:%M:%S')
        self.assertEqual(
            date_time_model_element.get_match_element(
                'match1', match_context).get_match_string(),
            b'07.02.2019 11:40:00')

        self.assertRaises(Exception, DateTimeModelElement, 'path', b'%h %b')
        self.assertRaises(Exception, DateTimeModelElement, 'path', b'%H%H')
        self.assertRaises(Exception, DateTimeModelElement, 'path', b'%H%s')
 def test11get_match_element_with_leap_start_year(self):
     """Check if leap start_years can parse the 29th February."""
     data = b"29.02 11:40:00: it still works"
     date = b"29.02 11:40:00"
     date_time_model_element = DateTimeModelElement(self.id_,
                                                    b"%d.%m %H:%M:%S",
                                                    timezone.utc,
                                                    start_year=2020)
     match_context = DummyMatchContext(data)
     match_element = date_time_model_element.get_match_element(
         self.path, match_context)
     self.compare_match_results(data, match_element, match_context,
                                self.id_, self.path, date, 1582976400, None)
    def test9get_match_element_with_start_year(self):
        """Test if dates without year can be parsed, when the start_year is defined."""
        data = b"07.02 11:40:00: it still works"
        date = b"07.02 11:40:00"
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc,
                                                       start_year=2017)
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1486467600, None)

        match_context = DummyMatchContext(data)
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc,
                                                       start_year=2019)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1549539600, None)
    def test13learn_new_start_year_with_start_year_set(self):
        """Test if a new year is learned successfully with the start year being set."""
        data = b"31.12 23:59:00: it still works"
        date = b"31.12 23:59:00"
        start_year = 2020
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m %H:%M:%S",
                                                       timezone.utc,
                                                       start_year=start_year)
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1609459140, None)
        self.assertEqual(date_time_model_element.start_year, start_year)

        data = b"01.01 11:20:00: it still works"
        date = b"01.01 11:20:00"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_match_results(data, match_element, match_context,
                                   self.id_, self.path, date, 1609500000, None)
        self.assertEqual(date_time_model_element.start_year, start_year + 1)
 def test10get_match_element_without_start_year_defined(self):
     """Test if dates without year can still be parsed, even without defining the start_year."""
     data = b"07.02 11:40:00: it still works"
     date = b"07.02 11:40:00"
     date_time_model_element = DateTimeModelElement(self.id_,
                                                    b"%d.%m %H:%M:%S",
                                                    timezone.utc)
     match_context = DummyMatchContext(data)
     dtm = datetime(datetime.now().year, 2, 7, 11, 40, tzinfo=timezone.utc)
     total_seconds = (
         dtm - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
     match_element = date_time_model_element.get_match_element(
         self.path, match_context)
     self.compare_match_results(data, match_element, match_context,
                                self.id_, self.path, date, total_seconds,
                                None)
    def test4wrong_date(self):
        """Test if wrong input data does not return a match."""
        # wrong day
        data = b"32.03.2019 11:40:00: it still works"
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m.%Y %H:%M:%S",
                                                       timezone.utc)
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_no_match_results(data, match_element, match_context)

        # wrong month
        data = b"01.13.2019 11:40:00: it still works"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_no_match_results(data, match_element, match_context)

        # wrong year
        data = b"01.01.00 11:40:00: it still works"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_no_match_results(data, match_element, match_context)

        # wrong date leap year
        data = b"29.02.2019 11:40:00: it still works"
        match_context = DummyMatchContext(data)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_no_match_results(data, match_element, match_context)

        # missing T
        data = b"07.02.2019 11:40:00: it still works"
        match_context = DummyMatchContext(data)
        date_time_model_element = DateTimeModelElement(self.id_,
                                                       b"%d.%m.%YT%H:%M:%S",
                                                       timezone.utc)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_no_match_results(data, match_element, match_context)

        # missing fractions
        data = b"07.02.2019 11:40:00.: it still works"
        match_context = DummyMatchContext(data)
        date_time_model_element = DateTimeModelElement(
            self.id_, b"%d.%m.%Y %H:%M:%S.%f", timezone.utc)
        match_element = date_time_model_element.get_match_element(
            self.path, match_context)
        self.compare_no_match_results(data, match_element, match_context)