From nobody Fri Nov 14 21:03:23 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1760109400; cv=none; d=zohomail.com; s=zohoarc; b=AYYDBroX3tXWC37aJW5JqcDYeNISW3hjwGRONcEVWz/X5XeVdg3JfbiCdegq9tGWc4eG9rvcaNVXUh8G0NwukcbmEzbAkRTWnqbASlumSod743TF2EZobvvbuiGJtmfdlefS/e1RIZOgvLAG9emWgEtHaECaZMCx1y9UuFWmaTM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760109400; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=F1yOkbzuojTrlKk+buHl+5dXDqrmMnV6cjefuiyhlyQ=; b=A0cx5Fbw9FVwG+SKoRCRmfSPaMAg6bAE5/HQ6d8PngAjU2geqTQ5tmy3tjdTJ2au5h5rTGvUFIw83EDPsQlU34HNC9/uByiZ0X83F69Qy/Ymp4b3wPBHURHz9zjjie0UHufd/ERsvCCRPS5E3E17pGe+z0oCb0sHEC/zI+AKh+o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1760109400415267.8657798063157; Fri, 10 Oct 2025 08:16:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v7Emm-0000o5-6H; Fri, 10 Oct 2025 11:12:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v7Emj-0000md-5W for qemu-devel@nongnu.org; Fri, 10 Oct 2025 11:11:57 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v7Els-0004En-V3 for qemu-devel@nongnu.org; Fri, 10 Oct 2025 11:11:54 -0400 Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-55-3jDyQ7E7PwaQoV0Zd-VeEg-1; Fri, 10 Oct 2025 11:11:00 -0400 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-b4626b2d07bso270001066b.1 for ; Fri, 10 Oct 2025 08:10:59 -0700 (PDT) Received: from [192.168.10.48] ([151.49.231.162]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b55d95257c8sm259085866b.77.2025.10.10.08.10.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Oct 2025 08:10:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760109061; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F1yOkbzuojTrlKk+buHl+5dXDqrmMnV6cjefuiyhlyQ=; b=a3y+omdhqycTwHx9EmsAmYpKG5rv1JLlHkjeK+oTdO9GE1L4vsoemt7fb2H9H9zSNhcWuO Cr3fPNaTLVOmdjtcl1Gb2HHloNWVwtd18M6Ttg9mUCVRaE6PixYYFMhBlze729NVVRZ09P 14+u0bUcyoEf4Ug+SyB4MiRNldVujSo= X-MC-Unique: 3jDyQ7E7PwaQoV0Zd-VeEg-1 X-Mimecast-MFC-AGG-ID: 3jDyQ7E7PwaQoV0Zd-VeEg_1760109059 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760109058; x=1760713858; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F1yOkbzuojTrlKk+buHl+5dXDqrmMnV6cjefuiyhlyQ=; b=nCh8QhKlLPdMmDAOE3+xYepzPTbZGahgK/XYc/OX9LllLjWHqFa0PFFFnOOzFiL9Q1 L6uL9J/ANl9GMvfJtYO/mcCi3ECVkozn6WbCLuI/HM9kaHE2LTJrz7dRHbvQdc3M5W4g +MPb6jbPyIRYV584soxVeUKjINqpfpbXeljImVK4M9rhzTc1rMh+TQJxncptQHSzlEZs 7PONjPFITW3L4FaZYSgSzKWv2d74ODUJMJkW/RKtBT9oRhBol9vRN3E2O9dWJyDK/yIN m20tn4Tm5V4apWL0v6rfg4i56c52WEe351ocScyFVnQ5s6u5hy0V2RGcTh9jcMF3HCBQ +DSg== X-Gm-Message-State: AOJu0YzpNSkzCBkZVP6HR6q1d0ZBFXm0wnwYSSGDP1hf7aJ+FDf04fYr OUGTOe4CWKKkBHuOB5gY3nX3NYtgt5X3xEfMAXW4nF3H+Fhfq9xP2dzYDXwVWRUcWci4UA85kzb 3K/nkQj2L0XqPmc0KVk+3ZARrmCYMff03IpVrGAIDc+Q0+QvFSeoiaDgLxV+vQFxSQqgoIRsEuR UriKaAyX1IDxXnzNzfmFDQF1zbx3AZA7Njeh3UaTXP X-Gm-Gg: ASbGncuFmYv48bt5ou6Ct7CJTTDyQ5DKw7wDZ5Yi1ORz2Pg6JX928cyXIpxp1CwiSpH Ej9JwWVL+Lvf2a9LtVzGizOh8tm+MWY3jsN1Mxx5Ly9ajxXKvczmo8M0XTvm/zFOWKhAAV1qDy2 vKHRtDZ+b5f6kZiIjGURaMAawFDpSCYUYPhe2X/NbKrcL9elmoKYbBTZW1W4wMDmupIqsoyCW8+ 2YwMaK/zeCrgfF6Gc9oLcLevVEpmHpZk8mdx0uu0D2GeK/TREoEP5LNetxCMPxBopEAVF2gvGPQ ykRweW9qzADusTPYgUGP0C49NSx2WMPZDA54h5W4waYNBoh5M1R2snok11ZIN0nHe30cDHHO6Am RAsXZKmlby19chTwSCRiFz+MiJrqKSFhj0OPinN6+3e0B X-Received: by 2002:a17:906:6a14:b0:afe:764d:6b22 with SMTP id a640c23a62f3a-b50aa48c4cfmr1202296666b.9.1760109058013; Fri, 10 Oct 2025 08:10:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVVmh/0bAHP7hdXC/LRNn8SnraAOcqmHRskIba6Qi+nb/2xvuxezyMS07uKWG40QALxvHtfg== X-Received: by 2002:a17:906:6a14:b0:afe:764d:6b22 with SMTP id a640c23a62f3a-b50aa48c4cfmr1202276966b.9.1760109055296; Fri, 10 Oct 2025 08:10:55 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, marcandre.lureau@redhat.com, qemu-rust@nongnu.org Subject: [PATCH 19/19] rust/tests: QAPI integration tests Date: Fri, 10 Oct 2025 17:10:04 +0200 Message-ID: <20251010151006.791038-20-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251010151006.791038-1-pbonzini@redhat.com> References: <20251010151006.791038-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.441, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, T_SPF_TEMPERROR=0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1760109401414154100 From: Marc-Andr=C3=A9 Lureau Signed-off-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Paolo Bonzini --- rust/tests/meson.build | 25 +- rust/tests/tests/integration.rs | 2 + rust/tests/tests/qapi.rs | 444 ++++++++++++++++++++++++++++++++ 3 files changed, 468 insertions(+), 3 deletions(-) create mode 100644 rust/tests/tests/integration.rs create mode 100644 rust/tests/tests/qapi.rs diff --git a/rust/tests/meson.build b/rust/tests/meson.build index 00688c66fb1..663c1e1aafc 100644 --- a/rust/tests/meson.build +++ b/rust/tests/meson.build @@ -1,11 +1,30 @@ +test_qapi_rs_files =3D custom_target('QAPI Rust', + output: 'test-qapi-types.rs', + input: [ files(meson.project_source_root() + '/tests/qapi-schema/qapi-sc= hema-test.json') ], + command: [ qapi_gen, '-o', meson.current_build_dir(), '-b', '@INPUT0@', = '-B', 'qapi.backend.QAPIRsBackend', '-p', 'test-' ], + depend_files: [ qapi_inputs, qapi_gen_depends ]) + +_test_qapi_rs =3D static_library( + 'test_qapi', + test_qapi_rs_files, + override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], + rust_abi: 'rust', + dependencies: [common_rs, util_rs, serde_rs, serde_derive_rs]) + +test_qapi_rs =3D declare_dependency(link_with: [_test_qapi_rs]) + test('rust-integration', executable( 'rust-integration', - files('tests/vmstate_tests.rs'), + files( + 'tests/integration.rs', + 'tests/vmstate_tests.rs', + 'tests/qapi.rs', + ), override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], - rust_args: ['--test'], + rust_args: ['--test'] + _qapi_cfg, install: false, - dependencies: [bql_rs, common_rs, util_rs, migration_rs, qom_rs]), + dependencies: [bql_rs, common_rs, util_rs, migration_rs, qom_rs, q= api_rs, test_qapi_rs]), args: [ '--test', '--test-threads', '1', '--format', 'pretty', diff --git a/rust/tests/tests/integration.rs b/rust/tests/tests/integration= .rs new file mode 100644 index 00000000000..ebc17cb5550 --- /dev/null +++ b/rust/tests/tests/integration.rs @@ -0,0 +1,2 @@ +mod qapi; +mod vmstate_tests; diff --git a/rust/tests/tests/qapi.rs b/rust/tests/tests/qapi.rs new file mode 100644 index 00000000000..f8a585e5802 --- /dev/null +++ b/rust/tests/tests/qapi.rs @@ -0,0 +1,444 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#![allow(unexpected_cfgs)] +#![allow(clippy::shadow_unrelated)] + +use util::qobject::{from_qobject, to_qobject, QObject}; + +#[test] +fn test_char() { + let json =3D "\"v\""; + let qo =3D QObject::from_json(json).unwrap(); + let c: char =3D from_qobject(qo).unwrap(); + assert_eq!(c, 'v'); + assert_eq!(to_qobject(c).unwrap().to_json(), json); + + let json =3D "'va'"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); +} + +#[test] +fn test_enum() { + let json =3D "\"value1\""; + let qo =3D QObject::from_json(json).unwrap(); + let e: test_qapi::EnumOne =3D from_qobject(qo).unwrap(); + assert_eq!(e, test_qapi::EnumOne::VALUE1); + assert_eq!(to_qobject(e).unwrap().to_json(), json); +} + +#[test] +fn test_struct() { + let expected =3D test_qapi::TestStruct { + integer: -42, + boolean: true, + string: "foo".into(), + }; + let json =3D "{\"integer\": -42, \"boolean\": true, \"string\": \"foo\= "}"; + let qo =3D QObject::from_json(json).unwrap(); + let ts: test_qapi::TestStruct =3D from_qobject(qo).unwrap(); + assert_eq!(ts, expected); + assert_eq!(to_qobject(ts).unwrap().to_json(), json); +} + +#[test] +fn test_struct_nested() { + let expected =3D test_qapi::UserDefTwo { + string0: "string0".into(), + dict1: test_qapi::UserDefTwoDict { + string1: "string1".into(), + dict2: test_qapi::UserDefTwoDictDict { + userdef: test_qapi::UserDefOne { + integer: 42, + string: "string".into(), + enum1: None, + }, + string: "string2".into(), + }, + dict3: None, + }, + }; + let json =3D "{\"string0\": \"string0\", \"dict1\": {\"dict2\": {\"str= ing\": \"string2\", \ + \"userdef\": {\"integer\": 42, \"string\": \"string\"}}, \= "string1\": \ + \"string1\"}}"; + let qo =3D QObject::from_json(json).unwrap(); + let udt: test_qapi::UserDefTwo =3D from_qobject(qo).unwrap(); + assert_eq!(udt, expected); + assert_eq!(to_qobject(udt).unwrap().to_json(), json); +} + +#[test] +fn test_list() { + let expected =3D [ + test_qapi::UserDefOne { + integer: 42, + string: "string0".into(), + enum1: None, + }, + test_qapi::UserDefOne { + integer: 43, + string: "string1".into(), + enum1: None, + }, + test_qapi::UserDefOne { + integer: 44, + string: "string2".into(), + enum1: None, + }, + ]; + let json =3D "[{\"integer\": 42, \"string\": \"string0\"}, {\"integer\= ": 43, \"string\": \ + \"string1\"}, {\"integer\": 44, \"string\": \"string2\"}]"; + let qo =3D QObject::from_json(json).unwrap(); + let ud_list: Vec =3D from_qobject(qo).unwrap(); + assert_eq!(ud_list, expected); + assert_eq!(to_qobject(ud_list).unwrap().to_json(), json); +} + +#[test] +fn test_flat_union() { + let expected =3D test_qapi::UserDefFlatUnion { + integer: 41, + string: "str".into(), + u: test_qapi::UserDefFlatUnionVariant::Value1(test_qapi::UserDefA { + boolean: true, + a_b: None, + }), + }; + let json =3D "{\"integer\": 41, \"boolean\": true, \"enum1\": \"value1= \", \"string\": \"str\"}"; + let qo =3D QObject::from_json(json).unwrap(); + let ud_fu: test_qapi::UserDefFlatUnion =3D from_qobject(qo).unwrap(); + assert_eq!(ud_fu, expected); + assert_eq!(to_qobject(ud_fu).unwrap().to_json(), json); +} + +#[test] +fn test_union_in_union() { + let expected =3D test_qapi::TestUnionInUnion { + u: test_qapi::TestUnionInUnionVariant::ValueA(test_qapi::TestUnion= TypeA { + u: test_qapi::TestUnionTypeAVariant::ValueA1(test_qapi::TestUn= ionTypeA1 { + integer: 2, + name: "fish".into(), + }), + }), + }; + let json =3D + "{\"name\": \"fish\", \"integer\": 2, \"type-a\": \"value-a1\", \"= type\": \"value-a\"}"; + let qo =3D QObject::from_json(json).unwrap(); + let uu: test_qapi::TestUnionInUnion =3D from_qobject(qo).unwrap(); + assert_eq!(uu, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::TestUnionInUnion { + u: test_qapi::TestUnionInUnionVariant::ValueA(test_qapi::TestUnion= TypeA { + u: test_qapi::TestUnionTypeAVariant::ValueA2(test_qapi::TestUn= ionTypeA2 { + integer: 1729, + size: 87539319, + }), + }), + }; + let json =3D + "{\"integer\": 1729, \"type-a\": \"value-a2\", \"size\": 87539319,= \"type\": \"value-a\"}"; + let qo =3D QObject::from_json(json).unwrap(); + let uu: test_qapi::TestUnionInUnion =3D from_qobject(qo).unwrap(); + assert_eq!(uu, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::TestUnionInUnion { + u: test_qapi::TestUnionInUnionVariant::ValueB(test_qapi::TestUnion= TypeB { + integer: 1729, + onoff: true, + }), + }; + let json =3D "{\"integer\": 1729, \"onoff\": true, \"type\": \"value-b= \"}"; + let qo =3D QObject::from_json(json).unwrap(); + let uu: test_qapi::TestUnionInUnion =3D from_qobject(qo).unwrap(); + assert_eq!(uu, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); +} + +#[test] +fn test_alternate() { + let expected =3D test_qapi::UserDefAlternate::I(42); + let json =3D "42"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::UserDefAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::UserDefAlternate::E(test_qapi::EnumOne::VA= LUE1); + let json =3D "\"value1\""; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::UserDefAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::UserDefAlternate::N(()); + let json =3D "null"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::UserDefAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::UserDefAlternate::Udfu(test_qapi::UserDefF= latUnion { + integer: 42, + string: "str".to_string(), + u: test_qapi::UserDefFlatUnionVariant::Value1(test_qapi::UserDefA { + boolean: true, + a_b: None, + }), + }); + let json =3D "{\"integer\": 42, \"boolean\": true, \"enum1\": \"value1= \", \"string\": \"str\"}"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::UserDefAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::WrapAlternate { + alt: test_qapi::UserDefAlternate::I(42), + }; + let json =3D "{\"alt\": 42}"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::WrapAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::WrapAlternate { + alt: test_qapi::UserDefAlternate::E(test_qapi::EnumOne::VALUE1), + }; + let json =3D "{\"alt\": \"value1\"}"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::WrapAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::WrapAlternate { + alt: test_qapi::UserDefAlternate::Udfu(test_qapi::UserDefFlatUnion= { + integer: 1, + string: "str".to_string(), + u: test_qapi::UserDefFlatUnionVariant::Value1(test_qapi::UserD= efA { + boolean: true, + a_b: None, + }), + }), + }; + let json =3D "{\"alt\": {\"integer\": 1, \"boolean\": true, \"enum1\":= \"value1\", \"string\": \ + \"str\"}}"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::WrapAlternate =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); +} + +#[test] +fn test_alternate_number() { + let expected =3D test_qapi::AltEnumNum::N(42.0); + let json =3D "42"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltEnumNum =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::AltNumEnum::N(42.0); + let json =3D "42"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltNumEnum =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::AltEnumInt::I(42); + let json =3D "42"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltEnumInt =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::AltListInt::I(42); + let json =3D "42"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltListInt =3D from_qobject(qo).unwrap(); + assert_eq!(&uda, &expected); + assert_eq!(to_qobject(&expected).unwrap().to_json(), json); + + // double + let json =3D "42.5"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); + + let expected =3D test_qapi::AltEnumNum::N(42.5); + let json =3D "42.5"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltEnumNum =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let expected =3D test_qapi::AltNumEnum::N(42.5); + let json =3D "42.5"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltNumEnum =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); + + let json =3D "42.5"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); +} + +#[test] +fn test_alternate_list() { + let expected =3D test_qapi::AltListInt::L(vec![42, 43, 44]); + let json =3D "[42, 43, 44]"; + let qo =3D QObject::from_json(json).unwrap(); + let uda: test_qapi::AltListInt =3D from_qobject(qo).unwrap(); + assert_eq!(uda, expected); + assert_eq!(to_qobject(expected).unwrap().to_json(), json); +} + +#[test] +fn test_errors() { + let json =3D "{ 'integer': false, 'boolean': 'foo', 'string': -42 }"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); + + let json =3D "[ '1', '2', false, '3' ]"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); + + let json =3D "{ 'str': 'hi' }"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); + + let json =3D "{}"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); +} + +#[test] +fn test_wrong_type() { + let json =3D "[]"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); + + let json =3D "{}"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); + + let json =3D "1"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); + + let json =3D "{}"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); + + let json =3D "1"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); + + let json =3D "[]"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); +} + +#[test] +fn test_fail_struct() { + let json =3D "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'ext= ra': 42 }"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); +} + +#[test] +fn test_fail_struct_nested() { + let json =3D "{ 'string0': 'string0', 'dict1': { 'string1': 'string1',= 'dict2': { 'userdef1': { \ + 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo= ':'bar'}] }, 'string2': \ + 'string2'}}}"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::(qo).unwrap_err(); +} + +#[test] +fn test_fail_struct_in_list() { + let json =3D "[ { 'string': 'string0', 'integer': 42 }, { 'string': 's= tring1', 'integer': 43 }, \ + { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); +} + +#[test] +fn test_fail_union_flat() { + let json =3D "{ 'enum1': 'value2', 'string': 'c', 'integer': 41, 'bool= ean': true }"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); +} + +#[test] +fn test_fail_union_flat_no_discrim() { + // test situation where discriminator field ('enum1' here) is missing + let json =3D "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2= ': 'e' }"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); +} + +#[test] +fn test_fail_alternate() { + let json =3D "3.14"; + let qo =3D QObject::from_json(json).unwrap(); + from_qobject::>(qo).unwrap_err(); +} + +#[test] +fn test_qapi() { + let expected =3D qapi::InetSocketAddress { + host: "host-val".to_string(), + port: "port-val".to_string(), + numeric: None, + to: None, + ipv4: None, + ipv6: None, + keep_alive: None, + #[cfg(HAVE_TCP_KEEPCNT)] + keep_alive_count: None, + #[cfg(HAVE_TCP_KEEPIDLE)] + keep_alive_idle: Some(42), + #[cfg(HAVE_TCP_KEEPINTVL)] + keep_alive_interval: None, + #[cfg(HAVE_IPPROTO_MPTCP)] + mptcp: None, + }; + + let qsa =3D to_qobject(&expected).unwrap(); + let json =3D qsa.to_json(); + assert_eq!( + json, + "{\"port\": \"port-val\", \"keep_alive_idle\": 42, \"host\": \"hos= t-val\"}" + ); + let sa: qapi::InetSocketAddress =3D from_qobject(qsa).unwrap(); + assert_eq!(sa, expected); + + let expected =3D qapi::SocketAddressVariant::Inet(expected); + let qsav =3D to_qobject(&expected).unwrap(); + let json =3D qsav.to_json(); + assert_eq!( + json, + "{\"port\": \"port-val\", \"keep_alive_idle\": 42, \"host\": \"hos= t-val\", \"type\": \ + \"inet\"}" + ); + let sav: qapi::SocketAddressVariant =3D from_qobject(qsav).unwrap(); + assert_eq!(sav, expected); + + let expected =3D qapi::Qcow2BitmapInfo { + name: "name-val".to_string(), + granularity: 4096, + flags: vec![ + qapi::Qcow2BitmapInfoFlags::IN_USE, + qapi::Qcow2BitmapInfoFlags::AUTO, + ], + }; + let qbi =3D to_qobject(&expected).unwrap(); + let json =3D qbi.to_json(); + assert_eq!( + json, + "{\"flags\": [\"in-use\", \"auto\"], \"name\": \"name-val\", \"gra= nularity\": 4096}" + ); + let bi: qapi::Qcow2BitmapInfo =3D from_qobject(qbi).unwrap(); + assert_eq!(bi, expected); +} --=20 2.51.0