Skip to content

Commit 04c8df1

Browse files
committed
[Rust] Add simple architecture tests
Still need to add a custom architecture for unit tests like in python
1 parent 84b1944 commit 04c8df1

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

rust/tests/architecture.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use binaryninja::architecture::{Architecture, CoreArchitecture};
2+
use binaryninja::disassembly::{
3+
InstructionTextToken, InstructionTextTokenContext, InstructionTextTokenKind,
4+
};
5+
use binaryninja::headless::Session;
6+
7+
#[test]
8+
fn test_architecture_info() {
9+
let _session = Session::new().expect("Failed to initialize session");
10+
let arch = CoreArchitecture::by_name("x86_64").expect("Failed to get architecture");
11+
assert_eq!(arch.name(), "x86_64");
12+
assert_eq!(arch.endianness(), binaryninja::Endianness::LittleEndian);
13+
assert_eq!(arch.address_size(), 8);
14+
}
15+
16+
#[test]
17+
fn test_architecture_disassembly() {
18+
let _session = Session::new().expect("Failed to initialize session");
19+
let arch = CoreArchitecture::by_name("x86_64").expect("Failed to get architecture");
20+
21+
// mov rax, 0x10
22+
let data = b"\x48\xC7\xC0\x10\x00\x00\x00";
23+
let address = 0x1000;
24+
25+
let (instr_len, tokens) = arch
26+
.instruction_text(data, address)
27+
.expect("Failed to disassemble instruction");
28+
assert_eq!(instr_len, 7);
29+
30+
let expected_tokens: Vec<InstructionTextToken> = vec![
31+
InstructionTextToken {
32+
address: 0,
33+
text: "mov".to_string(),
34+
confidence: 255,
35+
context: InstructionTextTokenContext::Normal,
36+
expr_index: None,
37+
kind: InstructionTextTokenKind::Instruction,
38+
},
39+
InstructionTextToken {
40+
address: 0,
41+
text: " ".to_string(),
42+
confidence: 255,
43+
context: InstructionTextTokenContext::Normal,
44+
expr_index: None,
45+
kind: InstructionTextTokenKind::Text,
46+
},
47+
InstructionTextToken {
48+
address: 0,
49+
text: "rax".to_string(),
50+
confidence: 255,
51+
context: InstructionTextTokenContext::Normal,
52+
expr_index: None,
53+
kind: InstructionTextTokenKind::Register,
54+
},
55+
InstructionTextToken {
56+
address: 0,
57+
text: ", ".to_string(),
58+
confidence: 255,
59+
context: InstructionTextTokenContext::Normal,
60+
expr_index: None,
61+
kind: InstructionTextTokenKind::OperandSeparator,
62+
},
63+
InstructionTextToken {
64+
address: 0,
65+
text: "0x10".to_string(),
66+
confidence: 255,
67+
context: InstructionTextTokenContext::Normal,
68+
expr_index: None,
69+
kind: InstructionTextTokenKind::PossibleAddress {
70+
value: 16,
71+
size: Some(8),
72+
},
73+
},
74+
];
75+
76+
assert_eq!(tokens, expected_tokens);
77+
}

0 commit comments

Comments
 (0)