firewall.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2016
  5. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import datetime
  22. import os
  23. import lxml.etree
  24. import unittest
  25. import qubes.firewall
  26. import qubes.tests
  27. class TestOption(qubes.firewall.RuleChoice):
  28. opt1 = 'opt1'
  29. opt2 = 'opt2'
  30. another = 'another'
  31. class TestVMM(object):
  32. def __init__(self):
  33. self.offline_mode = True
  34. class TestApp(object):
  35. def __init__(self):
  36. self.vmm = TestVMM()
  37. class TestVM(object):
  38. def __init__(self):
  39. self.firewall_conf = 'test-firewall.xml'
  40. self.dir_path = '/tmp'
  41. self.app = TestApp()
  42. def fire_event(self, event):
  43. pass
  44. # noinspection PyPep8Naming
  45. class TC_00_RuleChoice(qubes.tests.QubesTestCase):
  46. def test_000_accept_allowed(self):
  47. with self.assertNotRaises(ValueError):
  48. TestOption('opt1')
  49. TestOption('opt2')
  50. TestOption('another')
  51. def test_001_value_list(self):
  52. instance = TestOption('opt1')
  53. self.assertEqual(
  54. set(instance.allowed_values), {'opt1', 'opt2', 'another'})
  55. def test_010_reject_others(self):
  56. self.assertRaises(ValueError, lambda: TestOption('invalid'))
  57. class TC_01_Action(qubes.tests.QubesTestCase):
  58. def test_000_allowed_values(self):
  59. with self.assertNotRaises(ValueError):
  60. instance = qubes.firewall.Action('accept')
  61. self.assertEqual(
  62. set(instance.allowed_values), {'accept', 'drop'})
  63. def test_001_rule(self):
  64. instance = qubes.firewall.Action('accept')
  65. self.assertEqual(instance.rule, 'action=accept')
  66. # noinspection PyPep8Naming
  67. class TC_02_Proto(qubes.tests.QubesTestCase):
  68. def test_000_allowed_values(self):
  69. with self.assertNotRaises(ValueError):
  70. instance = qubes.firewall.Proto('tcp')
  71. self.assertEqual(
  72. set(instance.allowed_values), {'tcp', 'udp', 'icmp'})
  73. def test_001_rule(self):
  74. instance = qubes.firewall.Proto('tcp')
  75. self.assertEqual(instance.rule, 'proto=tcp')
  76. # noinspection PyPep8Naming
  77. class TC_02_DstHost(qubes.tests.QubesTestCase):
  78. def test_000_hostname(self):
  79. with self.assertNotRaises(ValueError):
  80. instance = qubes.firewall.DstHost('qubes-os.org')
  81. self.assertEqual(instance.type, 'dsthost')
  82. def test_001_ipv4(self):
  83. with self.assertNotRaises(ValueError):
  84. instance = qubes.firewall.DstHost('127.0.0.1')
  85. self.assertEqual(instance.type, 'dst4')
  86. self.assertEqual(instance.prefixlen, 32)
  87. self.assertEqual(str(instance), '127.0.0.1/32')
  88. self.assertEqual(instance.rule, 'dst4=127.0.0.1/32')
  89. def test_002_ipv4_prefixlen(self):
  90. with self.assertNotRaises(ValueError):
  91. instance = qubes.firewall.DstHost('127.0.0.0', 8)
  92. self.assertEqual(instance.type, 'dst4')
  93. self.assertEqual(instance.prefixlen, 8)
  94. self.assertEqual(str(instance), '127.0.0.0/8')
  95. self.assertEqual(instance.rule, 'dst4=127.0.0.0/8')
  96. def test_003_ipv4_parse_prefixlen(self):
  97. with self.assertNotRaises(ValueError):
  98. instance = qubes.firewall.DstHost('127.0.0.0/8')
  99. self.assertEqual(instance.type, 'dst4')
  100. self.assertEqual(instance.prefixlen, 8)
  101. self.assertEqual(str(instance), '127.0.0.0/8')
  102. self.assertEqual(instance.rule, 'dst4=127.0.0.0/8')
  103. def test_004_ipv4_invalid_prefix(self):
  104. with self.assertRaises(ValueError):
  105. qubes.firewall.DstHost('127.0.0.0/33')
  106. with self.assertRaises(ValueError):
  107. qubes.firewall.DstHost('127.0.0.0', 33)
  108. with self.assertRaises(ValueError):
  109. qubes.firewall.DstHost('127.0.0.0/-1')
  110. def test_005_ipv4_reject_shortened(self):
  111. # not strictly required, but ppl are used to it
  112. with self.assertRaises(ValueError):
  113. qubes.firewall.DstHost('127/8')
  114. def test_006_ipv4_invalid_addr(self):
  115. with self.assertRaises(ValueError):
  116. qubes.firewall.DstHost('137.327.0.0/16')
  117. with self.assertRaises(ValueError):
  118. qubes.firewall.DstHost('1.2.3.4.5/32')
  119. @unittest.expectedFailure
  120. def test_007_ipv4_invalid_network(self):
  121. with self.assertRaises(ValueError):
  122. qubes.firewall.DstHost('127.0.0.1/32')
  123. def test_010_ipv6(self):
  124. with self.assertNotRaises(ValueError):
  125. instance = qubes.firewall.DstHost('2001:abcd:efab::3')
  126. self.assertEqual(instance.type, 'dst6')
  127. self.assertEqual(instance.prefixlen, 128)
  128. self.assertEqual(str(instance), '2001:abcd:efab::3/128')
  129. self.assertEqual(instance.rule, 'dst6=2001:abcd:efab::3/128')
  130. def test_011_ipv6_prefixlen(self):
  131. with self.assertNotRaises(ValueError):
  132. instance = qubes.firewall.DstHost('2001:abcd:efab::', 64)
  133. self.assertEqual(instance.type, 'dst6')
  134. self.assertEqual(instance.prefixlen, 64)
  135. self.assertEqual(str(instance), '2001:abcd:efab::/64')
  136. self.assertEqual(instance.rule, 'dst6=2001:abcd:efab::/64')
  137. def test_012_ipv6_parse_prefixlen(self):
  138. with self.assertNotRaises(ValueError):
  139. instance = qubes.firewall.DstHost('2001:abcd:efab::/64')
  140. self.assertEqual(instance.type, 'dst6')
  141. self.assertEqual(instance.prefixlen, 64)
  142. self.assertEqual(str(instance), '2001:abcd:efab::/64')
  143. self.assertEqual(instance.rule, 'dst6=2001:abcd:efab::/64')
  144. def test_013_ipv6_invalid_prefix(self):
  145. with self.assertRaises(ValueError):
  146. qubes.firewall.DstHost('2001:abcd:efab::3/129')
  147. with self.assertRaises(ValueError):
  148. qubes.firewall.DstHost('2001:abcd:efab::3', 129)
  149. with self.assertRaises(ValueError):
  150. qubes.firewall.DstHost('2001:abcd:efab::3/-1')
  151. def test_014_ipv6_invalid_addr(self):
  152. with self.assertRaises(ValueError):
  153. qubes.firewall.DstHost('2001:abcd:efab0123::3/128')
  154. with self.assertRaises(ValueError):
  155. qubes.firewall.DstHost('2001:abcd:efab:3/128')
  156. with self.assertRaises(ValueError):
  157. qubes.firewall.DstHost('2001:abcd:efab:a:a:a:a:a:a:3/128')
  158. with self.assertRaises(ValueError):
  159. qubes.firewall.DstHost('2001:abcd:efgh::3/128')
  160. @unittest.expectedFailure
  161. def test_015_ipv6_invalid_network(self):
  162. with self.assertRaises(ValueError):
  163. qubes.firewall.DstHost('2001:abcd:efab::3/64')
  164. @unittest.expectedFailure
  165. def test_020_invalid_hostname(self):
  166. with self.assertRaises(ValueError):
  167. qubes.firewall.DstHost('www qubes-os.org')
  168. with self.assertRaises(ValueError):
  169. qubes.firewall.DstHost('https://qubes-os.org')
  170. class TC_03_DstPorts(qubes.tests.QubesTestCase):
  171. def test_000_single_str(self):
  172. with self.assertNotRaises(ValueError):
  173. instance = qubes.firewall.DstPorts('80')
  174. self.assertEqual(str(instance), '80')
  175. self.assertEqual(instance.range, [80, 80])
  176. self.assertEqual(instance.rule, 'dstports=80-80')
  177. def test_001_single_int(self):
  178. with self.assertNotRaises(ValueError):
  179. instance = qubes.firewall.DstPorts(80)
  180. self.assertEqual(str(instance), '80')
  181. self.assertEqual(instance.range, [80, 80])
  182. self.assertEqual(instance.rule, 'dstports=80-80')
  183. def test_002_range(self):
  184. with self.assertNotRaises(ValueError):
  185. instance = qubes.firewall.DstPorts('80-90')
  186. self.assertEqual(str(instance), '80-90')
  187. self.assertEqual(instance.range, [80, 90])
  188. self.assertEqual(instance.rule, 'dstports=80-90')
  189. def test_003_invalid(self):
  190. with self.assertRaises(ValueError):
  191. qubes.firewall.DstPorts('80-90-100')
  192. with self.assertRaises(ValueError):
  193. qubes.firewall.DstPorts('abcdef')
  194. with self.assertRaises(ValueError):
  195. qubes.firewall.DstPorts('80 90')
  196. with self.assertRaises(ValueError):
  197. qubes.firewall.DstPorts('')
  198. def test_004_reversed_range(self):
  199. with self.assertRaises(ValueError):
  200. qubes.firewall.DstPorts('100-20')
  201. def test_005_out_of_range(self):
  202. with self.assertRaises(ValueError):
  203. qubes.firewall.DstPorts('1000000000000')
  204. with self.assertRaises(ValueError):
  205. qubes.firewall.DstPorts(1000000000000)
  206. with self.assertRaises(ValueError):
  207. qubes.firewall.DstPorts('1-1000000000000')
  208. class TC_04_IcmpType(qubes.tests.QubesTestCase):
  209. def test_000_number(self):
  210. with self.assertNotRaises(ValueError):
  211. instance = qubes.firewall.IcmpType(8)
  212. self.assertEqual(str(instance), '8')
  213. self.assertEqual(instance.rule, 'icmptype=8')
  214. def test_001_str(self):
  215. with self.assertNotRaises(ValueError):
  216. instance = qubes.firewall.IcmpType('8')
  217. self.assertEqual(str(instance), '8')
  218. self.assertEqual(instance.rule, 'icmptype=8')
  219. def test_002_invalid(self):
  220. with self.assertRaises(ValueError):
  221. qubes.firewall.IcmpType(600)
  222. with self.assertRaises(ValueError):
  223. qubes.firewall.IcmpType(-1)
  224. with self.assertRaises(ValueError):
  225. qubes.firewall.IcmpType('abcde')
  226. with self.assertRaises(ValueError):
  227. qubes.firewall.IcmpType('')
  228. class TC_05_SpecialTarget(qubes.tests.QubesTestCase):
  229. def test_000_allowed_values(self):
  230. with self.assertNotRaises(ValueError):
  231. instance = qubes.firewall.SpecialTarget('dns')
  232. self.assertEqual(
  233. set(instance.allowed_values), {'dns'})
  234. def test_001_rule(self):
  235. instance = qubes.firewall.SpecialTarget('dns')
  236. self.assertEqual(instance.rule, 'specialtarget=dns')
  237. class TC_06_Expire(qubes.tests.QubesTestCase):
  238. def test_000_number(self):
  239. with self.assertNotRaises(ValueError):
  240. instance = qubes.firewall.Expire(1463292452)
  241. self.assertEqual(str(instance), '1463292452')
  242. self.assertEqual(instance.datetime,
  243. datetime.datetime(2016, 5, 15, 6, 7, 32))
  244. self.assertIsNone(instance.rule)
  245. def test_001_str(self):
  246. with self.assertNotRaises(ValueError):
  247. instance = qubes.firewall.Expire('1463292452')
  248. self.assertEqual(str(instance), '1463292452')
  249. self.assertEqual(instance.datetime,
  250. datetime.datetime(2016, 5, 15, 6, 7, 32))
  251. self.assertIsNone(instance.rule)
  252. def test_002_invalid(self):
  253. with self.assertRaises(ValueError):
  254. qubes.firewall.Expire('abcdef')
  255. with self.assertRaises(ValueError):
  256. qubes.firewall.Expire('')
  257. def test_003_expired(self):
  258. with self.assertNotRaises(ValueError):
  259. instance = qubes.firewall.Expire('1463292452')
  260. self.assertTrue(instance.expired)
  261. with self.assertNotRaises(ValueError):
  262. instance = qubes.firewall.Expire('1583292452')
  263. self.assertFalse(instance.expired)
  264. class TC_07_Comment(qubes.tests.QubesTestCase):
  265. def test_000_str(self):
  266. with self.assertNotRaises(ValueError):
  267. instance = qubes.firewall.Comment('Some comment')
  268. self.assertEqual(str(instance), 'Some comment')
  269. self.assertIsNone(instance.rule)
  270. class TC_08_Rule(qubes.tests.QubesTestCase):
  271. def test_000_simple(self):
  272. with self.assertNotRaises(ValueError):
  273. rule = qubes.firewall.Rule(None, action='accept', proto='icmp')
  274. self.assertEqual(rule.rule, 'action=accept proto=icmp')
  275. self.assertIsNone(rule.dsthost)
  276. self.assertIsNone(rule.dstports)
  277. self.assertIsNone(rule.icmptype)
  278. self.assertIsNone(rule.comment)
  279. self.assertIsNone(rule.expire)
  280. self.assertEqual(str(rule.action), 'accept')
  281. self.assertEqual(str(rule.proto), 'icmp')
  282. def test_001_expire(self):
  283. with self.assertNotRaises(ValueError):
  284. rule = qubes.firewall.Rule(None, action='accept', proto='icmp',
  285. expire='1463292452')
  286. self.assertIsNone(rule.rule)
  287. with self.assertNotRaises(ValueError):
  288. rule = qubes.firewall.Rule(None, action='accept', proto='icmp',
  289. expire='1663292452')
  290. self.assertIsNotNone(rule.rule)
  291. def test_002_dstports(self):
  292. with self.assertNotRaises(ValueError):
  293. rule = qubes.firewall.Rule(None, action='accept', proto='tcp',
  294. dstports=80)
  295. self.assertEqual(str(rule.dstports), '80')
  296. with self.assertNotRaises(ValueError):
  297. rule = qubes.firewall.Rule(None, action='accept', proto='udp',
  298. dstports=80)
  299. self.assertEqual(str(rule.dstports), '80')
  300. def test_003_reject_invalid(self):
  301. with self.assertRaises((ValueError, AssertionError)):
  302. # missing action
  303. qubes.firewall.Rule(None, proto='icmp')
  304. with self.assertRaises(ValueError):
  305. # not proto=tcp or proto=udp for dstports
  306. qubes.firewall.Rule(None, action='accept', proto='icmp',
  307. dstports=80)
  308. with self.assertRaises(ValueError):
  309. # not proto=tcp or proto=udp for dstports
  310. qubes.firewall.Rule(None, action='accept', dstports=80)
  311. with self.assertRaises(ValueError):
  312. # not proto=icmp for icmptype
  313. qubes.firewall.Rule(None, action='accept', proto='tcp',
  314. icmptype=8)
  315. with self.assertRaises(ValueError):
  316. # not proto=icmp for icmptype
  317. qubes.firewall.Rule(None, action='accept', icmptype=8)
  318. def test_004_proto_change(self):
  319. rule = qubes.firewall.Rule(None, action='accept', proto='tcp')
  320. with self.assertNotRaises(ValueError):
  321. rule.proto = 'udp'
  322. self.assertEqual(rule.rule, 'action=accept proto=udp')
  323. rule = qubes.firewall.Rule(None, action='accept', proto='tcp',
  324. dstports=80)
  325. with self.assertNotRaises(ValueError):
  326. rule.proto = 'udp'
  327. self.assertEqual(rule.rule, 'action=accept proto=udp dstports=80-80')
  328. rule = qubes.firewall.Rule(None, action='accept')
  329. with self.assertNotRaises(ValueError):
  330. rule.proto = 'udp'
  331. self.assertEqual(rule.rule, 'action=accept proto=udp')
  332. with self.assertNotRaises(ValueError):
  333. rule.dstports = 80
  334. self.assertEqual(rule.rule, 'action=accept proto=udp dstports=80-80')
  335. with self.assertNotRaises(ValueError):
  336. rule.proto = 'icmp'
  337. self.assertEqual(rule.rule, 'action=accept proto=icmp')
  338. self.assertIsNone(rule.dstports)
  339. rule.icmptype = 8
  340. self.assertEqual(rule.rule, 'action=accept proto=icmp icmptype=8')
  341. with self.assertNotRaises(ValueError):
  342. rule.proto = qubes.property.DEFAULT
  343. self.assertEqual(rule.rule, 'action=accept')
  344. self.assertIsNone(rule.dstports)
  345. def test_005_from_xml_v1(self):
  346. xml_txt = \
  347. '<rule address="192.168.0.0" proto="tcp" netmask="24" port="443"/>'
  348. with self.assertNotRaises(ValueError):
  349. rule = qubes.firewall.Rule.from_xml_v1(
  350. lxml.etree.fromstring(xml_txt), 'accept')
  351. self.assertEqual(rule.dsthost, '192.168.0.0/24')
  352. self.assertEqual(rule.proto, 'tcp')
  353. self.assertEqual(rule.dstports, '443')
  354. self.assertIsNone(rule.expire)
  355. self.assertIsNone(rule.comment)
  356. def test_006_from_xml_v1(self):
  357. xml_txt = \
  358. '<rule address="qubes-os.org" proto="tcp" ' \
  359. 'port="443" toport="1024"/>'
  360. with self.assertNotRaises(ValueError):
  361. rule = qubes.firewall.Rule.from_xml_v1(
  362. lxml.etree.fromstring(xml_txt), 'drop')
  363. self.assertEqual(rule.dsthost, 'qubes-os.org')
  364. self.assertEqual(rule.proto, 'tcp')
  365. self.assertEqual(rule.dstports, '443-1024')
  366. self.assertEqual(rule.action, 'drop')
  367. self.assertIsNone(rule.expire)
  368. self.assertIsNone(rule.comment)
  369. def test_007_from_xml_v1(self):
  370. xml_txt = \
  371. '<rule address="192.168.0.0" netmask="24" expire="1463292452"/>'
  372. with self.assertNotRaises(ValueError):
  373. rule = qubes.firewall.Rule.from_xml_v1(
  374. lxml.etree.fromstring(xml_txt), 'accept')
  375. self.assertEqual(rule.dsthost, '192.168.0.0/24')
  376. self.assertEqual(rule.expire, '1463292452')
  377. self.assertEqual(rule.action, 'accept')
  378. self.assertIsNone(rule.proto)
  379. self.assertIsNone(rule.dstports)
  380. class TC_10_Firewall(qubes.tests.QubesTestCase):
  381. def setUp(self):
  382. super(TC_10_Firewall, self).setUp()
  383. self.vm = TestVM()
  384. firewall_path = os.path.join('/tmp', self.vm.firewall_conf)
  385. if os.path.exists(firewall_path):
  386. os.unlink(firewall_path)
  387. def tearDown(self):
  388. firewall_path = os.path.join('/tmp', self.vm.firewall_conf)
  389. if os.path.exists(firewall_path):
  390. os.unlink(firewall_path)
  391. return super(TC_10_Firewall, self).tearDown()
  392. def test_000_defaults(self):
  393. fw = qubes.firewall.Firewall(self.vm, False)
  394. fw.load_defaults()
  395. self.assertEqual(fw.policy, 'accept')
  396. self.assertEqual(fw.rules, [])
  397. def test_001_save_load_empty(self):
  398. fw = qubes.firewall.Firewall(self.vm, True)
  399. self.assertEqual(fw.policy, 'accept')
  400. self.assertEqual(fw.rules, [])
  401. fw.save()
  402. fw.load()
  403. self.assertEqual(fw.policy, 'accept')
  404. self.assertEqual(fw.rules, [])
  405. def test_002_save_load_rules(self):
  406. fw = qubes.firewall.Firewall(self.vm, True)
  407. rules = [
  408. qubes.firewall.Rule(None, action='drop', proto='icmp'),
  409. qubes.firewall.Rule(None, action='drop', proto='tcp', dstports=80),
  410. qubes.firewall.Rule(None, action='accept', proto='udp',
  411. dstports=67),
  412. qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
  413. ]
  414. fw.rules.extend(rules)
  415. fw.policy = qubes.firewall.Action.drop
  416. fw.save()
  417. self.assertTrue(os.path.exists(os.path.join(
  418. self.vm.dir_path, self.vm.firewall_conf)))
  419. fw = qubes.firewall.Firewall(TestVM(), True)
  420. self.assertEqual(fw.policy, qubes.firewall.Action.drop)
  421. self.assertEqual(fw.rules, rules)
  422. def test_003_load_v1(self):
  423. xml_txt = """<QubesFirewallRules dns="allow" icmp="allow"
  424. policy="deny" yumProxy="allow">
  425. <rule address="192.168.0.0" proto="tcp" netmask="24" port="80"/>
  426. <rule address="qubes-os.org" proto="tcp" port="443"/>
  427. </QubesFirewallRules>
  428. """
  429. with open(os.path.join('/tmp', self.vm.firewall_conf), 'w') as f:
  430. f.write(xml_txt)
  431. with self.assertNotRaises(ValueError):
  432. fw = qubes.firewall.Firewall(self.vm)
  433. self.assertEqual(str(fw.policy), 'drop')
  434. rules = [
  435. qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
  436. qubes.firewall.Rule(None, action='accept', proto='icmp'),
  437. qubes.firewall.Rule(None, action='accept', proto='tcp',
  438. dsthost='192.168.0.0/24', dstports='80'),
  439. qubes.firewall.Rule(None, action='accept', proto='tcp',
  440. dsthost='qubes-os.org', dstports='443')
  441. ]
  442. self.assertEqual(fw.rules, rules)
  443. def test_004_save_skip_expired(self):
  444. fw = qubes.firewall.Firewall(self.vm, True)
  445. rules = [
  446. qubes.firewall.Rule(None, action='drop', proto='icmp'),
  447. qubes.firewall.Rule(None, action='drop', proto='tcp', dstports=80),
  448. qubes.firewall.Rule(None, action='accept', proto='udp',
  449. dstports=67, expire=1373300257),
  450. qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
  451. ]
  452. fw.rules.extend(rules)
  453. fw.policy = qubes.firewall.Action.drop
  454. fw.save()
  455. rules.pop(2)
  456. fw = qubes.firewall.Firewall(self.vm, True)
  457. self.assertEqual(fw.rules, rules)
  458. def test_005_qdb_entries(self):
  459. fw = qubes.firewall.Firewall(self.vm, True)
  460. rules = [
  461. qubes.firewall.Rule(None, action='drop', proto='icmp'),
  462. qubes.firewall.Rule(None, action='drop', proto='tcp', dstports=80),
  463. qubes.firewall.Rule(None, action='accept', proto='udp'),
  464. qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
  465. ]
  466. fw.rules.extend(rules)
  467. fw.policy = qubes.firewall.Action.drop
  468. expected_qdb_entries = {
  469. 'policy': 'drop',
  470. '0000': 'action=drop proto=icmp',
  471. '0001': 'action=drop proto=tcp dstports=80-80',
  472. '0002': 'action=accept proto=udp',
  473. '0003': 'action=accept specialtarget=dns',
  474. }
  475. self.assertEqual(fw.qdb_entries(), expected_qdb_entries)