2019-02-09 19:43:33 +02:00
|
|
|
#! /usr/bin/python3
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Otto Kekäläinen <otto@kekalainen.net>
|
|
|
|
#
|
|
|
|
# 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 atexit
|
2019-02-09 19:43:33 +02:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import sys
|
2022-08-07 20:32:07 -04:00
|
|
|
import unittest
|
2019-02-09 19:43:33 +02:00
|
|
|
|
|
|
|
import apparmor.aa as aa
|
|
|
|
import apparmor.ui as aaui
|
|
|
|
from apparmor.common import DebugLogger
|
|
|
|
from apparmor.fail import enable_aa_exception_handler
|
|
|
|
from apparmor.translations import init_translation
|
2022-08-07 20:32:07 -04:00
|
|
|
from common_test import AATest, setup_aa, setup_all_loops
|
2019-02-09 19:43:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AACliBootstrapTest(AATest):
|
2022-08-07 14:57:30 -04:00
|
|
|
"""
|
2019-02-09 19:43:33 +02:00
|
|
|
Generic test of the core AppArmor Python libraries that all command
|
|
|
|
line tools rely on.
|
2022-08-07 14:57:30 -04:00
|
|
|
"""
|
2019-02-09 19:43:33 +02:00
|
|
|
def AASetup(self):
|
|
|
|
# Redirect sys.stdout to a buffer
|
|
|
|
sys.stdout = io.StringIO()
|
|
|
|
|
|
|
|
global _, debug_logger
|
|
|
|
|
|
|
|
enable_aa_exception_handler()
|
|
|
|
_ = init_translation()
|
|
|
|
atexit.register(aa.on_exit)
|
|
|
|
debug_logger = DebugLogger('Test AA')
|
|
|
|
debug_logger.debug('Starting test')
|
|
|
|
|
|
|
|
def AATeardown(self):
|
|
|
|
debug_logger.debug('Ended test')
|
|
|
|
|
|
|
|
def test_loadincludes(self):
|
|
|
|
self.assertEqual(aa.loadincludes(), None)
|
|
|
|
|
|
|
|
def test_i18n(self):
|
|
|
|
self.assertEqual('Test string - do not translate', _('Test string - do not translate'))
|
|
|
|
|
|
|
|
def test_aa_conf(self):
|
|
|
|
confdir = os.getenv('__AA_CONFDIR')
|
|
|
|
if confdir:
|
|
|
|
self.assertEqual(aa.conf.CONF_DIR, confdir)
|
|
|
|
else:
|
|
|
|
self.assertEqual(aa.conf.CONF_DIR, '/etc/apparmor')
|
|
|
|
|
|
|
|
def test_aa_ui_info(self):
|
|
|
|
aaui.UI_Info('Test string')
|
|
|
|
self.assertEqual(sys.stdout.getvalue(), 'Test string\n')
|
|
|
|
|
|
|
|
def test_aa_ui_info_json(self):
|
2023-07-30 21:14:36 +02:00
|
|
|
aaui.set_json_mode({'settings': {}})
|
2019-02-09 19:43:33 +02:00
|
|
|
sys.stdout.getvalue()
|
|
|
|
aaui.UI_Info('Test string')
|
2022-08-07 12:26:24 -04:00
|
|
|
self.assertEqual(
|
|
|
|
sys.stdout.getvalue(),
|
|
|
|
'{"dialog": "apparmor-json-version","data": "2.12"}\n{"dialog": "info","data": "Test string"}\n')
|
2019-02-09 19:43:33 +02:00
|
|
|
aaui.set_text_mode()
|
|
|
|
|
|
|
|
|
|
|
|
setup_aa(aa) # Wrapper for aa.init_aa()
|
|
|
|
setup_all_loops(__name__)
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=1)
|