Skip to content

Commit 5427af6

Browse files
committed
refactor: move VirtiDevice trait function testing into device.rs
Instead of testing default implementations for avail_features_by_page and ack_features_by_page in rng and net devices, test it in device.rs Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 763f2f9 commit 5427af6

File tree

3 files changed

+33
-64
lines changed

3 files changed

+33
-64
lines changed

src/vmm/src/devices/virtio/device.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,23 @@ pub(crate) mod tests {
197197

198198
#[derive(Debug)]
199199
struct MockVirtioDevice {
200+
avail_features: u64,
200201
acked_features: u64,
201202
}
202203

203204
impl VirtioDevice for MockVirtioDevice {
204205
impl_device_type!(0);
205206

206207
fn avail_features(&self) -> u64 {
207-
todo!()
208+
self.avail_features
208209
}
209210

210211
fn acked_features(&self) -> u64 {
211212
self.acked_features
212213
}
213214

214-
fn set_acked_features(&mut self, _acked_features: u64) {
215-
todo!()
215+
fn set_acked_features(&mut self, acked_features: u64) {
216+
self.acked_features = acked_features
216217
}
217218

218219
fn queues(&self) -> &[Queue] {
@@ -254,7 +255,10 @@ pub(crate) mod tests {
254255

255256
#[test]
256257
fn test_has_feature() {
257-
let mut device = MockVirtioDevice { acked_features: 0 };
258+
let mut device = MockVirtioDevice {
259+
avail_features: 0,
260+
acked_features: 0,
261+
};
258262

259263
let mock_feature_1 = 1u64;
260264
assert!(!device.has_feature(mock_feature_1));
@@ -267,4 +271,29 @@ pub(crate) mod tests {
267271
assert!(device.has_feature(mock_feature_1));
268272
assert!(device.has_feature(mock_feature_2));
269273
}
274+
275+
#[test]
276+
fn test_features() {
277+
let features: u64 = 0x11223344_55667788;
278+
279+
let mut device = MockVirtioDevice {
280+
avail_features: features,
281+
acked_features: 0,
282+
};
283+
284+
assert_eq!(
285+
device.avail_features_by_page(0),
286+
(features & 0xFFFFFFFF) as u32,
287+
);
288+
assert_eq!(device.avail_features_by_page(1), (features >> 32) as u32);
289+
for i in 2..10 {
290+
assert_eq!(device.avail_features_by_page(i), 0u32);
291+
}
292+
293+
for i in 0..10 {
294+
device.ack_features_by_page(i, u32::MAX);
295+
}
296+
297+
assert_eq!(device.acked_features, features);
298+
}
270299
}

src/vmm/src/devices/virtio/net/device.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,41 +1156,6 @@ pub mod tests {
11561156
assert_eq!(net.device_type(), VIRTIO_ID_NET);
11571157
}
11581158

1159-
#[test]
1160-
fn test_virtio_device_features() {
1161-
let mut net = default_net();
1162-
set_mac(&mut net, MacAddr::from_str("11:22:33:44:55:66").unwrap());
1163-
1164-
// Test `features()` and `ack_features()`.
1165-
let features = (1 << VIRTIO_NET_F_GUEST_CSUM)
1166-
| (1 << VIRTIO_NET_F_CSUM)
1167-
| (1 << VIRTIO_NET_F_GUEST_TSO4)
1168-
| (1 << VIRTIO_NET_F_GUEST_TSO6)
1169-
| (1 << VIRTIO_NET_F_MAC)
1170-
| (1 << VIRTIO_NET_F_GUEST_UFO)
1171-
| (1 << VIRTIO_NET_F_HOST_TSO4)
1172-
| (1 << VIRTIO_NET_F_HOST_TSO6)
1173-
| (1 << VIRTIO_NET_F_HOST_UFO)
1174-
| (1 << VIRTIO_F_VERSION_1)
1175-
| (1 << VIRTIO_NET_F_MRG_RXBUF)
1176-
| (1 << VIRTIO_RING_F_EVENT_IDX);
1177-
1178-
assert_eq!(
1179-
net.avail_features_by_page(0),
1180-
(features & 0xFFFFFFFF) as u32,
1181-
);
1182-
assert_eq!(net.avail_features_by_page(1), (features >> 32) as u32);
1183-
for i in 2..10 {
1184-
assert_eq!(net.avail_features_by_page(i), 0u32);
1185-
}
1186-
1187-
for i in 0..10 {
1188-
net.ack_features_by_page(i, u32::MAX);
1189-
}
1190-
1191-
assert_eq!(net.acked_features, features);
1192-
}
1193-
11941159
#[test]
11951160
// Test that `Net::build_tap_offload_features` creates the TAP offload features that we expect
11961161
// it to do, based on the available guest features

src/vmm/src/devices/virtio/rng/device.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -410,31 +410,6 @@ mod tests {
410410
assert_eq!(read_config, vec![0; 10]);
411411
}
412412

413-
#[test]
414-
fn test_virtio_device_features() {
415-
let mut entropy_dev = default_entropy();
416-
417-
let features = 1 << VIRTIO_F_VERSION_1;
418-
419-
assert_eq!(
420-
entropy_dev.avail_features_by_page(0),
421-
(features & 0xFFFFFFFF) as u32,
422-
);
423-
assert_eq!(
424-
entropy_dev.avail_features_by_page(1),
425-
(features >> 32) as u32
426-
);
427-
for i in 2..10 {
428-
assert_eq!(entropy_dev.avail_features_by_page(i), 0u32);
429-
}
430-
431-
for i in 0..10 {
432-
entropy_dev.ack_features_by_page(i, u32::MAX);
433-
}
434-
435-
assert_eq!(entropy_dev.acked_features, features);
436-
}
437-
438413
#[test]
439414
fn test_handle_one() {
440415
let mem = create_virtio_mem();

0 commit comments

Comments
 (0)