@@ -1563,6 +1563,44 @@ mod snapshot_file_tests {
15631563 ) ;
15641564 }
15651565
1566+ #[ test]
1567+ fn hypervisor_mismatch_rejected ( ) {
1568+ let snapshot = create_snapshot_from_binary ( ) ;
1569+
1570+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
1571+ let snap_path = dir. path ( ) . join ( "wrong_hv.hls" ) ;
1572+ snapshot. to_file ( & snap_path) . unwrap ( ) ;
1573+
1574+ // Overwrite the hypervisor tag (offset 168, 8 bytes) with a
1575+ // valid but wrong hypervisor tag.
1576+ use super :: HypervisorTag ;
1577+ let current = HypervisorTag :: current ( ) . unwrap ( ) ;
1578+ let wrong_tag = match current {
1579+ HypervisorTag :: Whp => HypervisorTag :: Kvm ,
1580+ _ => HypervisorTag :: Whp ,
1581+ } ;
1582+ {
1583+ use std:: io:: { Seek , SeekFrom , Write } ;
1584+ let mut file = std:: fs:: OpenOptions :: new ( )
1585+ . write ( true )
1586+ . open ( & snap_path)
1587+ . unwrap ( ) ;
1588+ file. seek ( SeekFrom :: Start ( 168 ) ) . unwrap ( ) ;
1589+ file. write_all ( & ( wrong_tag as u64 ) . to_le_bytes ( ) ) . unwrap ( ) ;
1590+ }
1591+
1592+ let result = Snapshot :: from_file ( & snap_path) ;
1593+ let err_msg = match result {
1594+ Err ( e) => format ! ( "{}" , e) ,
1595+ Ok ( _) => panic ! ( "expected load to fail with hypervisor mismatch" ) ,
1596+ } ;
1597+ assert ! (
1598+ err_msg. contains( "hypervisor mismatch" ) ,
1599+ "expected hypervisor mismatch error, got: {}" ,
1600+ err_msg
1601+ ) ;
1602+ }
1603+
15661604 #[ test]
15671605 fn restore_from_loaded_snapshot ( ) {
15681606 let mut sbox = create_test_sandbox ( ) ;
0 commit comments