2013-09-28 20:43:06 +05:30
# ----------------------------------------------------------------------
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
#
# 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 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# ----------------------------------------------------------------------
2013-08-31 04:08:26 +05:30
import atexit
import os
2013-08-30 03:54:31 +05:30
import shutil
2013-08-31 04:08:26 +05:30
import subprocess
2013-09-12 14:42:15 +05:30
import sys
2013-08-31 04:08:26 +05:30
import unittest
2013-09-22 15:01:34 +05:30
import filecmp
2013-08-30 03:54:31 +05:30
2013-08-31 04:08:26 +05:30
import apparmor . aa as apparmor
2013-08-30 03:54:31 +05:30
2013-09-21 12:36:51 +05:30
# Path for the program
2013-09-12 14:42:15 +05:30
test_path = ' /usr/sbin/ntpd '
2013-09-21 12:36:51 +05:30
# Path for the target file containing profile
local_profilename = ' ./profiles/usr.sbin.ntpd '
2013-09-12 14:42:15 +05:30
python_interpreter = ' python '
2013-12-20 03:12:58 +05:30
if sys . version_info > = ( 3 , 0 ) :
2013-09-12 14:42:15 +05:30
python_interpreter = ' python3 '
2013-08-30 03:54:31 +05:30
2013-09-12 14:42:15 +05:30
class Test ( unittest . TestCase ) :
2013-09-22 22:51:30 +05:30
2013-08-30 03:54:31 +05:30
def test_audit ( self ) :
2013-08-31 04:08:26 +05:30
#Set ntpd profile to audit mode and check if it was correctly set
2014-02-28 16:09:00 +05:30
str ( subprocess . check_output ( ' %s ./../aa-audit -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True ) )
2013-09-21 12:36:51 +05:30
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , ' audit ' , ' Audit flag could not be set in profile %s ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
#Remove audit mode from ntpd profile and check if it was correctly removed
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-audit -d ./profiles -r %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
2014-09-14 17:27:01 +02:00
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , None , ' Audit flag could not be removed in profile %s ' % local_profilename )
2013-08-31 04:08:26 +05:30
2013-09-22 22:51:30 +05:30
2013-08-30 03:54:31 +05:30
def test_complain ( self ) :
2013-08-31 04:08:26 +05:30
#Set ntpd profile to complain mode and check if it was correctly set
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-complain -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
2014-09-14 17:27:01 +02:00
# self.assertEqual(os.path.islink('./profiles/force-complain/%s'%os.path.basename(local_profilename)), True, 'Failed to create a symlink for %s in force-complain'%local_profilename)
2013-09-21 12:36:51 +05:30
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , ' complain ' , ' Complain flag could not be set in profile %s ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
#Set ntpd profile to enforce mode and check if it was correctly set
2014-09-14 17:27:01 +02:00
subprocess . check_output ( ' %s ./../aa-enforce -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
self . assertEqual ( os . path . islink ( ' ./profiles/force-complain/ %s ' % os . path . basename ( local_profilename ) ) , False , ' Failed to remove symlink for %s from force-complain ' % local_profilename )
self . assertEqual ( os . path . islink ( ' ./profiles/disable/ %s ' % os . path . basename ( local_profilename ) ) , False , ' Failed to remove symlink for %s from disable ' % local_profilename )
2013-09-21 12:36:51 +05:30
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , None , ' Complain flag could not be removed in profile %s ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
# Set audit flag and then complain flag in a profile
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-audit -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
subprocess . check_output ( ' %s ./../aa-complain -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
2014-09-14 17:27:01 +02:00
# self.assertEqual(os.path.islink('./profiles/force-complain/%s'%os.path.basename(local_profilename)), True, 'Failed to create a symlink for %s in force-complain'%local_profilename)
2013-09-21 12:36:51 +05:30
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , ' audit,complain ' , ' Complain flag could not be set in profile %s ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
#Remove complain flag first i.e. set to enforce mode
2014-09-14 17:27:01 +02:00
subprocess . check_output ( ' %s ./../aa-enforce -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
self . assertEqual ( os . path . islink ( ' ./profiles/force-complain/ %s ' % os . path . basename ( local_profilename ) ) , False , ' Failed to remove symlink for %s from force-complain ' % local_profilename )
self . assertEqual ( os . path . islink ( ' ./profiles/disable/ %s ' % os . path . basename ( local_profilename ) ) , False , ' Failed to remove symlink for %s from disable ' % local_profilename )
2013-09-21 12:36:51 +05:30
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , ' audit ' , ' Complain flag could not be removed in profile %s ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
#Remove audit flag
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-audit -d ./profiles -r %s ' % ( python_interpreter , test_path ) , shell = True )
2013-09-22 22:51:30 +05:30
2013-08-30 03:54:31 +05:30
def test_enforce ( self ) :
2013-08-31 04:08:26 +05:30
#Set ntpd profile to complain mode and check if it was correctly set
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
#Set ntpd profile to enforce mode and check if it was correctly set
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-enforce -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
self . assertEqual ( os . path . islink ( ' ./profiles/force-complain/ %s ' % os . path . basename ( local_profilename ) ) , False , ' Failed to remove symlink for %s from force-complain ' % local_profilename )
self . assertEqual ( os . path . islink ( ' ./profiles/disable/ %s ' % os . path . basename ( local_profilename ) ) , False , ' Failed to remove symlink for %s from disable ' % local_profilename )
2013-09-21 12:36:51 +05:30
self . assertEqual ( apparmor . get_profile_flags ( local_profilename , test_path ) , None , ' Complain flag could not be removed in profile %s ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-30 03:54:31 +05:30
def test_disable ( self ) :
2013-08-31 04:08:26 +05:30
#Disable the ntpd profile and check if it was correctly disabled
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-disable -d ./profiles %s ' % ( python_interpreter , test_path ) , shell = True )
2013-08-31 04:08:26 +05:30
self . assertEqual ( os . path . islink ( ' ./profiles/disable/ %s ' % os . path . basename ( local_profilename ) ) , True , ' Failed to create a symlink for %s in disable ' % local_profilename )
2013-09-22 22:51:30 +05:30
2013-08-30 03:54:31 +05:30
def test_autodep ( self ) :
pass
2013-12-20 03:12:58 +05:30
2013-10-22 03:06:23 +05:30
def test_unconfined ( self ) :
2014-02-28 16:09:00 +05:30
output = subprocess . check_output ( ' %s ./../aa-unconfined ' % python_interpreter , shell = True )
2013-12-20 03:12:58 +05:30
2014-02-28 16:09:00 +05:30
output_force = subprocess . check_output ( ' %s ./../aa-unconfined --paranoid ' % python_interpreter , shell = True )
2013-12-20 03:12:58 +05:30
2013-10-22 03:06:23 +05:30
self . assertIsNot ( output , ' ' , ' Failed to run aa-unconfined ' )
2013-12-20 03:12:58 +05:30
2013-10-22 03:06:23 +05:30
self . assertIsNot ( output_force , ' ' , ' Failed to run aa-unconfined in paranoid mode ' )
2013-12-20 03:12:58 +05:30
2013-09-22 22:51:30 +05:30
2013-09-22 15:01:34 +05:30
def test_cleanprof ( self ) :
input_file = ' cleanprof_test.in '
output_file = ' cleanprof_test.out '
#We position the local testfile
shutil . copy ( ' ./ %s ' % input_file , ' ./profiles ' )
#Our silly test program whose profile we wish to clean
cleanprof_test = ' /usr/bin/a/simple/cleanprof/test/profile '
2013-09-22 22:51:30 +05:30
2014-02-28 16:09:00 +05:30
subprocess . check_output ( ' %s ./../aa-cleanprof -d ./profiles -s %s ' % ( python_interpreter , cleanprof_test ) , shell = True )
2013-09-22 22:51:30 +05:30
2013-09-22 15:01:34 +05:30
#Strip off the first line (#modified line)
subprocess . check_output ( ' sed -i 1d ./profiles/ %s ' % ( input_file ) , shell = True )
2013-09-22 22:51:30 +05:30
2013-09-22 15:01:34 +05:30
self . assertEqual ( filecmp . cmp ( ' ./profiles/ %s ' % input_file , ' ./ %s ' % output_file , False ) , True , ' Failed to cleanup profile properly ' )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
def clean_profile_dir ( ) :
#Wipe the local profiles from the test directory
shutil . rmtree ( ' ./profiles ' )
2013-08-30 03:54:31 +05:30
if __name__ == " __main__ " :
#import sys;sys.argv = ['', 'Test.testName']
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
if os . path . exists ( ' ./profiles ' ) :
shutil . rmtree ( ' ./profiles ' )
#copy the local profiles to the test directory
2013-09-22 15:01:34 +05:30
#Should be the set of cleanprofile
2014-07-17 15:59:57 +02:00
shutil . copytree ( ' ../../profiles/apparmor.d/ ' , ' ./profiles ' , symlinks = True )
2013-09-22 22:51:30 +05:30
2013-12-20 03:12:58 +05:30
apparmor . profile_dir = ' ./profiles '
2013-09-21 12:36:51 +05:30
2013-08-31 04:08:26 +05:30
atexit . register ( clean_profile_dir )
2013-09-22 22:51:30 +05:30
2013-08-31 04:08:26 +05:30
unittest . main ( )