1#!/usr/bin/env python
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.verify_image_format(supported_fmts=['luks'])
27iotests.verify_protocol(supported=['file'])
28
29def blockdev_create(vm, options):
30    result = vm.qmp_log('blockdev-create', job_id='job0', options=options,
31                        filters=[iotests.filter_qmp_testfiles])
32
33    if 'return' in result:
34        assert result['return'] == {}
35        vm.run_job('job0')
36    iotests.log("")
37
38with iotests.FilePath('t.luks') as disk_path, \
39     iotests.VM() as vm:
40
41    vm.add_object('secret,id=keysec0,data=foo')
42
43    #
44    # Successful image creation (defaults)
45    #
46    iotests.log("=== Successful image creation (defaults) ===")
47    iotests.log("")
48
49    size = 128 * 1024 * 1024
50
51    vm.launch()
52    blockdev_create(vm, { 'driver': 'file',
53                          'filename': disk_path,
54                          'size': 0 })
55
56    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
57               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
58
59    blockdev_create(vm, { 'driver': imgfmt,
60                          'file': 'imgfile',
61                          'key-secret': 'keysec0',
62                          'size': size,
63                          'iter-time': 10 })
64    vm.shutdown()
65
66    # TODO Proper support for images to be used with imgopts and/or protocols
67    iotests.img_info_log(
68        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
69        filter_path=disk_path,
70        extra_args=['--object', 'secret,id=keysec0,data=foo'],
71        imgopts=True)
72
73    #
74    # Successful image creation (with non-default options)
75    #
76    iotests.log("=== Successful image creation (with non-default options) ===")
77    iotests.log("")
78
79    size = 64 * 1024 * 1024
80
81    vm.launch()
82    blockdev_create(vm, { 'driver': 'file',
83                          'filename': disk_path,
84                          'size': 0 })
85    blockdev_create(vm, { 'driver': imgfmt,
86                          'file': {
87                              'driver': 'file',
88                              'filename': disk_path,
89                          },
90                          'size': size,
91                          'key-secret': 'keysec0',
92                          'cipher-alg': 'twofish-128',
93                          'cipher-mode': 'ctr',
94                          'ivgen-alg': 'plain64',
95                          'ivgen-hash-alg': 'md5',
96                          'hash-alg': 'sha1',
97                          'iter-time': 10 })
98    vm.shutdown()
99
100    # TODO Proper support for images to be used with imgopts and/or protocols
101    iotests.img_info_log(
102        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
103        filter_path=disk_path,
104        extra_args=['--object', 'secret,id=keysec0,data=foo'],
105        imgopts=True)
106
107    #
108    # Invalid BlockdevRef
109    #
110    iotests.log("=== Invalid BlockdevRef ===")
111    iotests.log("")
112
113    size = 64 * 1024 * 1024
114
115    vm.launch()
116    blockdev_create(vm, { 'driver': imgfmt,
117                          'file': "this doesn't exist",
118                          'size': size })
119    vm.shutdown()
120
121    #
122    # Zero size
123    #
124    iotests.log("=== Zero size ===")
125    iotests.log("")
126
127    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
128    vm.launch()
129    blockdev_create(vm, { 'driver': imgfmt,
130                          'file': 'node0',
131                          'key-secret': 'keysec0',
132                          'size': 0,
133                          'iter-time': 10 })
134    vm.shutdown()
135
136    # TODO Proper support for images to be used with imgopts and/or protocols
137    iotests.img_info_log(
138        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
139        filter_path=disk_path,
140        extra_args=['--object', 'secret,id=keysec0,data=foo'],
141        imgopts=True)
142
143    #
144    # Invalid sizes
145    #
146
147    # TODO Negative image sizes aren't handled correctly, but this is a problem
148    # with QAPI's implementation of the 'size' type and affects other commands as
149    # well. Once this is fixed, we may want to add a test case here.
150
151    # 1. 2^64 - 512
152    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
153    # 3. 2^63 - 512 (generally valid, but with the crypto header the file will
154    #                exceed 63 bits)
155    iotests.log("=== Invalid sizes ===")
156    iotests.log("")
157
158    vm.launch()
159    for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]:
160        blockdev_create(vm, { 'driver': imgfmt,
161                              'file': 'node0',
162                              'key-secret': 'keysec0',
163                              'size': size })
164    vm.shutdown()
165
166    #
167    # Resize image with invalid sizes
168    #
169    iotests.log("=== Resize image with invalid sizes ===")
170    iotests.log("")
171
172    vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1')
173    vm.launch()
174    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296)
175    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808)
176    vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104)
177    vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808)
178    vm.shutdown()
179
180    # TODO Proper support for images to be used with imgopts and/or protocols
181    iotests.img_info_log(
182        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
183        filter_path=disk_path,
184        extra_args=['--object', 'secret,id=keysec0,data=foo'],
185        imgopts=True)
186