1
0

test_win_lgpo_netsh.py 21 KB

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