1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#
# Copyright (c) 2021 Nutanix Inc. All rights reserved.
#
# Authors: John Levon <john.levon@nutanix.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Nutanix nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
from libvfio_user import *
import errno
import struct
ctx = None
def client_version_json(expect=0, json=''):
sock = connect_sock()
payload = struct.pack("HH%dsc" % len(json),
LIBVFIO_USER_MAJOR, LIBVFIO_USER_MINOR, json, b'\0')
hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload))
sock.send(hdr + payload)
vfu_attach_ctx(ctx, expect=expect)
payload = get_reply(sock, expect=expect)
sock.close()
return payload
def test_server_setup():
global ctx
ctx = vfu_create_ctx()
assert ctx is not None
ret = vfu_realize_ctx(ctx)
assert ret == 0
def test_short_write():
sock = connect_sock()
hdr = vfio_user_header(VFIO_USER_VERSION, size=0)
sock.send(hdr)
vfu_attach_ctx(ctx, expect=errno.EINVAL)
get_reply(sock, expect=errno.EINVAL)
def test_long_write():
sock = connect_sock()
hdr = vfio_user_header(VFIO_USER_VERSION, size=SERVER_MAX_MSG_SIZE + 1)
sock.send(hdr)
ret = vfu_attach_ctx(ctx, expect=errno.EINVAL)
assert ret == -1
assert c.get_errno() == errno.EINVAL
def test_bad_command():
sock = connect_sock()
payload = struct.pack("HHs", LIBVFIO_USER_MAJOR, LIBVFIO_USER_MINOR, b"")
hdr = vfio_user_header(999, size=len(payload))
sock.send(hdr + payload)
vfu_attach_ctx(ctx, expect=errno.EINVAL)
get_reply(sock, expect=errno.EINVAL)
def test_invalid_major():
sock = connect_sock()
payload = struct.pack("HHs", 999, LIBVFIO_USER_MINOR, b"")
hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload))
sock.send(hdr + payload)
vfu_attach_ctx(ctx, expect=errno.EINVAL)
get_reply(sock, expect=errno.EINVAL)
def test_invalid_json_missing_NUL():
sock = connect_sock()
payload = struct.pack("HHcc", LIBVFIO_USER_MAJOR, LIBVFIO_USER_MINOR,
b"{", b"}")
hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload))
sock.send(hdr + payload)
vfu_attach_ctx(ctx, expect=errno.EINVAL)
get_reply(sock, expect=errno.EINVAL)
def test_invalid_json_missing_closing_brace():
client_version_json(errno.EINVAL, b"{")
def test_invalid_json_missing_closing_quote():
client_version_json(errno.EINVAL, b'"')
def test_invalid_json_bad_capabilities_object():
client_version_json(errno.EINVAL, b'{ "capabilities": "23" }')
def test_invalid_json_bad_max_fds():
client_version_json(errno.EINVAL,
b'{ "capabilities": { "max_msg_fds": "foo" } }')
def test_invalid_json_bad_max_fds2():
client_version_json(errno.EINVAL,
b'{ "capabilities": { "max_msg_fds": -1 } }')
def test_invalid_json_bad_max_fds3():
client_version_json(errno.EINVAL,
b'{ "capabilities": { "max_msg_fds": %d } }' %
(VFIO_USER_CLIENT_MAX_FDS_LIMIT + 1))
def test_invalid_json_bad_migration_object():
client_version_json(errno.EINVAL,
b'{ "capabilities": { "migration": "23" } }')
def test_invalid_json_bad_pgsize():
client_version_json(errno.EINVAL, b'{ "capabilities": ' +
b'{ "migration": { "pgsize": "foo" } } }')
#
# FIXME: need vfu_setup_device_migration_callbacks() to be able to test this
# failure mode.
#
def test_invalid_json_bad_pgsize2():
if False:
client_version_json(errno.EINVAL,
b'{ "capabilities": { "migration": { "pgsize": 4095 } } }')
def test_valid_negotiate_no_json():
sock = connect_sock()
payload = struct.pack("HH", LIBVFIO_USER_MAJOR, LIBVFIO_USER_MINOR)
hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload))
sock.send(hdr + payload)
vfu_attach_ctx(ctx)
payload = get_reply(sock)
(major, minor, json_str, _) = struct.unpack("HH%dsc" % (len(payload) - 5),
payload)
assert major == LIBVFIO_USER_MAJOR
assert minor == LIBVFIO_USER_MINOR
json = parse_json(json_str)
assert json.capabilities.max_msg_fds == SERVER_MAX_FDS
assert json.capabilities.max_data_xfer_size == SERVER_MAX_DATA_XFER_SIZE
# FIXME: migration object checks
disconnect_client(ctx, sock)
def test_valid_negotiate_empty_json():
client_version_json(json=b'{}')
# notice client closed connection
vfu_run_ctx(ctx, expect=errno.ENOTCONN)
def test_valid_negotiate_json():
client_version_json(json=bytes(
'{ "capabilities": { "max_msg_fds": %s, "max_data_xfer_size": %u } }' %
(VFIO_USER_CLIENT_MAX_FDS_LIMIT, VFIO_USER_DEFAULT_MAX_DATA_XFER_SIZE),
"utf-8"))
# notice client closed connection
vfu_run_ctx(ctx, expect=errno.ENOTCONN)
def test_destroying():
vfu_destroy_ctx(ctx)
# ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab:
|