2020-08-03 15:29:19 +05:30
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2020-07-31 16:42:40 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
use std::fs::OpenOptions;
|
2020-07-31 21:01:10 +05:30
|
|
|
use std::io::{Read, Write};
|
2021-05-04 13:40:20 +05:30
|
|
|
use std::path::PathBuf;
|
2020-07-31 16:42:40 +05:30
|
|
|
use thinp::file_utils;
|
2020-08-13 18:19:11 +05:30
|
|
|
use thinp::io_engine::*;
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2020-08-07 19:00:00 +05:30
|
|
|
pub mod cache_xml_generator;
|
2021-07-01 19:57:37 +05:30
|
|
|
pub mod common_args;
|
|
|
|
pub mod input_arg;
|
|
|
|
pub mod output_option;
|
2020-08-07 19:00:00 +05:30
|
|
|
pub mod test_dir;
|
2021-05-04 13:40:20 +05:30
|
|
|
pub mod thin_xml_generator;
|
2020-08-07 19:00:00 +05:30
|
|
|
|
|
|
|
use crate::common::thin_xml_generator::{write_xml, SingleThinS};
|
|
|
|
use test_dir::TestDir;
|
2020-07-31 16:42:40 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub mod cpp_msg {
|
2021-06-23 15:12:41 +05:30
|
|
|
pub const FILE_NOT_FOUND: &str = "No such file or directory";
|
2021-06-21 13:14:56 +05:30
|
|
|
pub const MISSING_INPUT_ARG: &str = "No input file provided";
|
|
|
|
pub const MISSING_OUTPUT_ARG: &str = "No output file provided";
|
2021-07-06 07:21:27 +05:30
|
|
|
pub const BAD_SUPERBLOCK: &str = "bad checksum in superblock";
|
|
|
|
|
|
|
|
pub fn bad_option_hint(option: &str) -> String {
|
|
|
|
format!("unrecognized option '{}'", option)
|
|
|
|
}
|
2021-06-21 13:14:56 +05:30
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub mod rust_msg {
|
2021-06-21 13:14:56 +05:30
|
|
|
pub const FILE_NOT_FOUND: &str = "Couldn't find input file";
|
2021-07-06 07:21:27 +05:30
|
|
|
pub const MISSING_INPUT_ARG: &str = "The following required arguments were not provided"; // TODO: be specific
|
|
|
|
pub const MISSING_OUTPUT_ARG: &str = "The following required arguments were not provided"; // TODO: be specific
|
|
|
|
pub const BAD_SUPERBLOCK: &str = "bad checksum in superblock";
|
|
|
|
|
|
|
|
pub fn bad_option_hint(option: &str) -> String {
|
|
|
|
format!("Found argument '{}' which wasn't expected", option)
|
|
|
|
}
|
2021-06-21 13:14:56 +05:30
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
|
|
|
pub use cpp_msg as msg;
|
|
|
|
#[cfg(feature = "rust_tests")]
|
|
|
|
pub use rust_msg as msg;
|
|
|
|
|
2021-06-21 13:14:56 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
2021-06-18 19:54:23 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! path_to_cpp {
|
|
|
|
($name: literal) => {
|
|
|
|
concat!("bin/", $name)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! path_to_rust {
|
|
|
|
($name: literal) => {
|
|
|
|
env!(concat!("CARGO_BIN_EXE_", $name))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! path_to {
|
|
|
|
($name: literal) => {
|
|
|
|
path_to_cpp!($name)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "rust_tests")]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! path_to {
|
|
|
|
($name: literal) => {
|
|
|
|
path_to_rust!($name)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
2020-08-05 12:54:52 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
pub const CACHE_CHECK: &str = path_to!("cache_check");
|
|
|
|
pub const CACHE_DUMP: &str = path_to!("cache_dump");
|
|
|
|
|
|
|
|
pub const THIN_CHECK: &str = path_to!("thin_check");
|
|
|
|
pub const THIN_DELTA: &str = path_to_cpp!("thin_delta"); // TODO: rust version
|
|
|
|
pub const THIN_DUMP: &str = path_to!("thin_dump");
|
|
|
|
pub const THIN_METADATA_PACK: &str = path_to_rust!("thin_metadata_pack"); // rust-only
|
|
|
|
pub const THIN_METADATA_UNPACK: &str = path_to_rust!("thin_metadata_unpack"); // rust-only
|
|
|
|
pub const THIN_REPAIR: &str = path_to_cpp!("thin_repair"); // TODO: rust version
|
|
|
|
pub const THIN_RESTORE: &str = path_to!("thin_restore");
|
|
|
|
pub const THIN_RMAP: &str = path_to_cpp!("thin_rmap"); // TODO: rust version
|
|
|
|
pub const THIN_GENERATE_METADATA: &str = path_to_cpp!("thin_generate_metadata"); // cpp-only
|
|
|
|
pub const THIN_GENERATE_MAPPINGS: &str = path_to_cpp!("thin_generate_mappings"); // cpp-only
|
|
|
|
pub const THIN_GENERATE_DAMAGE: &str = path_to_cpp!("thin_generate_damage"); // cpp-only
|
2020-08-05 13:39:18 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
2020-08-05 13:39:18 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub enum ArgType {
|
|
|
|
InputArg,
|
|
|
|
IoOptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Program<'a> {
|
|
|
|
fn name() -> &'a str;
|
|
|
|
fn path() -> &'a str;
|
|
|
|
fn usage() -> &'a str;
|
|
|
|
fn arg_type() -> ArgType;
|
|
|
|
|
|
|
|
// error messages
|
|
|
|
fn bad_option_hint(option: &str) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait InputProgram<'a>: Program<'a> {
|
|
|
|
fn mk_valid_input(td: &mut TestDir) -> Result<PathBuf>;
|
|
|
|
|
|
|
|
// error messages
|
|
|
|
fn missing_input_arg() -> &'a str;
|
|
|
|
fn file_not_found() -> &'a str;
|
|
|
|
fn corrupted_input() -> &'a str;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait BinaryInputProgram<'a>: InputProgram<'a> {}
|
|
|
|
|
|
|
|
pub trait OutputProgram<'a>: InputProgram<'a> {
|
|
|
|
// error messages
|
|
|
|
fn missing_output_arg() -> &'a str;
|
|
|
|
fn file_not_found() -> &'a str;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait BinaryOutputProgram<'a>: OutputProgram<'a> {}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// Returns stdout. The command must return zero.
|
|
|
|
pub fn run_ok(program: &str, args: &[&str]) -> Result<String> {
|
|
|
|
let command = duct::cmd(program, args).stdout_capture().stderr_capture();
|
|
|
|
let output = command.run()?;
|
|
|
|
assert!(output.status.success());
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout[..])
|
|
|
|
.unwrap()
|
|
|
|
.trim_end_matches(|c| c == '\n' || c == '\r')
|
|
|
|
.to_string();
|
|
|
|
Ok(stdout)
|
2020-07-31 18:56:22 +05:30
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// Returns the entire output. The command must return zero.
|
|
|
|
pub fn run_ok_raw(program: &str, args: &[&str]) -> Result<std::process::Output> {
|
|
|
|
let command = duct::cmd(program, args).stdout_capture().stderr_capture();
|
|
|
|
let output = command.run()?;
|
|
|
|
assert!(output.status.success());
|
|
|
|
Ok(output)
|
2020-08-13 18:19:11 +05:30
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// Returns stderr, a non zero status must be returned
|
|
|
|
pub fn run_fail(program: &str, args: &[&str]) -> Result<String> {
|
|
|
|
let command = duct::cmd(program, args).stdout_capture().stderr_capture();
|
|
|
|
let output = command.unchecked().run()?;
|
|
|
|
assert!(!output.status.success());
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr[..]).unwrap().to_string();
|
|
|
|
Ok(stderr)
|
2020-08-13 18:19:11 +05:30
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// Returns the entire output, a non zero status must be returned
|
|
|
|
pub fn run_fail_raw(program: &str, args: &[&str]) -> Result<std::process::Output> {
|
|
|
|
let command = duct::cmd(program, args).stdout_capture().stderr_capture();
|
|
|
|
let output = command.unchecked().run()?;
|
|
|
|
assert!(!output.status.success());
|
|
|
|
Ok(output)
|
2020-08-13 18:19:11 +05:30
|
|
|
}
|
|
|
|
|
2020-08-07 19:00:00 +05:30
|
|
|
//------------------------------------------
|
2020-07-31 16:42:40 +05:30
|
|
|
|
2020-07-31 18:56:22 +05:30
|
|
|
pub fn mk_valid_xml(td: &mut TestDir) -> Result<PathBuf> {
|
|
|
|
let xml = td.mk_path("meta.xml");
|
2020-07-31 16:42:40 +05:30
|
|
|
let mut gen = SingleThinS::new(0, 1024, 2048, 2048);
|
|
|
|
write_xml(&xml, &mut gen)?;
|
|
|
|
Ok(xml)
|
|
|
|
}
|
|
|
|
|
2020-07-31 18:56:22 +05:30
|
|
|
pub fn mk_valid_md(td: &mut TestDir) -> Result<PathBuf> {
|
|
|
|
let xml = td.mk_path("meta.xml");
|
|
|
|
let md = td.mk_path("meta.bin");
|
2020-07-31 16:42:40 +05:30
|
|
|
|
2020-08-03 15:17:03 +05:30
|
|
|
let mut gen = SingleThinS::new(0, 1024, 20480, 20480);
|
2020-07-31 16:42:40 +05:30
|
|
|
write_xml(&xml, &mut gen)?;
|
|
|
|
|
|
|
|
let _file = file_utils::create_sized_file(&md, 4096 * 4096);
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = ["-i", xml.to_str().unwrap(), "-o", md.to_str().unwrap()];
|
|
|
|
run_ok(THIN_RESTORE, &args)?;
|
|
|
|
|
2020-07-31 16:42:40 +05:30
|
|
|
Ok(md)
|
|
|
|
}
|
|
|
|
|
2020-07-31 18:56:22 +05:30
|
|
|
pub fn mk_zeroed_md(td: &mut TestDir) -> Result<PathBuf> {
|
|
|
|
let md = td.mk_path("meta.bin");
|
2020-07-31 21:01:10 +05:30
|
|
|
eprintln!("path = {:?}", md);
|
2020-08-13 18:19:11 +05:30
|
|
|
let _file = file_utils::create_sized_file(&md, 1024 * 1024 * 16);
|
2020-07-31 16:42:40 +05:30
|
|
|
Ok(md)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn superblock_all_zeroes(path: &PathBuf) -> Result<bool> {
|
|
|
|
let mut input = OpenOptions::new().read(true).write(false).open(path)?;
|
|
|
|
let mut buf = vec![0; 4096];
|
|
|
|
input.read_exact(&mut buf[0..])?;
|
|
|
|
for b in buf {
|
|
|
|
if b != 0 {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 21:01:10 +05:30
|
|
|
|
2020-07-31 16:42:40 +05:30
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
pub fn damage_superblock(path: &PathBuf) -> Result<()> {
|
|
|
|
let mut output = OpenOptions::new().read(false).write(true).open(path)?;
|
|
|
|
let buf = [0u8; 512];
|
|
|
|
output.write_all(&buf)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
//-----------------------------------------------
|
|
|
|
|
|
|
|
// FIXME: replace mk_valid_md with this?
|
|
|
|
pub fn prep_metadata(td: &mut TestDir) -> Result<PathBuf> {
|
|
|
|
let md = mk_zeroed_md(td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = [
|
|
|
|
"-o",
|
|
|
|
md.to_str().unwrap(),
|
|
|
|
"--format",
|
|
|
|
"--nr-data-blocks",
|
|
|
|
"102400",
|
|
|
|
];
|
|
|
|
run_ok(THIN_GENERATE_METADATA, &args)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
// Create a 2GB device
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = ["-o", md.to_str().unwrap(), "--create-thin", "1"];
|
|
|
|
run_ok(THIN_GENERATE_METADATA, &args)?;
|
|
|
|
let args = [
|
2020-08-13 18:19:11 +05:30
|
|
|
"-o",
|
2021-07-01 19:57:37 +05:30
|
|
|
md.to_str().unwrap(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--dev-id",
|
|
|
|
"1",
|
|
|
|
"--size",
|
2021-07-01 19:57:37 +05:30
|
|
|
"2097152",
|
2020-08-13 18:19:11 +05:30
|
|
|
"--rw=randwrite",
|
2021-07-01 19:57:37 +05:30
|
|
|
"--seq-nr=16",
|
|
|
|
];
|
|
|
|
run_ok(THIN_GENERATE_MAPPINGS, &args)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
// Take a few snapshots.
|
|
|
|
let mut snap_id = 2;
|
|
|
|
for _i in 0..10 {
|
|
|
|
// take a snapshot
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = [
|
2020-08-13 18:19:11 +05:30
|
|
|
"-o",
|
2021-07-01 19:57:37 +05:30
|
|
|
md.to_str().unwrap(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--create-snap",
|
2021-07-01 19:57:37 +05:30
|
|
|
&snap_id.to_string(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--origin",
|
2021-07-01 19:57:37 +05:30
|
|
|
"1",
|
|
|
|
];
|
|
|
|
run_ok(THIN_GENERATE_METADATA, &args)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
// partially overwrite the origin (64MB)
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = [
|
2020-08-13 18:19:11 +05:30
|
|
|
"-o",
|
2021-07-01 19:57:37 +05:30
|
|
|
md.to_str().unwrap(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--dev-id",
|
2021-07-01 19:57:37 +05:30
|
|
|
"1",
|
2020-08-13 18:19:11 +05:30
|
|
|
"--size",
|
2021-07-01 19:57:37 +05:30
|
|
|
"2097152",
|
2020-08-13 18:19:11 +05:30
|
|
|
"--io-size",
|
2021-07-01 19:57:37 +05:30
|
|
|
"131072",
|
2020-08-13 18:19:11 +05:30
|
|
|
"--rw=randwrite",
|
2021-07-01 19:57:37 +05:30
|
|
|
"--seq-nr=16",
|
|
|
|
];
|
|
|
|
run_ok(THIN_GENERATE_MAPPINGS, &args)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
snap_id += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(md)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_needs_check(md: &PathBuf) -> Result<()> {
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = ["-o", md.to_str().unwrap(), "--set-needs-check"];
|
|
|
|
run_ok(THIN_GENERATE_METADATA, &args)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-04 13:40:20 +05:30
|
|
|
pub fn generate_metadata_leaks(
|
|
|
|
md: &PathBuf,
|
|
|
|
nr_blocks: u64,
|
|
|
|
expected: u32,
|
|
|
|
actual: u32,
|
|
|
|
) -> Result<()> {
|
2021-07-01 19:57:37 +05:30
|
|
|
let args = [
|
2020-08-13 18:19:11 +05:30
|
|
|
"-o",
|
2021-07-01 19:57:37 +05:30
|
|
|
md.to_str().unwrap(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--create-metadata-leaks",
|
|
|
|
"--nr-blocks",
|
2021-07-01 19:57:37 +05:30
|
|
|
&nr_blocks.to_string(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--expected",
|
2021-07-01 19:57:37 +05:30
|
|
|
&expected.to_string(),
|
2020-08-13 18:19:11 +05:30
|
|
|
"--actual",
|
2021-07-01 19:57:37 +05:30
|
|
|
&actual.to_string(),
|
|
|
|
];
|
|
|
|
run_ok(THIN_GENERATE_DAMAGE, &args)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_needs_check(md: &PathBuf) -> Result<bool> {
|
|
|
|
use thinp::thin::superblock::*;
|
|
|
|
|
2020-08-19 19:01:01 +05:30
|
|
|
let engine = SyncIoEngine::new(&md, 1, false)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
let sb = read_superblock(&engine, SUPERBLOCK_LOCATION)?;
|
|
|
|
Ok(sb.flags.needs_check)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn md5(md: &PathBuf) -> Result<String> {
|
2021-07-01 19:57:37 +05:30
|
|
|
let output = duct::cmd!("md5sum", "-b", &md).stdout_capture().run()?;
|
2020-08-13 18:19:11 +05:30
|
|
|
let csum = std::str::from_utf8(&output.stdout[0..])?.to_string();
|
|
|
|
let csum = csum.split_ascii_whitespace().next().unwrap().to_string();
|
|
|
|
Ok(csum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This checksums the file before and after the thunk is run to
|
|
|
|
// ensure it is unchanged.
|
|
|
|
pub fn ensure_untouched<F>(p: &PathBuf, thunk: F) -> Result<()>
|
|
|
|
where
|
|
|
|
F: Fn() -> Result<()>,
|
|
|
|
{
|
|
|
|
let csum = md5(p)?;
|
|
|
|
thunk()?;
|
|
|
|
assert_eq!(csum, md5(p)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
pub fn ensure_superblock_zeroed<F>(p: &PathBuf, thunk: F) -> Result<()>
|
|
|
|
where
|
|
|
|
F: Fn() -> Result<()>,
|
|
|
|
{
|
|
|
|
thunk()?;
|
|
|
|
assert!(superblock_all_zeroes(p)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|