2017-03-20 22:54:03 +01:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2017-03-20 22:54:03 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2017-03-20 22:54:03 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2017-03-20 22:54:03 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2017-03-20 22:54:03 +01:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import unittest.mock
|
|
|
|
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import qubes.tests
|
|
|
|
import qubespolicy
|
|
|
|
|
|
|
|
tmp_policy_dir = '/tmp/policy'
|
|
|
|
|
|
|
|
system_info = {
|
|
|
|
'domains': {
|
|
|
|
'dom0': {
|
2017-06-27 02:36:42 +02:00
|
|
|
'tags': ['dom0-tag'],
|
2017-03-20 22:54:03 +01:00
|
|
|
'type': 'AdminVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-vm1': {
|
|
|
|
'tags': ['tag1', 'tag2'],
|
|
|
|
'type': 'AppVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-vm2': {
|
|
|
|
'tags': ['tag2'],
|
|
|
|
'type': 'AppVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-vm3': {
|
2017-09-02 20:12:37 +02:00
|
|
|
'tags': ['tag3'],
|
2017-03-20 22:54:03 +01:00
|
|
|
'type': 'AppVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': True,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'default-dvm': {
|
|
|
|
'tags': [],
|
|
|
|
'type': 'AppVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': True,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-invalid-dvm': {
|
|
|
|
'tags': ['tag1', 'tag2'],
|
|
|
|
'type': 'AppVM',
|
|
|
|
'default_dispvm': 'test-vm1',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-no-dvm': {
|
|
|
|
'tags': ['tag1', 'tag2'],
|
|
|
|
'type': 'AppVM',
|
|
|
|
'default_dispvm': None,
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-template': {
|
|
|
|
'tags': ['tag1', 'tag2'],
|
|
|
|
'type': 'TemplateVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
'test-standalone': {
|
|
|
|
'tags': ['tag1', 'tag2'],
|
|
|
|
'type': 'StandaloneVM',
|
|
|
|
'default_dispvm': 'default-dvm',
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms': False,
|
2017-03-20 22:54:03 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class TC_00_PolicyRule(qubes.tests.QubesTestCase):
|
|
|
|
def test_000_verify_target_value(self):
|
|
|
|
self.assertTrue(
|
|
|
|
qubespolicy.verify_target_value(system_info, 'test-vm1'))
|
|
|
|
self.assertTrue(
|
|
|
|
qubespolicy.verify_target_value(system_info, 'default-dvm'))
|
|
|
|
self.assertTrue(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@dispvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertTrue(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@dispvm:default-dvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertTrue(
|
|
|
|
qubespolicy.verify_target_value(system_info, 'test-template'))
|
|
|
|
self.assertTrue(
|
|
|
|
qubespolicy.verify_target_value(system_info, 'test-standalone'))
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertTrue(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@adminvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(
|
|
|
|
qubespolicy.verify_target_value(system_info, 'no-such-vm'))
|
|
|
|
self.assertFalse(
|
|
|
|
qubespolicy.verify_target_value(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:test-invalid-dvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@dispvm:test-vm1'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(
|
|
|
|
qubespolicy.verify_target_value(system_info, ''))
|
|
|
|
self.assertFalse(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@default'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@anyvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@tag:tag1'))
|
2017-09-02 20:12:37 +02:00
|
|
|
self.assertFalse(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@dispvm:@tag:tag1'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(
|
2018-02-16 05:17:20 +01:00
|
|
|
qubespolicy.verify_target_value(system_info, '@invalid'))
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_010_verify_special_value(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(qubespolicy.verify_special_value('@tag:tag',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(qubespolicy.verify_special_value('@tag:other-tag',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(qubespolicy.verify_special_value('@type:AppVM',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(qubespolicy.verify_special_value('@adminvm',
|
2017-06-27 02:36:42 +02:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(qubespolicy.verify_special_value('@dispvm:some-vm',
|
2017-09-02 20:12:37 +02:00
|
|
|
for_target=True))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(qubespolicy.verify_special_value('@dispvm:@tag:tag1',
|
2017-09-02 20:12:37 +02:00
|
|
|
for_target=True))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@default',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@dispvm',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@dispvm:some-vm',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@dispvm:@tag:tag1',
|
2017-09-02 20:12:37 +02:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@invalid',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
|
|
|
self.assertFalse(qubespolicy.verify_special_value('vm-name',
|
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@tag:',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(qubespolicy.verify_special_value('@type:',
|
2017-03-20 22:54:03 +01:00
|
|
|
for_target=False))
|
|
|
|
|
|
|
|
def test_020_line_simple(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
line = qubespolicy.PolicyRule('@anyvm @anyvm ask', 'filename', 12)
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(line.filename, 'filename')
|
|
|
|
self.assertEqual(line.lineno, 12)
|
|
|
|
self.assertEqual(line.action, qubespolicy.Action.ask)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(line.source, '@anyvm')
|
|
|
|
self.assertEqual(line.target, '@anyvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(line.full_action, 'ask')
|
|
|
|
self.assertIsNone(line.override_target)
|
|
|
|
self.assertIsNone(line.override_user)
|
|
|
|
self.assertIsNone(line.default_target)
|
|
|
|
|
|
|
|
def test_021_line_simple(self):
|
2017-06-27 02:01:46 +02:00
|
|
|
# also check spaces in action field
|
2017-03-20 22:54:03 +01:00
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@tag:tag1 @type:AppVM ask, target=test-vm2, user=user',
|
2017-03-20 22:54:03 +01:00
|
|
|
'filename', 12)
|
|
|
|
self.assertEqual(line.filename, 'filename')
|
|
|
|
self.assertEqual(line.lineno, 12)
|
|
|
|
self.assertEqual(line.action, qubespolicy.Action.ask)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(line.source, '@tag:tag1')
|
|
|
|
self.assertEqual(line.target, '@type:AppVM')
|
2017-06-27 02:01:46 +02:00
|
|
|
self.assertEqual(line.full_action, 'ask, target=test-vm2, user=user')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(line.override_target, 'test-vm2')
|
|
|
|
self.assertEqual(line.override_user, 'user')
|
|
|
|
self.assertIsNone(line.default_target)
|
|
|
|
|
|
|
|
def test_022_line_simple(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @default allow,target=@dispvm:test-vm2',
|
2017-03-20 22:54:03 +01:00
|
|
|
'filename', 12)
|
|
|
|
self.assertEqual(line.filename, 'filename')
|
|
|
|
self.assertEqual(line.lineno, 12)
|
|
|
|
self.assertEqual(line.action, qubespolicy.Action.allow)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(line.source, '@anyvm')
|
|
|
|
self.assertEqual(line.target, '@default')
|
|
|
|
self.assertEqual(line.full_action, 'allow,target=@dispvm:test-vm2')
|
|
|
|
self.assertEqual(line.override_target, '@dispvm:test-vm2')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertIsNone(line.override_user)
|
|
|
|
self.assertIsNone(line.default_target)
|
|
|
|
|
|
|
|
def test_023_line_simple(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @default ask,default_target=test-vm1',
|
2017-03-20 22:54:03 +01:00
|
|
|
'filename', 12)
|
|
|
|
self.assertEqual(line.filename, 'filename')
|
|
|
|
self.assertEqual(line.lineno, 12)
|
|
|
|
self.assertEqual(line.action, qubespolicy.Action.ask)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(line.source, '@anyvm')
|
|
|
|
self.assertEqual(line.target, '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(line.full_action, 'ask,default_target=test-vm1')
|
|
|
|
self.assertIsNone(line.override_target)
|
|
|
|
self.assertIsNone(line.override_user)
|
|
|
|
self.assertEqual(line.default_target, 'test-vm1')
|
|
|
|
|
2017-06-27 02:36:42 +02:00
|
|
|
def test_024_line_simple(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @adminvm ask,default_target=@adminvm',
|
2017-06-27 02:36:42 +02:00
|
|
|
'filename', 12)
|
|
|
|
self.assertEqual(line.filename, 'filename')
|
|
|
|
self.assertEqual(line.lineno, 12)
|
|
|
|
self.assertEqual(line.action, qubespolicy.Action.ask)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(line.source, '@anyvm')
|
|
|
|
self.assertEqual(line.target, '@adminvm')
|
|
|
|
self.assertEqual(line.full_action, 'ask,default_target=@adminvm')
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertIsNone(line.override_target)
|
|
|
|
self.assertIsNone(line.override_user)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(line.default_target, '@adminvm')
|
2017-06-27 02:36:42 +02:00
|
|
|
|
2017-03-20 22:54:03 +01:00
|
|
|
def test_030_line_invalid(self):
|
|
|
|
invalid_lines = [
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm @default allow', # @dispvm can't be a source
|
|
|
|
'@default @default allow', # @default can't be a source
|
|
|
|
'@anyvm @default allow,target=@dispvm:@tag:tag1', # @dispvm:@tag
|
2017-09-02 20:12:37 +02:00
|
|
|
# as override target
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @default allow,target=@tag:tag1', # @tag as override target
|
|
|
|
'@anyvm @default deny,target=test-vm1', # target= used with deny
|
|
|
|
'@anyvm @anyvm deny,default_target=test-vm1', # default_target=
|
2017-03-20 22:54:03 +01:00
|
|
|
# with deny
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm deny,user=user', # user= with deny
|
|
|
|
'@anyvm @anyvm invalid', # invalid action
|
|
|
|
'@anyvm @anyvm allow,invalid=xx', # invalid option
|
|
|
|
'@anyvm @anyvm', # missing action
|
|
|
|
'@anyvm @anyvm allow,default_target=test-vm1', # default_target=
|
2017-03-20 22:54:03 +01:00
|
|
|
# with allow
|
2018-02-16 05:17:20 +01:00
|
|
|
'@invalid @anyvm allow', # invalid source
|
|
|
|
'@anyvm @invalid deny', # invalid target
|
2017-03-20 22:54:03 +01:00
|
|
|
'', # empty line
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow extra', # trailing words
|
|
|
|
'@anyvm @default allow', # @default allow without target=
|
2017-03-20 22:54:03 +01:00
|
|
|
]
|
|
|
|
for line in invalid_lines:
|
|
|
|
with self.subTest(line):
|
|
|
|
with self.assertRaises(qubespolicy.PolicySyntaxError):
|
|
|
|
qubespolicy.PolicyRule(line, 'filename', 12)
|
|
|
|
|
|
|
|
def test_040_match_single(self):
|
|
|
|
is_match_single = qubespolicy.PolicyRule.is_match_single
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', 'test-vm1'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', '@default'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@default', '@default'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@tag:tag1', 'test-vm1'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@type:AppVM', 'test-vm1'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertTrue(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@type:TemplateVM', 'test-template'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', '@dispvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertTrue(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm', '@dispvm:default-dvm'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@dispvm', '@dispvm'))
|
2017-09-02 20:12:37 +02:00
|
|
|
self.assertTrue(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:@tag:tag3', '@dispvm:test-vm3'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@adminvm', '@adminvm'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@adminvm', 'dom0'))
|
|
|
|
self.assertTrue(is_match_single(system_info, 'dom0', '@adminvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertTrue(is_match_single(system_info, 'dom0', 'dom0'))
|
|
|
|
self.assertTrue(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:default-dvm', '@dispvm:default-dvm'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', '@dispvm'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', 'test-vm1'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', 'test-vm1'))
|
|
|
|
self.assertTrue(is_match_single(system_info, '@anyvm', 'test-vm1'))
|
|
|
|
|
|
|
|
self.assertFalse(is_match_single(system_info, '@default', 'test-vm1'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@tag:tag1', 'test-vm3'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', 'no-such-vm'))
|
2017-09-03 03:11:48 +02:00
|
|
|
# test-vm1.template_for_dispvms=False
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm', '@dispvm:test-vm1'))
|
2017-09-03 03:11:48 +02:00
|
|
|
# test-vm1.template_for_dispvms=False
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:test-vm1', '@dispvm:test-vm1'))
|
2017-09-02 20:12:37 +02:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:@tag:tag1', '@dispvm:test-vm1'))
|
2017-09-02 20:12:37 +02:00
|
|
|
# test-vm3 has not tag1
|
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:@tag:tag1', '@dispvm:test-vm3'))
|
2017-09-02 20:12:37 +02:00
|
|
|
# default-dvm has no tag3
|
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:@tag:tag3', '@dispvm:default-dvm'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', 'dom0'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', '@adminvm'))
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@tag:dom0-tag', '@adminvm'))
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@type:AdminVM', '@adminvm'))
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@tag:dom0-tag', 'dom0'))
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@type:AdminVM', 'dom0'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@tag:tag1', 'dom0'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', '@tag:tag1'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', '@type:AppVM'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', '@invalid'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@invalid', '@invalid'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', 'no-such-vm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
|
|
|
'no-such-vm', 'no-such-vm'))
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertFalse(is_match_single(system_info, '@dispvm', 'test-vm1'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@dispvm', 'default-dvm'))
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(is_match_single(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:default-dvm', 'default-dvm'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', 'test-vm1\n'))
|
|
|
|
self.assertFalse(is_match_single(system_info, '@anyvm', 'test-vm1 '))
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_050_match(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
line = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertTrue(line.is_match(system_info, 'test-vm1', 'test-vm2'))
|
2018-02-16 05:17:20 +01:00
|
|
|
line = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(line.is_match(system_info, 'no-such-vm', 'test-vm2'))
|
2018-02-16 05:17:20 +01:00
|
|
|
line = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertFalse(line.is_match(system_info, 'test-vm1', 'no-such-vm'))
|
2018-02-16 05:17:20 +01:00
|
|
|
line = qubespolicy.PolicyRule('@anyvm @dispvm allow')
|
|
|
|
self.assertTrue(line.is_match(system_info, 'test-vm1', '@dispvm'))
|
|
|
|
line = qubespolicy.PolicyRule('@anyvm @dispvm allow')
|
2017-09-02 20:12:37 +02:00
|
|
|
self.assertFalse(line.is_match(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'test-vm1', '@dispvm:default-dvm'))
|
|
|
|
line = qubespolicy.PolicyRule('@anyvm @dispvm:default-dvm allow')
|
|
|
|
self.assertTrue(line.is_match(system_info, 'test-vm1', '@dispvm'))
|
|
|
|
line = qubespolicy.PolicyRule('@anyvm @dispvm:default-dvm allow')
|
2017-09-02 20:12:37 +02:00
|
|
|
self.assertTrue(line.is_match(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'test-vm1', '@dispvm:default-dvm'))
|
|
|
|
line = qubespolicy.PolicyRule('@anyvm @dispvm:@tag:tag3 allow')
|
2017-09-02 20:12:37 +02:00
|
|
|
self.assertTrue(line.is_match(system_info,
|
2018-02-16 05:17:20 +01:00
|
|
|
'test-vm1', '@dispvm:test-vm3'))
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_060_expand_target(self):
|
|
|
|
lines = {
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow': ['test-vm1', 'test-vm2', 'test-vm3',
|
|
|
|
'@dispvm:test-vm3',
|
|
|
|
'default-dvm', '@dispvm:default-dvm', 'test-invalid-dvm',
|
|
|
|
'test-no-dvm', 'test-template', 'test-standalone', '@dispvm'],
|
|
|
|
'@anyvm @dispvm allow': ['@dispvm'],
|
|
|
|
'@anyvm @dispvm:default-dvm allow': ['@dispvm:default-dvm'],
|
2017-03-20 22:54:03 +01:00
|
|
|
# no DispVM from test-vm1 allowed
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @dispvm:test-vm1 allow': [],
|
|
|
|
'@anyvm @dispvm:test-vm3 allow': ['@dispvm:test-vm3'],
|
|
|
|
'@anyvm @dispvm:@tag:tag1 allow': [],
|
|
|
|
'@anyvm @dispvm:@tag:tag3 allow': ['@dispvm:test-vm3'],
|
|
|
|
'@anyvm test-vm1 allow': ['test-vm1'],
|
|
|
|
'@anyvm @type:AppVM allow': ['test-vm1', 'test-vm2', 'test-vm3',
|
2017-03-20 22:54:03 +01:00
|
|
|
'default-dvm', 'test-invalid-dvm', 'test-no-dvm'],
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @type:TemplateVM allow': ['test-template'],
|
|
|
|
'@anyvm @tag:tag1 allow': ['test-vm1', 'test-invalid-dvm',
|
2017-03-20 22:54:03 +01:00
|
|
|
'test-template', 'test-standalone', 'test-no-dvm'],
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @tag:tag2 allow': ['test-vm1', 'test-vm2',
|
2017-03-20 22:54:03 +01:00
|
|
|
'test-invalid-dvm', 'test-template', 'test-standalone',
|
|
|
|
'test-no-dvm'],
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @tag:no-such-tag allow': [],
|
2017-03-20 22:54:03 +01:00
|
|
|
}
|
|
|
|
for line in lines:
|
|
|
|
with self.subTest(line):
|
|
|
|
policy_line = qubespolicy.PolicyRule(line)
|
|
|
|
self.assertCountEqual(list(policy_line.expand_target(system_info)),
|
|
|
|
lines[line])
|
|
|
|
|
|
|
|
def test_070_expand_override_target(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow,target=test-vm2')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
line.expand_override_target(system_info, 'test-vm1'),
|
|
|
|
'test-vm2')
|
|
|
|
|
|
|
|
def test_071_expand_override_target_dispvm(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow,target=@dispvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
line.expand_override_target(system_info, 'test-vm1'),
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:default-dvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_072_expand_override_target_dispvm_specific(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow,target=@dispvm:test-vm3')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
line.expand_override_target(system_info, 'test-vm1'),
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:test-vm3')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_073_expand_override_target_dispvm_none(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow,target=@dispvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
line.expand_override_target(system_info, 'test-no-dvm'),
|
|
|
|
None)
|
|
|
|
|
|
|
|
def test_074_expand_override_target_dom0(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow,target=dom0')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
line.expand_override_target(system_info, 'test-no-dvm'),
|
|
|
|
'dom0')
|
|
|
|
|
2017-06-27 02:36:42 +02:00
|
|
|
def test_075_expand_override_target_dom0(self):
|
|
|
|
line = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm allow,target=@adminvm')
|
2017-06-27 02:36:42 +02:00
|
|
|
self.assertEqual(
|
|
|
|
line.expand_override_target(system_info, 'test-no-dvm'),
|
2018-02-16 05:17:20 +01:00
|
|
|
'@adminvm')
|
2017-06-27 02:36:42 +02:00
|
|
|
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
class TC_10_PolicyAction(qubes.tests.QubesTestCase):
|
|
|
|
def test_000_init(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm deny')
|
2017-03-20 22:54:03 +01:00
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
|
|
|
qubespolicy.PolicyAction('test.service', 'test-vm1', 'test-vm2',
|
|
|
|
rule, 'test-vm2')
|
|
|
|
|
|
|
|
def test_001_init(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm ask')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule, 'test-vm2', ['test-vm2', 'test-vm3'])
|
|
|
|
self.assertEqual(action.service, 'test.service')
|
|
|
|
self.assertEqual(action.source, 'test-vm1')
|
|
|
|
self.assertIsNone(action.target)
|
|
|
|
self.assertEqual(action.original_target, 'test-vm2')
|
|
|
|
self.assertEqual(action.targets_for_ask, ['test-vm2', 'test-vm3'])
|
|
|
|
self.assertEqual(action.rule, rule)
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.ask)
|
|
|
|
|
|
|
|
def test_002_init_invalid(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule_ask = qubespolicy.PolicyRule('@anyvm @anyvm ask')
|
|
|
|
rule_allow = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule_allow, 'test-vm2', None)
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'test-vm2', rule_allow, 'test-vm2', ['test-vm2', 'test-vm3'])
|
|
|
|
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule_ask, 'test-vm2', None)
|
|
|
|
|
|
|
|
def test_003_init_default_target(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule_ask = qubespolicy.PolicyRule('@anyvm @anyvm ask')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'test-vm1', rule_ask, 'test-vm2', ['test-vm2'])
|
|
|
|
self.assertIsNone(action.target)
|
|
|
|
|
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'test-vm2', rule_ask, 'test-vm2', ['test-vm2'])
|
|
|
|
self.assertEqual(action.target, 'test-vm2')
|
|
|
|
|
|
|
|
def test_010_handle_user_response(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm ask')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule, 'test-vm2', ['test-vm2', 'test-vm3'])
|
|
|
|
action.handle_user_response(True, 'test-vm2')
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.allow)
|
|
|
|
self.assertEqual(action.target, 'test-vm2')
|
|
|
|
|
|
|
|
def test_011_handle_user_response(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm ask')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule, 'test-vm2', ['test-vm2', 'test-vm3'])
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
action.handle_user_response(True, 'test-no-dvm')
|
|
|
|
|
|
|
|
def test_012_handle_user_response(self):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm ask')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule, 'test-vm2', ['test-vm2', 'test-vm3'])
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
|
|
|
action.handle_user_response(False, None)
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.deny)
|
|
|
|
|
2017-07-30 14:48:06 +02:00
|
|
|
def test_013_handle_user_response_with_default_target(self):
|
|
|
|
rule = qubespolicy.PolicyRule(
|
2018-02-16 05:17:20 +01:00
|
|
|
'@anyvm @anyvm ask,default_target=test-vm2')
|
2017-07-30 14:48:06 +02:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
None, rule, 'test-vm2', ['test-vm2', 'test-vm3'])
|
|
|
|
action.handle_user_response(True, 'test-vm2')
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.allow)
|
|
|
|
self.assertEqual(action.target, 'test-vm2')
|
|
|
|
|
2017-03-20 22:54:03 +01:00
|
|
|
@unittest.mock.patch('qubespolicy.qubesd_call')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_020_execute(self, mock_subprocess, mock_qubesd_call):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'test-vm2', rule, 'test-vm2')
|
|
|
|
action.execute('some-ident')
|
|
|
|
self.assertEqual(mock_qubesd_call.mock_calls,
|
2017-08-06 12:35:35 +02:00
|
|
|
[unittest.mock.call('test-vm2', 'admin.vm.Start')])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(mock_subprocess.mock_calls,
|
|
|
|
[unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'test-vm2',
|
|
|
|
'-c', 'some-ident', 'DEFAULT:QUBESRPC test.service test-vm1'])])
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubespolicy.qubesd_call')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_021_execute_dom0(self, mock_subprocess, mock_qubesd_call):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm dom0 allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'dom0', rule, 'dom0')
|
|
|
|
action.execute('some-ident')
|
2017-08-06 12:35:35 +02:00
|
|
|
self.assertEqual(mock_qubesd_call.mock_calls, [])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(mock_subprocess.mock_calls,
|
|
|
|
[unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'dom0',
|
|
|
|
'-c', 'some-ident',
|
2018-02-16 04:36:42 +01:00
|
|
|
'QUBESRPC test.service test-vm1 name dom0'])])
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubespolicy.qubesd_call')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_021_execute_dom0_keyword(self, mock_subprocess, mock_qubesd_call):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm dom0 allow')
|
2018-02-16 04:36:42 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
2018-02-16 05:17:20 +01:00
|
|
|
'dom0', rule, '@adminvm')
|
2018-02-16 04:36:42 +01:00
|
|
|
action.execute('some-ident')
|
|
|
|
self.assertEqual(mock_qubesd_call.mock_calls, [])
|
|
|
|
self.assertEqual(mock_subprocess.mock_calls,
|
|
|
|
[unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'dom0',
|
|
|
|
'-c', 'some-ident',
|
|
|
|
'QUBESRPC test.service test-vm1 keyword adminvm'])])
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
@unittest.mock.patch('qubespolicy.qubesd_call')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_022_execute_dispvm(self, mock_subprocess, mock_qubesd_call):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @dispvm:default-dvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:default-dvm', rule, '@dispvm:default-dvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
mock_qubesd_call.side_effect = (lambda target, call:
|
2017-08-06 12:35:35 +02:00
|
|
|
b'dispvm-name' if call == 'admin.vm.CreateDisposable' else
|
2017-03-20 22:54:03 +01:00
|
|
|
unittest.mock.DEFAULT)
|
|
|
|
action.execute('some-ident')
|
|
|
|
self.assertEqual(mock_qubesd_call.mock_calls,
|
2017-08-06 12:35:35 +02:00
|
|
|
[unittest.mock.call('default-dvm', 'admin.vm.CreateDisposable'),
|
|
|
|
unittest.mock.call('dispvm-name', 'admin.vm.Start'),
|
|
|
|
unittest.mock.call('dispvm-name', 'admin.vm.Kill')])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(mock_subprocess.mock_calls,
|
|
|
|
[unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'dispvm-name',
|
|
|
|
'-c', 'some-ident', '-W',
|
|
|
|
'DEFAULT:QUBESRPC test.service test-vm1'])])
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubespolicy.qubesd_call')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_023_execute_already_running(self, mock_subprocess,
|
|
|
|
mock_qubesd_call):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'test-vm2', rule, 'test-vm2')
|
|
|
|
mock_qubesd_call.side_effect = \
|
|
|
|
qubespolicy.QubesMgmtException('QubesVMNotHaltedError')
|
|
|
|
action.execute('some-ident')
|
|
|
|
self.assertEqual(mock_qubesd_call.mock_calls,
|
2017-08-06 12:35:35 +02:00
|
|
|
[unittest.mock.call('test-vm2', 'admin.vm.Start')])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(mock_subprocess.mock_calls,
|
|
|
|
[unittest.mock.call([qubespolicy.QREXEC_CLIENT, '-d', 'test-vm2',
|
|
|
|
'-c', 'some-ident', 'DEFAULT:QUBESRPC test.service test-vm1'])])
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubespolicy.qubesd_call')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_024_execute_startup_error(self, mock_subprocess,
|
|
|
|
mock_qubesd_call):
|
2018-02-16 05:17:20 +01:00
|
|
|
rule = qubespolicy.PolicyRule('@anyvm @anyvm allow')
|
2017-03-20 22:54:03 +01:00
|
|
|
action = qubespolicy.PolicyAction('test.service', 'test-vm1',
|
|
|
|
'test-vm2', rule, 'test-vm2')
|
|
|
|
mock_qubesd_call.side_effect = \
|
|
|
|
qubespolicy.QubesMgmtException('QubesVMError')
|
|
|
|
with self.assertRaises(qubespolicy.QubesMgmtException):
|
|
|
|
action.execute('some-ident')
|
|
|
|
self.assertEqual(mock_qubesd_call.mock_calls,
|
2017-08-06 12:35:35 +02:00
|
|
|
[unittest.mock.call('test-vm2', 'admin.vm.Start')])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(mock_subprocess.mock_calls, [])
|
|
|
|
|
|
|
|
class TC_20_Policy(qubes.tests.QubesTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TC_20_Policy, self).setUp()
|
|
|
|
if not os.path.exists(tmp_policy_dir):
|
|
|
|
os.mkdir(tmp_policy_dir)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(tmp_policy_dir)
|
|
|
|
super(TC_20_Policy, self).tearDown()
|
|
|
|
|
|
|
|
def test_000_load(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
|
|
|
f.write('\n')
|
|
|
|
f.write('# comment\n')
|
|
|
|
f.write('test-vm2 test-vm3 ask\n')
|
|
|
|
f.write(' # comment \n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('@anyvm @anyvm ask\n')
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.service, 'test.service')
|
|
|
|
self.assertEqual(len(policy.policy_rules), 3)
|
|
|
|
self.assertEqual(policy.policy_rules[0].source, 'test-vm1')
|
|
|
|
self.assertEqual(policy.policy_rules[0].target, 'test-vm2')
|
|
|
|
self.assertEqual(policy.policy_rules[0].action,
|
|
|
|
qubespolicy.Action.allow)
|
|
|
|
|
|
|
|
def test_001_not_existent(self):
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
2017-06-27 05:38:54 +02:00
|
|
|
qubespolicy.Policy('no-such.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_002_include(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('@include:test.service2\n')
|
|
|
|
f.write('@anyvm @anyvm deny\n')
|
2017-03-20 22:54:03 +01:00
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service2'), 'w') as f:
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm3 @default allow,target=test-vm2\n')
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.service, 'test.service')
|
|
|
|
self.assertEqual(len(policy.policy_rules), 3)
|
|
|
|
self.assertEqual(policy.policy_rules[0].source, 'test-vm1')
|
|
|
|
self.assertEqual(policy.policy_rules[0].target, 'test-vm2')
|
|
|
|
self.assertEqual(policy.policy_rules[0].action,
|
|
|
|
qubespolicy.Action.allow)
|
|
|
|
self.assertEqual(policy.policy_rules[0].filename,
|
|
|
|
tmp_policy_dir + '/test.service')
|
|
|
|
self.assertEqual(policy.policy_rules[0].lineno, 1)
|
|
|
|
self.assertEqual(policy.policy_rules[1].source, 'test-vm3')
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(policy.policy_rules[1].target, '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.policy_rules[1].action,
|
|
|
|
qubespolicy.Action.allow)
|
|
|
|
self.assertEqual(policy.policy_rules[1].filename,
|
|
|
|
tmp_policy_dir + '/test.service2')
|
|
|
|
self.assertEqual(policy.policy_rules[1].lineno, 1)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(policy.policy_rules[2].source, '@anyvm')
|
|
|
|
self.assertEqual(policy.policy_rules[2].target, '@anyvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.policy_rules[2].action,
|
|
|
|
qubespolicy.Action.deny)
|
|
|
|
self.assertEqual(policy.policy_rules[2].filename,
|
|
|
|
tmp_policy_dir + '/test.service')
|
|
|
|
self.assertEqual(policy.policy_rules[2].lineno, 3)
|
|
|
|
|
2018-02-16 05:17:20 +01:00
|
|
|
def test_003_load_convert(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm2 test-vm3 ask\n')
|
|
|
|
f.write(' # comment \n')
|
|
|
|
f.write('$anyvm $dispvm ask,default_target=$dispvm\n')
|
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
|
|
|
self.assertEqual(policy.service, 'test.service')
|
|
|
|
self.assertEqual(len(policy.policy_rules), 2)
|
|
|
|
self.assertEqual(policy.policy_rules[1].source, '@anyvm')
|
|
|
|
self.assertEqual(policy.policy_rules[1].target, '@dispvm')
|
|
|
|
self.assertEqual(policy.policy_rules[1].action,
|
|
|
|
qubespolicy.Action.ask)
|
|
|
|
self.assertEqual(policy.policy_rules[1].default_target,
|
|
|
|
'@dispvm')
|
|
|
|
|
2017-03-20 22:54:03 +01:00
|
|
|
def test_010_find_rule(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm1 @anyvm ask\n')
|
|
|
|
f.write('test-vm2 @tag:tag1 deny\n')
|
|
|
|
f.write('test-vm2 @tag:tag2 allow\n')
|
|
|
|
f.write('test-vm2 @dispvm:@tag:tag3 allow\n')
|
|
|
|
f.write('test-vm2 @dispvm:@tag:tag2 allow\n')
|
|
|
|
f.write('test-vm2 @dispvm:default-dvm allow\n')
|
|
|
|
f.write('@type:AppVM @default allow,target=test-vm3\n')
|
|
|
|
f.write('@tag:tag1 @type:AppVM allow\n')
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.find_matching_rule(
|
|
|
|
system_info, 'test-vm1', 'test-vm2'), policy.policy_rules[0])
|
|
|
|
self.assertEqual(policy.find_matching_rule(
|
|
|
|
system_info, 'test-vm1', 'test-vm3'), policy.policy_rules[1])
|
|
|
|
self.assertEqual(policy.find_matching_rule(
|
|
|
|
system_info, 'test-vm2', 'test-vm2'), policy.policy_rules[3])
|
|
|
|
self.assertEqual(policy.find_matching_rule(
|
|
|
|
system_info, 'test-vm2', 'test-no-dvm'), policy.policy_rules[2])
|
2018-02-16 05:17:20 +01:00
|
|
|
# @anyvm matches @default too
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.find_matching_rule(
|
2018-03-03 03:35:57 +01:00
|
|
|
system_info, 'test-vm1', '@default'), policy.policy_rules[1])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.find_matching_rule(
|
2018-02-16 05:17:20 +01:00
|
|
|
system_info, 'test-vm2', '@default'), policy.policy_rules[7])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(policy.find_matching_rule(
|
2017-09-02 20:12:37 +02:00
|
|
|
system_info, 'test-no-dvm', 'test-vm3'), policy.policy_rules[8])
|
|
|
|
self.assertEqual(policy.find_matching_rule(
|
2018-02-16 05:17:20 +01:00
|
|
|
system_info, 'test-vm2', '@dispvm:test-vm3'),
|
2017-09-02 20:12:37 +02:00
|
|
|
policy.policy_rules[4])
|
|
|
|
self.assertEqual(policy.find_matching_rule(
|
2018-02-16 05:17:20 +01:00
|
|
|
system_info, 'test-vm2', '@dispvm'),
|
2017-09-02 20:12:37 +02:00
|
|
|
policy.policy_rules[6])
|
2017-03-20 22:54:03 +01:00
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
|
|
|
policy.find_matching_rule(
|
|
|
|
system_info, 'test-no-dvm', 'test-standalone')
|
2017-09-02 20:12:37 +02:00
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.find_matching_rule(system_info, 'test-no-dvm', '@dispvm')
|
2017-03-20 22:54:03 +01:00
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
|
|
|
policy.find_matching_rule(
|
2018-02-16 05:17:20 +01:00
|
|
|
system_info, 'test-standalone', '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_020_collect_targets_for_ask(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm1 @anyvm ask\n')
|
|
|
|
f.write('test-vm2 @tag:tag1 deny\n')
|
|
|
|
f.write('test-vm2 @tag:tag2 allow\n')
|
|
|
|
f.write('test-no-dvm @type:AppVM deny\n')
|
|
|
|
f.write('@type:AppVM @default allow,target=test-vm3\n')
|
|
|
|
f.write('@tag:tag1 @type:AppVM allow\n')
|
|
|
|
f.write('test-no-dvm @dispvm allow\n')
|
|
|
|
f.write('test-standalone @dispvm allow\n')
|
|
|
|
f.write('test-standalone @adminvm allow\n')
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertCountEqual(policy.collect_targets_for_ask(system_info,
|
|
|
|
'test-vm1'), ['test-vm1', 'test-vm2', 'test-vm3',
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:test-vm3',
|
|
|
|
'default-dvm', '@dispvm:default-dvm', 'test-invalid-dvm',
|
2017-03-20 22:54:03 +01:00
|
|
|
'test-no-dvm', 'test-template', 'test-standalone'])
|
|
|
|
self.assertCountEqual(policy.collect_targets_for_ask(system_info,
|
|
|
|
'test-vm2'), ['test-vm2', 'test-vm3'])
|
|
|
|
self.assertCountEqual(policy.collect_targets_for_ask(system_info,
|
|
|
|
'test-vm3'), ['test-vm3'])
|
|
|
|
self.assertCountEqual(policy.collect_targets_for_ask(system_info,
|
|
|
|
'test-standalone'), ['test-vm1', 'test-vm2', 'test-vm3',
|
|
|
|
'default-dvm', 'test-no-dvm', 'test-invalid-dvm',
|
2018-02-16 05:17:20 +01:00
|
|
|
'@dispvm:default-dvm', 'dom0'])
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertCountEqual(policy.collect_targets_for_ask(system_info,
|
|
|
|
'test-no-dvm'), [])
|
|
|
|
|
|
|
|
def test_030_eval_simple(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
|
|
|
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
action = policy.evaluate(system_info, 'test-vm1', 'test-vm2')
|
|
|
|
self.assertEqual(action.rule, policy.policy_rules[0])
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.allow)
|
|
|
|
self.assertEqual(action.target, 'test-vm2')
|
|
|
|
self.assertEqual(action.original_target, 'test-vm2')
|
|
|
|
self.assertEqual(action.service, 'test.service')
|
|
|
|
self.assertIsNone(action.targets_for_ask)
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.evaluate(system_info, 'test-vm2', '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_031_eval_default(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm1 @default allow,target=test-vm2\n')
|
|
|
|
f.write('@tag:tag1 test-vm2 ask\n')
|
|
|
|
f.write('@tag:tag2 @anyvm allow\n')
|
|
|
|
f.write('test-vm3 @anyvm deny\n')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2018-02-16 05:17:20 +01:00
|
|
|
action = policy.evaluate(system_info, 'test-vm1', '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(action.rule, policy.policy_rules[1])
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.allow)
|
|
|
|
self.assertEqual(action.target, 'test-vm2')
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(action.original_target, '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(action.service, 'test.service')
|
|
|
|
self.assertIsNone(action.targets_for_ask)
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
|
|
|
# action allow should hit, but no target specified (either by
|
|
|
|
# caller or policy)
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.evaluate(system_info, 'test-standalone', '@default')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
def test_032_eval_ask(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm1 @default allow,target=test-vm2\n')
|
|
|
|
f.write('@tag:tag1 test-vm2 ask\n')
|
|
|
|
f.write('@tag:tag1 test-vm3 ask,default_target=test-vm3\n')
|
|
|
|
f.write('@tag:tag2 @anyvm allow\n')
|
|
|
|
f.write('test-vm3 @anyvm deny\n')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
action = policy.evaluate(system_info, 'test-standalone', 'test-vm2')
|
|
|
|
self.assertEqual(action.rule, policy.policy_rules[2])
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.ask)
|
|
|
|
self.assertIsNone(action.target)
|
|
|
|
self.assertEqual(action.original_target, 'test-vm2')
|
|
|
|
self.assertEqual(action.service, 'test.service')
|
|
|
|
self.assertCountEqual(action.targets_for_ask,
|
2018-02-16 05:17:20 +01:00
|
|
|
['test-vm1', 'test-vm2', 'test-vm3', '@dispvm:test-vm3',
|
|
|
|
'default-dvm', '@dispvm:default-dvm', 'test-invalid-dvm',
|
2017-03-20 22:54:03 +01:00
|
|
|
'test-no-dvm', 'test-template', 'test-standalone'])
|
|
|
|
|
|
|
|
def test_033_eval_ask(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
|
|
|
f.write('test-vm1 test-vm2 allow\n')
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm1 @default allow,target=test-vm2\n')
|
|
|
|
f.write('@tag:tag1 test-vm2 ask\n')
|
|
|
|
f.write('@tag:tag1 test-vm3 ask,default_target=test-vm3\n')
|
|
|
|
f.write('@tag:tag2 @anyvm allow\n')
|
|
|
|
f.write('test-vm3 @anyvm deny\n')
|
2017-03-20 22:54:03 +01:00
|
|
|
|
2017-06-27 05:38:54 +02:00
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2017-03-20 22:54:03 +01:00
|
|
|
action = policy.evaluate(system_info, 'test-standalone', 'test-vm3')
|
|
|
|
self.assertEqual(action.rule, policy.policy_rules[3])
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.ask)
|
|
|
|
self.assertEqual(action.target, 'test-vm3')
|
|
|
|
self.assertEqual(action.original_target, 'test-vm3')
|
|
|
|
self.assertEqual(action.service, 'test.service')
|
|
|
|
self.assertCountEqual(action.targets_for_ask,
|
2018-02-16 05:17:20 +01:00
|
|
|
['test-vm1', 'test-vm2', 'test-vm3', '@dispvm:test-vm3',
|
|
|
|
'default-dvm', '@dispvm:default-dvm', 'test-invalid-dvm',
|
2017-03-20 22:54:03 +01:00
|
|
|
'test-no-dvm', 'test-template', 'test-standalone'])
|
|
|
|
|
2017-06-27 05:30:40 +02:00
|
|
|
def test_034_eval_resolve_dispvm(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm3 @dispvm allow\n')
|
2017-06-27 05:30:40 +02:00
|
|
|
|
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
2018-02-16 05:17:20 +01:00
|
|
|
action = policy.evaluate(system_info, 'test-vm3', '@dispvm')
|
2017-06-27 05:30:40 +02:00
|
|
|
self.assertEqual(action.rule, policy.policy_rules[0])
|
|
|
|
self.assertEqual(action.action, qubespolicy.Action.allow)
|
2018-02-16 05:17:20 +01:00
|
|
|
self.assertEqual(action.target, '@dispvm:default-dvm')
|
|
|
|
self.assertEqual(action.original_target, '@dispvm')
|
2017-06-27 05:30:40 +02:00
|
|
|
self.assertEqual(action.service, 'test.service')
|
|
|
|
self.assertIsNone(action.targets_for_ask)
|
|
|
|
|
2017-09-02 20:12:37 +02:00
|
|
|
def test_035_eval_resolve_dispvm_fail(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-no-dvm @dispvm allow\n')
|
2017-09-02 20:12:37 +02:00
|
|
|
|
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.evaluate(system_info, 'test-no-dvm', '@dispvm')
|
2017-09-02 20:12:37 +02:00
|
|
|
|
|
|
|
def test_036_eval_invalid_override_target(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm3 @anyvm allow,target=no-such-vm\n')
|
2017-09-02 20:12:37 +02:00
|
|
|
|
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.evaluate(system_info, 'test-vm3', '@default')
|
2017-09-02 20:12:37 +02:00
|
|
|
|
|
|
|
def test_037_eval_ask_no_targets(self):
|
|
|
|
with open(os.path.join(tmp_policy_dir, 'test.service'), 'w') as f:
|
2018-02-16 05:17:20 +01:00
|
|
|
f.write('test-vm3 @default ask\n')
|
2017-09-02 20:12:37 +02:00
|
|
|
|
|
|
|
policy = qubespolicy.Policy('test.service', tmp_policy_dir)
|
|
|
|
with self.assertRaises(qubespolicy.AccessDenied):
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.evaluate(system_info, 'test-vm3', '@default')
|
2017-09-02 20:12:37 +02:00
|
|
|
|
2017-03-20 22:54:03 +01:00
|
|
|
|
|
|
|
class TC_30_Misc(qubes.tests.QubesTestCase):
|
|
|
|
@unittest.mock.patch('socket.socket')
|
|
|
|
def test_000_qubesd_call(self, mock_socket):
|
|
|
|
mock_config = {
|
|
|
|
'return_value.makefile.return_value.read.return_value': b'0\x00data'
|
|
|
|
}
|
|
|
|
mock_socket.configure_mock(**mock_config)
|
2017-08-06 12:35:35 +02:00
|
|
|
result = qubespolicy.qubesd_call('test', 'internal.method')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(result, b'data')
|
|
|
|
self.assertEqual(mock_socket.mock_calls, [
|
|
|
|
unittest.mock.call(socket.AF_UNIX, socket.SOCK_STREAM),
|
|
|
|
unittest.mock.call().connect(qubespolicy.QUBESD_INTERNAL_SOCK),
|
|
|
|
unittest.mock.call().sendall(b'dom0'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
2017-08-06 12:35:35 +02:00
|
|
|
unittest.mock.call().sendall(b'internal.method'),
|
2017-03-20 22:54:03 +01:00
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'test'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().shutdown(socket.SHUT_WR),
|
|
|
|
unittest.mock.call().makefile('rb'),
|
|
|
|
unittest.mock.call().makefile().read(),
|
|
|
|
])
|
|
|
|
|
|
|
|
@unittest.mock.patch('socket.socket')
|
|
|
|
def test_001_qubesd_call_arg_payload(self, mock_socket):
|
|
|
|
mock_config = {
|
|
|
|
'return_value.makefile.return_value.read.return_value': b'0\x00data'
|
|
|
|
}
|
|
|
|
mock_socket.configure_mock(**mock_config)
|
2017-08-06 12:35:35 +02:00
|
|
|
result = qubespolicy.qubesd_call('test', 'internal.method', 'arg',
|
|
|
|
b'payload')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(result, b'data')
|
|
|
|
self.assertEqual(mock_socket.mock_calls, [
|
|
|
|
unittest.mock.call(socket.AF_UNIX, socket.SOCK_STREAM),
|
|
|
|
unittest.mock.call().connect(qubespolicy.QUBESD_INTERNAL_SOCK),
|
|
|
|
unittest.mock.call().sendall(b'dom0'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
2017-08-06 12:35:35 +02:00
|
|
|
unittest.mock.call().sendall(b'internal.method'),
|
2017-03-20 22:54:03 +01:00
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'test'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'arg'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'payload'),
|
|
|
|
unittest.mock.call().shutdown(socket.SHUT_WR),
|
|
|
|
unittest.mock.call().makefile('rb'),
|
|
|
|
unittest.mock.call().makefile().read(),
|
|
|
|
])
|
|
|
|
|
|
|
|
@unittest.mock.patch('socket.socket')
|
|
|
|
def test_002_qubesd_call_exception(self, mock_socket):
|
|
|
|
mock_config = {
|
|
|
|
'return_value.makefile.return_value.read.return_value':
|
|
|
|
b'2\x00SomeError\x00traceback\x00message\x00'
|
|
|
|
}
|
|
|
|
mock_socket.configure_mock(**mock_config)
|
|
|
|
with self.assertRaises(qubespolicy.QubesMgmtException) as e:
|
2017-08-06 12:35:35 +02:00
|
|
|
qubespolicy.qubesd_call('test', 'internal.method')
|
2017-03-20 22:54:03 +01:00
|
|
|
self.assertEqual(e.exception.exc_type, 'SomeError')
|
|
|
|
self.assertEqual(mock_socket.mock_calls, [
|
|
|
|
unittest.mock.call(socket.AF_UNIX, socket.SOCK_STREAM),
|
|
|
|
unittest.mock.call().connect(qubespolicy.QUBESD_INTERNAL_SOCK),
|
|
|
|
unittest.mock.call().sendall(b'dom0'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
2017-08-06 12:35:35 +02:00
|
|
|
unittest.mock.call().sendall(b'internal.method'),
|
2017-03-20 22:54:03 +01:00
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'test'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().sendall(b'\x00'),
|
|
|
|
unittest.mock.call().shutdown(socket.SHUT_WR),
|
|
|
|
unittest.mock.call().makefile('rb'),
|
|
|
|
unittest.mock.call().makefile().read(),
|
|
|
|
])
|
|
|
|
|