17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw 3d06195e6SKevin Wolf# 4d06195e6SKevin Wolf# Test luks and file image creation 5d06195e6SKevin Wolf# 6d06195e6SKevin Wolf# Copyright (C) 2018 Red Hat, Inc. 7d06195e6SKevin Wolf# 85ba141dcSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 95ba141dcSKevin Wolf# 10d06195e6SKevin Wolf# This program is free software; you can redistribute it and/or modify 11d06195e6SKevin Wolf# it under the terms of the GNU General Public License as published by 12d06195e6SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 13d06195e6SKevin Wolf# (at your option) any later version. 14d06195e6SKevin Wolf# 15d06195e6SKevin Wolf# This program is distributed in the hope that it will be useful, 16d06195e6SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 17d06195e6SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18d06195e6SKevin Wolf# GNU General Public License for more details. 19d06195e6SKevin Wolf# 20d06195e6SKevin Wolf# You should have received a copy of the GNU General Public License 21d06195e6SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 22d06195e6SKevin Wolf# 23d06195e6SKevin Wolf 245ba141dcSKevin Wolfimport iotests 255ba141dcSKevin Wolffrom iotests import imgfmt 26d06195e6SKevin Wolf 277d814059SJohn Snowiotests.script_initialize( 287d814059SJohn Snow supported_fmts=['luks'], 297d814059SJohn Snow supported_protocols=['file'], 307d814059SJohn Snow) 31d06195e6SKevin Wolf 325ba141dcSKevin Wolfwith iotests.FilePath('t.luks') as disk_path, \ 335ba141dcSKevin Wolf iotests.VM() as vm: 34d06195e6SKevin Wolf 355ba141dcSKevin Wolf vm.add_object('secret,id=keysec0,data=foo') 36d06195e6SKevin Wolf 375ba141dcSKevin Wolf # 385ba141dcSKevin Wolf # Successful image creation (defaults) 395ba141dcSKevin Wolf # 405ba141dcSKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 415ba141dcSKevin Wolf iotests.log("") 42d06195e6SKevin Wolf 435ba141dcSKevin Wolf size = 128 * 1024 * 1024 44d06195e6SKevin Wolf 455ba141dcSKevin Wolf vm.launch() 46e55c2413SKevin Wolf vm.blockdev_create({ 'driver': 'file', 475ba141dcSKevin Wolf 'filename': disk_path, 485ba141dcSKevin Wolf 'size': 0 }) 49d06195e6SKevin Wolf 505ba141dcSKevin Wolf vm.qmp_log('blockdev-add', driver='file', filename=disk_path, 51250c04f5SMax Reitz node_name='imgfile', filters=[iotests.filter_qmp_testfiles]) 52d06195e6SKevin Wolf 53e55c2413SKevin Wolf vm.blockdev_create({ 'driver': imgfmt, 545ba141dcSKevin Wolf 'file': 'imgfile', 555ba141dcSKevin Wolf 'key-secret': 'keysec0', 565ba141dcSKevin Wolf 'size': size, 575ba141dcSKevin Wolf 'iter-time': 10 }) 585ba141dcSKevin Wolf vm.shutdown() 59d06195e6SKevin Wolf 605ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 615ba141dcSKevin Wolf iotests.img_info_log( 625ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 635ba141dcSKevin Wolf filter_path=disk_path, 645ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 65*3bd2b942SVladimir Sementsov-Ogievskiy use_image_opts=True) 66d06195e6SKevin Wolf 675ba141dcSKevin Wolf # 685ba141dcSKevin Wolf # Successful image creation (with non-default options) 695ba141dcSKevin Wolf # 705ba141dcSKevin Wolf iotests.log("=== Successful image creation (with non-default options) ===") 715ba141dcSKevin Wolf iotests.log("") 72d06195e6SKevin Wolf 735ba141dcSKevin Wolf size = 64 * 1024 * 1024 745ba141dcSKevin Wolf 755ba141dcSKevin Wolf vm.launch() 76e55c2413SKevin Wolf vm.blockdev_create({ 'driver': 'file', 775ba141dcSKevin Wolf 'filename': disk_path, 785ba141dcSKevin Wolf 'size': 0 }) 79e55c2413SKevin Wolf vm.blockdev_create({ 'driver': imgfmt, 805ba141dcSKevin Wolf 'file': { 815ba141dcSKevin Wolf 'driver': 'file', 825ba141dcSKevin Wolf 'filename': disk_path, 83d06195e6SKevin Wolf }, 845ba141dcSKevin Wolf 'size': size, 855ba141dcSKevin Wolf 'key-secret': 'keysec0', 86cb5a24d7SHanna Reitz 'cipher-alg': 'aes-128', 87cb5a24d7SHanna Reitz 'cipher-mode': 'cbc', 885ba141dcSKevin Wolf 'ivgen-alg': 'plain64', 895ba141dcSKevin Wolf 'ivgen-hash-alg': 'md5', 905ba141dcSKevin Wolf 'hash-alg': 'sha1', 915ba141dcSKevin Wolf 'iter-time': 10 }) 925ba141dcSKevin Wolf vm.shutdown() 93d06195e6SKevin Wolf 945ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 955ba141dcSKevin Wolf iotests.img_info_log( 965ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 975ba141dcSKevin Wolf filter_path=disk_path, 985ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 99*3bd2b942SVladimir Sementsov-Ogievskiy use_image_opts=True) 100d06195e6SKevin Wolf 1015ba141dcSKevin Wolf # 1025ba141dcSKevin Wolf # Invalid BlockdevRef 1035ba141dcSKevin Wolf # 1045ba141dcSKevin Wolf iotests.log("=== Invalid BlockdevRef ===") 1055ba141dcSKevin Wolf iotests.log("") 106d06195e6SKevin Wolf 1075ba141dcSKevin Wolf size = 64 * 1024 * 1024 108d06195e6SKevin Wolf 1095ba141dcSKevin Wolf vm.launch() 110e55c2413SKevin Wolf vm.blockdev_create({ 'driver': imgfmt, 1115ba141dcSKevin Wolf 'file': "this doesn't exist", 1125ba141dcSKevin Wolf 'size': size }) 1135ba141dcSKevin Wolf vm.shutdown() 114d06195e6SKevin Wolf 1155ba141dcSKevin Wolf # 1165ba141dcSKevin Wolf # Zero size 1175ba141dcSKevin Wolf # 1185ba141dcSKevin Wolf iotests.log("=== Zero size ===") 1195ba141dcSKevin Wolf iotests.log("") 120d06195e6SKevin Wolf 1215ba141dcSKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path)) 1225ba141dcSKevin Wolf vm.launch() 123e55c2413SKevin Wolf vm.blockdev_create({ 'driver': imgfmt, 1245ba141dcSKevin Wolf 'file': 'node0', 1255ba141dcSKevin Wolf 'key-secret': 'keysec0', 1265ba141dcSKevin Wolf 'size': 0, 1275ba141dcSKevin Wolf 'iter-time': 10 }) 1285ba141dcSKevin Wolf vm.shutdown() 129d06195e6SKevin Wolf 1305ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 1315ba141dcSKevin Wolf iotests.img_info_log( 1325ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 1335ba141dcSKevin Wolf filter_path=disk_path, 1345ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 135*3bd2b942SVladimir Sementsov-Ogievskiy use_image_opts=True) 136d06195e6SKevin Wolf 1375ba141dcSKevin Wolf # 1385ba141dcSKevin Wolf # Invalid sizes 1395ba141dcSKevin Wolf # 140d06195e6SKevin Wolf 141d06195e6SKevin Wolf # TODO Negative image sizes aren't handled correctly, but this is a problem 142d06195e6SKevin Wolf # with QAPI's implementation of the 'size' type and affects other commands as 143d06195e6SKevin Wolf # well. Once this is fixed, we may want to add a test case here. 144d06195e6SKevin Wolf 145d06195e6SKevin Wolf # 1. 2^64 - 512 146d06195e6SKevin Wolf # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this) 147d06195e6SKevin Wolf # 3. 2^63 - 512 (generally valid, but with the crypto header the file will 148d06195e6SKevin Wolf # exceed 63 bits) 1495ba141dcSKevin Wolf iotests.log("=== Invalid sizes ===") 1505ba141dcSKevin Wolf iotests.log("") 151d06195e6SKevin Wolf 1525ba141dcSKevin Wolf vm.launch() 1535ba141dcSKevin Wolf for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]: 154e55c2413SKevin Wolf vm.blockdev_create({ 'driver': imgfmt, 1555ba141dcSKevin Wolf 'file': 'node0', 1565ba141dcSKevin Wolf 'key-secret': 'keysec0', 1575ba141dcSKevin Wolf 'size': size }) 1585ba141dcSKevin Wolf vm.shutdown() 159d06195e6SKevin Wolf 1605ba141dcSKevin Wolf # 1615ba141dcSKevin Wolf # Resize image with invalid sizes 1625ba141dcSKevin Wolf # 1635ba141dcSKevin Wolf iotests.log("=== Resize image with invalid sizes ===") 1645ba141dcSKevin Wolf iotests.log("") 16550880f25SKevin Wolf 1665ba141dcSKevin Wolf vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1') 1675ba141dcSKevin Wolf vm.launch() 1685ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296) 1695ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808) 1705ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104) 1715ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808) 1725ba141dcSKevin Wolf vm.shutdown() 17350880f25SKevin Wolf 1745ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 1755ba141dcSKevin Wolf iotests.img_info_log( 1765ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 1775ba141dcSKevin Wolf filter_path=disk_path, 1785ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 179*3bd2b942SVladimir Sementsov-Ogievskiy use_image_opts=True) 180