1
0

test_win_lgpo_netsh.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import pytest
  4. import salt.utils.platform
  5. import salt.utils.win_lgpo_netsh as win_lgpo_netsh
  6. from salt.exceptions import CommandExecutionError
  7. from tests.support.unit import TestCase, skipIf
  8. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  9. class WinLgpoNetshTestCase(TestCase):
  10. def test_get_settings_firewallpolicy_local(self):
  11. ret = win_lgpo_netsh.get_settings(
  12. profile="domain", section="firewallpolicy", store="local"
  13. )
  14. self.assertIn("Inbound", ret)
  15. self.assertIn("Outbound", ret)
  16. def test_get_settings_firewallpolicy_lgpo(self):
  17. ret = win_lgpo_netsh.get_settings(
  18. profile="domain", section="firewallpolicy", store="lgpo"
  19. )
  20. self.assertIn("Inbound", ret)
  21. self.assertIn("Outbound", ret)
  22. def test_get_settings_logging_local(self):
  23. ret = win_lgpo_netsh.get_settings(
  24. profile="domain", section="logging", store="local"
  25. )
  26. self.assertIn("FileName", ret)
  27. self.assertIn("LogAllowedConnections", ret)
  28. self.assertIn("LogDroppedConnections", ret)
  29. self.assertIn("MaxFileSize", ret)
  30. def test_get_settings_logging_lgpo(self):
  31. ret = win_lgpo_netsh.get_settings(
  32. profile="domain", section="logging", store="lgpo"
  33. )
  34. self.assertIn("FileName", ret)
  35. self.assertIn("LogAllowedConnections", ret)
  36. self.assertIn("LogDroppedConnections", ret)
  37. self.assertIn("MaxFileSize", ret)
  38. def test_get_settings_settings_local(self):
  39. ret = win_lgpo_netsh.get_settings(
  40. profile="domain", section="settings", store="local"
  41. )
  42. self.assertIn("InboundUserNotification", ret)
  43. self.assertIn("LocalConSecRules", ret)
  44. self.assertIn("LocalFirewallRules", ret)
  45. self.assertIn("RemoteManagement", ret)
  46. self.assertIn("UnicastResponseToMulticast", ret)
  47. def test_get_settings_settings_lgpo(self):
  48. ret = win_lgpo_netsh.get_settings(
  49. profile="domain", section="settings", store="lgpo"
  50. )
  51. self.assertIn("InboundUserNotification", ret)
  52. self.assertIn("LocalConSecRules", ret)
  53. self.assertIn("LocalFirewallRules", ret)
  54. self.assertIn("RemoteManagement", ret)
  55. self.assertIn("UnicastResponseToMulticast", ret)
  56. def test_get_settings_state_local(self):
  57. ret = win_lgpo_netsh.get_settings(
  58. profile="domain", section="state", store="local"
  59. )
  60. self.assertIn("State", ret)
  61. def test_get_settings_state_lgpo(self):
  62. ret = win_lgpo_netsh.get_settings(
  63. profile="domain", section="state", store="lgpo"
  64. )
  65. self.assertIn("State", ret)
  66. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  67. def test_get_all_settings_local(self):
  68. ret = win_lgpo_netsh.get_all_settings(profile="domain", store="local")
  69. self.assertIn("Inbound", ret)
  70. self.assertIn("Outbound", ret)
  71. self.assertIn("FileName", ret)
  72. self.assertIn("LogAllowedConnections", ret)
  73. self.assertIn("LogDroppedConnections", ret)
  74. self.assertIn("MaxFileSize", ret)
  75. self.assertIn("InboundUserNotification", ret)
  76. self.assertIn("LocalConSecRules", ret)
  77. self.assertIn("LocalFirewallRules", ret)
  78. self.assertIn("RemoteManagement", ret)
  79. self.assertIn("UnicastResponseToMulticast", ret)
  80. self.assertIn("State", ret)
  81. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  82. def test_get_all_settings_lgpo(self):
  83. ret = win_lgpo_netsh.get_all_settings(profile="domain", store="local")
  84. self.assertIn("Inbound", ret)
  85. self.assertIn("Outbound", ret)
  86. self.assertIn("FileName", ret)
  87. self.assertIn("LogAllowedConnections", ret)
  88. self.assertIn("LogDroppedConnections", ret)
  89. self.assertIn("MaxFileSize", ret)
  90. self.assertIn("InboundUserNotification", ret)
  91. self.assertIn("LocalConSecRules", ret)
  92. self.assertIn("LocalFirewallRules", ret)
  93. self.assertIn("RemoteManagement", ret)
  94. self.assertIn("UnicastResponseToMulticast", ret)
  95. self.assertIn("State", ret)
  96. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  97. def test_get_all_profiles_local(self):
  98. ret = win_lgpo_netsh.get_all_profiles(store="local")
  99. self.assertIn("Domain Profile", ret)
  100. self.assertIn("Private Profile", ret)
  101. self.assertIn("Public Profile", ret)
  102. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  103. def test_get_all_profiles_lgpo(self):
  104. ret = win_lgpo_netsh.get_all_profiles(store="lgpo")
  105. self.assertIn("Domain Profile", ret)
  106. self.assertIn("Private Profile", ret)
  107. self.assertIn("Public Profile", ret)
  108. @pytest.mark.destructive_test
  109. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  110. def test_set_firewall_settings_inbound_local(self):
  111. current = win_lgpo_netsh.get_settings(
  112. profile="domain", section="firewallpolicy", store="local"
  113. )["Inbound"]
  114. try:
  115. ret = win_lgpo_netsh.set_firewall_settings(
  116. profile="domain", inbound="allowinbound", store="local"
  117. )
  118. self.assertTrue(ret)
  119. new = win_lgpo_netsh.get_settings(
  120. profile="domain", section="firewallpolicy", store="local"
  121. )["Inbound"]
  122. self.assertEqual("AllowInbound", new)
  123. finally:
  124. ret = win_lgpo_netsh.set_firewall_settings(
  125. profile="domain", inbound=current, store="local"
  126. )
  127. self.assertTrue(ret)
  128. @pytest.mark.destructive_test
  129. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  130. def test_set_firewall_settings_inbound_local_notconfigured(self):
  131. current = win_lgpo_netsh.get_settings(
  132. profile="domain", section="firewallpolicy", store="local"
  133. )["Inbound"]
  134. try:
  135. self.assertRaises(
  136. CommandExecutionError,
  137. win_lgpo_netsh.set_firewall_settings,
  138. profile="domain",
  139. inbound="notconfigured",
  140. store="local",
  141. )
  142. finally:
  143. ret = win_lgpo_netsh.set_firewall_settings(
  144. profile="domain", inbound=current, store="local"
  145. )
  146. self.assertTrue(ret)
  147. @pytest.mark.destructive_test
  148. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  149. def test_set_firewall_settings_inbound_lgpo_notconfigured(self):
  150. current = win_lgpo_netsh.get_settings(
  151. profile="domain", section="firewallpolicy", store="lgpo"
  152. )["Inbound"]
  153. try:
  154. ret = win_lgpo_netsh.set_firewall_settings(
  155. profile="domain", inbound="notconfigured", store="lgpo"
  156. )
  157. self.assertTrue(ret)
  158. new = win_lgpo_netsh.get_settings(
  159. profile="domain", section="firewallpolicy", store="lgpo"
  160. )["Inbound"]
  161. self.assertEqual("NotConfigured", new)
  162. finally:
  163. ret = win_lgpo_netsh.set_firewall_settings(
  164. profile="domain", inbound=current, store="lgpo"
  165. )
  166. self.assertTrue(ret)
  167. @pytest.mark.destructive_test
  168. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  169. def test_set_firewall_settings_outbound_local(self):
  170. current = win_lgpo_netsh.get_settings(
  171. profile="domain", section="firewallpolicy", store="local"
  172. )["Outbound"]
  173. try:
  174. ret = win_lgpo_netsh.set_firewall_settings(
  175. profile="domain", outbound="allowoutbound", store="local"
  176. )
  177. self.assertTrue(ret)
  178. new = win_lgpo_netsh.get_settings(
  179. profile="domain", section="firewallpolicy", store="local"
  180. )["Outbound"]
  181. self.assertEqual("AllowOutbound", new)
  182. finally:
  183. ret = win_lgpo_netsh.set_firewall_settings(
  184. profile="domain", outbound=current, store="local"
  185. )
  186. self.assertTrue(ret)
  187. @pytest.mark.destructive_test
  188. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  189. def test_set_firewall_logging_allowed_local_enable(self):
  190. current = win_lgpo_netsh.get_settings(
  191. profile="domain", section="logging", store="local"
  192. )["LogAllowedConnections"]
  193. try:
  194. ret = win_lgpo_netsh.set_logging_settings(
  195. profile="domain",
  196. setting="allowedconnections",
  197. value="enable",
  198. store="local",
  199. )
  200. self.assertTrue(ret)
  201. new = win_lgpo_netsh.get_settings(
  202. profile="domain", section="logging", store="local"
  203. )["LogAllowedConnections"]
  204. self.assertEqual("Enable", new)
  205. finally:
  206. ret = win_lgpo_netsh.set_logging_settings(
  207. profile="domain",
  208. setting="allowedconnections",
  209. value=current,
  210. store="local",
  211. )
  212. self.assertTrue(ret)
  213. @pytest.mark.destructive_test
  214. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  215. def test_set_firewall_logging_allowed_local_notconfigured(self):
  216. current = win_lgpo_netsh.get_settings(
  217. profile="domain", section="logging", store="local"
  218. )["LogAllowedConnections"]
  219. try:
  220. self.assertRaises(
  221. CommandExecutionError,
  222. win_lgpo_netsh.set_logging_settings,
  223. profile="domain",
  224. setting="allowedconnections",
  225. value="notconfigured",
  226. store="local",
  227. )
  228. finally:
  229. ret = win_lgpo_netsh.set_logging_settings(
  230. profile="domain",
  231. setting="allowedconnections",
  232. value=current,
  233. store="local",
  234. )
  235. self.assertTrue(ret)
  236. @pytest.mark.destructive_test
  237. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  238. def test_set_firewall_logging_allowed_lgpo_notconfigured(self):
  239. current = win_lgpo_netsh.get_settings(
  240. profile="domain", section="logging", store="lgpo"
  241. )["LogAllowedConnections"]
  242. try:
  243. ret = win_lgpo_netsh.set_logging_settings(
  244. profile="domain",
  245. setting="allowedconnections",
  246. value="notconfigured",
  247. store="lgpo",
  248. )
  249. self.assertTrue(ret)
  250. new = win_lgpo_netsh.get_settings(
  251. profile="domain", section="logging", store="lgpo"
  252. )["LogAllowedConnections"]
  253. self.assertEqual("NotConfigured", new)
  254. finally:
  255. ret = win_lgpo_netsh.set_logging_settings(
  256. profile="domain",
  257. setting="allowedconnections",
  258. value=current,
  259. store="lgpo",
  260. )
  261. self.assertTrue(ret)
  262. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  263. def test_set_firewall_logging_dropped_local_enable(self):
  264. current = win_lgpo_netsh.get_settings(
  265. profile="domain", section="logging", store="local"
  266. )["LogDroppedConnections"]
  267. try:
  268. ret = win_lgpo_netsh.set_logging_settings(
  269. profile="domain",
  270. setting="droppedconnections",
  271. value="enable",
  272. store="local",
  273. )
  274. self.assertTrue(ret)
  275. new = win_lgpo_netsh.get_settings(
  276. profile="domain", section="logging", store="local"
  277. )["LogDroppedConnections"]
  278. self.assertEqual("Enable", new)
  279. finally:
  280. ret = win_lgpo_netsh.set_logging_settings(
  281. profile="domain",
  282. setting="droppedconnections",
  283. value=current,
  284. store="local",
  285. )
  286. self.assertTrue(ret)
  287. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  288. def test_set_firewall_logging_filename_local(self):
  289. current = win_lgpo_netsh.get_settings(
  290. profile="domain", section="logging", store="local"
  291. )["FileName"]
  292. try:
  293. ret = win_lgpo_netsh.set_logging_settings(
  294. profile="domain",
  295. setting="filename",
  296. value="C:\\Temp\\test.log",
  297. store="local",
  298. )
  299. self.assertTrue(ret)
  300. new = win_lgpo_netsh.get_settings(
  301. profile="domain", section="logging", store="local"
  302. )["FileName"]
  303. self.assertEqual("C:\\Temp\\test.log", new)
  304. finally:
  305. ret = win_lgpo_netsh.set_logging_settings(
  306. profile="domain", setting="filename", value=current, store="local"
  307. )
  308. self.assertTrue(ret)
  309. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  310. def test_set_firewall_logging_maxfilesize_local(self):
  311. current = win_lgpo_netsh.get_settings(
  312. profile="domain", section="logging", store="local"
  313. )["MaxFileSize"]
  314. try:
  315. ret = win_lgpo_netsh.set_logging_settings(
  316. profile="domain", setting="maxfilesize", value="16384", store="local"
  317. )
  318. self.assertTrue(ret)
  319. new = win_lgpo_netsh.get_settings(
  320. profile="domain", section="logging", store="local"
  321. )["MaxFileSize"]
  322. self.assertEqual("16384", new)
  323. finally:
  324. ret = win_lgpo_netsh.set_logging_settings(
  325. profile="domain", setting="maxfilesize", value=current, store="local"
  326. )
  327. self.assertTrue(ret)
  328. @pytest.mark.destructive_test
  329. def test_set_firewall_settings_fwrules_local_enable(self):
  330. self.assertRaises(
  331. CommandExecutionError,
  332. win_lgpo_netsh.set_settings,
  333. profile="domain",
  334. setting="localfirewallrules",
  335. value="enable",
  336. store="local",
  337. )
  338. @pytest.mark.destructive_test
  339. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  340. def test_set_firewall_settings_fwrules_lgpo_notconfigured(self):
  341. current = win_lgpo_netsh.get_settings(
  342. profile="domain", section="settings", store="lgpo"
  343. )["LocalFirewallRules"]
  344. try:
  345. ret = win_lgpo_netsh.set_settings(
  346. profile="domain",
  347. setting="localfirewallrules",
  348. value="notconfigured",
  349. store="lgpo",
  350. )
  351. self.assertTrue(ret)
  352. new = win_lgpo_netsh.get_settings(
  353. profile="domain", section="settings", store="lgpo"
  354. )["LocalFirewallRules"]
  355. self.assertEqual("NotConfigured", new)
  356. finally:
  357. ret = win_lgpo_netsh.set_settings(
  358. profile="domain",
  359. setting="localfirewallrules",
  360. value=current,
  361. store="lgpo",
  362. )
  363. self.assertTrue(ret)
  364. @pytest.mark.destructive_test
  365. def test_set_firewall_settings_consecrules_local_enable(self):
  366. self.assertRaises(
  367. CommandExecutionError,
  368. win_lgpo_netsh.set_settings,
  369. profile="domain",
  370. setting="localconsecrules",
  371. value="enable",
  372. store="local",
  373. )
  374. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  375. def test_set_firewall_settings_notification_local_enable(self):
  376. current = win_lgpo_netsh.get_settings(
  377. profile="domain", section="settings", store="local"
  378. )["InboundUserNotification"]
  379. try:
  380. ret = win_lgpo_netsh.set_settings(
  381. profile="domain",
  382. setting="inboundusernotification",
  383. value="enable",
  384. store="local",
  385. )
  386. self.assertTrue(ret)
  387. new = win_lgpo_netsh.get_settings(
  388. profile="domain", section="settings", store="local"
  389. )["InboundUserNotification"]
  390. self.assertEqual("Enable", new)
  391. finally:
  392. ret = win_lgpo_netsh.set_settings(
  393. profile="domain",
  394. setting="inboundusernotification",
  395. value=current,
  396. store="local",
  397. )
  398. self.assertTrue(ret)
  399. @pytest.mark.destructive_test
  400. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  401. def test_set_firewall_settings_notification_local_notconfigured(self):
  402. current = win_lgpo_netsh.get_settings(
  403. profile="domain", section="settings", store="local"
  404. )["InboundUserNotification"]
  405. try:
  406. self.assertRaises(
  407. CommandExecutionError,
  408. win_lgpo_netsh.set_settings,
  409. profile="domain",
  410. setting="inboundusernotification",
  411. value="notconfigured",
  412. store="local",
  413. )
  414. finally:
  415. ret = win_lgpo_netsh.set_settings(
  416. profile="domain",
  417. setting="inboundusernotification",
  418. value=current,
  419. store="local",
  420. )
  421. self.assertTrue(ret)
  422. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  423. def test_set_firewall_settings_notification_lgpo_notconfigured(self):
  424. current = win_lgpo_netsh.get_settings(
  425. profile="domain", section="settings", store="lgpo"
  426. )["InboundUserNotification"]
  427. try:
  428. ret = win_lgpo_netsh.set_settings(
  429. profile="domain",
  430. setting="inboundusernotification",
  431. value="notconfigured",
  432. store="lgpo",
  433. )
  434. self.assertTrue(ret)
  435. new = win_lgpo_netsh.get_settings(
  436. profile="domain", section="settings", store="lgpo"
  437. )["InboundUserNotification"]
  438. self.assertEqual("NotConfigured", new)
  439. finally:
  440. ret = win_lgpo_netsh.set_settings(
  441. profile="domain",
  442. setting="inboundusernotification",
  443. value=current,
  444. store="lgpo",
  445. )
  446. self.assertTrue(ret)
  447. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  448. def test_set_firewall_settings_remotemgmt_local_enable(self):
  449. current = win_lgpo_netsh.get_settings(
  450. profile="domain", section="settings", store="local"
  451. )["RemoteManagement"]
  452. try:
  453. ret = win_lgpo_netsh.set_settings(
  454. profile="domain",
  455. setting="remotemanagement",
  456. value="enable",
  457. store="local",
  458. )
  459. self.assertTrue(ret)
  460. new = win_lgpo_netsh.get_settings(
  461. profile="domain", section="settings", store="local"
  462. )["RemoteManagement"]
  463. self.assertEqual("Enable", new)
  464. finally:
  465. ret = win_lgpo_netsh.set_settings(
  466. profile="domain",
  467. setting="remotemanagement",
  468. value=current,
  469. store="local",
  470. )
  471. self.assertTrue(ret)
  472. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  473. def test_set_firewall_settings_unicast_local_disable(self):
  474. current = win_lgpo_netsh.get_settings(
  475. profile="domain", section="settings", store="local"
  476. )["UnicastResponseToMulticast"]
  477. try:
  478. ret = win_lgpo_netsh.set_settings(
  479. profile="domain",
  480. setting="unicastresponsetomulticast",
  481. value="disable",
  482. store="local",
  483. )
  484. self.assertTrue(ret)
  485. new = win_lgpo_netsh.get_settings(
  486. profile="domain", section="settings", store="local"
  487. )["UnicastResponseToMulticast"]
  488. self.assertEqual("Disable", new)
  489. finally:
  490. ret = win_lgpo_netsh.set_settings(
  491. profile="domain",
  492. setting="unicastresponsetomulticast",
  493. value=current,
  494. store="local",
  495. )
  496. self.assertTrue(ret)
  497. @pytest.mark.destructive_test
  498. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  499. def test_set_firewall_state_local_on(self):
  500. current = win_lgpo_netsh.get_settings(
  501. profile="domain", section="state", store="local"
  502. )["State"]
  503. try:
  504. ret = win_lgpo_netsh.set_state(profile="domain", state="off", store="local")
  505. self.assertTrue(ret)
  506. new = win_lgpo_netsh.get_settings(
  507. profile="domain", section="state", store="local"
  508. )["State"]
  509. self.assertEqual("OFF", new)
  510. finally:
  511. ret = win_lgpo_netsh.set_state(
  512. profile="domain", state=current, store="local"
  513. )
  514. self.assertTrue(ret)
  515. @pytest.mark.destructive_test
  516. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  517. def test_set_firewall_state_local_notconfigured(self):
  518. current = win_lgpo_netsh.get_settings(
  519. profile="domain", section="state", store="local"
  520. )["State"]
  521. try:
  522. self.assertRaises(
  523. CommandExecutionError,
  524. win_lgpo_netsh.set_state,
  525. profile="domain",
  526. state="notconfigured",
  527. store="local",
  528. )
  529. finally:
  530. ret = win_lgpo_netsh.set_state(
  531. profile="domain", state=current, store="local"
  532. )
  533. self.assertTrue(ret)
  534. @pytest.mark.destructive_test
  535. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  536. def test_set_firewall_state_lgpo_notconfigured(self):
  537. current = win_lgpo_netsh.get_settings(
  538. profile="domain", section="state", store="local"
  539. )["State"]
  540. try:
  541. ret = win_lgpo_netsh.set_state(
  542. profile="domain", state="notconfigured", store="lgpo"
  543. )
  544. self.assertTrue(ret)
  545. new = win_lgpo_netsh.get_settings(
  546. profile="domain", section="state", store="lgpo"
  547. )["State"]
  548. self.assertEqual("NotConfigured", new)
  549. finally:
  550. ret = win_lgpo_netsh.set_state(
  551. profile="domain", state=current, store="lgpo"
  552. )
  553. self.assertTrue(ret)