2021-07-01 19:57:37 +05:30
|
|
|
use crate::common::thin_xml_generator::{write_xml, FragmentedS};
|
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// wrappers
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
type ArgsBuilder = fn(&mut TestDir, &str, &dyn Fn(&[&str]) -> Result<()>) -> Result<()>;
|
|
|
|
|
|
|
|
fn with_output_md_untouched(
|
2021-07-01 19:57:37 +05:30
|
|
|
td: &mut TestDir,
|
|
|
|
input: &str,
|
|
|
|
thunk: &dyn Fn(&[&str]) -> Result<()>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let output = mk_zeroed_md(td)?;
|
|
|
|
ensure_untouched(&output, || {
|
|
|
|
let args = ["-i", input, "-o", output.to_str().unwrap()];
|
|
|
|
thunk(&args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
fn with_output_superblock_zeroed(
|
|
|
|
td: &mut TestDir,
|
|
|
|
input: &str,
|
|
|
|
thunk: &dyn Fn(&[&str]) -> Result<()>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let output = mk_zeroed_md(td)?;
|
|
|
|
ensure_superblock_zeroed(&output, || {
|
|
|
|
let args = ["-i", input, "-o", output.to_str().unwrap()];
|
|
|
|
thunk(&args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn input_arg_only(
|
2021-07-01 19:57:37 +05:30
|
|
|
_td: &mut TestDir,
|
|
|
|
input: &str,
|
|
|
|
thunk: &dyn Fn(&[&str]) -> Result<()>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let args = [input];
|
|
|
|
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>,
|
|
|
|
{
|
|
|
|
let stderr = run_fail(P::path(), &[])?;
|
|
|
|
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, || {
|
|
|
|
let args = ["-o", output.to_str().unwrap()];
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), &args)?;
|
|
|
|
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-01 19:57:37 +05:30
|
|
|
wrapper(&mut td, "no-such-file", &|args: &[&str]| {
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), args)?;
|
|
|
|
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-01 19:57:37 +05:30
|
|
|
wrapper(&mut td, "/tmp", &|args: &[&str]| {
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), 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
|
|
|
|
let input = mk_valid_md(&mut td)?;
|
|
|
|
duct::cmd!("chmod", "-r", &input).run()?;
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
let wrapper = build_args_fn(P::arg_type())?;
|
2021-07-01 19:57:37 +05:30
|
|
|
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), 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-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-07-06 07:21:27 +05:30
|
|
|
P: BinaryInputProgram<'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-01 19:57:37 +05:30
|
|
|
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), args)?;
|
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-07-06 07:21:27 +05:30
|
|
|
P: BinaryInputProgram<'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-01 19:57:37 +05:30
|
|
|
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), args)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
eprintln!("{}", stderr);
|
|
|
|
let msg = format!(
|
|
|
|
"This looks like XML. {} only checks the binary metadata format.",
|
2021-07-06 07:21:27 +05:30
|
|
|
P::name()
|
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-01 19:57:37 +05:30
|
|
|
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
|
2021-07-06 07:21:27 +05:30
|
|
|
let stderr = run_fail(P::path(), args)?;
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|