Skip to content

Commit 6aa32c7

Browse files
gabhijitalexandruag
authored andcommitted
vcpu: Test Case for num_entries too small CpuId
Kernel returns an error `E2BIG` if the number of entries passed to `get_cpuid2` is too small. Added a test case for that assertion. Also, added assertion to check `errno` to `ENOMEM`, when passed number of entries is greater than `KVM_MAX_CPUID_ENTRIES` (previously the assertion was there only for `is_err`). Signed-off-by: Abhijit Gadgil <[email protected]>
1 parent 8c7b417 commit 6aa32c7

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/ioctls/vcpu.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,8 +1427,27 @@ mod tests {
14271427
if kvm.check_extension(Cap::ExtCpuid) {
14281428
let vm = kvm.create_vm().unwrap();
14291429
let vcpu = vm.create_vcpu(0).unwrap();
1430-
let err_cpuid = vcpu.get_cpuid2(KVM_MAX_CPUID_ENTRIES + 1 as usize);
1431-
assert!(err_cpuid.is_err());
1430+
let err_cpuid = vcpu.get_cpuid2(KVM_MAX_CPUID_ENTRIES + 1 as usize).err();
1431+
assert_eq!(err_cpuid.unwrap().errno(), libc::ENOMEM);
1432+
}
1433+
}
1434+
1435+
#[cfg(target_arch = "x86_64")]
1436+
#[test]
1437+
fn test_get_cpuid_fail_num_entries_too_small() {
1438+
let kvm = Kvm::new().unwrap();
1439+
if kvm.check_extension(Cap::ExtCpuid) {
1440+
let vm = kvm.create_vm().unwrap();
1441+
let cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
1442+
let ncpuids = cpuid.as_slice().len();
1443+
assert!(ncpuids <= KVM_MAX_CPUID_ENTRIES);
1444+
let nr_vcpus = kvm.get_nr_vcpus();
1445+
for cpu_idx in 0..nr_vcpus {
1446+
let vcpu = vm.create_vcpu(cpu_idx as u8).unwrap();
1447+
vcpu.set_cpuid2(&cpuid).unwrap();
1448+
let err = vcpu.get_cpuid2(ncpuids - 1 as usize).err();
1449+
assert_eq!(err.unwrap().errno(), libc::E2BIG);
1450+
}
14321451
}
14331452
}
14341453

0 commit comments

Comments
 (0)