2017-07-11 13:32:33 +02:00
|
|
|
#! /usr/bin/python3
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 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.
|
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
from common_test import AATest, setup_all_loops
|
|
|
|
|
|
|
|
from apparmor.common import AppArmorBug
|
2018-07-25 20:44:39 +02:00
|
|
|
from apparmor.profile_storage import ProfileStorage, add_or_remove_flag, split_flags, var_transform
|
2017-07-11 13:32:33 +02:00
|
|
|
|
|
|
|
class TestUnknownKey(AATest):
|
|
|
|
def AASetup(self):
|
|
|
|
self.storage = ProfileStorage('/test/foo', 'hat', 'TEST')
|
|
|
|
|
|
|
|
def test_read(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
self.storage['foo']
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
self.storage.get('foo')
|
|
|
|
|
|
|
|
def test_get_with_fallback(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
self.storage.get('foo', 'bar')
|
|
|
|
|
|
|
|
def test_set(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
self.storage['foo'] = 'bar'
|
|
|
|
|
2018-07-25 20:44:39 +02:00
|
|
|
class AaTest_add_or_remove_flag(AATest):
|
|
|
|
tests = [
|
|
|
|
# existing flag(s) flag to change add or remove? expected flags
|
|
|
|
([ [], 'complain', True ], ['complain'] ),
|
|
|
|
([ [], 'complain', False ], [] ),
|
|
|
|
([ ['complain'], 'complain', True ], ['complain'] ),
|
|
|
|
([ ['complain'], 'complain', False ], [] ),
|
|
|
|
([ [], 'audit', True ], ['audit'] ),
|
|
|
|
([ [], 'audit', False ], [] ),
|
|
|
|
([ ['complain'], 'audit', True ], ['audit', 'complain'] ),
|
|
|
|
([ ['complain'], 'audit', False ], ['complain'] ),
|
2018-07-25 20:59:34 +02:00
|
|
|
([ '', 'audit', True ], ['audit'] ),
|
|
|
|
([ None, 'audit', False ], [] ),
|
|
|
|
([ 'complain', 'audit', True ], ['audit', 'complain'] ),
|
|
|
|
([ ' complain ', 'audit', False ], ['complain'] ),
|
2020-09-24 23:03:35 +02:00
|
|
|
([ 'audit complain', ['audit', 'complain'], False ], [] ),
|
|
|
|
([ 'audit complain', 'audit complain', False ], [] ),
|
|
|
|
([ 'audit complain', ['audit', 'enforce'], False ], ['complain'] ),
|
|
|
|
([ 'audit complain', 'audit enforce', False ], ['complain'] ),
|
|
|
|
([ '', ['audit', 'complain'], True ], ['audit', 'complain'] ),
|
|
|
|
([ '', 'audit complain', True ], ['audit', 'complain'] ),
|
|
|
|
([ 'audit', ['audit', 'enforce'], True ], ['audit', 'enforce'] ),
|
|
|
|
([ 'audit', 'audit enforce', True ], ['audit', 'enforce'] ),
|
2018-07-25 20:44:39 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def _run_test(self, params, expected):
|
|
|
|
new_flags = add_or_remove_flag(params[0], params[1], params[2])
|
|
|
|
self.assertEqual(new_flags, expected)
|
|
|
|
|
2018-07-25 20:33:52 +02:00
|
|
|
class AaTest_split_flags(AATest):
|
|
|
|
tests = [
|
|
|
|
(None , [] ),
|
|
|
|
('' , [] ),
|
|
|
|
(' ' , [] ),
|
|
|
|
(' , ' , [] ),
|
|
|
|
('complain' , ['complain'] ),
|
|
|
|
(' complain attach_disconnected' , ['attach_disconnected', 'complain'] ),
|
|
|
|
(' complain , attach_disconnected' , ['attach_disconnected', 'complain'] ),
|
|
|
|
(' complain , , audit , , ' , ['audit', 'complain'] ),
|
|
|
|
]
|
|
|
|
|
|
|
|
def _run_test(self, params, expected):
|
|
|
|
split = split_flags(params)
|
|
|
|
self.assertEqual(split, expected)
|
|
|
|
|
2018-05-09 22:08:44 +02:00
|
|
|
class AaTest_var_transform(AATest):
|
|
|
|
tests = [
|
2018-05-31 21:57:13 +02:00
|
|
|
(['foo', ''], '"" foo' ),
|
|
|
|
(['foo', 'bar'], 'bar foo' ),
|
2018-05-09 22:08:44 +02:00
|
|
|
([''], '""' ),
|
|
|
|
(['bar baz', 'foo'], '"bar baz" foo' ),
|
|
|
|
]
|
|
|
|
|
|
|
|
def _run_test(self, params, expected):
|
|
|
|
self.assertEqual(var_transform(params), expected)
|
|
|
|
|
2017-07-11 13:32:33 +02:00
|
|
|
|
|
|
|
setup_all_loops(__name__)
|
|
|
|
if __name__ == '__main__':
|
2018-04-08 20:18:30 +02:00
|
|
|
unittest.main(verbosity=1)
|