|
| 1 | +use binaryninja::binary_view::BinaryViewExt; |
| 2 | +use binaryninja::headless::Session; |
| 3 | +use binaryninja::platform::Platform; |
| 4 | +use binaryninja::type_library::TypeLibrary; |
| 5 | +use binaryninja::types::{Type, TypeClass}; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_type_library() { |
| 10 | + let _session = Session::new().expect("Failed to initialize session"); |
| 11 | + let platform = Platform::by_name("windows-x86").expect("windows-x86 exists"); |
| 12 | + let library = platform |
| 13 | + .get_type_library_by_name("crypt32.dll") |
| 14 | + .expect("crypt32.dll exists"); |
| 15 | + |
| 16 | + println!("{:#?}", library); |
| 17 | + assert_eq!(library.name(), "crypt32.dll"); |
| 18 | + assert_eq!(library.dependency_name(), "crypt32.dll"); |
| 19 | + assert!(library.alternate_names().is_empty()); |
| 20 | + assert_eq!(library.platform_names().to_vec(), vec!["windows-x86"]); |
| 21 | + |
| 22 | + // Check some types. |
| 23 | + let type_0 = library |
| 24 | + .get_named_type("SIP_ADD_NEWPROVIDER".into()) |
| 25 | + .unwrap(); |
| 26 | + println!("{:#?}", type_0); |
| 27 | + assert_eq!(type_0.width(), 48); |
| 28 | + assert_eq!(type_0.type_class(), TypeClass::StructureTypeClass); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn test_applying_type_library() { |
| 33 | + let _session = Session::new().expect("Failed to initialize session"); |
| 34 | + let platform = Platform::by_name("windows-x86").expect("windows-x86 exists"); |
| 35 | + let library = platform |
| 36 | + .get_type_library_by_name("crypt32.dll") |
| 37 | + .expect("crypt32.dll exists"); |
| 38 | + |
| 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 | + view.add_type_library(&library); |
| 42 | + |
| 43 | + let view_library = view |
| 44 | + .type_library_by_name("crypt32.dll") |
| 45 | + .expect("crypt32.dll exists"); |
| 46 | + assert_eq!(view_library.name(), "crypt32.dll"); |
| 47 | + |
| 48 | + // Type library types don't exist in the view until they are imported. |
| 49 | + // Adding the type library to the view will let you import types from it without necessarily knowing "where" they came from. |
| 50 | + let found_lib_type = view |
| 51 | + .import_type_library("SIP_ADD_NEWPROVIDER", None) |
| 52 | + .expect("SIP_ADD_NEWPROVIDER exists"); |
| 53 | + assert_eq!(found_lib_type.width(), 48); |
| 54 | + // Per docs type is returned as a NamedTypeReferenceClass. |
| 55 | + assert_eq!( |
| 56 | + found_lib_type.type_class(), |
| 57 | + TypeClass::NamedTypeReferenceClass |
| 58 | + ); |
| 59 | + |
| 60 | + // Check that the type is actually in the view now. |
| 61 | + view.type_by_name("SIP_ADD_NEWPROVIDER") |
| 62 | + .expect("SIP_ADD_NEWPROVIDER exists"); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn test_create_type_library() { |
| 67 | + let _session = Session::new().expect("Failed to initialize session"); |
| 68 | + let platform = Platform::by_name("windows-x86").expect("windows-x86 exists"); |
| 69 | + let arch = platform.arch(); |
| 70 | + |
| 71 | + // Create the new type library. |
| 72 | + let my_library = TypeLibrary::new(arch, "test_type_lib"); |
| 73 | + my_library.add_alternate_name("alternate_test"); |
| 74 | + my_library.add_platform(&platform); |
| 75 | + my_library.add_named_type("test_type".into(), &Type::int(7, true)); |
| 76 | + |
| 77 | + // Write the library to a file. |
| 78 | + let temp_dir = tempfile::tempdir().expect("Failed to create temp dir"); |
| 79 | + let my_library_path = temp_dir.path().join("test_type_lib.bntl"); |
| 80 | + assert!(my_library.write_to_file(&my_library_path)); |
| 81 | + |
| 82 | + // Verify the contents of the created file. |
| 83 | + let loaded_library = |
| 84 | + TypeLibrary::load_from_file(&my_library_path).expect("Failed to load type library"); |
| 85 | + assert_eq!(loaded_library.name(), "test_type_lib"); |
| 86 | + assert_eq!( |
| 87 | + loaded_library.alternate_names().to_vec(), |
| 88 | + vec!["alternate_test"] |
| 89 | + ); |
| 90 | + assert_eq!( |
| 91 | + loaded_library.platform_names().to_vec(), |
| 92 | + vec!["windows-x86"] |
| 93 | + ); |
| 94 | + assert_eq!( |
| 95 | + loaded_library |
| 96 | + .get_named_type("test_type".into()) |
| 97 | + .unwrap() |
| 98 | + .width(), |
| 99 | + 7 |
| 100 | + ); |
| 101 | +} |
0 commit comments