Skip to content

Commit 5af9a46

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 b10bb6a commit 5af9a46

File tree

3 files changed

+34
-64
lines changed

3 files changed

+34
-64
lines changed

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,27 @@ macro_rules! impl_device_type {
194194
#[cfg(test)]
195195
pub(crate) mod tests {
196196
use super::*;
197+
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
197198

198199
#[derive(Debug)]
199200
struct MockVirtioDevice {
201+
avail_features: u64,
200202
acked_features: u64,
201203
}
202204

203205
impl VirtioDevice for MockVirtioDevice {
204206
impl_device_type!(0);
205207

206208
fn avail_features(&self) -> u64 {
207-
todo!()
209+
self.avail_features
208210
}
209211

210212
fn acked_features(&self) -> u64 {
211213
self.acked_features
212214
}
213215

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

218220
fn queues(&self) -> &[Queue] {
@@ -254,7 +256,10 @@ pub(crate) mod tests {
254256

255257
#[test]
256258
fn test_has_feature() {
257-
let mut device = MockVirtioDevice { acked_features: 0 };
259+
let mut device = MockVirtioDevice {
260+
avail_features: 0,
261+
acked_features: 0,
262+
};
258263

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

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)