diff --git a/tests/plugins/test_creation_date.py b/tests/plugins/test_creation_date.py index dd0ef1e1..ef15a906 100644 --- a/tests/plugins/test_creation_date.py +++ b/tests/plugins/test_creation_date.py @@ -153,3 +153,43 @@ def test_wrong_length(self): "False or incorrectly formatted creation_date.", results[0].message, ) + + def test_creation_date_greater_than_last_modification(self): + path = Path("some/file.nasl") + content = ( + ' script_tag(name:"creation_date", value:"2025-01-01 00:00:01 ' + '+0200 (Wed, 01 Jan 2025)");\n' + ' script_tag(name:"last_modification", value:"2025-01-01 00:00:00 ' + '+0200 (Wed, 01 Jan 2025)");\n' + ) + fake_context = self.create_file_plugin_context( + nasl_file=path, file_content=content + ) + plugin = CheckCreationDate(fake_context) + + results = list(plugin.run()) + + self.assertEqual(len(results), 1) + + self.assertIsInstance(results[0], LinterError) + self.assertEqual( + "The creation_date must not be greater than the last modification date.", + results[0].message, + ) + + def test_creation_date_equal_last_modification(self): + path = Path("some/file.nasl") + content = ( + ' script_tag(name:"creation_date", value:"2025-01-01 00:00:00 ' + '+0200 (Wed, 01 Jan 2025)");\n' + ' script_tag(name:"last_modification", value:"2025-01-01 00:00:00 ' + '+0200 (Wed, 01 Jan 2025)");\n' + ) + fake_context = self.create_file_plugin_context( + nasl_file=path, file_content=content + ) + plugin = CheckCreationDate(fake_context) + + results = list(plugin.run()) + + self.assertEqual(len(results), 0) diff --git a/troubadix/plugins/creation_date.py b/troubadix/plugins/creation_date.py index 858f2842..738cee2d 100644 --- a/troubadix/plugins/creation_date.py +++ b/troubadix/plugins/creation_date.py @@ -52,45 +52,59 @@ def check_content( # Example: "2017-11-29 13:56:41 +0100 (Wed, 29 Nov 2017)" match = tag_pattern.search(file_content) - if match: - try: - date_left = datetime.strptime( - match.group("value")[:25], "%Y-%m-%d %H:%M:%S %z" - ) - # 2017-11-29 13:56:41 +0100 (error if no timezone) - date_right = datetime.strptime( - match.group("value")[27:43], "%a, %d %b %Y" - ) - week_day_parsed = date_right.strftime("%a") - except ValueError: - yield LinterError( - "False or incorrectly formatted creation_date.", - file=nasl_file, - plugin=self.name, - ) - return + if not match: + yield LinterError( + "False or incorrectly formatted creation_date.", + file=nasl_file, + plugin=self.name, + ) + return - week_day_str = match.group("value")[27:30] - # Wed, 29 Nov 2017 - if date_left.date() != date_right.date(): - yield LinterError( - "The creation_date consists of two different dates.", - file=nasl_file, - plugin=self.name, - ) - # Check correct weekday - elif week_day_str != week_day_parsed: - formatted_date = week_day_parsed - yield LinterError( - f"Wrong day of week. Please change it from '{week_day_str}" - f"' to '{formatted_date}'.", - file=nasl_file, - plugin=self.name, - ) - else: + try: + date_left = datetime.strptime( + match.group("value")[:25], "%Y-%m-%d %H:%M:%S %z" + ) + # 2017-11-29 13:56:41 +0100 (error if no timezone) + date_right = datetime.strptime( + match.group("value")[27:43], "%a, %d %b %Y" + ) + week_day_parsed = date_right.strftime("%a") + except ValueError: yield LinterError( "False or incorrectly formatted creation_date.", file=nasl_file, plugin=self.name, ) return + + week_day_str = match.group("value")[27:30] + # Wed, 29 Nov 2017 + if date_left.date() != date_right.date(): + yield LinterError( + "The creation_date consists of two different dates.", + file=nasl_file, + plugin=self.name, + ) + # Check correct weekday + elif week_day_str != week_day_parsed: + formatted_date = week_day_parsed + yield LinterError( + f"Wrong day of week. Please change it from '{week_day_str}" + f"' to '{formatted_date}'.", + file=nasl_file, + plugin=self.name, + ) + + last_modification_pattern = get_script_tag_pattern( + ScriptTag.LAST_MODIFICATION + ) + if match := last_modification_pattern.search(file_content): + last_modification = datetime.strptime( + match.group("value")[:25], "%Y-%m-%d %H:%M:%S %z" + ) + if date_left > last_modification: + yield LinterError( + "The creation_date must not be greater than the last modification date.", + file=nasl_file, + plugin=self.name, + )