test_firewall.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. import logging
  2. import operator
  3. from unittest import TestCase
  4. from unittest.mock import patch
  5. import qubesagent.firewall
  6. class DummyIptablesRestore(object):
  7. # pylint: disable=too-few-public-methods
  8. def __init__(self, worker_mock, family):
  9. self._worker_mock = worker_mock
  10. self._family = family
  11. self.returncode = 0
  12. def communicate(self, stdin=None):
  13. self._worker_mock.loaded_iptables[self._family] = stdin
  14. return ("", None)
  15. class DummyQubesDB(object):
  16. def __init__(self, worker_mock):
  17. self._worker_mock = worker_mock
  18. self.entries = {}
  19. self.pending_watches = []
  20. def read(self, key):
  21. try:
  22. return self.entries[key]
  23. except KeyError:
  24. return None
  25. def multiread(self, prefix):
  26. result = {}
  27. for key, value in self.entries.items():
  28. if key.startswith(prefix):
  29. result[key] = value
  30. return result
  31. def list(self, prefix):
  32. result = []
  33. for key in self.entries.keys():
  34. if key.startswith(prefix):
  35. result.append(key)
  36. return result
  37. def watch(self, path):
  38. pass
  39. def read_watch(self):
  40. try:
  41. return self.pending_watches.pop(0)
  42. except IndexError:
  43. return None
  44. class FirewallWorker(qubesagent.firewall.FirewallWorker):
  45. def __init__(self):
  46. # pylint: disable=super-init-not-called
  47. # don't call super on purpose - avoid connecting to QubesDB
  48. # super(FirewallWorker, self).__init__()
  49. self.qdb = DummyQubesDB(self)
  50. self.log = logging.getLogger('qubes.tests')
  51. self.init_called = False
  52. self.cleanup_called = False
  53. self.user_script_called = False
  54. self.rules = {}
  55. def apply_rules(self, source_addr, rules):
  56. self.rules[source_addr] = rules
  57. def cleanup(self):
  58. self.init_called = True
  59. def init(self):
  60. self.cleanup_called = True
  61. def run_user_script(self):
  62. self.user_script_called = True
  63. class IptablesWorker(qubesagent.firewall.IptablesWorker):
  64. '''Override methods actually modifying system state to only log what
  65. would be done'''
  66. def __init__(self):
  67. # pylint: disable=super-init-not-called
  68. # don't call super on purpose - avoid connecting to QubesDB
  69. # super(IptablesWorker, self).__init__()
  70. # copied __init__:
  71. self.qdb = DummyQubesDB(self)
  72. self.log = logging.getLogger('qubes.tests')
  73. self.chains = {
  74. 4: set(),
  75. 6: set(),
  76. }
  77. #: instead of really running `iptables`, log what would be called
  78. self.called_commands = {
  79. 4: [],
  80. 6: [],
  81. }
  82. #: rules that would be loaded with `iptables-restore`
  83. self.loaded_iptables = {
  84. 4: None,
  85. 6: None,
  86. }
  87. def run_ipt(self, family, args, **kwargs):
  88. self.called_commands[family].append(args)
  89. def run_ipt_restore(self, family, args):
  90. return DummyIptablesRestore(self, family)
  91. @staticmethod
  92. def dns_addresses(family=None):
  93. if family == 4:
  94. return ['1.1.1.1', '2.2.2.2']
  95. else:
  96. return ['2001::1', '2001::2']
  97. class NftablesWorker(qubesagent.firewall.NftablesWorker):
  98. '''Override methods actually modifying system state to only log what
  99. would be done'''
  100. def __init__(self):
  101. # pylint: disable=super-init-not-called
  102. # don't call super on purpose - avoid connecting to QubesDB
  103. # super(IptablesWorker, self).__init__()
  104. # copied __init__:
  105. self.qdb = DummyQubesDB(self)
  106. self.log = logging.getLogger('qubes.tests')
  107. self.chains = {
  108. 4: set(),
  109. 6: set(),
  110. }
  111. #: instead of really running `nft`, log what would be loaded
  112. #: rules that would be loaded with `nft`
  113. self.loaded_rules = []
  114. def run_nft(self, nft_input):
  115. self.loaded_rules.append(nft_input)
  116. @staticmethod
  117. def dns_addresses(family=None):
  118. if family == 4:
  119. return ['1.1.1.1', '2.2.2.2']
  120. else:
  121. return ['2001::1', '2001::2']
  122. class TestIptablesWorker(TestCase):
  123. def setUp(self):
  124. super(TestIptablesWorker, self).setUp()
  125. self.obj = IptablesWorker()
  126. self.subprocess_patch = patch('subprocess.call')
  127. self.subprocess_mock = self.subprocess_patch.start()
  128. def tearDown(self):
  129. self.subprocess_patch.stop()
  130. def test_000_chain_for_addr(self):
  131. self.assertEqual(
  132. self.obj.chain_for_addr('10.137.0.1'), 'qbs-10-137-0-1')
  133. self.assertEqual(
  134. self.obj.chain_for_addr('fd09:24ef:4179:0000::3'),
  135. 'qbs-09-24ef-4179-0000--3')
  136. def test_001_create_chain(self):
  137. testdata = [
  138. (4, '10.137.0.1', 'qbs-10-137-0-1'),
  139. (6, 'fd09:24ef:4179:0000::3', 'qbs-fd09-24ef-4179-0000--3')
  140. ]
  141. for family, addr, chain in testdata:
  142. self.obj.create_chain(addr, chain, family)
  143. self.assertEqual(self.obj.called_commands[family],
  144. [['-N', chain],
  145. ['-I', 'QBS-FORWARD', '-s', addr, '-j', chain]])
  146. def test_002_prepare_rules4(self):
  147. rules = [
  148. {'action': 'accept', 'proto': 'tcp',
  149. 'dstports': '80-80', 'dst4': '1.2.3.0/24'},
  150. {'action': 'accept', 'proto': 'udp',
  151. 'dstports': '443-1024', 'dsthost': 'yum.qubes-os.org'},
  152. {'action': 'accept', 'specialtarget': 'dns'},
  153. {'action': 'drop', 'proto': 'udp', 'specialtarget': 'dns'},
  154. {'action': 'drop', 'proto': 'icmp'},
  155. {'action': 'drop'},
  156. ]
  157. expected_iptables = (
  158. "*filter\n"
  159. "-A chain -d 1.2.3.0/24 -p tcp --dport 80:80 -j ACCEPT\n"
  160. "-A chain -d 82.94.215.165/32 -p udp --dport 443:1024 -j ACCEPT\n"
  161. "-A chain -d 1.1.1.1/32 -p tcp --dport 53:53 -j ACCEPT\n"
  162. "-A chain -d 2.2.2.2/32 -p tcp --dport 53:53 -j ACCEPT\n"
  163. "-A chain -d 1.1.1.1/32 -p udp --dport 53:53 -j ACCEPT\n"
  164. "-A chain -d 2.2.2.2/32 -p udp --dport 53:53 -j ACCEPT\n"
  165. "-A chain -d 1.1.1.1/32 -p udp --dport 53:53 -j DROP\n"
  166. "-A chain -d 2.2.2.2/32 -p udp --dport 53:53 -j DROP\n"
  167. "-A chain -p icmp -j DROP\n"
  168. "-A chain -j DROP\n"
  169. "COMMIT\n"
  170. )
  171. self.assertEqual(self.obj.prepare_rules('chain', rules, 4),
  172. expected_iptables)
  173. with self.assertRaises(qubesagent.firewall.RuleParseError):
  174. self.obj.prepare_rules('chain', [{'unknown': 'xxx'}], 4)
  175. with self.assertRaises(qubesagent.firewall.RuleParseError):
  176. self.obj.prepare_rules('chain', [{'dst6': 'a::b'}], 4)
  177. with self.assertRaises(qubesagent.firewall.RuleParseError):
  178. self.obj.prepare_rules('chain', [{'dst4': '3.3.3.3'}], 6)
  179. def test_003_prepare_rules6(self):
  180. rules = [
  181. {'action': 'accept', 'proto': 'tcp',
  182. 'dstports': '80-80', 'dst6': 'a::b/128'},
  183. {'action': 'accept', 'proto': 'tcp',
  184. 'dsthost': 'ripe.net'},
  185. {'action': 'accept', 'specialtarget': 'dns'},
  186. {'action': 'drop', 'proto': 'udp', 'specialtarget': 'dns'},
  187. {'action': 'drop', 'proto': 'icmp'},
  188. {'action': 'drop'},
  189. ]
  190. expected_iptables = (
  191. "*filter\n"
  192. "-A chain -d a::b/128 -p tcp --dport 80:80 -j ACCEPT\n"
  193. "-A chain -d 2001:67c:2e8:22::c100:68b/128 -p tcp -j ACCEPT\n"
  194. "-A chain -d 2001::1/128 -p tcp --dport 53:53 -j ACCEPT\n"
  195. "-A chain -d 2001::2/128 -p tcp --dport 53:53 -j ACCEPT\n"
  196. "-A chain -d 2001::1/128 -p udp --dport 53:53 -j ACCEPT\n"
  197. "-A chain -d 2001::2/128 -p udp --dport 53:53 -j ACCEPT\n"
  198. "-A chain -d 2001::1/128 -p udp --dport 53:53 -j DROP\n"
  199. "-A chain -d 2001::2/128 -p udp --dport 53:53 -j DROP\n"
  200. "-A chain -p icmpv6 -j DROP\n"
  201. "-A chain -j DROP\n"
  202. "COMMIT\n"
  203. )
  204. self.assertEqual(self.obj.prepare_rules('chain', rules, 6),
  205. expected_iptables)
  206. def test_004_apply_rules4(self):
  207. rules = [{'action': 'accept'}]
  208. chain = 'qbs-10-137-0-1'
  209. self.obj.apply_rules('10.137.0.1', rules)
  210. self.assertEqual(self.obj.called_commands[4],
  211. [
  212. ['-N', chain],
  213. ['-I', 'QBS-FORWARD', '-s', '10.137.0.1', '-j', chain],
  214. ['-F', chain]])
  215. self.assertEqual(self.obj.loaded_iptables[4],
  216. self.obj.prepare_rules(chain, rules, 4))
  217. self.assertEqual(self.obj.called_commands[6], [])
  218. self.assertIsNone(self.obj.loaded_iptables[6])
  219. def test_005_apply_rules6(self):
  220. rules = [{'action': 'accept'}]
  221. chain = 'qbs-2000--a'
  222. self.obj.apply_rules('2000::a', rules)
  223. self.assertEqual(self.obj.called_commands[6],
  224. [
  225. ['-N', chain],
  226. ['-I', 'QBS-FORWARD', '-s', '2000::a', '-j', chain],
  227. ['-F', chain]])
  228. self.assertEqual(self.obj.loaded_iptables[6],
  229. self.obj.prepare_rules(chain, rules, 6))
  230. self.assertEqual(self.obj.called_commands[4], [])
  231. self.assertIsNone(self.obj.loaded_iptables[4])
  232. def test_006_init(self):
  233. self.obj.init()
  234. self.assertEqual(self.obj.called_commands[4],
  235. [['-F', 'QBS-FORWARD'], ['-A', 'QBS-FORWARD', '-j', 'DROP']])
  236. self.assertEqual(self.obj.called_commands[6],
  237. [['-F', 'QBS-FORWARD'], ['-A', 'QBS-FORWARD', '-j', 'DROP']])
  238. def test_007_cleanup(self):
  239. self.obj.init()
  240. self.obj.create_chain('1.2.3.4', 'chain-ip4-1', 4)
  241. self.obj.create_chain('1.2.3.6', 'chain-ip4-2', 4)
  242. self.obj.create_chain('2000::1', 'chain-ip6-1', 6)
  243. self.obj.create_chain('2000::2', 'chain-ip6-2', 6)
  244. # forget about commands called earlier
  245. self.obj.called_commands[4] = []
  246. self.obj.called_commands[6] = []
  247. self.obj.cleanup()
  248. self.assertEqual([self.obj.called_commands[4][0]] +
  249. sorted(self.obj.called_commands[4][1:], key=operator.itemgetter(1)),
  250. [['-F', 'QBS-FORWARD'],
  251. ['-F', 'chain-ip4-1'],
  252. ['-X', 'chain-ip4-1'],
  253. ['-F', 'chain-ip4-2'],
  254. ['-X', 'chain-ip4-2']])
  255. self.assertEqual([self.obj.called_commands[6][0]] +
  256. sorted(self.obj.called_commands[6][1:], key=operator.itemgetter(1)),
  257. [['-F', 'QBS-FORWARD'],
  258. ['-F', 'chain-ip6-1'],
  259. ['-X', 'chain-ip6-1'],
  260. ['-F', 'chain-ip6-2'],
  261. ['-X', 'chain-ip6-2']])
  262. class TestNftablesWorker(TestCase):
  263. def setUp(self):
  264. super(TestNftablesWorker, self).setUp()
  265. self.obj = NftablesWorker()
  266. self.subprocess_patch = patch('subprocess.call')
  267. self.subprocess_mock = self.subprocess_patch.start()
  268. def tearDown(self):
  269. self.subprocess_patch.stop()
  270. def test_000_chain_for_addr(self):
  271. self.assertEqual(
  272. self.obj.chain_for_addr('10.137.0.1'), 'qbs-10-137-0-1')
  273. self.assertEqual(
  274. self.obj.chain_for_addr('fd09:24ef:4179:0000::3'),
  275. 'qbs-fd09-24ef-4179-0000--3')
  276. def expected_create_chain(self, family, addr, chain):
  277. return (
  278. 'table {family} qubes-firewall {{\n'
  279. ' chain {chain} {{\n'
  280. ' }}\n'
  281. ' chain forward {{\n'
  282. ' {family} saddr {addr} jump {chain}\n'
  283. ' }}\n'
  284. '}}\n'.format(family=family, addr=addr, chain=chain))
  285. def test_001_create_chain(self):
  286. testdata = [
  287. (4, '10.137.0.1', 'qbs-10-137-0-1'),
  288. (6, 'fd09:24ef:4179:0000::3', 'qbs-fd09-24ef-4179-0000--3')
  289. ]
  290. for family, addr, chain in testdata:
  291. self.obj.create_chain(addr, chain, family)
  292. self.assertEqual(self.obj.loaded_rules,
  293. [self.expected_create_chain('ip', '10.137.0.1', 'qbs-10-137-0-1'),
  294. self.expected_create_chain(
  295. 'ip6', 'fd09:24ef:4179:0000::3', 'qbs-fd09-24ef-4179-0000--3'),
  296. ])
  297. def test_002_prepare_rules4(self):
  298. rules = [
  299. {'action': 'accept', 'proto': 'tcp',
  300. 'dstports': '80-80', 'dst4': '1.2.3.0/24'},
  301. {'action': 'accept', 'proto': 'udp',
  302. 'dstports': '443-1024', 'dsthost': 'yum.qubes-os.org'},
  303. {'action': 'accept', 'specialtarget': 'dns'},
  304. {'action': 'drop', 'proto': 'udp', 'specialtarget': 'dns'},
  305. {'action': 'drop', 'proto': 'icmp'},
  306. {'action': 'drop'},
  307. ]
  308. expected_nft = (
  309. 'flush chain ip qubes-firewall chain\n'
  310. 'table ip qubes-firewall {\n'
  311. ' chain chain {\n'
  312. ' ip protocol tcp ip daddr 1.2.3.0/24 tcp dport 80 accept\n'
  313. ' ip protocol udp ip daddr { 82.94.215.165/32 } '
  314. 'udp dport 443-1024 accept\n'
  315. ' ip daddr { 1.1.1.1/32, 2.2.2.2/32 } tcp dport 53 accept\n'
  316. ' ip daddr { 1.1.1.1/32, 2.2.2.2/32 } udp dport 53 accept\n'
  317. ' ip protocol udp ip daddr { 1.1.1.1/32, 2.2.2.2/32 } udp dport '
  318. '53 drop\n'
  319. ' ip protocol icmp drop\n'
  320. ' drop\n'
  321. ' }\n'
  322. '}\n'
  323. )
  324. self.assertEqual(self.obj.prepare_rules('chain', rules, 4),
  325. expected_nft)
  326. with self.assertRaises(qubesagent.firewall.RuleParseError):
  327. self.obj.prepare_rules('chain', [{'unknown': 'xxx'}], 4)
  328. with self.assertRaises(qubesagent.firewall.RuleParseError):
  329. self.obj.prepare_rules('chain', [{'dst6': 'a::b'}], 4)
  330. with self.assertRaises(qubesagent.firewall.RuleParseError):
  331. self.obj.prepare_rules('chain', [{'dst4': '3.3.3.3'}], 6)
  332. def test_003_prepare_rules6(self):
  333. rules = [
  334. {'action': 'accept', 'proto': 'tcp',
  335. 'dstports': '80-80', 'dst6': 'a::b/128'},
  336. {'action': 'accept', 'proto': 'tcp',
  337. 'dsthost': 'ripe.net'},
  338. {'action': 'accept', 'specialtarget': 'dns'},
  339. {'action': 'drop', 'proto': 'udp', 'specialtarget': 'dns'},
  340. {'action': 'drop', 'proto': 'icmp', 'icmptype': '128'},
  341. {'action': 'drop'},
  342. ]
  343. expected_nft = (
  344. 'flush chain ip6 qubes-firewall chain\n'
  345. 'table ip6 qubes-firewall {\n'
  346. ' chain chain {\n'
  347. ' ip6 nexthdr tcp ip6 daddr a::b/128 tcp dport 80 accept\n'
  348. ' ip6 nexthdr tcp ip6 daddr { 2001:67c:2e8:22::c100:68b/128 } '
  349. 'accept\n'
  350. ' ip6 daddr { 2001::1/128, 2001::2/128 } tcp dport 53 accept\n'
  351. ' ip6 daddr { 2001::1/128, 2001::2/128 } udp dport 53 accept\n'
  352. ' ip6 nexthdr udp ip6 daddr { 2001::1/128, 2001::2/128 } '
  353. 'udp dport 53 drop\n'
  354. ' ip6 nexthdr icmpv6 icmpv6 type 128 drop\n'
  355. ' drop\n'
  356. ' }\n'
  357. '}\n'
  358. )
  359. self.assertEqual(self.obj.prepare_rules('chain', rules, 6),
  360. expected_nft)
  361. def test_004_apply_rules4(self):
  362. rules = [{'action': 'accept'}]
  363. chain = 'qbs-10-137-0-1'
  364. self.obj.apply_rules('10.137.0.1', rules)
  365. self.assertEqual(self.obj.loaded_rules,
  366. [self.expected_create_chain('ip', '10.137.0.1', chain),
  367. self.obj.prepare_rules(chain, rules, 4),
  368. ])
  369. def test_005_apply_rules6(self):
  370. rules = [{'action': 'accept'}]
  371. chain = 'qbs-2000--a'
  372. self.obj.apply_rules('2000::a', rules)
  373. self.assertEqual(self.obj.loaded_rules,
  374. [self.expected_create_chain('ip6', '2000::a', chain),
  375. self.obj.prepare_rules(chain, rules, 6),
  376. ])
  377. def test_006_init(self):
  378. self.obj.init()
  379. self.assertEqual(self.obj.loaded_rules,
  380. [
  381. 'table ip qubes-firewall {\n'
  382. ' chain forward {\n'
  383. ' type filter hook forward priority 0;\n'
  384. ' policy drop;\n'
  385. ' ct state established,related accept\n'
  386. ' }\n'
  387. '}\n'
  388. 'table ip6 qubes-firewall {\n'
  389. ' chain forward {\n'
  390. ' type filter hook forward priority 0;\n'
  391. ' policy drop;\n'
  392. ' ct state established,related accept\n'
  393. ' }\n'
  394. '}\n'
  395. ])
  396. def test_007_cleanup(self):
  397. self.obj.init()
  398. self.obj.create_chain('1.2.3.4', 'chain-ip4-1', 4)
  399. self.obj.create_chain('1.2.3.6', 'chain-ip4-2', 4)
  400. self.obj.create_chain('2000::1', 'chain-ip6-1', 6)
  401. self.obj.create_chain('2000::2', 'chain-ip6-2', 6)
  402. # forget about commands called earlier
  403. self.obj.loaded_rules = []
  404. self.obj.cleanup()
  405. self.assertEqual(self.obj.loaded_rules,
  406. ['delete table ip qubes-firewall\n'
  407. 'delete table ip6 qubes-firewall\n',
  408. ])
  409. class TestFirewallWorker(TestCase):
  410. def setUp(self):
  411. self.obj = FirewallWorker()
  412. rules = {
  413. '10.137.0.1': {
  414. 'policy': 'accept',
  415. '0000': 'proto=tcp dstports=80-80 action=drop',
  416. '0001': 'proto=udp specialtarget=dns action=accept',
  417. '0002': 'proto=udp action=drop',
  418. },
  419. '10.137.0.2': {'policy': 'accept'},
  420. # no policy
  421. '10.137.0.3': {'0000': 'proto=tcp action=accept'},
  422. # no action
  423. '10.137.0.4': {
  424. 'policy': 'drop',
  425. '0000': 'proto=tcp'
  426. },
  427. }
  428. for addr, entries in rules.items():
  429. for key, value in entries.items():
  430. self.obj.qdb.entries[
  431. '/qubes-firewall/{}/{}'.format(addr, key)] = value
  432. self.subprocess_patch = patch('subprocess.call')
  433. self.subprocess_mock = self.subprocess_patch.start()
  434. def tearDown(self):
  435. self.subprocess_patch.stop()
  436. def test_read_rules(self):
  437. expected_rules1 = [
  438. {'proto': 'tcp', 'dstports': '80-80', 'action': 'drop'},
  439. {'proto': 'udp', 'specialtarget': 'dns', 'action': 'accept'},
  440. {'proto': 'udp', 'action': 'drop'},
  441. {'action': 'accept'},
  442. ]
  443. expected_rules2 = [
  444. {'action': 'accept'},
  445. ]
  446. self.assertEqual(self.obj.read_rules('10.137.0.1'), expected_rules1)
  447. self.assertEqual(self.obj.read_rules('10.137.0.2'), expected_rules2)
  448. with self.assertRaises(qubesagent.firewall.RuleParseError):
  449. self.obj.read_rules('10.137.0.3')
  450. with self.assertRaises(qubesagent.firewall.RuleParseError):
  451. self.obj.read_rules('10.137.0.4')
  452. def test_list_targets(self):
  453. self.assertEqual(self.obj.list_targets(), set(['10.137.0.{}'.format(x)
  454. for x in range(1, 5)]))
  455. def test_is_ip6(self):
  456. self.assertTrue(self.obj.is_ip6('2000::abcd'))
  457. self.assertTrue(self.obj.is_ip6('2000:1:2:3:4:5:6:abcd'))
  458. self.assertFalse(self.obj.is_ip6('10.137.0.1'))
  459. def test_handle_addr(self):
  460. self.obj.handle_addr('10.137.0.2')
  461. self.assertEqual(self.obj.rules['10.137.0.2'], [{'action': 'accept'}])
  462. # fallback to block all
  463. self.obj.handle_addr('10.137.0.3')
  464. self.assertEqual(self.obj.rules['10.137.0.3'], [{'action': 'drop'}])
  465. self.obj.handle_addr('10.137.0.4')
  466. self.assertEqual(self.obj.rules['10.137.0.4'], [{'action': 'drop'}])
  467. @patch('os.path.isfile')
  468. @patch('os.access')
  469. @patch('subprocess.call')
  470. def test_run_user_script(self, mock_subprocess, mock_os_access,
  471. mock_os_path_isfile):
  472. mock_os_path_isfile.return_value = False
  473. mock_os_access.return_value = False
  474. super(FirewallWorker, self.obj).run_user_script()
  475. self.assertFalse(mock_subprocess.called)
  476. mock_os_path_isfile.return_value = True
  477. mock_os_access.return_value = False
  478. super(FirewallWorker, self.obj).run_user_script()
  479. self.assertFalse(mock_subprocess.called)
  480. mock_os_path_isfile.return_value = True
  481. mock_os_access.return_value = True
  482. super(FirewallWorker, self.obj).run_user_script()
  483. mock_subprocess.assert_called_once_with(
  484. ['/rw/config/qubes-firewall-user-script'])
  485. def test_main(self):
  486. self.obj.main()
  487. self.assertTrue(self.obj.init_called)
  488. self.assertTrue(self.obj.cleanup_called)
  489. self.assertTrue(self.obj.user_script_called)
  490. self.assertEqual(set(self.obj.rules.keys()), self.obj.list_targets())
  491. # rules content were already tested