xref: /openbmc/qemu/tests/qemu-iotests/045 (revision f6476697)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Tests for fdsets and getfd.
5#
6# Copyright (C) 2012 IBM Corp.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22import os
23import iotests
24from iotests import qemu_img
25
26image0 = os.path.join(iotests.test_dir, 'image0')
27image1 = os.path.join(iotests.test_dir, 'image1')
28image2 = os.path.join(iotests.test_dir, 'image2')
29image3 = os.path.join(iotests.test_dir, 'image3')
30image4 = os.path.join(iotests.test_dir, 'image4')
31
32class TestFdSets(iotests.QMPTestCase):
33
34    def setUp(self):
35        self.vm = iotests.VM()
36        qemu_img('create', '-f', iotests.imgfmt, image0, '128K')
37        qemu_img('create', '-f', iotests.imgfmt, image1, '128K')
38        qemu_img('create', '-f', iotests.imgfmt, image2, '128K')
39        qemu_img('create', '-f', iotests.imgfmt, image3, '128K')
40        qemu_img('create', '-f', iotests.imgfmt, image4, '128K')
41        self.file0 = open(image0, 'r')
42        self.file1 = open(image1, 'w+')
43        self.file2 = open(image2, 'r')
44        self.file3 = open(image3, 'r')
45        self.file4 = open(image4, 'r')
46        self.vm.add_fd(self.file0.fileno(), 1, 'image0:r')
47        self.vm.add_fd(self.file1.fileno(), 1, 'image1:w+')
48        self.vm.add_fd(self.file2.fileno(), 0, 'image2:r')
49        self.vm.add_fd(self.file3.fileno(), 2, 'image3:r')
50        self.vm.add_fd(self.file4.fileno(), 2, 'image4:r')
51        self.vm.add_drive("/dev/fdset/1")
52        self.vm.launch()
53
54    def tearDown(self):
55        self.vm.shutdown()
56        self.file0.close()
57        self.file1.close()
58        self.file2.close()
59        self.file3.close()
60        self.file4.close()
61        os.remove(image0)
62        os.remove(image1)
63        os.remove(image2)
64        os.remove(image3)
65        os.remove(image4)
66
67    def test_query_fdset(self):
68        result = self.vm.qmp('query-fdsets')
69        self.assert_qmp(result, 'return[0]/fdset-id', 2)
70        self.assert_qmp(result, 'return[1]/fdset-id', 1)
71        self.assert_qmp(result, 'return[2]/fdset-id', 0)
72        self.assert_qmp(result, 'return[0]/fds[0]/opaque', 'image3:r')
73        self.assert_qmp(result, 'return[0]/fds[1]/opaque', 'image4:r')
74        self.assert_qmp(result, 'return[1]/fds[0]/opaque', 'image0:r')
75        self.assert_qmp(result, 'return[1]/fds[1]/opaque', 'image1:w+')
76        self.assert_qmp(result, 'return[2]/fds[0]/opaque', 'image2:r')
77        self.vm.shutdown()
78
79    def test_remove_fdset(self):
80        result = self.vm.qmp('remove-fd', fdset_id=2)
81        self.assert_qmp(result, 'return', {})
82        result = self.vm.qmp('query-fdsets')
83        self.assert_qmp(result, 'return[0]/fdset-id', 1)
84        self.assert_qmp(result, 'return[1]/fdset-id', 0)
85        self.assert_qmp(result, 'return[0]/fds[0]/opaque', 'image0:r')
86        self.assert_qmp(result, 'return[0]/fds[1]/opaque', 'image1:w+')
87        self.assert_qmp(result, 'return[1]/fds[0]/opaque', 'image2:r')
88        self.vm.shutdown()
89
90    def test_remove_fd(self):
91        result = self.vm.qmp('query-fdsets')
92        fd_image3 = result['return'][0]['fds'][0]['fd']
93        result = self.vm.qmp('remove-fd', fdset_id=2, fd=fd_image3)
94        self.assert_qmp(result, 'return', {})
95        result = self.vm.qmp('query-fdsets')
96        self.assert_qmp(result, 'return[0]/fdset-id', 2)
97        self.assert_qmp(result, 'return[1]/fdset-id', 1)
98        self.assert_qmp(result, 'return[2]/fdset-id', 0)
99        self.assert_qmp(result, 'return[0]/fds[0]/opaque', 'image4:r')
100        self.assert_qmp(result, 'return[1]/fds[0]/opaque', 'image0:r')
101        self.assert_qmp(result, 'return[1]/fds[1]/opaque', 'image1:w+')
102        self.assert_qmp(result, 'return[2]/fds[0]/opaque', 'image2:r')
103        self.vm.shutdown()
104
105    def test_remove_fd_invalid_fdset(self):
106        result = self.vm.qmp('query-fdsets')
107        fd_image3 = result['return'][0]['fds'][0]['fd']
108        result = self.vm.qmp('remove-fd', fdset_id=3, fd=fd_image3)
109        self.assert_qmp(result, 'error/class', 'GenericError')
110        self.assert_qmp(result, 'error/desc',
111            'File descriptor named \'fdset-id:3, fd:%d\' not found' % fd_image3)
112        self.vm.shutdown()
113
114    def test_remove_fd_invalid_fd(self):
115        result = self.vm.qmp('query-fdsets')
116        result = self.vm.qmp('remove-fd', fdset_id=2, fd=999)
117        self.assert_qmp(result, 'error/class', 'GenericError')
118        self.assert_qmp(result, 'error/desc',
119            'File descriptor named \'fdset-id:2, fd:999\' not found')
120        self.vm.shutdown()
121
122    def test_add_fd_invalid_fd(self):
123        result = self.vm.qmp('add-fd', fdset_id=2)
124        self.assert_qmp(result, 'error/class', 'GenericError')
125        self.assert_qmp(result, 'error/desc',
126                'No file descriptor supplied via SCM_RIGHTS')
127        self.vm.shutdown()
128
129# Add fd at runtime, there are two ways: monitor related or fdset related
130class TestSCMFd(iotests.QMPTestCase):
131    def setUp(self):
132        self.vm = iotests.VM()
133        qemu_img('create', '-f', iotests.imgfmt, image0, '128K')
134        # Add an unused monitor, to verify it works fine when two monitor
135        # instances present
136        self.vm.add_monitor_null()
137        self.vm.launch()
138
139    def tearDown(self):
140        self.vm.shutdown()
141        os.remove(image0)
142
143    def _send_fd_by_SCM(self):
144        ret = self.vm.send_fd_scm(file_path=image0)
145        self.assertEqual(ret, 0, 'Failed to send fd with UNIX SCM')
146
147    def test_add_fd(self):
148        self._send_fd_by_SCM()
149        result = self.vm.qmp('add-fd', fdset_id=2, opaque='image0:r')
150        self.assert_qmp(result, 'return/fdset-id', 2)
151
152    def test_getfd(self):
153        self._send_fd_by_SCM()
154        result = self.vm.qmp('getfd', fdname='image0:r')
155        self.assert_qmp(result, 'return', {})
156
157    def test_getfd_invalid_fdname(self):
158        self._send_fd_by_SCM()
159        result = self.vm.qmp('getfd', fdname='0image0:r')
160        self.assert_qmp(result, 'error/class', 'GenericError')
161        self.assert_qmp(result, 'error/desc',
162            "Parameter 'fdname' expects a name not starting with a digit")
163
164    def test_closefd(self):
165        self._send_fd_by_SCM()
166        result = self.vm.qmp('getfd', fdname='image0:r')
167        self.assert_qmp(result, 'return', {})
168        result = self.vm.qmp('closefd', fdname='image0:r')
169        self.assert_qmp(result, 'return', {})
170
171    def test_closefd_fd_not_found(self):
172        fdname = 'image0:r'
173        result = self.vm.qmp('closefd', fdname=fdname)
174        self.assert_qmp(result, 'error/class', 'GenericError')
175        self.assert_qmp(result, 'error/desc',
176            "File descriptor named '%s' not found" % fdname)
177
178if __name__ == '__main__':
179    iotests.main(supported_fmts=['raw'],
180                 supported_protocols=['file'])
181