From 6e3eb43ad3bac9833c2db1b3d9d5a406f6fcbd2d Mon Sep 17 00:00:00 2001 From: Vasant Patil Date: Fri, 11 Sep 2020 00:55:38 -0700 Subject: [PATCH 1/3] If the passed in time string isnegative, return 0 as delta time in lldp_syncd daemon --- src/lldp_syncd/daemon.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lldp_syncd/daemon.py b/src/lldp_syncd/daemon.py index c248a06..825d756 100644 --- a/src/lldp_syncd/daemon.py +++ b/src/lldp_syncd/daemon.py @@ -43,9 +43,18 @@ def parse_time(time_str): return sage; } :return: parsed age in time ticks (or seconds) + 0 in case passed in string has negative time + due to setting system clock backwards """ + if '-' in time_str: + return 0 + days, hour_min_secs = re.split(LLDPD_UPTIME_RE_SPLIT_PATTERN, time_str) - struct_time = time.strptime(hour_min_secs, LLDPD_TIME_FORMAT) + try: + struct_time = time.strptime(hour_min_secs, LLDPD_TIME_FORMAT) + except ValueError: + logger.warning("Incorrect time {}, did you change system time?".format(hour_min_secs)) + return 0 time_delta = datetime.timedelta(days=int(days), hours=struct_time.tm_hour, minutes=struct_time.tm_min, seconds=struct_time.tm_sec) From 292477fb5aafaba61b675fcbbb2abdf3b180c1f6 Mon Sep 17 00:00:00 2001 From: Vasant Patil Date: Fri, 11 Sep 2020 10:32:32 -0700 Subject: [PATCH 2/3] Updated comments based on review --- src/lldp_syncd/daemon.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lldp_syncd/daemon.py b/src/lldp_syncd/daemon.py index 825d756..8177826 100644 --- a/src/lldp_syncd/daemon.py +++ b/src/lldp_syncd/daemon.py @@ -43,8 +43,7 @@ def parse_time(time_str): return sage; } :return: parsed age in time ticks (or seconds) - 0 in case passed in string has negative time - due to setting system clock backwards + 0 on invalid time_str """ if '-' in time_str: return 0 @@ -53,7 +52,7 @@ def parse_time(time_str): try: struct_time = time.strptime(hour_min_secs, LLDPD_TIME_FORMAT) except ValueError: - logger.warning("Incorrect time {}, did you change system time?".format(hour_min_secs)) + logger.warning("Invalid time {}, possibly system time changed?".format(hour_min_secs)) return 0 time_delta = datetime.timedelta(days=int(days), hours=struct_time.tm_hour, minutes=struct_time.tm_min, From 013c56780ab261ee9b6fd6c24a2fab6919810bf9 Mon Sep 17 00:00:00 2001 From: Vasant Patil Date: Sun, 13 Sep 2020 21:23:48 -0700 Subject: [PATCH 3/3] Included test cases --- tests/test_lldpSyncDaemon.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_lldpSyncDaemon.py b/tests/test_lldpSyncDaemon.py index 1302fc1..7d84d0d 100644 --- a/tests/test_lldpSyncDaemon.py +++ b/tests/test_lldpSyncDaemon.py @@ -96,6 +96,10 @@ def test_sync_roundtrip(self): def test_timeparse(self): self.assertEquals(lldp_syncd.daemon.parse_time("0 day, 05:09:02"), make_seconds(0, 5, 9, 2)) self.assertEquals(lldp_syncd.daemon.parse_time("2 days, 05:59:02"), make_seconds(2, 5, 59, 2)) + self.assertEquals(lldp_syncd.daemon.parse_time("2 days, -05:-59:-02"), make_seconds(0, 0, 0, 0)) + self.assertEquals(lldp_syncd.daemon.parse_time("-2 days, 05:59:02"), make_seconds(0, 0, 0, 0)) + self.assertEquals(lldp_syncd.daemon.parse_time("0 days, 00:00:-02"), make_seconds(0, 0, 0, 0)) + self.assertEquals(lldp_syncd.daemon.parse_time("0 days, 00:-10:00"), make_seconds(0, 0, 0, 0)) def parse_mgmt_ip(self, json_file): parsed_update = self.daemon.parse_update(json_file)