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
extern crate inflate;
#[macro_use]
extern crate nom;
extern crate num_traits;
extern crate swf_fixed;
extern crate swf_tree;

pub mod parsers {
  pub mod basic_data_types;
  pub mod button;
  pub mod display;
  pub mod gradient;
  pub mod header;
  pub mod image;
  pub mod morph_shape;
  pub mod movie;
  pub mod shape;
  pub mod sound;
  pub mod tags;
  pub mod text;
}

pub mod state;

#[cfg(test)]
mod tests {
  use std::io::{Read, Write};
  use std::path::Path;

  use ::swf_tree::Movie;
  use nom::IResult as NomResult;
  use swf_tree::Tag;

  use ::test_generator::test_expand_paths;

  use crate::parsers::movie::parse_movie;
  use crate::parsers::tags::parse_tag;
  use crate::state::ParseState;

  test_expand_paths! { test_parse_movie; "../tests/movies/*/" }
  fn test_parse_movie(path: &str) {
    use serde::Serialize;

    let path: &Path = Path::new(path);
    let _name = path
      .components()
      .last()
      .unwrap()
      .as_os_str()
      .to_str()
      .expect("Failed to retrieve sample name");
    let movie_path = path.join("main.swf");
    let mut movie_file = ::std::fs::File::open(movie_path).expect("Failed to open movie");
    let mut movie_bytes: Vec<u8> = Vec::new();
    movie_file.read_to_end(&mut movie_bytes).expect("Failed to read movie");

    let (_remaining_input, actual_movie) = parse_movie(&movie_bytes).expect("Failed to parse movie");

    let actual_ast_path = path.join("local-ast.rs.json");
    let actual_ast_file = ::std::fs::File::create(actual_ast_path).expect("Failed to create actual AST file");
    let actual_ast_writer = ::std::io::BufWriter::new(actual_ast_file);

    let mut ser = serde_json_v8::Serializer::pretty(actual_ast_writer);
    actual_movie.serialize(&mut ser).expect("Failed to write actual AST");
    ser.into_inner().write_all("\n".as_bytes()).unwrap();

    // assert_eq!(remaining_input, &[] as &[u8]);

    let ast_path = path.join("ast.json");
    let ast_file = ::std::fs::File::open(ast_path).expect("Failed to open AST");
    let ast_reader = ::std::io::BufReader::new(ast_file);
    let expected_movie = serde_json_v8::from_reader::<_, Movie>(ast_reader).expect("Failed to read AST");

    assert_eq!(actual_movie, expected_movie);
  }

  test_expand_paths! { test_parse_tag; "../tests/tags/*/*/" }
  fn test_parse_tag(path: &str) {
    let path: &Path = Path::new(path);
    let name = path
      .components()
      .last()
      .unwrap()
      .as_os_str()
      .to_str()
      .expect("Failed to retrieve sample name");
    let input_path = path.join("input.bytes");
    let input_bytes: Vec<u8> = ::std::fs::read(input_path).expect("Failed to read input");

    let swf_version: u8 = match name {
      "po2-swf5" => 5,
      _ => 10,
    };

    let mut state = ParseState::new(swf_version);
    state.set_glyph_count(1, 11);
    let (remaining_bytes, actual_value) = parse_tag(&input_bytes, &mut state).expect("Failed to parse");

    let expected_path = path.join("value.json");
    let expected_file = ::std::fs::File::open(expected_path).expect("Failed to open expected value file");
    let expected_reader = ::std::io::BufReader::new(expected_file);
    let expected_value = serde_json_v8::from_reader::<_, Tag>(expected_reader).expect("Failed to read AST");

    assert_eq!(actual_value, expected_value);
    assert_eq!(remaining_bytes, &[] as &[u8]);
  }

  macro_rules! test_various_parser_impl {
    ($name:ident, $glob:expr, $parser:ident, $type:ty) => {
      test_expand_paths! { $name; $glob }
      fn $name(path: &str) {
        let path: &Path = Path::new(path);
        let _name = path
          .components()
          .last()
          .unwrap()
          .as_os_str()
          .to_str()
          .expect("Failed to retrieve sample name");
        let input_path = path.join("input.bytes");
        let input_bytes: Vec<u8> = ::std::fs::read(input_path).expect("Failed to read input");

        let (remaining_bytes, actual_value): (&[u8], $type) = $parser(&input_bytes).expect("Failed to parse");

        let expected_path = path.join("value.json");
        let expected_file = ::std::fs::File::open(expected_path).expect("Failed to open expected value file");
        let expected_reader = ::std::io::BufReader::new(expected_file);
        let expected_value = serde_json_v8::from_reader::<_, $type>(expected_reader).expect("Failed to read AST");

        assert_eq!(actual_value, expected_value);
        assert_eq!(remaining_bytes, &[] as &[u8]);
      }
    };
  }

  use crate::parsers::basic_data_types::parse_le_f16;
  test_various_parser_impl!(test_parse_le_f16, "../tests/various/float16-le/*/", parse_le_f16, f32);

  use crate::parsers::header::parse_header;
  use swf_tree::Header;

  fn parse_header34(input: &[u8]) -> NomResult<&[u8], Header> {
    parse_header(input, 34)
  }
  test_various_parser_impl!(test_parse_header, "../tests/various/header/*/", parse_header34, Header);

  use crate::parsers::basic_data_types::parse_matrix;
  use swf_tree::Matrix;
  test_various_parser_impl!(test_parse_matrix, "../tests/various/matrix/*/", parse_matrix, Matrix);

  use crate::parsers::basic_data_types::parse_rect;
  use swf_tree::Rect;
  test_various_parser_impl!(test_parse_rect, "../tests/various/rect/*/", parse_rect, Rect);

  use crate::parsers::header::parse_swf_signature;
  use swf_tree::SwfSignature;
  test_various_parser_impl!(
    test_parse_swf_signature,
    "../tests/various/swf-signature/*/",
    parse_swf_signature,
    SwfSignature
  );

  use crate::parsers::basic_data_types::parse_leb128_u32;
  test_various_parser_impl!(
    test_parse_leb128_u32,
    "../tests/various/uint32-leb128/*/",
    parse_leb128_u32,
    u32
  );
}