-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.rs
123 lines (108 loc) · 4.19 KB
/
verify.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::{
bytes::{hexdump, serde::hkx_header::HkxHeader},
errors::SerdeHkxError,
from_bytes, from_str,
tests::{diff, ClassMap},
to_bytes, to_string, HavokSort as _,
};
use pretty_assertions::assert_eq;
use winnow::Parser;
type Result<T> = core::result::Result<T, SerdeHkxError>;
#[cfg_attr(miri, ignore)] // Unexplained hang
#[test]
#[cfg_attr(
all(feature = "tracing", not(miri)),
quick_tracing::init(test = "should_reproduce_xml_to_amd64", stdio = false)
)]
fn should_reproduce_xml_to_amd64() {
let xml = {
// include_str!("../../../docs/handson_hex_dump/defaultmale/defaultmale_x86.xml")
include_str!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton.xml")
// include_str!("../../../tests/test/test.xml")
};
let expected_bytes = {
// include_bytes!("../../../docs/handson_hex_dump/defaultmale/defaultmale.hkx")
// include_bytes!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton.hkx")
include_bytes!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton_x64_reconverted.hkx")
// include_bytes!("../../../tests/test/test_x64_reconverted.hkx")
};
if let Err(err) = assert_bytes(xml, expected_bytes) {
tracing::error!("{err}");
panic!("{err}")
}
}
#[cfg_attr(miri, ignore)] // Unexplained hang
#[test]
#[cfg_attr(
all(feature = "tracing", not(miri)),
quick_tracing::init(test = "should_reproduce_xml_to_win32", stdio = false)
)]
fn should_reproduce_xml_to_win32() {
let xml = {
// include_str!("../../../docs/handson_hex_dump/defaultmale/defaultmale_x86.xml")
include_str!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton.xml")
// include_str!("../../../tests/test/test.xml")
};
let expected_bytes =
include_bytes!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton_x86_reconverted.hkx");
if let Err(err) = assert_bytes(xml, expected_bytes) {
tracing::error!("{err}");
panic!("{err}")
}
}
fn assert_bytes(xml: &str, expected_bytes: &[u8]) -> Result<()> {
let actual_bytes = {
let mut actual_classes: ClassMap = from_str(xml)?;
actual_classes.sort_for_bytes();
let (_remain, header) = HkxHeader::from_bytes().parse_peek(expected_bytes).unwrap();
to_bytes(&actual_classes, &header)?
};
// Assert hexdump
{
let actual_hex_dump = hexdump::to_string(&actual_bytes);
let expected_hex_dump = hexdump::to_string(expected_bytes);
let hexdump_diff = diff(&actual_hex_dump, &expected_hex_dump);
tracing::debug!("hexdump_diff = \n{hexdump_diff}");
assert_eq!(actual_hex_dump, expected_hex_dump);
}
// Ast diff
{
let actual_classes: ClassMap = from_bytes(&actual_bytes)?;
let expected_classes: ClassMap = from_bytes(expected_bytes)?;
let ast_diff = diff(
format!("{expected_classes:#?}"),
format!("{actual_classes:#?}"),
);
tracing::debug!("ast_diff = \n{ast_diff}");
}
Ok(())
}
#[test]
#[ignore = "Because it can't be fully reproduced yet"] // Should ignore miri test, because unexplained hang.
#[quick_tracing::try_init(test = "should_reproduce_xml", stdio = false)]
fn should_reproduce_xml() -> Result<()> {
let bytes = {
// include_bytes!("../../../docs/handson_hex_dump/defaultmale/defaultmale.hkx")
// include_bytes!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton.hkx")
include_bytes!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton_x64_reconverted.hkx")
};
let expected = {
// include_str!("../../../docs/handson_hex_dump/defaultmale/defaultmale_x86.xml")
include_str!("../../../docs/handson_hex_dump/wisp_skeleton/skeleton.xml")
};
let bytes_to_xml = || {
let mut actual_classes: ClassMap = from_bytes(bytes)?;
let top_ptr = actual_classes.sort_for_xml()?;
Result::Ok(to_string(&actual_classes, top_ptr)?)
};
let actual = match bytes_to_xml() {
Ok(xml) => xml,
Err(err) => {
tracing::error!("{err}");
panic!("{err}")
}
};
let xml_diff = diff(actual, expected);
tracing::debug!("map_diff = \n{xml_diff}");
Ok(())
}