Skip to content

Commit b5949dc

Browse files
committed
Uncomment tests and change tests to new system
Signed-off-by: Gabriel Keller <gabrieljameskeller@gmail.com>
1 parent 88e12f9 commit b5949dc

File tree

1 file changed

+73
-136
lines changed

1 file changed

+73
-136
lines changed

src/vmm/src/snapshot/mod.rs

Lines changed: 73 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -261,140 +261,77 @@ mod tests {
261261
);
262262
}
263263

264-
// #[test]
265-
// fn test_bad_snapshot_size() {
266-
// let snapshot_data = vec![0u8; 1];
267-
268-
// let snapshot = SnapshotHdr::new(Version::new(1, 6, 1));
269-
// assert!(matches!(
270-
// snapshot.load_with_version_check::<_, u8>(
271-
// &mut snapshot_data.as_slice(),
272-
// snapshot_data.len()
273-
// ),
274-
// Err(SnapshotError::InvalidSnapshotSize)
275-
// ));
276-
// }
277-
278-
// #[test]
279-
// fn test_bad_reader() {
280-
// #[derive(Debug)]
281-
// struct BadReader;
282-
283-
// impl Read for BadReader {
284-
// fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
285-
// Err(std::io::ErrorKind::InvalidInput.into())
286-
// }
287-
// }
288-
289-
// let mut reader = BadReader {};
290-
291-
// let snapshot = Snapshot::new(Version::new(42, 27, 18));
292-
// assert!(matches!(
293-
// snapshot.load_with_version_check::<_, u8>(&mut reader, 1024),
294-
// Err(SnapshotError::Io(_))
295-
// ));
296-
// }
297-
298-
// #[test]
299-
// fn test_bad_magic() {
300-
// let mut data = vec![0u8; 100];
301-
302-
// let snapshot = Snapshot::new(Version::new(24, 16, 1));
303-
// snapshot.save(&mut data.as_mut_slice(), &42u8).unwrap();
304-
305-
// // Writing dummy values in the first bytes of the snapshot data (we are on little-endian
306-
// // machines) should trigger an `Error::InvalidMagic` error.
307-
// data[0] = 0x01;
308-
// data[1] = 0x02;
309-
// data[2] = 0x03;
310-
// data[3] = 0x04;
311-
// data[4] = 0x42;
312-
// data[5] = 0x43;
313-
// data[6] = 0x44;
314-
// data[7] = 0x45;
315-
// assert!(matches!(
316-
// Snapshot::unchecked_load::<_, u8>(&mut data.as_slice()),
317-
// Err(SnapshotError::InvalidMagic(0x4544_4342_0403_0201u64))
318-
// ));
319-
// }
320-
321-
// #[test]
322-
// fn test_bad_crc() {
323-
// let mut data = vec![0u8; 100];
324-
325-
// let snapshot = Snapshot::new(Version::new(12, 1, 3));
326-
// snapshot.save(&mut data.as_mut_slice(), &42u8).unwrap();
327-
328-
// // Tamper the bytes written, without touching the previously CRC.
329-
// snapshot
330-
// .save_without_crc(&mut data.as_mut_slice(), &43u8)
331-
// .unwrap();
332-
333-
// assert!(matches!(
334-
// snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
335-
// Err(SnapshotError::Crc64(_))
336-
// ));
337-
// }
338-
339-
// #[test]
340-
// fn test_bad_version() {
341-
// let mut data = vec![0u8; 100];
342-
343-
// // We write a snapshot with version "v1.3.12"
344-
// let snapshot = Snapshot::new(Version::new(1, 3, 12));
345-
// snapshot.save(&mut data.as_mut_slice(), &42u8).unwrap();
346-
347-
// // Different major versions should not work
348-
// let snapshot = Snapshot::new(Version::new(2, 3, 12));
349-
// assert!(matches!(
350-
// snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
351-
// Err(SnapshotError::InvalidFormatVersion(Version {
352-
// major: 1,
353-
// minor: 3,
354-
// patch: 12,
355-
// ..
356-
// }))
357-
// ));
358-
// let snapshot = Snapshot::new(Version::new(0, 3, 12));
359-
// assert!(matches!(
360-
// snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
361-
// Err(SnapshotError::InvalidFormatVersion(Version {
362-
// major: 1,
363-
// minor: 3,
364-
// patch: 12,
365-
// ..
366-
// }))
367-
// ));
368-
369-
// // We can't support minor versions bigger than ours
370-
// let snapshot = Snapshot::new(Version::new(1, 2, 12));
371-
// assert!(matches!(
372-
// snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
373-
// Err(SnapshotError::InvalidFormatVersion(Version {
374-
// major: 1,
375-
// minor: 3,
376-
// patch: 12,
377-
// ..
378-
// }))
379-
// ));
380-
381-
// // But we can support minor versions smaller or equeal to ours. We also support
382-
// // all patch versions within our supported major.minor version.
383-
// let snapshot = Snapshot::new(Version::new(1, 4, 12));
384-
// snapshot
385-
// .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
386-
// .unwrap();
387-
// let snapshot = Snapshot::new(Version::new(1, 3, 0));
388-
// snapshot
389-
// .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
390-
// .unwrap();
391-
// let snapshot = Snapshot::new(Version::new(1, 3, 12));
392-
// snapshot
393-
// .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
394-
// .unwrap();
395-
// let snapshot = Snapshot::new(Version::new(1, 3, 1024));
396-
// snapshot
397-
// .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
398-
// .unwrap();
399-
// }
264+
#[test]
265+
fn test_bad_snapshot_size() {
266+
let snapshot_data = vec![0u8; 1];
267+
268+
let snapshot = SnapshotHdr::new(Version::new(1, 6, 1));
269+
assert!(matches!(
270+
Snapshot::load::<_, u8>(
271+
&mut snapshot_data.as_slice(),
272+
),
273+
Err(SnapshotError::InvalidSnapshotSize)
274+
));
275+
}
276+
277+
#[test]
278+
fn test_bad_reader() {
279+
#[derive(Debug)]
280+
struct BadReader;
281+
282+
impl Read for BadReader {
283+
fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
284+
Err(std::io::ErrorKind::InvalidInput.into())
285+
}
286+
}
287+
288+
let mut reader = BadReader {};
289+
290+
assert!(matches!(
291+
Snapshot::load::<_, u8>(&mut reader),
292+
Err(SnapshotError::Io(_))
293+
));
294+
}
295+
296+
#[test]
297+
fn test_bad_magic() {
298+
let mut data = vec![0u8; 100];
299+
300+
let snapshot = Snapshot::new(Version::new(24, 16, 1), &42u8);
301+
snapshot.save(&mut data.as_mut_slice()).unwrap();
302+
303+
// Writing dummy values in the first bytes of the snapshot data (we are on little-endian
304+
// machines) should trigger an `Error::InvalidMagic` error.
305+
data[0] = 0x01;
306+
data[1] = 0x02;
307+
data[2] = 0x03;
308+
data[3] = 0x04;
309+
data[4] = 0x42;
310+
data[5] = 0x43;
311+
data[6] = 0x44;
312+
data[7] = 0x45;
313+
assert!(matches!(
314+
Snapshot::unchecked_load::<_, u8>(&mut data.as_slice()),
315+
Err(SnapshotError::InvalidMagic(0x4544_4342_0403_0201u64))
316+
));
317+
}
318+
319+
#[test]
320+
fn test_bad_crc() {
321+
let mut data = vec![0u8; 100];
322+
323+
let snapshot = Snapshot::new(Version::new(12, 1, 3), &42u8);
324+
snapshot.save(&mut data.as_mut_slice()).unwrap();
325+
326+
// Tamper the bytes written, without touching the previously CRC.
327+
let snapshot2 = Snapshot::new(Version::new(12, 1, 3), &43u8);
328+
snapshot2
329+
.save_without_crc(&mut data.as_mut_slice())
330+
.unwrap();
331+
332+
assert!(matches!(
333+
Snapshot::load::<_, u8>(&mut data.as_slice()),
334+
Err(SnapshotError::Crc64(_))
335+
));
336+
}
400337
}

0 commit comments

Comments
 (0)