Rename get_last_login_timestamp to get_last_login_timestamp_wtmp

... and add a wrapper function with the old name

Also rename the tests to the new name, and create a copy with the
original name. The copy will be adjusted to also check/expect lastlog2
results in a later commit.
This commit is contained in:
Christian Boltz 2024-07-28 14:08:40 +02:00
parent 9629bc8b6f
commit 7d537efcb0
Failed to generate hash of commit
2 changed files with 39 additions and 13 deletions

View file

@ -20,6 +20,8 @@ from apparmor.common import AppArmorBug, DebugLogger
debug_logger = DebugLogger('apparmor.notify')
def get_last_login_timestamp(username, filename='/var/log/wtmp'):
return get_last_login_timestamp_wtmp(username, filename)
def sane_timestamp(timestamp):
"""Check if the given timestamp is in a date range that makes sense for a wtmp file"""
@ -32,7 +34,7 @@ def sane_timestamp(timestamp):
return True
def get_last_login_timestamp(username, filename='/var/log/wtmp'):
def get_last_login_timestamp_wtmp(username, filename='/var/log/wtmp'):
"""Directly read wtmp and get last login for user as epoch timestamp"""
timestamp = 0
last_login = 0

View file

@ -12,21 +12,10 @@
import unittest
from apparmor.common import AppArmorBug
from apparmor.notify import get_last_login_timestamp, sane_timestamp
from apparmor.notify import get_last_login_timestamp, get_last_login_timestamp_wtmp, sane_timestamp
from common_test import AATest, setup_all_loops
class TestSane_timestamp(AATest):
tests = (
(2524704400, False), # Sun Jan 2 03:46:40 CET 2050
(944780400, False), # Fri Dec 10 00:00:00 CET 1999
(1635026400, True), # Sun Oct 24 00:00:00 CEST 2021
)
def _run_test(self, params, expected):
self.assertEqual(sane_timestamp(params), expected)
class TestGet_last_login_timestamp(AATest):
tests = (
(('wtmp-x86_64', 'root'), 1635070346), # Sun Oct 24 12:12:26 CEST 2021
@ -51,6 +40,41 @@ class TestGet_last_login_timestamp(AATest):
get_last_login_timestamp('root', 'wtmp-examples/wtmp-x86_64-past')
class TestSane_timestamp(AATest):
tests = (
(2524704400, False), # Sun Jan 2 03:46:40 CET 2050
(944780400, False), # Fri Dec 10 00:00:00 CET 1999
(1635026400, True), # Sun Oct 24 00:00:00 CEST 2021
)
def _run_test(self, params, expected):
self.assertEqual(sane_timestamp(params), expected)
class TestGet_last_login_timestamp_wtmp(AATest):
tests = (
(('wtmp-x86_64', 'root'), 1635070346), # Sun Oct 24 12:12:26 CEST 2021
(('wtmp-x86_64', 'whoever'), 0),
(('wtmp-s390x', 'root'), 1626368763), # Thu Jul 15 19:06:03 CEST 2021
(('wtmp-s390x', 'linux1'), 1626368772), # Thu Jul 15 19:06:12 CEST 2021
(('wtmp-s390x', 'whoever'), 0),
(('wtmp-aarch64', 'guillaume'), 1611562789), # Mon Jan 25 09:19:49 CET 2021
(('wtmp-aarch64', 'whoever'), 0),
(('wtmp-truncated', 'root'), 0),
(('wtmp-truncated', 'whoever'), 0),
)
def _run_test(self, params, expected):
filename, user = params
filename = 'wtmp-examples/' + filename
self.assertEqual(get_last_login_timestamp_wtmp(user, filename), expected)
def test_date_1999(self):
with self.assertRaises(AppArmorBug):
# wtmp-x86_64-past is hand-edited to Thu Dec 30 00:00:00 CET 1999, which is outside the expected data range
get_last_login_timestamp_wtmp('root', 'wtmp-examples/wtmp-x86_64-past')
setup_all_loops(__name__)
if __name__ == '__main__':
unittest.main(verbosity=1)