1 /* Copyright 2017 Mozilla Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #[cfg(test)]
17 mod simple_tests {
18     use crate::parser::{Parser, ParserInput, ParserState, WasmDecoder};
19     use crate::primitives::{Operator, SectionCode};
20     use std::fs::File;
21     use std::io::prelude::*;
22     use std::path::PathBuf;
23 
read_file_data(path: &PathBuf) -> Vec<u8>24     fn read_file_data(path: &PathBuf) -> Vec<u8> {
25         println!("Parsing {:?}", path);
26         let mut data = Vec::new();
27         let mut f = File::open(path).ok().unwrap();
28         f.read_to_end(&mut data).unwrap();
29         data
30     }
31 
32     macro_rules! expect_state {
33         ($state:expr, $expected:pat) => {{
34             {
35                 let state: &ParserState = $state;
36                 match *state {
37                     $expected => (),
38                     _ => panic!("Unexpected state during testing: {:?}", state),
39                 }
40             }
41         }};
42     }
43 
44     #[test]
default_read()45     fn default_read() {
46         let data = read_file_data(&PathBuf::from("../../tests/local/simple.wasm"));
47         let mut parser = Parser::new(data.as_slice());
48 
49         expect_state!(parser.read(), ParserState::BeginWasm { .. });
50         expect_state!(parser.read(), ParserState::BeginSection { code: SectionCode::Type, .. });
51         expect_state!(parser.read(), ParserState::TypeSectionEntry(_));
52         expect_state!(parser.read(), ParserState::EndSection);
53         expect_state!(parser.read(), ParserState::BeginSection { code: SectionCode::Function, .. });
54         expect_state!(parser.read(), ParserState::FunctionSectionEntry(_));
55         expect_state!(parser.read(), ParserState::EndSection);
56         expect_state!(parser.read(), ParserState::BeginSection { code: SectionCode::Code, .. });
57         expect_state!(parser.read(), ParserState::BeginFunctionBody { .. });
58         expect_state!(parser.read(), ParserState::FunctionBodyLocals { .. });
59         expect_state!(parser.read(), ParserState::CodeOperator(_));
60         expect_state!(parser.read(), ParserState::CodeOperator(Operator::End));
61         expect_state!(parser.read(), ParserState::EndFunctionBody);
62         expect_state!(parser.read(), ParserState::EndSection);
63         expect_state!(parser.read(), ParserState::EndWasm);
64     }
65 
66     #[test]
default_read_with_input()67     fn default_read_with_input() {
68         let data = read_file_data(&PathBuf::from("../../tests/local/simple.wasm"));
69         let mut parser = Parser::new(data.as_slice());
70 
71         expect_state!(parser.read(), ParserState::BeginWasm { .. });
72         expect_state!(parser.read_with_input(ParserInput::Default),
73             ParserState::BeginSection { code: SectionCode::Type, .. });
74         expect_state!(parser.read(), ParserState::TypeSectionEntry(_));
75         expect_state!(parser.read(), ParserState::EndSection);
76         expect_state!(parser.read(), ParserState::BeginSection { code: SectionCode::Function, ..});
77         expect_state!(
78             parser.read_with_input(ParserInput::ReadSectionRawData),
79             ParserState::SectionRawData(_)
80         );
81         expect_state!(parser.read(), ParserState::EndSection);
82         expect_state!(parser.read(), ParserState::BeginSection { code: SectionCode::Code, .. });
83         expect_state!(parser.read(), ParserState::BeginFunctionBody { .. });
84         expect_state!(
85             parser.read_with_input(ParserInput::SkipFunctionBody),
86             ParserState::EndSection
87         );
88         expect_state!(parser.read(), ParserState::EndWasm);
89     }
90 
91     #[test]
skipping()92     fn skipping() {
93         let data = read_file_data(&PathBuf::from("../../tests/local/naming.wasm"));
94         let mut parser = Parser::new(data.as_slice());
95 
96         expect_state!(parser.read(),
97             ParserState::BeginWasm { .. });
98         expect_state!(parser.read_with_input(ParserInput::Default),
99             ParserState::BeginSection { code: SectionCode::Type, .. });
100         expect_state!(parser.read_with_input(ParserInput::SkipSection),
101             ParserState::BeginSection { code: SectionCode::Import, ..});
102         expect_state!(parser.read_with_input(ParserInput::SkipSection),
103             ParserState::BeginSection { code: SectionCode::Function, ..});
104         expect_state!(parser.read_with_input(ParserInput::SkipSection),
105             ParserState::BeginSection { code: SectionCode::Global, ..});
106         expect_state!(parser.read_with_input(ParserInput::SkipSection),
107             ParserState::BeginSection { code: SectionCode::Export, ..});
108         expect_state!(parser.read_with_input(ParserInput::SkipSection),
109             ParserState::BeginSection { code: SectionCode::Element, ..});
110         expect_state!(parser.read_with_input(ParserInput::SkipSection),
111             ParserState::BeginSection { code: SectionCode::Code, .. });
112         expect_state!(parser.read(),
113             ParserState::BeginFunctionBody { .. });
114         expect_state!(parser.read_with_input(ParserInput::SkipFunctionBody),
115             ParserState::BeginFunctionBody { .. });
116         expect_state!(parser.read_with_input(ParserInput::SkipFunctionBody),
117             ParserState::BeginFunctionBody { .. });
118         expect_state!(parser.read_with_input(ParserInput::SkipFunctionBody),
119             ParserState::BeginFunctionBody { .. });
120         expect_state!(parser.read_with_input(ParserInput::SkipFunctionBody),
121             ParserState::BeginFunctionBody { .. });
122         expect_state!(parser.read_with_input(ParserInput::SkipFunctionBody),
123             ParserState::BeginFunctionBody { .. });
124         expect_state!(parser.read_with_input(ParserInput::SkipFunctionBody),
125             ParserState::BeginFunctionBody { .. });
126         expect_state!(
127             parser.read_with_input(ParserInput::SkipFunctionBody),
128             ParserState::EndSection
129         );
130         expect_state!(parser.read(),
131             ParserState::BeginSection { code: SectionCode::Custom { .. }, ..});
132         expect_state!(parser.read_with_input(ParserInput::SkipSection),
133             ParserState::BeginSection { code: SectionCode::Custom { .. }, .. });
134         expect_state!(
135             parser.read_with_input(ParserInput::SkipSection),
136             ParserState::EndWasm
137         );
138     }
139 }
140