test_win_lgpo_netsh.py 26 KB

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