| 1 |
|
| 2 |
|
| 3 |
import unittest |
| 4 |
from cpuinfo import * |
| 5 |
import helpers |
| 6 |
|
| 7 |
|
| 8 |
class MockDataSource_enforcing: |
| 9 |
@staticmethod |
| 10 |
def has_sestatus(): |
| 11 |
return True |
| 12 |
|
| 13 |
@staticmethod |
| 14 |
def sestatus_b(): |
| 15 |
returncode = 0 |
| 16 |
output = r''' |
| 17 |
SELinux status: enabled |
| 18 |
SELinuxfs mount: /sys/fs/selinux |
| 19 |
SELinux root directory: /etc/selinux |
| 20 |
Loaded policy name: targeted |
| 21 |
Current mode: enforcing |
| 22 |
Mode from config file: enforcing |
| 23 |
Policy MLS status: enabled |
| 24 |
Policy deny_unknown status: allowed |
| 25 |
Memory protection checking: actual (secure) |
| 26 |
Max kernel policy version: 31 |
| 27 |
''' |
| 28 |
return returncode, output |
| 29 |
|
| 30 |
class MockDataSource_not_enforcing: |
| 31 |
@staticmethod |
| 32 |
def has_sestatus(): |
| 33 |
return True |
| 34 |
|
| 35 |
@staticmethod |
| 36 |
def sestatus_b(): |
| 37 |
returncode = 0 |
| 38 |
output = r''' |
| 39 |
SELinux status: enabled |
| 40 |
SELinuxfs mount: /sys/fs/selinux |
| 41 |
SELinux root directory: /etc/selinux |
| 42 |
Loaded policy name: targeted |
| 43 |
Current mode: eating |
| 44 |
Mode from config file: enforcing |
| 45 |
Policy MLS status: enabled |
| 46 |
Policy deny_unknown status: allowed |
| 47 |
Memory protection checking: actual (secure) |
| 48 |
Max kernel policy version: 31 |
| 49 |
''' |
| 50 |
return returncode, output |
| 51 |
|
| 52 |
class MockDataSource_exec_mem_and_heap: |
| 53 |
@staticmethod |
| 54 |
def has_sestatus(): |
| 55 |
return True |
| 56 |
|
| 57 |
@staticmethod |
| 58 |
def sestatus_b(): |
| 59 |
returncode = 0 |
| 60 |
output = r''' |
| 61 |
allow_execheap on |
| 62 |
allow_execmem on |
| 63 |
''' |
| 64 |
return returncode, output |
| 65 |
|
| 66 |
class MockDataSource_no_exec_mem_and_heap: |
| 67 |
@staticmethod |
| 68 |
def has_sestatus(): |
| 69 |
return True |
| 70 |
|
| 71 |
@staticmethod |
| 72 |
def sestatus_b(): |
| 73 |
returncode = 0 |
| 74 |
output = r''' |
| 75 |
allow_execheap off |
| 76 |
allow_execmem off |
| 77 |
''' |
| 78 |
return returncode, output |
| 79 |
|
| 80 |
|
| 81 |
class TestSELinux(unittest.TestCase): |
| 82 |
def setUp(self): |
| 83 |
helpers.backup_data_source(cpuinfo) |
| 84 |
self.trace = Trace(False, False) |
| 85 |
|
| 86 |
def tearDown(self): |
| 87 |
helpers.restore_data_source(cpuinfo) |
| 88 |
|
| 89 |
def test_enforcing(self): |
| 90 |
helpers.monkey_patch_data_source(cpuinfo, MockDataSource_enforcing) |
| 91 |
self.assertEqual(True, cpuinfo._is_selinux_enforcing(self.trace)) |
| 92 |
|
| 93 |
def test_not_enforcing(self): |
| 94 |
helpers.monkey_patch_data_source(cpuinfo, MockDataSource_not_enforcing) |
| 95 |
self.assertEqual(False, cpuinfo._is_selinux_enforcing(self.trace)) |
| 96 |
|
| 97 |
def test_exec_mem_and_heap(self): |
| 98 |
helpers.monkey_patch_data_source(cpuinfo, MockDataSource_exec_mem_and_heap) |
| 99 |
self.assertEqual(False, cpuinfo._is_selinux_enforcing(self.trace)) |
| 100 |
|
| 101 |
def test_no_exec_mem_and_heap(self): |
| 102 |
helpers.monkey_patch_data_source(cpuinfo, MockDataSource_no_exec_mem_and_heap) |
| 103 |
self.assertEqual(True, cpuinfo._is_selinux_enforcing(self.trace)) |