use tempfile::TempDir;
use trivet::parse_from_path;
use crate::generator::write_hexdump;
use crate::generator::Generator;
use crate::options::HhhArgs;
use crate::reader::read_hexdump;
use crate::reader::Reader;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
const TEST_FILE_DIR: &str = "test_files";
#[test]
fn hex_to_bin_1() -> std::io::Result<()> {
let paths = std::fs::read_dir(TEST_FILE_DIR)?
.filter_map(|result| result.ok())
.map(|entry| entry.path())
.filter_map(|path| {
if path
.extension()
.map_or(false, |extension| extension == "hex")
{
Some(path)
} else {
None
}
})
.collect::<Vec<_>>();
for hex_path in paths {
println!("Integration test: Parsing {:?}...", &hex_path);
let mut args = HhhArgs::default();
let mut bin_path = hex_path.clone();
bin_path.set_extension("bin");
if !bin_path.exists() {
continue;
}
let mut file_parser = parse_from_path(&hex_path)?;
let mut hex_parser = Reader::new(&mut args);
let _ = hex_parser.parse(&mut file_parser);
let hex_bytes = hex_parser.finish().get_bytes();
let mut bin_bytes = vec![];
let mut bin_file = File::open(bin_path)?;
bin_file.read_to_end(&mut bin_bytes)?;
assert_eq!(hex_bytes, bin_bytes);
println!("Integration test: Parsing {:?} PASSED", &hex_path);
}
Ok(())
}
#[test]
fn round_trip_1() -> std::io::Result<()> {
let temp_dir = TempDir::new()?;
let paths = std::fs::read_dir(TEST_FILE_DIR)?
.filter_map(|result| result.ok())
.map(|entry| entry.path())
.filter_map(|path| {
if path
.extension()
.map_or(false, |extension| extension == "bin")
{
Some(path)
} else {
None
}
})
.collect::<Vec<_>>();
for bin_path in paths {
println!("Round-trip test: Parsing {:?}...", &bin_path);
let mut args = HhhArgs::default();
let mut hex_path = PathBuf::from(temp_dir.path());
hex_path.push(bin_path.file_name().unwrap());
hex_path.set_extension("hex");
let mut bin_file = File::open(&bin_path)?;
let mut bin_bytes = vec![];
bin_file.read_to_end(&mut bin_bytes)?;
let hex_file = File::create(&hex_path)?;
let mut generator = Generator::new(hex_file);
generator.config(&args);
generator.add(&bin_bytes);
generator.finish();
let mut file_parser = parse_from_path(&hex_path)?;
let mut hex_parser = Reader::new(&mut args);
let _ = hex_parser.parse(&mut file_parser);
let hex_bytes = hex_parser.finish().get_bytes();
assert_eq!(hex_bytes, bin_bytes);
println!("Integration test: Parsing {:?} PASSED", &hex_path);
}
Ok(())
}
#[test]
fn hex_to_bin_2() -> std::io::Result<()> {
let temp_dir = TempDir::new()?;
let paths = std::fs::read_dir(TEST_FILE_DIR)?
.filter_map(|result| result.ok())
.map(|entry| entry.path())
.filter_map(|path| {
if path
.extension()
.map_or(false, |extension| extension == "hex")
{
Some(path)
} else {
None
}
})
.collect::<Vec<_>>();
for hex_path in paths {
println!(
"Integration test: Parsing {:?}...",
&hex_path.canonicalize().unwrap()
);
let mut oracle_path = hex_path.clone();
oracle_path.set_extension("bin");
if !oracle_path.exists() {
continue;
}
let mut args = HhhArgs::default();
args.files.push(hex_path.clone());
let mut bin_path = PathBuf::from(temp_dir.path());
bin_path.push(hex_path.file_name().unwrap());
bin_path.set_extension("bin");
let bin_file = File::create(&bin_path)?;
read_hexdump(args.clone(), bin_file).unwrap();
let mut bin_file = File::open(&bin_path)?;
let mut oracle_file = File::open(&oracle_path)?;
let mut bin_bytes = vec![];
let mut oracle_bytes = vec![];
bin_file.read_to_end(&mut bin_bytes)?;
oracle_file.read_to_end(&mut oracle_bytes)?;
assert_eq!(oracle_bytes, bin_bytes);
println!("Integration test: Parsing {:?} PASSED", &hex_path);
}
Ok(())
}
#[test]
fn round_trip_2() -> std::io::Result<()> {
let temp_dir = TempDir::new()?;
let paths = std::fs::read_dir(TEST_FILE_DIR)?
.filter_map(|result| result.ok())
.map(|entry| entry.path())
.filter_map(|path| {
if path
.extension()
.map_or(false, |extension| extension == "bin")
{
Some(path)
} else {
None
}
})
.collect::<Vec<_>>();
for oracle_path in paths {
println!("Round-trip test: Parsing {:?}...", &oracle_path);
let mut args = HhhArgs::default();
args.files.push(oracle_path.clone());
let mut hex_path = PathBuf::from(temp_dir.path());
hex_path.push(oracle_path.file_name().unwrap());
hex_path.set_extension("hex");
let map = BTreeMap::new();
let hex_file = File::create(&hex_path)?;
write_hexdump(args, map, hex_file).unwrap();
let mut args = HhhArgs::default();
args.files.push(hex_path);
let mut bin_path = PathBuf::from(temp_dir.path());
bin_path.push(oracle_path.file_name().unwrap());
bin_path.set_extension("bin");
let bin_file = File::create(&bin_path)?;
read_hexdump(args, bin_file).unwrap();
let mut oracle_file = File::open(&oracle_path)?;
let mut bin_file = File::open(&bin_path)?;
let mut oracle_bytes = vec![];
let mut bin_bytes = vec![];
oracle_file.read_to_end(&mut oracle_bytes)?;
bin_file.read_to_end(&mut bin_bytes)?;
assert_eq!(oracle_bytes, bin_bytes);
println!("Integration test: Parsing {:?} PASSED", &oracle_path);
}
Ok(())
}