MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / test_virtio_device

Function test_virtio_device

virtio-devices/src/vsock/device.rs:583–678  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

581
582 #[test]
583 fn test_virtio_device() {
584 let mut ctx = TestContext::new();
585 let avail_features = (1u64 << VIRTIO_F_VERSION_1) | (1u64 << VIRTIO_F_IN_ORDER);
586 let device_features = avail_features;
587 let driver_features: u64 = avail_features | 1 | (1 << 32);
588 let device_pages = [
589 (device_features & 0xffff_ffff) as u32,
590 (device_features >> 32) as u32,
591 ];
592 let driver_pages = [
593 (driver_features & 0xffff_ffff) as u32,
594 (driver_features >> 32) as u32,
595 ];
596 assert_eq!(ctx.device.device_type(), VirtioDeviceType::Vsock as u32);
597 assert_eq!(ctx.device.queue_max_sizes(), QUEUE_SIZES);
598 assert_eq!(ctx.device.features() as u32, device_pages[0]);
599 assert_eq!((ctx.device.features() >> 32) as u32, device_pages[1]);
600
601 // Ack device features, page 0.
602 ctx.device.ack_features(u64::from(driver_pages[0]));
603 // Ack device features, page 1.
604 ctx.device.ack_features(u64::from(driver_pages[1]) << 32);
605 // Check that no side effect are present, and that the acked features are exactly the same
606 // as the device features.
607 assert_eq!(
608 ctx.device.common.acked_features,
609 device_features & driver_features
610 );
611
612 // Test reading 32-bit chunks.
613 let mut data = [0u8; 8];
614 ctx.device.read_config(0, &mut data[..4]);
615 assert_eq!(
616 u64::from(LittleEndian::read_u32(&data)),
617 ctx.cid & 0xffff_ffff
618 );
619 ctx.device.read_config(4, &mut data[4..]);
620 assert_eq!(
621 u64::from(LittleEndian::read_u32(&data[4..])),
622 (ctx.cid >> 32) & 0xffff_ffff
623 );
624
625 // Test reading 64-bit.
626 let mut data = [0u8; 8];
627 ctx.device.read_config(0, &mut data);
628 assert_eq!(LittleEndian::read_u64(&data), ctx.cid);
629
630 // Check that out-of-bounds reading doesn't mutate the destination buffer.
631 let mut data = [0u8, 1, 2, 3, 4, 5, 6, 7];
632 ctx.device.read_config(2, &mut data);
633 assert_eq!(data, [0u8, 1, 2, 3, 4, 5, 6, 7]);
634
635 // Just covering lines here, since the vsock device has no writable config.
636 // A warning is, however, logged, if the guest driver attempts to write any config data.
637 ctx.device.write_config(0, &data[..4]);
638
639 let memory = GuestMemoryAtomic::new(ctx.mem.clone());
640

Callers

nothing calls this directly

Calls 6

newFunction · 0.85
ack_featuresMethod · 0.45
read_configMethod · 0.45
write_configMethod · 0.45
cloneMethod · 0.45
activateMethod · 0.45

Tested by

no test coverage detected