test_selinux.py 7.7 KB

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