class AutopartitioningInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the auto partitioning module."""
    def setUp(self):
        """Set up the module."""
        self.module = AutoPartitioningModule()
        self.interface = AutoPartitioningInterface(self.module)

    def _test_dbus_property(self, *args, **kwargs):
        check_dbus_property(self, AUTO_PARTITIONING, self.interface, *args,
                            **kwargs)

    def publication_test(self):
        """Test the DBus representation."""
        self.assertIsInstance(self.module.for_publication(),
                              AutoPartitioningInterface)

    def enabled_property_test(self):
        """Test the property enabled."""
        self._test_dbus_property("Enabled", True)

    def request_property_test(self):
        """Test the property request."""
        in_value = {
            'partitioning-scheme': AUTOPART_TYPE_LVM_THINP,
            'file-system-type': 'ext4',
            'excluded-mount-points': ['/home', '/boot', 'swap'],
            'encrypted': True,
            'passphrase': '123456',
            'cipher': 'aes-xts-plain64',
            'luks-version': 'luks1',
            'pbkdf': 'argon2i',
            'pbkdf-memory': 256,
            'pbkdf-time': 100,
            'pbkdf-iterations': 1000,
            'escrow-certificate': 'file:///tmp/escrow.crt',
            'backup-passphrase-enabled': True,
        }

        out_value = {
            'partitioning-scheme':
            get_variant(Int, AUTOPART_TYPE_LVM_THINP),
            'file-system-type':
            get_variant(Str, 'ext4'),
            'excluded-mount-points':
            get_variant(List[Str], ['/home', '/boot', 'swap']),
            'encrypted':
            get_variant(Bool, True),
            'passphrase':
            get_variant(Str, '123456'),
            'cipher':
            get_variant(Str, 'aes-xts-plain64'),
            'luks-version':
            get_variant(Str, 'luks1'),
            'pbkdf':
            get_variant(Str, 'argon2i'),
            'pbkdf-memory':
            get_variant(Int, 256),
            'pbkdf-time':
            get_variant(Int, 100),
            'pbkdf-iterations':
            get_variant(Int, 1000),
            'escrow-certificate':
            get_variant(Str, 'file:///tmp/escrow.crt'),
            'backup-passphrase-enabled':
            get_variant(Bool, True),
        }

        self._test_dbus_property("Request", in_value, out_value)

    def requires_passphrase_test(self):
        """Test RequiresPassphrase."""
        self.assertEqual(self.interface.RequiresPassphrase(), False)

        self.module.request.encrypted = True
        self.assertEqual(self.interface.RequiresPassphrase(), True)

        self.module.request.passphrase = "123456"
        self.assertEqual(self.interface.RequiresPassphrase(), False)

    def reset_test(self):
        """Test the reset of the storage."""
        with self.assertRaises(UnavailableStorageError):
            if self.module.storage:
                self.fail("The storage shouldn't be available.")

        storage = Mock()
        self.module.on_storage_reset(storage)

        self.assertEqual(self.module._current_storage, storage)
        self.assertIsNone(self.module._storage_playground)

        self.assertNotEqual(self.module.storage, storage)
        self.assertIsNotNone(self.module._storage_playground)

    def remove_device_test(self):
        """Test RemoveDevice."""
        self.module.on_storage_reset(create_storage())

        dev1 = StorageDevice("dev1",
                             exists=False,
                             size=Size("15 GiB"),
                             fmt=get_format("disklabel"))
        dev2 = StorageDevice("dev2",
                             exists=False,
                             parents=[dev1],
                             size=Size("6 GiB"),
                             fmt=get_format("ext4"))
        dev3 = StorageDevice("dev3",
                             exists=False,
                             parents=[dev1],
                             size=Size("9 GiB"),
                             fmt=get_format("ext4"))

        self.module.storage.devicetree._add_device(dev1)
        self.module.storage.devicetree._add_device(dev2)
        self.module.storage.devicetree._add_device(dev3)

        dev1.protected = True
        with self.assertRaises(ProtectedDeviceError):
            self.interface.RemoveDevice("dev1")

        self.assertIn(dev1, self.module.storage.devices)
        self.assertIn(dev2, self.module.storage.devices)
        self.assertIn(dev3, self.module.storage.devices)

        dev1.protected = False
        dev2.protected = True
        self.interface.RemoveDevice("dev1")

        self.assertIn(dev1, self.module.storage.devices)
        self.assertIn(dev2, self.module.storage.devices)
        self.assertNotIn(dev3, self.module.storage.devices)

        dev2.protected = False
        self.interface.RemoveDevice("dev1")

        self.assertNotIn(dev1, self.module.storage.devices)
        self.assertNotIn(dev2, self.module.storage.devices)
        self.assertNotIn(dev3, self.module.storage.devices)

    def shrink_device_test(self):
        """Test ShrinkDevice."""
        self.module.on_storage_reset(create_storage())

        sda1 = StorageDevice("sda1",
                             exists=False,
                             size=Size("10 GiB"),
                             fmt=get_format("ext4"))
        self.module.storage.devicetree._add_device(sda1)

        def resize_device(device, size):
            device.size = size

        self.module.storage.resize_device = resize_device

        sda1.protected = True
        with self.assertRaises(ProtectedDeviceError):
            self.interface.ShrinkDevice("sda1", Size("3 GiB").get_bytes())

        sda1.protected = False
        self.interface.ShrinkDevice("sda1", Size("3 GiB").get_bytes())
        self.assertEqual(sda1.size, Size("3 GiB"))

        self.interface.ShrinkDevice("sda1", Size("5 GiB").get_bytes())
        self.assertEqual(sda1.size, Size("3 GiB"))

    @patch_dbus_publish_object
    def configure_with_task_test(self, publisher):
        """Test ConfigureWithTask."""
        self.module.on_storage_reset(Mock())
        task_path = self.interface.ConfigureWithTask()

        obj = check_task_creation(self, task_path, publisher,
                                  AutomaticPartitioningTask)

        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._request, self.module.request)

    @patch_dbus_publish_object
    def validate_with_task_test(self, publisher):
        """Test ValidateWithTask."""
        self.module.on_storage_reset(Mock())
        task_path = self.interface.ValidateWithTask()

        obj = check_task_creation(self, task_path, publisher,
                                  StorageValidateTask)

        self.assertEqual(obj.implementation._storage, self.module.storage)
Example #2
0
class AutopartitioningInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the auto partitioning module."""
    def setUp(self):
        """Set up the module."""
        self.module = AutoPartitioningModule()
        self.interface = AutoPartitioningInterface(self.module)

    def _test_dbus_property(self, *args, **kwargs):
        check_dbus_property(self, AUTO_PARTITIONING, self.interface, *args,
                            **kwargs)

    def enabled_property_test(self):
        """Test the property enabled."""
        self._test_dbus_property("Enabled", True)

    def type_property_test(self):
        """Test the type property."""
        self._test_dbus_property("Type", AUTOPART_TYPE_LVM_THINP)

        self._test_dbus_property("Type", AUTOPART_TYPE_PLAIN)

        self._test_dbus_property("Type", AUTOPART_TYPE_LVM)

    def filesystem_type_property_test(self):
        """Test the filesystem property."""
        self._test_dbus_property("FilesystemType", "ext4")

    def nohome_property_test(self):
        """Test the nohome property."""
        def setter(value):
            self.module.set_nohome(value)
            self.module.module_properties_changed.emit()

        self._test_dbus_property("NoHome", True, setter=setter)

    def noboot_property_test(self):
        """Test the noboot property."""
        def setter(value):
            self.module.set_noboot(value)
            self.module.module_properties_changed.emit()

        self._test_dbus_property("NoBoot", True, setter=setter)

    def noswap_property_test(self):
        """Test the noswap property."""
        def setter(value):
            self.module.set_noswap(value)
            self.module.module_properties_changed.emit()

        self._test_dbus_property("NoSwap", True, setter=setter)

    def encrypted_property_test(self):
        """Test the encrypted property."""
        self._test_dbus_property("Encrypted", True)

    def cipher_property_test(self):
        """Test the cipher property,"""
        self._test_dbus_property("Cipher", "aes-xts-plain64")

    def passphrase_property_test(self):
        """Test the passphrase property."""
        self._test_dbus_property("Passphrase", "123456")

    def requires_passphrase_test(self):
        """Test RequiresPassphrase."""
        self.assertEqual(self.interface.RequiresPassphrase(), False)
        self.interface.SetEncrypted(True)
        self.assertEqual(self.interface.RequiresPassphrase(), True)
        self.interface.SetPassphrase("123456")
        self.assertEqual(self.interface.RequiresPassphrase(), False)

    def luks_version_property_test(self):
        """Test the luks version property."""
        self._test_dbus_property("LUKSVersion", "luks1")

    def pbkdf_property_test(self):
        """Test the PBKDF property."""
        self._test_dbus_property("PBKDF", "argon2i")

    def pbkdf_memory_property_test(self):
        """Test the PBKDF memory property."""
        self._test_dbus_property("PBKDFMemory", 256)

    def pbkdf_time_property_test(self):
        """Test the PBKDF time property."""
        self._test_dbus_property("PBKDFTime", 100)

    def pbkdf_iterations_property_test(self):
        """Test the PBKDF iterations property."""
        self._test_dbus_property("PBKDFIterations", 1000)

    def escrowcert_property_test(self):
        """Test the escrowcert property."""
        self._test_dbus_property("Escrowcert", "file:///tmp/escrow.crt")

    def backup_passphrase_enabled_property_test(self):
        """Test the backup passphrase enabled property."""
        self._test_dbus_property("BackupPassphraseEnabled", True)

    def pbkdf_args_test(self):
        """Test the pbkdf_args property."""
        self.module.set_encrypted(False)
        self.assertEqual(self.module.pbkdf_args, None)

        self.module.set_encrypted(True)
        self.module.set_luks_version("luks1")
        self.assertEqual(self.module.pbkdf_args, None)

        self.module.set_encrypted(True)
        self.module.set_luks_version("luks2")
        self.assertEqual(self.module.pbkdf_args, None)

        self.module.set_encrypted(True)
        self.module.set_luks_version("luks2")
        self.module.set_pbkdf("argon2i")
        self.module.set_pbkdf_memory(256)
        self.module.set_pbkdf_iterations(1000)
        self.module.set_pbkdf_time(100)

        pbkdf_args = self.module.pbkdf_args
        self.assertIsInstance(pbkdf_args, LUKS2PBKDFArgs)
        self.assertEqual(pbkdf_args.type, "argon2i")
        self.assertEqual(pbkdf_args.max_memory_kb, 256)
        self.assertEqual(pbkdf_args.iterations, 1000)
        self.assertEqual(pbkdf_args.time_ms, 100)

    def luks_format_args_test(self):
        """Test the luks_format_args property."""
        storage = create_storage()
        storage._escrow_certificates["file:///tmp/escrow.crt"] = "CERTIFICATE"
        self.module.on_storage_reset(storage)

        self.module.set_encrypted(False)
        self.assertEqual(self.module.luks_format_args, {})

        self.module.set_encrypted(True)
        self.module.set_passphrase("default")
        self.assertEqual(
            self.module.luks_format_args, {
                "passphrase": "default",
                "cipher": "",
                "luks_version": "luks2",
                "pbkdf_args": None,
                "escrow_cert": None,
                "add_backup_passphrase": False,
                "min_luks_entropy": MIN_CREATE_ENTROPY,
            })

        self.module.set_encrypted(True)
        self.module.set_luks_version("luks1")
        self.module.set_passphrase("passphrase")
        self.module.set_cipher("aes-xts-plain64")
        self.module.set_escrowcert("file:///tmp/escrow.crt")
        self.module.set_backup_passphrase_enabled(True)
        self.assertEqual(
            self.module.luks_format_args, {
                "passphrase": "passphrase",
                "cipher": "aes-xts-plain64",
                "luks_version": "luks1",
                "pbkdf_args": None,
                "escrow_cert": "CERTIFICATE",
                "add_backup_passphrase": True,
                "min_luks_entropy": MIN_CREATE_ENTROPY,
            })

    def reset_test(self):
        """Test the reset of the storage."""
        with self.assertRaises(UnavailableStorageError):
            if self.module.storage:
                self.fail("The storage shouldn't be available.")

        storage = Mock()
        self.module.on_storage_reset(storage)

        self.assertEqual(self.module._current_storage, storage)
        self.assertIsNone(self.module._storage_playground)

        self.assertNotEqual(self.module.storage, storage)
        self.assertIsNotNone(self.module._storage_playground)

    @patch_dbus_publish_object
    def configure_with_task_test(self, publisher):
        """Test ConfigureWithTask."""
        self.module.on_storage_reset(Mock())
        task_path = self.interface.ConfigureWithTask()

        obj = check_task_creation(self, task_path, publisher,
                                  AutomaticPartitioningTask)

        self.assertEqual(obj.implementation._storage, self.module.storage)

    @patch_dbus_publish_object
    def validate_with_task_test(self, publisher):
        """Test ValidateWithTask."""
        self.module.on_storage_reset(Mock())
        task_path = self.interface.ValidateWithTask()

        obj = check_task_creation(self, task_path, publisher,
                                  StorageValidateTask)

        self.assertEqual(obj.implementation._storage, self.module.storage)