2021-07-20 15:07:55 +05:30
|
|
|
use anyhow::Result;
|
2021-07-20 23:41:46 +05:30
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
2021-07-20 15:07:55 +05:30
|
|
|
use thinp::file_utils;
|
|
|
|
|
2021-07-20 23:41:46 +05:30
|
|
|
use crate::args;
|
2021-07-20 15:07:55 +05:30
|
|
|
use crate::common::fixture::*;
|
|
|
|
use crate::common::process::*;
|
|
|
|
use crate::common::program::*;
|
|
|
|
use crate::common::test_dir::*;
|
2021-07-01 19:57:37 +05:30
|
|
|
use crate::common::thin_xml_generator::{write_xml, FragmentedS};
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// wrappers
|
|
|
|
|
2021-07-20 23:41:46 +05:30
|
|
|
type ArgsBuilder = fn(&mut TestDir, &OsStr, &dyn Fn(&[&OsStr]) -> Result<()>) -> Result<()>;
|
2021-07-06 07:21:27 +05:30
|
|
|
|
|
|
|
fn with_output_md_untouched(
|
2021-07-01 19:57:37 +05:30
|
|
|
td: &mut TestDir,
|
2021-07-20 23:41:46 +05:30
|
|
|
input: &OsStr,
|
|
|
|
thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
|
2021-07-01 19:57:37 +05:30
|
|
|
) -> Result<()> {
|
|
|
|
let output = mk_zeroed_md(td)?;
|
|
|
|
ensure_untouched(&output, || {
|
2021-07-20 23:41:46 +05:30
|
|
|
let args = args!["-i", input, "-o", &output];
|
2021-07-01 19:57:37 +05:30
|
|
|
thunk(&args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
fn with_output_superblock_zeroed(
|
|
|
|
td: &mut TestDir,
|
2021-07-20 23:41:46 +05:30
|
|
|
input: &OsStr,
|
|
|
|
thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
|
2021-07-06 07:21:27 +05:30
|
|
|
) -> Result<()> {
|
|
|
|
let output = mk_zeroed_md(td)?;
|
|
|
|
ensure_superblock_zeroed(&output, || {
|
2021-07-20 23:41:46 +05:30
|
|
|
let args = args!["-i", input, "-o", &output];
|
2021-07-06 07:21:27 +05:30
|
|
|
thunk(&args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn input_arg_only(
|
2021-07-01 19:57:37 +05:30
|
|
|
_td: &mut TestDir,
|
2021-07-20 23:41:46 +05:30
|
|
|
input: &OsStr,
|
|
|
|
thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
|
2021-07-01 19:57:37 +05:30
|
|
|
) -> Result<()> {
|
2021-07-20 23:41:46 +05:30
|
|
|
let args = args![input];
|
2021-07-01 19:57:37 +05:30
|
|
|
thunk(&args)
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
fn build_args_fn(t: ArgType) -> Result<ArgsBuilder> {
|
|
|
|
match t {
|
|
|
|
ArgType::InputArg => Ok(input_arg_only),
|
|
|
|
ArgType::IoOptions => Ok(with_output_md_untouched),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test invalid arguments
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_missing_input_arg<'a, P>() -> Result<()>
|
|
|
|
where
|
|
|
|
P: InputProgram<'a>,
|
|
|
|
{
|
2021-07-20 12:37:12 +05:30
|
|
|
let args: [&str; 0] = [];
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
2021-07-06 07:21:27 +05:30
|
|
|
assert!(stderr.contains(P::missing_input_arg()));
|
2021-07-01 19:57:37 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_missing_input_arg {
|
|
|
|
($program: ident) => {
|
|
|
|
#[test]
|
|
|
|
fn missing_input_arg() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_missing_input_arg::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_missing_input_option<'a, P>() -> Result<()>
|
|
|
|
where
|
|
|
|
P: InputProgram<'a>,
|
|
|
|
{
|
2021-07-01 19:57:37 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let output = mk_zeroed_md(&mut td)?;
|
|
|
|
ensure_untouched(&output, || {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args!["-o", &output]))?;
|
2021-07-06 07:21:27 +05:30
|
|
|
assert!(stderr.contains(P::missing_input_arg()));
|
2021-07-01 19:57:37 +05:30
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_missing_input_option {
|
|
|
|
($program: ident) => {
|
|
|
|
#[test]
|
|
|
|
fn missing_input_option() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_missing_input_option::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_input_file_not_found<'a, P>() -> Result<()>
|
2021-07-01 19:57:37 +05:30
|
|
|
where
|
2021-07-06 07:21:27 +05:30
|
|
|
P: InputProgram<'a>,
|
2021-07-01 19:57:37 +05:30
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
2021-07-20 23:41:46 +05:30
|
|
|
wrapper(&mut td, "no-such-file".as_ref(), &|args: &[&OsStr]| {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
2021-07-06 07:21:27 +05:30
|
|
|
assert!(stderr.contains(P::file_not_found()));
|
2021-07-01 19:57:37 +05:30
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_input_file_not_found {
|
2021-07-06 07:21:27 +05:30
|
|
|
($program: ident) => {
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn input_file_not_found() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_input_file_not_found::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_input_cannot_be_a_directory<'a, P>() -> Result<()>
|
2021-07-01 19:57:37 +05:30
|
|
|
where
|
2021-07-06 07:21:27 +05:30
|
|
|
P: InputProgram<'a>,
|
2021-07-01 19:57:37 +05:30
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
2021-07-20 23:41:46 +05:30
|
|
|
wrapper(&mut td, "/tmp".as_ref(), &|args: &[&OsStr]| {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
2021-07-01 19:57:37 +05:30
|
|
|
assert!(stderr.contains("Not a block device or regular file"));
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_input_cannot_be_a_directory {
|
2021-07-06 07:21:27 +05:30
|
|
|
($program: ident) => {
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn input_cannot_be_a_directory() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_input_cannot_be_a_directory::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_unreadable_input_file<'a, P>() -> Result<()>
|
2021-07-01 19:57:37 +05:30
|
|
|
where
|
2021-07-06 07:21:27 +05:30
|
|
|
P: InputProgram<'a>,
|
2021-07-01 19:57:37 +05:30
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
// input an unreadable file
|
2021-07-20 15:07:55 +05:30
|
|
|
let input = P::mk_valid_input(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
duct::cmd!("chmod", "-r", &input).run()?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
2021-07-20 23:41:46 +05:30
|
|
|
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
2021-07-01 19:57:37 +05:30
|
|
|
assert!(stderr.contains("Permission denied"));
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_unreadable_input_file {
|
2021-07-06 07:21:27 +05:30
|
|
|
($program: ident) => {
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn unreadable_input_file() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_unreadable_input_file::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// test invalid content
|
|
|
|
|
2021-10-05 12:52:23 +05:30
|
|
|
pub fn test_tiny_input_file<'a, P>() -> Result<()>
|
|
|
|
where
|
|
|
|
P: MetadataReader<'a>,
|
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
let input = td.mk_path("meta.bin");
|
|
|
|
file_utils::create_sized_file(&input, 1024)?;
|
|
|
|
|
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
|
|
|
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
|
|
|
|
run_fail(P::cmd(args))?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_tiny_input_file {
|
|
|
|
($program: ident) => {
|
|
|
|
#[test]
|
|
|
|
fn tiny_input_file() -> Result<()> {
|
|
|
|
test_tiny_input_file::<$program>()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_help_message_for_tiny_input_file<'a, P>() -> Result<()>
|
2021-07-01 19:57:37 +05:30
|
|
|
where
|
2021-08-30 21:49:44 +05:30
|
|
|
P: MetadataReader<'a>,
|
2021-07-01 19:57:37 +05:30
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
let input = td.mk_path("meta.bin");
|
|
|
|
file_utils::create_sized_file(&input, 1024)?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
2021-07-20 23:41:46 +05:30
|
|
|
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
|
|
|
eprintln!("actual: {:?}", stderr);
|
2021-07-01 19:57:37 +05:30
|
|
|
assert!(stderr.contains("Metadata device/file too small. Is this binary metadata?"));
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_help_message_for_tiny_input_file {
|
2021-07-06 07:21:27 +05:30
|
|
|
($program: ident) => {
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn prints_help_message_for_tiny_input_file() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_help_message_for_tiny_input_file::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_spot_xml_data<'a, P>() -> Result<()>
|
2021-07-01 19:57:37 +05:30
|
|
|
where
|
2021-08-30 21:49:44 +05:30
|
|
|
P: MetadataReader<'a>,
|
2021-07-01 19:57:37 +05:30
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
// input a large xml file
|
|
|
|
let input = td.mk_path("meta.xml");
|
|
|
|
let mut gen = FragmentedS::new(4, 10240);
|
|
|
|
write_xml(&input, &mut gen)?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
2021-07-20 23:41:46 +05:30
|
|
|
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
2021-10-19 20:05:06 +05:30
|
|
|
let msg =
|
|
|
|
"This looks like XML. This tool only checks the binary metadata format.".to_string();
|
2021-07-01 19:57:37 +05:30
|
|
|
assert!(stderr.contains(&msg));
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_spot_xml_data {
|
2021-07-06 07:21:27 +05:30
|
|
|
($program: ident) => {
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn spot_xml_data() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_spot_xml_data::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn test_corrupted_input_data<'a, P>() -> Result<()>
|
2021-07-01 19:57:37 +05:30
|
|
|
where
|
2021-07-06 07:21:27 +05:30
|
|
|
P: InputProgram<'a>,
|
2021-07-01 19:57:37 +05:30
|
|
|
{
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let input = mk_zeroed_md(&mut td)?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = match P::arg_type() {
|
|
|
|
ArgType::InputArg => input_arg_only,
|
|
|
|
ArgType::IoOptions => with_output_superblock_zeroed,
|
|
|
|
};
|
2021-07-20 23:41:46 +05:30
|
|
|
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(P::cmd(args))?;
|
2021-07-06 07:21:27 +05:30
|
|
|
assert!(stderr.contains(P::corrupted_input()));
|
2021-07-01 19:57:37 +05:30
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_corrupted_input_data {
|
2021-07-06 07:21:27 +05:30
|
|
|
($program: ident) => {
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn corrupted_input_data() -> Result<()> {
|
2021-07-06 07:21:27 +05:30
|
|
|
test_corrupted_input_data::<$program>()
|
2021-07-01 19:57:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|