|
| 1 | +use binaryninja::binary_view::BinaryViewExt; |
| 2 | +use binaryninja::headless::Session; |
| 3 | +use binaryninja::section::{SectionBuilder, Semantics}; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn test_binary_section() { |
| 8 | + let _session = Session::new().expect("Failed to initialize session"); |
| 9 | + let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap(); |
| 10 | + let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); |
| 11 | + // This binary has a lot of sections! |
| 12 | + assert_eq!(view.sections().len(), 139); |
| 13 | + |
| 14 | + // Make sure we are not grabbing garbage. |
| 15 | + assert_eq!(view.section_by_name("test"), None); |
| 16 | + |
| 17 | + // Just test a bunch of properties of a section. |
| 18 | + let section = view.section_by_name(".rdata").unwrap(); |
| 19 | + assert_eq!(section.name(), ".rdata".into()); |
| 20 | + let image_base = view.original_image_base(); |
| 21 | + let section_start = image_base + 0x25efc; |
| 22 | + let section_end = image_base + 0x25f04; |
| 23 | + assert_eq!(section.start(), section_start); |
| 24 | + assert_eq!(section.end(), image_base + 0x25f04); |
| 25 | + assert_eq!(section.len(), 0x8); |
| 26 | + assert_eq!(section.address_range(), section_start..section_end); |
| 27 | + assert_eq!(section.auto_defined(), true); |
| 28 | + assert_eq!(section.semantics(), Semantics::ReadOnlyData); |
| 29 | + |
| 30 | + let sections_at = view.sections_at(image_base + 0x25efc); |
| 31 | + assert_eq!(sections_at.len(), 1); |
| 32 | + let same_section = sections_at.to_vec()[0].to_owned(); |
| 33 | + assert_eq!(section, same_section); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_add_remove_section() { |
| 38 | + let _session = Session::new().expect("Failed to initialize session"); |
| 39 | + let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap(); |
| 40 | + let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); |
| 41 | + |
| 42 | + // Remove the rdata section! |
| 43 | + let old_section = view |
| 44 | + .section_by_name(".rdata") |
| 45 | + .expect("Failed to find rdata section"); |
| 46 | + view.remove_auto_section(".rdata"); |
| 47 | + assert!(view.section_by_name(".rdata").is_none()); |
| 48 | + |
| 49 | + // Add a new section and compare with the old section |
| 50 | + let new_section_builder = SectionBuilder::from(&old_section); |
| 51 | + view.add_section(new_section_builder); |
| 52 | + let new_section = view |
| 53 | + .section_by_name(".rdata") |
| 54 | + .expect("Failed to find new section"); |
| 55 | + assert_eq!(old_section, new_section); |
| 56 | +} |
0 commit comments