@@ -553,4 +553,30 @@ mod tests {
553553
554554 assert_eq ! ( p1, p2, "Deterministic patching failed" ) ;
555555 }
556+
557+ #[ test]
558+ fn test_inplace_repatching ( ) {
559+ let mock = create_mock_pe ( ) ;
560+
561+ // 1. First pass: appends the section
562+ let patched_v1 = patch_pe ( & mock, false , "app.exe" ) . unwrap ( ) ;
563+ let pe_ptr = read_u32 ( & patched_v1, 0x3C ) as usize ;
564+ let coff = pe_ptr + 4 ;
565+ assert_eq ! ( read_u16( & patched_v1, coff + 2 ) , 2 , "Should have generated section" ) ;
566+
567+ // 2. Second pass: updates it to disabled. It must NOT add another section!
568+ let patched_v2 = patch_pe ( & patched_v1, true , "app.exe" ) . unwrap ( ) ;
569+ let pe_ptr2 = read_u32 ( & patched_v2, 0x3C ) as usize ;
570+ assert_eq ! ( read_u16( & patched_v2, pe_ptr2 + 6 ) , 2 , "Should have kept 2 sections (in-place edit)" ) ;
571+
572+ // 3. Verify it's actually disabled (value is 0)
573+ // In our mock, target_val sits at the beginning of new section's raw data.
574+ // The new section's raw data is appended at 1024.
575+ assert_eq ! ( read_u32( & patched_v2, 1024 ) , 0 , "Symbol value should have been toggled to 0" ) ;
576+
577+ // 4. Third pass: toggle back to enabled.
578+ let patched_v3 = patch_pe ( & patched_v2, false , "app.exe" ) . unwrap ( ) ;
579+ assert_eq ! ( read_u32( & patched_v3, 1024 ) , 1 , "Symbol value should have been toggled back to 1" ) ;
580+ assert_eq ! ( read_u16( & patched_v3, pe_ptr2 + 6 ) , 2 , "Section count remains 2" ) ;
581+ }
556582}
0 commit comments