test_selinux.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. # Import Salt Testing Libs
  3. from __future__ import absolute_import
  4. from tests.support.mixins import LoaderModuleMockMixin
  5. from tests.support.unit import TestCase
  6. from tests.support.mock import (
  7. MagicMock,
  8. patch,
  9. )
  10. # Import Salt libs
  11. from salt.exceptions import SaltInvocationError
  12. import salt.modules.selinux as selinux
  13. class SelinuxModuleTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test cases for salt.modules.selinux
  16. '''
  17. def setup_loader_modules(self):
  18. return {selinux: {}}
  19. def test_fcontext_get_policy_parsing(self):
  20. '''
  21. Test to verify that the parsing of the semanage output into fields is
  22. correct. Added with #45784.
  23. '''
  24. cases = [
  25. {
  26. 'semanage_out': '/var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0',
  27. 'name': '/var/www(/.*)?',
  28. 'filetype': 'all files',
  29. 'sel_user': 'system_u',
  30. 'sel_role': 'object_r',
  31. 'sel_type': 'httpd_sys_content_t',
  32. 'sel_level': 's0'
  33. },
  34. {
  35. 'semanage_out': '/var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0',
  36. 'name': '/var/www(/.*)?',
  37. 'filetype': 'all files',
  38. 'sel_user': 'system_u',
  39. 'sel_role': 'object_r',
  40. 'sel_type': 'httpd_sys_content_t',
  41. 'sel_level': 's0'
  42. },
  43. {
  44. 'semanage_out': '/var/lib/dhcp3? directory system_u:object_r:dhcp_state_t:s0',
  45. 'name': '/var/lib/dhcp3?',
  46. 'filetype': 'directory',
  47. 'sel_user': 'system_u',
  48. 'sel_role': 'object_r',
  49. 'sel_type': 'dhcp_state_t',
  50. 'sel_level': 's0'
  51. },
  52. {
  53. 'semanage_out': '/var/lib/dhcp3? directory system_u:object_r:dhcp_state_t:s0',
  54. 'name': '/var/lib/dhcp3?',
  55. 'filetype': 'directory',
  56. 'sel_user': 'system_u',
  57. 'sel_role': 'object_r',
  58. 'sel_type': 'dhcp_state_t',
  59. 'sel_level': 's0'
  60. },
  61. {
  62. 'semanage_out': '/var/lib/dhcp3? directory system_u:object_r:dhcp_state_t:s0',
  63. 'name': '/var/lib/dhcp3?',
  64. 'filetype': 'directory',
  65. 'sel_user': 'system_u',
  66. 'sel_role': 'object_r',
  67. 'sel_type': 'dhcp_state_t',
  68. 'sel_level': 's0'
  69. }
  70. ]
  71. for case in cases:
  72. with patch.dict(selinux.__salt__, {'cmd.shell': MagicMock(return_value=case['semanage_out'])}):
  73. ret = selinux.fcontext_get_policy(case['name'])
  74. self.assertEqual(ret['filespec'], case['name'])
  75. self.assertEqual(ret['filetype'], case['filetype'])
  76. self.assertEqual(ret['sel_user'], case['sel_user'])
  77. self.assertEqual(ret['sel_role'], case['sel_role'])
  78. self.assertEqual(ret['sel_type'], case['sel_type'])
  79. self.assertEqual(ret['sel_level'], case['sel_level'])
  80. def test_parse_protocol_port_positive(self):
  81. '''
  82. Test to verify positive parsing name, protocol and port combinations
  83. '''
  84. cases = [
  85. {
  86. 'name': 'tcp/80',
  87. 'protocol': None,
  88. 'port': None,
  89. 'expected': ('tcp', '80')
  90. },
  91. {
  92. 'name': 'udp/53',
  93. 'protocol': None,
  94. 'port': None,
  95. 'expected': ('udp', '53')
  96. },
  97. {
  98. 'name': 'tcp_test_dns',
  99. 'protocol': 'tcp',
  100. 'port': '53',
  101. 'expected': ('tcp', '53')
  102. },
  103. {
  104. 'name': 'udp_test/dns',
  105. 'protocol': 'udp',
  106. 'port': '53',
  107. 'expected': ('udp', '53')
  108. },
  109. ]
  110. for case in cases:
  111. ret = selinux._parse_protocol_port(case['name'], case['protocol'], case['port'])
  112. self.assertTupleEqual(ret, case['expected'])
  113. def test_parse_protocol_port_negative(self):
  114. '''
  115. Test to verify negative parsing of name, protocol and port combinations
  116. '''
  117. cases = [
  118. {
  119. 'name': 'invalid_name_no_args',
  120. 'protocol': None,
  121. 'port': None,
  122. },
  123. {
  124. 'name': 'invalid_proto/80',
  125. 'protocol': 'nottcp',
  126. 'port': '80',
  127. },
  128. {
  129. 'name': 'invalid_port',
  130. 'protocol': 'tcp',
  131. 'port': 'notaport',
  132. },
  133. {
  134. 'name': 'missing_proto',
  135. 'protocol': None,
  136. 'port': '80',
  137. },
  138. {
  139. 'name': 'missing_port',
  140. 'protocol': 'udp',
  141. 'port': None,
  142. },
  143. ]
  144. for case in cases:
  145. self.assertRaises(SaltInvocationError, selinux._parse_protocol_port, case['name'], case['protocol'],
  146. case['port'])
  147. def test_port_get_policy_parsing(self):
  148. '''
  149. Test to verify that the parsing of the semanage port output into fields is correct.
  150. '''
  151. cases = [
  152. {
  153. 'semanage_out': 'cma_port_t tcp 1050',
  154. 'name': 'tcp/1050',
  155. 'expected': {'sel_type': 'cma_port_t', 'protocol': 'tcp', 'port': '1050'},
  156. },
  157. {
  158. 'semanage_out': 'cluster_port_t tcp 5149, 40040, 50006-50008',
  159. 'name': 'tcp/40040',
  160. 'expected': {'sel_type': 'cluster_port_t', 'protocol': 'tcp', 'port': '5149, 40040, 50006-50008'},
  161. },
  162. {
  163. 'semanage_out': 'http_port_t tcp 9008, 8010, 9002-9003, 80, 81, 443, 488, 8008, 8009, 8443, 9000',
  164. 'name': 'tcp/9000',
  165. 'expected': {'sel_type': 'http_port_t', 'protocol': 'tcp', 'port': '9008, 8010, 9002-9003, 80, 81, 443, 488, 8008, 8009, 8443, 9000'},
  166. },
  167. {
  168. 'semanage_out': 'vnc_port_t tcp 5985-5999, 5900-5983',
  169. 'name': 'tcp/5985-5999',
  170. 'expected': {'sel_type': 'vnc_port_t', 'protocol': 'tcp', 'port': '5985-5999, 5900-5983'},
  171. },
  172. {
  173. 'semanage_out': 'zebra_port_t tcp 2606, 2608-2609, 2600-2604',
  174. 'name': 'tcp/2608-2609',
  175. 'expected': {'sel_type': 'zebra_port_t', 'protocol': 'tcp', 'port': '2606, 2608-2609, 2600-2604'},
  176. },
  177. {
  178. 'semanage_out': 'radius_port_t udp 1645, 1812, 18120-18121',
  179. 'name': 'tcp/18120-18121',
  180. 'expected': {'sel_type': 'radius_port_t', 'protocol': 'udp', 'port': '1645, 1812, 18120-18121'},
  181. },
  182. ]
  183. for case in cases:
  184. with patch.dict(selinux.__salt__, {'cmd.shell': MagicMock(return_value=case['semanage_out'])}):
  185. ret = selinux.port_get_policy(case['name'])
  186. self.assertDictEqual(ret, case['expected'])