-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcheckstartfunc.rs
120 lines (97 loc) · 3.57 KB
/
checkstartfunc.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
use std::collections::HashMap;
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator, WasmModule};
/// Struct on which ModuleValidator is implemented.
pub struct CheckStartFunc {
start_required: bool,
}
impl CheckStartFunc {
pub fn new(is_start_required: bool) -> Self {
CheckStartFunc {
start_required: is_start_required,
}
}
}
impl<'a> ChiselModule<'a> for CheckStartFunc {
type ObjectReference = &'a dyn ModuleValidator;
fn id(&'a self) -> String {
"checkstartfunc".to_string()
}
fn kind(&'a self) -> ModuleKind {
ModuleKind::Validator
}
fn as_abstract(&'a self) -> Self::ObjectReference {
self as Self::ObjectReference
}
}
impl ModuleConfig for CheckStartFunc {
fn with_defaults() -> Result<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
let require_start = if let Some(value) = config.get("require_start") {
value == "true"
} else {
false
};
Ok(CheckStartFunc {
start_required: require_start,
})
}
}
impl ModuleValidator for CheckStartFunc {
fn validate(&self, module: &WasmModule) -> Result<bool, ModuleError> {
Ok(module.start_section().is_some() == self.start_required)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn start_required_good() {
let wasm: Vec<u8> = vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(true);
let result = checker.validate(&module).unwrap();
assert_eq!(true, result);
}
#[test]
fn start_required_bad() {
let wasm: Vec<u8> = vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(false);
let result = checker.validate(&module).unwrap();
assert_eq!(false, result);
}
#[test]
fn start_not_required_good() {
let wasm: Vec<u8> = vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(false);
let result = checker.validate(&module).unwrap();
assert_eq!(true, result);
}
#[test]
fn start_not_required_bad() {
let wasm: Vec<u8> = vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(true);
let result = checker.validate(&module).unwrap();
assert_eq!(false, result);
}
}