qvm_firewall.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Marek Marczykowski-Górecki
  5. # <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 qubes.firewall
  22. import qubes.tests
  23. import qubes.tests.tools
  24. import qubes.tools.qvm_firewall
  25. import qubes.vm.appvm
  26. class TC_10_ArgParser(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
  27. list_header = ['NO', 'ACTION', 'HOST', 'PROTOCOL', 'PORT(S)',
  28. 'SPECIAL TARGET', 'ICMP TYPE']
  29. def setUp(self):
  30. super(TC_10_ArgParser, self).setUp()
  31. self.init_default_template()
  32. self.vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, None,
  33. name=self.make_vm_name('vm'), label='red')
  34. self.vm.create_on_disk()
  35. self.app.save()
  36. def test_000_list(self):
  37. with qubes.tests.tools.StdoutBuffer() as stdout:
  38. qubes.tools.qvm_firewall.main([self.vm.name, 'list'])
  39. self.assertEqual(stdout.getvalue(),
  40. ' '.join(self.list_header) + '\n')
  41. def test_001_list(self):
  42. self.vm.firewall.rules.append(
  43. qubes.firewall.Rule(action='accept', dsthost='127.0.0.2',
  44. proto='tcp', dstports=80))
  45. self.vm.firewall.rules.append(
  46. qubes.firewall.Rule(action='accept', dsthost='127.0.0.3',
  47. proto='icmp', icmptype=8))
  48. self.vm.firewall.rules.append(
  49. qubes.firewall.Rule(action='accept', specialtarget='dns'))
  50. self.vm.firewall.save()
  51. expected_output = (
  52. 'NO ACTION HOST PROTOCOL PORT(S) SPECIAL TARGET ICMP '
  53. 'TYPE\n'
  54. '0 accept 127.0.0.2/32 tcp 80 '
  55. ' \n'
  56. '1 accept 127.0.0.3/32 icmp 8 '
  57. ' \n'
  58. '2 accept dns '
  59. ' \n'
  60. )
  61. with qubes.tests.tools.StdoutBuffer() as stdout:
  62. qubes.tools.qvm_firewall.main([self.vm.name, 'list'])
  63. self.assertEqual(
  64. '\n'.join(l.rstrip() for l in stdout.getvalue().splitlines()),
  65. '\n'.join(l.rstrip() for l in expected_output.splitlines()))
  66. def test_002_list_raw(self):
  67. self.vm.firewall.rules = [
  68. qubes.firewall.Rule(action='accept', dsthost='127.0.0.2',
  69. proto='tcp', dstports=80),
  70. qubes.firewall.Rule(action='accept', dsthost='127.0.0.3',
  71. proto='icmp', icmptype=8),
  72. qubes.firewall.Rule(action='accept', specialtarget='dns'),
  73. ]
  74. self.vm.firewall.save()
  75. expected_output = '\n'.join(rule.rule for rule in
  76. self.vm.firewall.rules) + '\n'
  77. with qubes.tests.tools.StdoutBuffer() as stdout:
  78. qubes.tools.qvm_firewall.main(['--raw', self.vm.name, 'list'])
  79. self.assertEqual(stdout.getvalue(), expected_output)
  80. def test_010_add(self):
  81. qubes.tools.qvm_firewall.main(
  82. [self.vm.name, 'add', 'accept', '1.2.3.0/24', 'tcp', '443'])
  83. self.assertEqual(self.vm.firewall.rules,
  84. [qubes.firewall.Rule(action='accept', dsthost='1.2.3.0/24',
  85. proto='tcp', dstports='443')])
  86. def test_011_add_before(self):
  87. self.vm.firewall.rules = [
  88. qubes.firewall.Rule(action='accept', dsthost='1.2.3.1'),
  89. qubes.firewall.Rule(action='accept', dsthost='1.2.3.2'),
  90. qubes.firewall.Rule(action='accept', dsthost='1.2.3.3'),
  91. ]
  92. self.vm.firewall.save()
  93. qubes.tools.qvm_firewall.main(
  94. [self.vm.name, 'add', '--before', '2',
  95. 'accept', '1.2.3.0/24', 'tcp', '443'])
  96. self.vm.firewall.load()
  97. self.assertEqual(self.vm.firewall.rules,
  98. [qubes.firewall.Rule(action='accept', dsthost='1.2.3.1'),
  99. qubes.firewall.Rule(action='accept', dsthost='1.2.3.2'),
  100. qubes.firewall.Rule(action='accept', dsthost='1.2.3.0/24',
  101. proto='tcp', dstports='443'),
  102. qubes.firewall.Rule(action='accept', dsthost='1.2.3.3'),
  103. ])
  104. def test_020_del(self):
  105. self.vm.firewall.rules = [
  106. qubes.firewall.Rule(action='accept', dsthost='1.2.3.1'),
  107. qubes.firewall.Rule(action='accept', dsthost='1.2.3.2'),
  108. qubes.firewall.Rule(action='accept', dsthost='1.2.3.3'),
  109. ]
  110. self.vm.firewall.save()
  111. qubes.tools.qvm_firewall.main(
  112. [self.vm.name, 'del', 'accept', '1.2.3.2'])
  113. self.vm.firewall.load()
  114. self.assertEqual(self.vm.firewall.rules,
  115. [qubes.firewall.Rule(action='accept', dsthost='1.2.3.1'),
  116. qubes.firewall.Rule(action='accept', dsthost='1.2.3.3'),
  117. ])
  118. def test_021_del_by_number(self):
  119. self.vm.firewall.rules = [
  120. qubes.firewall.Rule(action='accept', dsthost='1.2.3.1'),
  121. qubes.firewall.Rule(action='accept', dsthost='1.2.3.2'),
  122. qubes.firewall.Rule(action='accept', dsthost='1.2.3.3'),
  123. ]
  124. self.vm.firewall.save()
  125. qubes.tools.qvm_firewall.main(
  126. [self.vm.name, 'del', '--rule-no', '1'])
  127. self.vm.firewall.load()
  128. self.assertEqual(self.vm.firewall.rules,
  129. [qubes.firewall.Rule(action='accept', dsthost='1.2.3.1'),
  130. qubes.firewall.Rule(action='accept', dsthost='1.2.3.3'),
  131. ])
  132. def test_030_policy(self):
  133. with qubes.tests.tools.StdoutBuffer() as stdout:
  134. qubes.tools.qvm_firewall.main([self.vm.name, 'policy'])
  135. self.assertEqual(stdout.getvalue(), 'accept\n')
  136. self.vm.firewall.policy = 'drop'
  137. self.vm.firewall.save()
  138. with qubes.tests.tools.StdoutBuffer() as stdout:
  139. qubes.tools.qvm_firewall.main([self.vm.name, 'policy'])
  140. self.assertEqual(stdout.getvalue(), 'drop\n')
  141. def test_031_policy_set(self):
  142. qubes.tools.qvm_firewall.main([self.vm.name, 'policy', 'drop'])
  143. self.assertEqual(self.vm.firewall.policy, 'drop')
  144. qubes.tools.qvm_firewall.main([self.vm.name, 'policy', 'accept'])
  145. self.vm.firewall.load()
  146. self.assertEqual(self.vm.firewall.policy, 'accept')