1#!/usr/bin/env python3
2#
3# Test luks and file image creation
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23import iotests
24from iotests import imgfmt
25
26iotests.script_initialize(
27    supported_fmts=['luks'],
28    supported_protocols=['file'],
29)
30
31with iotests.FilePath('t.luks') as disk_path, \
32     iotests.VM() as vm:
33
34    vm.add_object('secret,id=keysec0,data=foo')
35
36    #
37    # Successful image creation (defaults)
38    #
39    iotests.log("=== Successful image creation (defaults) ===")
40    iotests.log("")
41
42    size = 128 * 1024 * 1024
43
44    vm.launch()
45    vm.blockdev_create({ 'driver': 'file',
46                         'filename': disk_path,
47                         'size': 0 })
48
49    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
50               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
51
52    vm.blockdev_create({ 'driver': imgfmt,
53                         'file': 'imgfile',
54                         'key-secret': 'keysec0',
55                         'size': size,
56                         'iter-time': 10 })
57    vm.shutdown()
58
59    # TODO Proper support for images to be used with imgopts and/or protocols
60    iotests.img_info_log(
61        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
62        filter_path=disk_path,
63        extra_args=['--object', 'secret,id=keysec0,data=foo'],
64        imgopts=True)
65
66    #
67    # Successful image creation (with non-default options)
68    #
69    iotests.log("=== Successful image creation (with non-default options) ===")
70    iotests.log("")
71
72    size = 64 * 1024 * 1024
73
74    vm.launch()
75    vm.blockdev_create({ 'driver': 'file',
76                         'filename': disk_path,
77                         'size': 0 })
78    vm.blockdev_create({ 'driver': imgfmt,
79                         'file': {
80                             'driver': 'file',
81                             'filename': disk_path,
82                         },
83                         'size': size,
84                         'key-secret': 'keysec0',
85                         'cipher-alg': 'twofish-128',
86                         'cipher-mode': 'ctr',
87                         'ivgen-alg': 'plain64',
88                         'ivgen-hash-alg': 'md5',
89                         'hash-alg': 'sha1',
90                         'iter-time': 10 })
91    vm.shutdown()
92
93    # TODO Proper support for images to be used with imgopts and/or protocols
94    iotests.img_info_log(
95        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
96        filter_path=disk_path,
97        extra_args=['--object', 'secret,id=keysec0,data=foo'],
98        imgopts=True)
99
100    #
101    # Invalid BlockdevRef
102    #
103    iotests.log("=== Invalid BlockdevRef ===")
104    iotests.log("")
105
106    size = 64 * 1024 * 1024
107
108    vm.launch()
109    vm.blockdev_create({ 'driver': imgfmt,
110                         'file': "this doesn't exist",
111                         'size': size })
112    vm.shutdown()
113
114    #
115    # Zero size
116    #
117    iotests.log("=== Zero size ===")
118    iotests.log("")
119
120    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
121    vm.launch()
122    vm.blockdev_create({ 'driver': imgfmt,
123                         'file': 'node0',
124                         'key-secret': 'keysec0',
125                         'size': 0,
126                         'iter-time': 10 })
127    vm.shutdown()
128
129    # TODO Proper support for images to be used with imgopts and/or protocols
130    iotests.img_info_log(
131        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
132        filter_path=disk_path,
133        extra_args=['--object', 'secret,id=keysec0,data=foo'],
134        imgopts=True)
135
136    #
137    # Invalid sizes
138    #
139
140    # TODO Negative image sizes aren't handled correctly, but this is a problem
141    # with QAPI's implementation of the 'size' type and affects other commands as
142    # well. Once this is fixed, we may want to add a test case here.
143
144    # 1. 2^64 - 512
145    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
146    # 3. 2^63 - 512 (generally valid, but with the crypto header the file will
147    #                exceed 63 bits)
148    iotests.log("=== Invalid sizes ===")
149    iotests.log("")
150
151    vm.launch()
152    for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]:
153        vm.blockdev_create({ 'driver': imgfmt,
154                             'file': 'node0',
155                             'key-secret': 'keysec0',
156                             'size': size })
157    vm.shutdown()
158
159    #
160    # Resize image with invalid sizes
161    #
162    iotests.log("=== Resize image with invalid sizes ===")
163    iotests.log("")
164
165    vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1')
166    vm.launch()
167    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296)
168    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808)
169    vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104)
170    vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808)
171    vm.shutdown()
172
173    # TODO Proper support for images to be used with imgopts and/or protocols
174    iotests.img_info_log(
175        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
176        filter_path=disk_path,
177        extra_args=['--object', 'secret,id=keysec0,data=foo'],
178        imgopts=True)
179