2020-01-20 10:38:52 +01:00
|
|
|
# -*- encoding: utf-8 -*-
|
2018-07-19 19:52:22 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program 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.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2019-11-11 12:14:18 +01:00
|
|
|
|
2018-07-19 19:52:22 +02:00
|
|
|
import qubesadmin.tests
|
|
|
|
import qubesadmin.utils
|
|
|
|
|
|
|
|
|
|
|
|
class TestVMUsage(qubesadmin.tests.QubesTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(TestVMUsage, self).setUp()
|
|
|
|
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00vm1 class=AppVM state=Running\n' \
|
|
|
|
b'template1 class=TemplateVM state=Halted\n' \
|
|
|
|
b'template2 class=TemplateVM state=Running\n' \
|
|
|
|
b'vm2 class=AppVM state=Running\n' \
|
|
|
|
b'sys-net class=AppVM state=Running\n' \
|
|
|
|
b'sys-firewall class=AppVM state=Running\n'
|
|
|
|
|
|
|
|
self.global_properties = ['default_dispvm', 'default_netvm',
|
2019-11-17 18:23:15 +01:00
|
|
|
'default_guivm', 'default_audiovm',
|
|
|
|
'default_template', 'clockvm', 'updatevm',
|
|
|
|
'management_dispvm']
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
for prop in self.global_properties:
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.property.Get', prop, None)] = \
|
2019-11-11 12:14:18 +01:00
|
|
|
b'0\x00default=True type=vm vm2'
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
self.vms = ['vm1', 'vm2', 'sys-net', 'sys-firewall',
|
|
|
|
'template1', 'template2']
|
2019-11-11 12:13:59 +01:00
|
|
|
|
2019-11-17 18:23:15 +01:00
|
|
|
self.vm_properties = ['template', 'netvm', 'guivm', 'audiovm',
|
|
|
|
'default_dispvm', 'management_dispvm']
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
for vm in self.vms:
|
|
|
|
for prop in self.vm_properties:
|
|
|
|
if not prop.startswith('template') or \
|
|
|
|
not vm.startswith('template'):
|
|
|
|
self.app.expected_calls[
|
|
|
|
(vm, 'admin.vm.property.Get', prop, None)] = \
|
|
|
|
b'0\x00default=False type=vm template1'
|
|
|
|
else:
|
|
|
|
self.app.expected_calls[
|
|
|
|
(vm, 'admin.vm.property.Get', prop, None)] = \
|
|
|
|
b'2\0QubesNoSuchPropertyError\0\0invalid property\0'
|
|
|
|
|
|
|
|
def test_00_only_global(self):
|
2019-11-11 12:14:18 +01:00
|
|
|
result = qubesadmin.utils.vm_dependencies(self.app,
|
|
|
|
self.app.domains['vm2'])
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
self.assertListEqual(result,
|
|
|
|
[(None, prop) for prop in self.global_properties],
|
|
|
|
"Incorrect global properties listed.")
|
|
|
|
|
|
|
|
def test_01_only_vm(self):
|
2018-07-19 20:06:00 +02:00
|
|
|
result = qubesadmin.utils.vm_dependencies(
|
2018-07-19 19:52:22 +02:00
|
|
|
self.app, self.app.domains['template1'])
|
|
|
|
|
2018-07-19 20:06:00 +02:00
|
|
|
self.assertSetEqual(
|
|
|
|
set(result),
|
|
|
|
set([(vm, prop) for vm in self.vms for prop in self.vm_properties
|
|
|
|
if (not vm.startswith('template')
|
2019-11-11 12:14:18 +01:00
|
|
|
or not prop.startswith(
|
|
|
|
'template')) and vm != 'template1']),
|
2018-07-19 19:52:22 +02:00
|
|
|
"Incorrect VM properties listed.")
|
|
|
|
|
|
|
|
def test_02_empty(self):
|
2019-11-11 12:14:18 +01:00
|
|
|
result = qubesadmin.utils.vm_dependencies(self.app,
|
|
|
|
self.app.domains['vm1'])
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
self.assertListEqual(result, [], "Incorrect use found.")
|
|
|
|
|
|
|
|
def test_03_access_error(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.property.Get', 'default_dispvm', None)] = b''
|
|
|
|
|
2019-11-11 12:14:18 +01:00
|
|
|
result = qubesadmin.utils.vm_dependencies(self.app,
|
|
|
|
self.app.domains['vm1'])
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
self.assertListEqual(result, [], "Incorrect use found.")
|
|
|
|
|
|
|
|
def test_04_defaults(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('vm2', 'admin.vm.property.Get', 'netvm', None)] = \
|
|
|
|
b'0\x00default=True type=vm sys-net'
|
|
|
|
|
|
|
|
self.app.expected_calls[
|
|
|
|
('vm1', 'admin.vm.property.Get', 'netvm', None)] = \
|
|
|
|
b'0\x00default=False type=vm sys-net'
|
|
|
|
|
2018-07-19 20:06:00 +02:00
|
|
|
result = qubesadmin.utils.vm_dependencies(self.app,
|
|
|
|
self.app.domains['sys-net'])
|
2018-07-19 19:52:22 +02:00
|
|
|
|
|
|
|
self.assertListEqual(result, [(self.app.domains['vm1'], 'netvm')])
|
2020-01-24 18:40:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestVMExecEncode(qubesadmin.tests.QubesTestCase):
|
|
|
|
def test_00_encode(self):
|
|
|
|
self.assertEqual(
|
2020-08-04 21:59:13 +02:00
|
|
|
qubesadmin.utils.encode_for_vmexec(['ls', '-a', '+x']),
|
|
|
|
'ls+--a+-2Bx')
|
2020-01-24 18:40:11 +01:00
|
|
|
self.assertEqual(
|
|
|
|
qubesadmin.utils.encode_for_vmexec(
|
|
|
|
['touch', '/home/user/.profile']),
|
|
|
|
'touch+-2Fhome-2Fuser-2F.profile')
|