2016-10-01 20:57:09 +02:00
|
|
|
#! /usr/bin/python3
|
2015-04-22 22:05:10 +02:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 Christian Boltz <apparmor@cboltz.de>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of version 2 of the GNU General Public
|
|
|
|
# License published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
2022-08-07 20:32:07 -04:00
|
|
|
import re
|
2015-04-22 22:05:10 +02:00
|
|
|
import unittest
|
|
|
|
|
2022-08-07 20:32:07 -04:00
|
|
|
import apparmor.severity as severity
|
2024-07-23 16:09:53 +00:00
|
|
|
from apparmor.common import AppArmorBug, hasher
|
2015-04-22 22:08:24 +02:00
|
|
|
from apparmor.rule import BaseRule, parse_modifiers
|
2022-08-07 20:32:07 -04:00
|
|
|
from common_test import AATest, setup_all_loops
|
2015-04-22 22:05:10 +02:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2015-04-22 22:05:10 +02:00
|
|
|
class TestBaserule(AATest):
|
2022-09-27 22:14:31 -04:00
|
|
|
|
|
|
|
class ValidSubclass(BaseRule):
|
|
|
|
@classmethod
|
2024-05-17 13:53:42 +02:00
|
|
|
def _create_instance(cls, raw_rule, matches):
|
|
|
|
pass
|
2022-09-27 22:14:31 -04:00
|
|
|
|
2024-05-17 13:53:42 +02:00
|
|
|
def get_clean(self, depth=0):
|
|
|
|
pass
|
2022-09-27 22:14:31 -04:00
|
|
|
|
2024-05-17 13:53:42 +02:00
|
|
|
def _is_covered_localvars(self, other_rule):
|
|
|
|
pass
|
2022-09-27 22:14:31 -04:00
|
|
|
|
2024-05-17 13:53:42 +02:00
|
|
|
def _is_equal_localvars(self, other_rule, strict):
|
|
|
|
pass
|
2022-09-27 22:14:31 -04:00
|
|
|
|
2024-05-17 13:53:42 +02:00
|
|
|
def _logprof_header_localvars(self):
|
|
|
|
pass
|
2022-09-27 22:14:31 -04:00
|
|
|
|
|
|
|
def test_implemented_abstract_methods(self):
|
|
|
|
self.ValidSubclass()
|
|
|
|
|
|
|
|
def test_unimplemented_abstract_methods(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
BaseRule()
|
|
|
|
|
|
|
|
class InvalidSubclass(BaseRule):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
InvalidSubclass()
|
|
|
|
|
2022-09-10 19:54:20 -04:00
|
|
|
def test_abstract__create_instance(self):
|
2015-11-24 00:16:35 +01:00
|
|
|
with self.assertRaises(NotImplementedError):
|
2022-11-15 22:30:49 -05:00
|
|
|
BaseRule._create_instance('foo', None)
|
2015-04-22 22:05:10 +02:00
|
|
|
|
2022-09-10 19:45:22 -04:00
|
|
|
def test_abstract__create_instance_2(self):
|
2022-11-15 22:49:16 -05:00
|
|
|
with self.assertRaises(AppArmorBug):
|
2022-09-10 19:45:22 -04:00
|
|
|
BaseRule.create_instance('foo')
|
2015-04-26 21:59:12 +02:00
|
|
|
|
|
|
|
def test_abstract__match(self):
|
2022-11-15 22:49:16 -05:00
|
|
|
with self.assertRaises(AppArmorBug):
|
2015-04-26 21:59:12 +02:00
|
|
|
BaseRule._match('foo')
|
|
|
|
|
|
|
|
def test_abstract__match2(self):
|
2022-11-15 22:49:16 -05:00
|
|
|
with self.assertRaises(AppArmorBug):
|
2015-04-26 21:59:12 +02:00
|
|
|
BaseRule.match('foo')
|
|
|
|
|
2022-11-15 22:49:16 -05:00
|
|
|
def test_abstract__match3(self):
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
self.ValidSubclass.match('foo')
|
|
|
|
|
2015-04-22 22:08:24 +02:00
|
|
|
def test_parse_modifiers_invalid(self):
|
2022-11-20 13:41:44 -05:00
|
|
|
regex = re.compile(r'^\s*(?P<audit>audit\s+)?(?P<allow>allow\s+|deny\s+|invalid\s+)?')
|
2015-04-22 22:08:24 +02:00
|
|
|
matches = regex.search('audit invalid ')
|
|
|
|
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
parse_modifiers(matches)
|
|
|
|
|
2015-06-06 13:56:26 +02:00
|
|
|
def test_default_severity(self):
|
2021-04-14 21:32:34 +02:00
|
|
|
sev_db = severity.Severity('../severity.db', 'unknown')
|
2022-09-27 22:14:31 -04:00
|
|
|
obj = self.ValidSubclass()
|
2015-06-06 13:56:26 +02:00
|
|
|
rank = obj.severity(sev_db)
|
|
|
|
self.assertEqual(rank, sev_db.NOT_IMPLEMENTED)
|
2015-04-22 22:08:24 +02:00
|
|
|
|
2016-10-01 20:00:32 +02:00
|
|
|
def test_edit_header_localvars(self):
|
2022-09-27 22:14:31 -04:00
|
|
|
obj = self.ValidSubclass()
|
2016-10-01 20:00:32 +02:00
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
obj.edit_header()
|
|
|
|
|
|
|
|
def test_validate_edit_localvars(self):
|
2022-09-27 22:14:31 -04:00
|
|
|
obj = self.ValidSubclass()
|
2016-10-01 20:00:32 +02:00
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
obj.validate_edit('/foo')
|
|
|
|
|
|
|
|
def test_store_edit_localvars(self):
|
2022-09-27 22:14:31 -04:00
|
|
|
obj = self.ValidSubclass()
|
2016-10-01 20:00:32 +02:00
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
obj.store_edit('/foo')
|
|
|
|
|
2024-07-23 16:09:53 +00:00
|
|
|
def test_from_hashlog(self):
|
|
|
|
obj = self.ValidSubclass()
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
obj.from_hashlog(hasher())
|
|
|
|
|
|
|
|
def test_hashlog_from_event(self):
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
BaseRule.hashlog_from_event(None, None)
|
|
|
|
|
2015-04-22 22:05:10 +02:00
|
|
|
|
|
|
|
setup_all_loops(__name__)
|
|
|
|
if __name__ == '__main__':
|
2018-04-08 20:18:30 +02:00
|
|
|
unittest.main(verbosity=1)
|