diff options
author | John Levon <john.levon@nutanix.com> | 2021-05-10 13:12:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-10 13:12:19 +0100 |
commit | e92b1f6b57d0131ec4aa581e57e10ab7d628a0cd (patch) | |
tree | 81fbf790b7bdc19bbb646ff3a4cbba4671971789 /test/py/test_negotiate.py | |
parent | b95c886ed23b4cc4c539030bf383b55aae8859a3 (diff) | |
download | libvfio-user-e92b1f6b57d0131ec4aa581e57e10ab7d628a0cd.zip libvfio-user-e92b1f6b57d0131ec4aa581e57e10ab7d628a0cd.tar.gz libvfio-user-e92b1f6b57d0131ec4aa581e57e10ab7d628a0cd.tar.bz2 |
start python-based testing framework (#447)
Trying to do our unit/functional testing with C is very tedious, and cmocka
especially is a continual pain point. This commit introduces a Python-based
testing infrastructure, and adds an initial set of functional tests for client
negotiation.
The tests work under Valgrind for leak/bad access detection of the C code, but
not under ASAN, which lacks any meaningful shared-library support.
We should be able to replace all of current C-based unit tests with this,
reverting samples/ back to demo code only.
Signed-off-by: John Levon <john.levon@nutanix.com>
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'test/py/test_negotiate.py')
-rw-r--r-- | test/py/test_negotiate.py | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/test/py/test_negotiate.py b/test/py/test_negotiate.py new file mode 100644 index 0000000..9a6afd9 --- /dev/null +++ b/test/py/test_negotiate.py @@ -0,0 +1,177 @@ +# +# 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 json +import os +import socket +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 != 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_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_fds": "foo" } }') + +def test_invalid_json_bad_max_fds(): + client_version_json(errno.EINVAL, + b'{ "capabilities": { "max_fds": -1 } }') + +def test_invalid_json_bad_max_fds2(): + client_version_json(errno.EINVAL, + b'{ "capabilities": { "max_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_pgsize(): + 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_fds == SERVER_MAX_FDS + assert json.capabilities.max_msg_size == SERVER_MAX_MSG_SIZE + # FIXME: migration object checks + + sock.close() + + # notice client closed connection + vfu_run_ctx(ctx) + +def test_valid_negotiate_empty_json(): + client_version_json(json=b'{}') + + # notice client closed connection + vfu_run_ctx(ctx) + +def test_valid_negotiate_json(): + client_version_json(json=bytes('{ "capabilities": { "max_fds": %s } }' % + VFIO_USER_CLIENT_MAX_FDS_LIMIT, "utf-8")) + + # notice client closed connection + vfu_run_ctx(ctx) + +def test_destroying(): + vfu_destroy_ctx(ctx) |