1
0

test_win_lgpo_netsh.py 26 KB

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