test_vm_types() only exercised creation of the SEV/SEV-ES/SEV-SNP VM
types that the platform actually supports, leaving the previously noted
TODO ("check that unsupported types cannot be created") unaddressed.

Add test_create_invalid_type() which issues a raw KVM_CREATE_VM for a
given type and asserts it fails with -EINVAL, and invoke it for
KVM_X86_SEV_ES_VM / KVM_X86_SNP_VM whenever the corresponding capability
is not advertised. Because vm_create_barebones_type() asserts that
KVM_CREATE_VM succeeds, the check is issued directly against the KVM fd
so the negative path can be observed.

TEST_ASSERT() aborts the program when its condition fails, so the helper
releases the fd and latches errno before asserting: if KVM_CREATE_VM
ever wrongly succeeds, the VM fd is closed rather than leaked, and errno
is captured before close() can clobber it.

While here, stop shadowing the file-scope kvm_fd with a local in main()
so the new helper can use it.

Signed-off-by: Hemanth Selam <[email protected]>
---
 .../selftests/kvm/x86/sev_init2_tests.c       | 27 +++++++++++++++----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86/sev_init2_tests.c 
b/tools/testing/selftests/kvm/x86/sev_init2_tests.c
index 8db88c355f16..7663418b3fa8 100644
--- a/tools/testing/selftests/kvm/x86/sev_init2_tests.c
+++ b/tools/testing/selftests/kvm/x86/sev_init2_tests.c
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <pthread.h>
+#include <unistd.h>
 
 #include "test_util.h"
 #include "kvm_util.h"
@@ -73,19 +74,34 @@ static void test_init2_invalid(unsigned long vm_type, 
struct kvm_sev_init *init,
        kvm_vm_free(vm);
 }
 
+static void test_create_invalid_type(unsigned long vm_type, const char *msg)
+{
+       int fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, (void *)vm_type);
+       int err = errno;
+
+       /* Clean up before asserting; TEST_ASSERT() aborts on failure. */
+       if (fd >= 0)
+               close(fd);
+
+       TEST_ASSERT(fd < 0 && err == EINVAL,
+                   "KVM_CREATE_VM should reject unsupported type (%s), got 
fd=%d errno=%d",
+                   msg, fd, err);
+}
+
 void test_vm_types(void)
 {
        test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){});
 
-       /*
-        * TODO: check that unsupported types cannot be created.  Probably
-        * a separate selftest.
-        */
+       /* Unsupported types must be rejected by KVM_CREATE_VM itself. */
        if (have_sev_es)
                test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){});
+       else
+               test_create_invalid_type(KVM_X86_SEV_ES_VM, "SEV-ES is 
unsupported");
 
        if (have_snp)
                test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){});
+       else
+               test_create_invalid_type(KVM_X86_SNP_VM, "SEV-SNP is 
unsupported");
 
        test_init2_invalid(0, &(struct kvm_sev_init){},
                           "VM type is KVM_X86_DEFAULT_VM");
@@ -121,9 +137,10 @@ void test_features(u32 vm_type, u64 supported_features)
 
 int main(int argc, char *argv[])
 {
-       int kvm_fd = open_kvm_dev_path_or_exit();
        bool have_sev;
 
+       kvm_fd = open_kvm_dev_path_or_exit();
+
        TEST_REQUIRE(__kvm_has_device_attr(kvm_fd, KVM_X86_GRP_SEV,
                                           KVM_X86_SEV_VMSA_FEATURES) == 0);
        kvm_device_attr_get(kvm_fd, KVM_X86_GRP_SEV,
-- 
2.43.7


Reply via email to