libchisel/
checkstartfunc.rs

1use parity_wasm::elements::Module;
2
3use super::{ChiselModule, ModuleError, ModuleKind, ModuleValidator};
4
5/// Struct on which ModuleValidator is implemented.
6pub struct CheckStartFunc {
7    start_required: bool,
8}
9
10impl CheckStartFunc {
11    pub fn new(is_start_required: bool) -> Self {
12        CheckStartFunc {
13            start_required: is_start_required,
14        }
15    }
16}
17
18impl<'a> ChiselModule<'a> for CheckStartFunc {
19    type ObjectReference = &'a dyn ModuleValidator;
20
21    fn id(&'a self) -> String {
22        "checkstartfunc".to_string()
23    }
24
25    fn kind(&'a self) -> ModuleKind {
26        ModuleKind::Validator
27    }
28
29    fn as_abstract(&'a self) -> Self::ObjectReference {
30        self as Self::ObjectReference
31    }
32}
33
34impl ModuleValidator for CheckStartFunc {
35    fn validate(&self, module: &Module) -> Result<bool, ModuleError> {
36        Ok(module.start_section().is_some() == self.start_required)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn start_required_good() {
46        let wasm: Vec<u8> = vec![
47            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
48            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
49            0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
50        ];
51
52        let module = Module::from_bytes(&wasm).unwrap();
53        let checker = CheckStartFunc::new(true);
54
55        let result = checker.validate(&module).unwrap();
56        assert_eq!(true, result);
57    }
58
59    #[test]
60    fn start_required_bad() {
61        let wasm: Vec<u8> = vec![
62            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
63            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
64            0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
65        ];
66
67        let module = Module::from_bytes(&wasm).unwrap();
68        let checker = CheckStartFunc::new(false);
69
70        let result = checker.validate(&module).unwrap();
71        assert_eq!(false, result);
72    }
73
74    #[test]
75    fn start_not_required_good() {
76        let wasm: Vec<u8> = vec![
77            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
78            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
79            0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
80        ];
81
82        let module = Module::from_bytes(&wasm).unwrap();
83        let checker = CheckStartFunc::new(false);
84
85        let result = checker.validate(&module).unwrap();
86        assert_eq!(true, result);
87    }
88
89    #[test]
90    fn start_not_required_bad() {
91        let wasm: Vec<u8> = vec![
92            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
93            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
94            0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
95        ];
96
97        let module = Module::from_bytes(&wasm).unwrap();
98        let checker = CheckStartFunc::new(true);
99
100        let result = checker.validate(&module).unwrap();
101        assert_eq!(false, result);
102    }
103}