forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_output.rs
More file actions
248 lines (229 loc) · 7.54 KB
/
Copy pathtest_output.rs
File metadata and controls
248 lines (229 loc) · 7.54 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use driver::DriverDataBase;
use hir::{
HirDb,
analysis::ty::ty_check::BodyOwner,
hir_def::{ItemKind, LitKind, attr::AttrArgValue},
};
use num_bigint::BigUint;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestMetadata {
pub display_name: String,
pub hir_name: String,
pub symbol_name: String,
pub object_name: String,
pub bytecode: Vec<u8>,
pub sonatina_observability_json: Option<String>,
pub value_param_count: usize,
pub effect_param_count: usize,
pub init_bytecode: Vec<u8>,
pub expected_revert: Option<ExpectedRevert>,
pub initial_balance: Option<Vec<u8>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExpectedRevert {
Any,
Selector([u8; 4]),
PanicCode(Vec<u8>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestModuleOutput {
pub tests: Vec<TestMetadata>,
}
impl TestModuleOutput {
pub(crate) fn extend(&mut self, other: Self) {
self.tests.extend(other.tests);
}
pub(crate) fn sort_tests(&mut self) {
self.tests.sort_by(|left, right| {
left.display_name
.cmp(&right.display_name)
.then_with(|| left.hir_name.cmp(&right.hir_name))
.then_with(|| left.symbol_name.cmp(&right.symbol_name))
.then_with(|| left.object_name.cmp(&right.object_name))
});
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TestRootMetadata {
pub hir_name: String,
pub display_name: String,
pub expected_revert: Option<ExpectedRevert>,
pub initial_balance: Option<Vec<u8>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TestRootMetadataError {
InvalidPackage(String),
Unsupported(String),
}
pub(crate) fn runtime_test_root_metadata<'db>(
db: &'db DriverDataBase,
owner: &mir::RuntimeFunctionOwner<'db>,
section_name: &mir::RuntimeSectionName,
) -> Result<TestRootMetadata, TestRootMetadataError> {
let mir::RuntimeSectionName::Test(hir_name) = section_name else {
return Err(TestRootMetadataError::InvalidPackage(format!(
"non-test section `{section_name:?}` used as a test root"
)));
};
let mir::RuntimeFunctionOwner::Synthetic(mir::RuntimeSyntheticSpec::TestRoot {
callee, ..
}) = owner
else {
return Err(TestRootMetadataError::InvalidPackage(format!(
"test section `{hir_name}` does not use a TestRoot wrapper"
)));
};
let Some(semantic) = callee.key(db).semantic(db) else {
return Err(TestRootMetadataError::InvalidPackage(format!(
"test section `{hir_name}` does not target a semantic instance"
)));
};
let BodyOwner::Func(func) = semantic.key(db).owner(db) else {
return Err(TestRootMetadataError::InvalidPackage(format!(
"test section `{hir_name}` does not target a function body"
)));
};
let attrs = ItemKind::from(func).attrs(db).ok_or_else(|| {
TestRootMetadataError::InvalidPackage(format!("test function `{hir_name}` has no attrs"))
})?;
let test_attr = attrs.get_attr(db, "test").ok_or_else(|| {
TestRootMetadataError::InvalidPackage(format!(
"test function `{hir_name}` is missing #[test]"
))
})?;
let expected_revert = parse_expected_revert(db, hir_name, test_attr)
.map_err(TestRootMetadataError::Unsupported)?;
let initial_balance = parse_test_balance_arg(db, hir_name, test_attr)?;
Ok(TestRootMetadata {
hir_name: hir_name.clone(),
display_name: hir_name.clone(),
expected_revert,
initial_balance,
})
}
fn parse_test_balance_arg<'db>(
db: &'db dyn HirDb,
test_name: &str,
test_attr: &hir::hir_def::attr::NormalAttr<'db>,
) -> Result<Option<Vec<u8>>, TestRootMetadataError> {
let balance = parse_test_attr_int_arg(
db,
test_name,
test_attr,
"balance",
"balance = ...",
"u256",
32,
)
.map_err(TestRootMetadataError::Unsupported)?;
Ok(balance.map(|value| value.to_bytes_be()))
}
pub fn parse_expected_revert<'db>(
db: &'db dyn HirDb,
test_name: &str,
test_attr: &hir::hir_def::attr::NormalAttr<'db>,
) -> Result<Option<ExpectedRevert>, String> {
let should_revert = test_attr.has_arg(db, "should_revert");
let has_panic = has_test_attr_key(db, test_attr, "panic");
let has_selector = has_test_attr_key(db, test_attr, "selector");
if !should_revert {
if has_panic && has_selector {
return Err(format!(
"invalid #[test] function `{test_name}`: `panic = ...` and `selector = ...` require `should_revert`"
));
}
if has_panic {
return Err(format!(
"invalid #[test] function `{test_name}`: `panic = ...` requires `should_revert`"
));
}
if has_selector {
return Err(format!(
"invalid #[test] function `{test_name}`: `selector = ...` requires `should_revert`"
));
}
return Ok(None);
}
let panic = parse_test_attr_int_arg(
db,
test_name,
test_attr,
"panic",
"should_revert, panic = ...",
"u256",
32,
)?;
let selector = parse_test_attr_int_arg(
db,
test_name,
test_attr,
"selector",
"should_revert, selector = ...",
"u32",
4,
)?;
if panic.is_some() && selector.is_some() {
return Err(format!(
"invalid #[test] function `{test_name}`: #[test(should_revert)] cannot combine `panic = ...` and `selector = ...`"
));
}
if let Some(code) = panic {
let mut payload = Vec::with_capacity(36);
payload.extend_from_slice(&[0x4e, 0x48, 0x7b, 0x71]);
let code_bytes = code.to_bytes_be();
let mut padded = [0u8; 32];
padded[32 - code_bytes.len()..].copy_from_slice(&code_bytes);
payload.extend_from_slice(&padded);
return Ok(Some(ExpectedRevert::PanicCode(payload)));
}
if let Some(sel) = selector {
let bytes = sel.to_bytes_be();
let mut selector = [0u8; 4];
selector[4 - bytes.len()..].copy_from_slice(&bytes);
return Ok(Some(ExpectedRevert::Selector(selector)));
}
Ok(Some(ExpectedRevert::Any))
}
fn has_test_attr_key<'db>(
db: &'db dyn HirDb,
test_attr: &hir::hir_def::attr::NormalAttr<'db>,
key: &str,
) -> bool {
test_attr
.args
.iter()
.any(|arg| arg.key_str(db) == Some(key))
}
fn parse_test_attr_int_arg<'db>(
db: &'db dyn HirDb,
test_name: &str,
test_attr: &hir::hir_def::attr::NormalAttr<'db>,
key: &str,
attr_form: &str,
type_name: &str,
max_bytes: usize,
) -> Result<Option<BigUint>, String> {
for arg in &test_attr.args {
if arg.key_str(db) != Some(key) {
continue;
}
let Some(value) = arg.value.as_ref() else {
return Err(format!(
"invalid #[test] function `{test_name}`: #[test({attr_form})] expects an integer literal"
));
};
let AttrArgValue::Lit(LitKind::Int(int_id)) = value else {
return Err(format!(
"invalid #[test] function `{test_name}`: #[test({attr_form})] expects an integer literal"
));
};
let value = int_id.data(db).clone();
if value.to_bytes_be().len() > max_bytes {
return Err(format!(
"invalid #[test] function `{test_name}`: #[test({attr_form})] must fit in {type_name}"
));
}
return Ok(Some(value));
}
Ok(None)
}