2020-11-28 14:29:23 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod must {
|
2021-10-31 00:25:37 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
use bakare::backup;
|
2020-12-25 21:52:40 +00:00
|
|
|
use bakare::test::assertions::in_memory::*;
|
|
|
|
use bakare::{repository::Repository, test::source::TestSource};
|
2021-10-30 22:07:28 +01:00
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use proptest::prelude::*;
|
2021-05-16 19:15:07 +01:00
|
|
|
use tempfile::tempdir;
|
2021-10-30 22:07:28 +01:00
|
|
|
use walkdir::WalkDir;
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
#[test]
|
|
|
|
fn restore_multiple_files() -> Result<()> {
|
|
|
|
let source = TestSource::new().unwrap();
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
source.write_text_to_file("first", "some contents").unwrap();
|
|
|
|
source.write_text_to_file("second", "some contents").unwrap();
|
|
|
|
source.write_text_to_file("third", "some other contents").unwrap();
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
assert_same_after_restore(source.path())
|
|
|
|
}
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
#[test]
|
|
|
|
fn restore_files_after_reopening_repository() -> Result<()> {
|
2021-02-06 16:40:32 +00:00
|
|
|
let source = TestSource::new()?;
|
2021-05-16 19:15:07 +01:00
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
|
|
|
let restore_target = tempdir()?;
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret)?;
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
let source_file_relative_path = "some file path";
|
|
|
|
let original_contents = "some old contents";
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, original_contents)?;
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
restore_all_from_reloaded_repository(repository_path, secret, restore_target.path())?;
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2020-12-25 21:52:40 +00:00
|
|
|
let source_file_full_path = &source.file_path(source_file_relative_path)?;
|
2021-10-31 00:25:37 +01:00
|
|
|
assert_restored_file_contents(repository_path, secret, source_file_full_path, original_contents.as_bytes())
|
2020-11-28 14:29:23 +00:00
|
|
|
}
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
#[test]
|
|
|
|
fn restore_older_version_of_file() -> Result<()> {
|
|
|
|
let source = TestSource::new().unwrap();
|
2021-05-16 19:15:07 +01:00
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret)?;
|
2019-09-01 11:05:22 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
let source_file_relative_path = "some path";
|
2020-12-25 21:52:40 +00:00
|
|
|
let source_file_full_path = source.file_path(source_file_relative_path)?;
|
2020-11-28 14:29:23 +00:00
|
|
|
let old_contents = "some old contents";
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, old_contents)?;
|
2019-09-07 14:42:30 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
let old_item = newest_item(repository_path, secret, &source_file_full_path)?;
|
2020-11-28 14:29:23 +00:00
|
|
|
let old_id = old_item.id();
|
2019-09-07 14:42:30 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
let new_contents = "totally new contents";
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, new_contents)?;
|
|
|
|
|
|
|
|
assert_restored_from_version_has_contents(
|
|
|
|
repository_path,
|
|
|
|
secret,
|
|
|
|
&source_file_full_path,
|
|
|
|
old_contents.as_bytes(),
|
|
|
|
old_id,
|
|
|
|
)
|
2020-11-28 14:29:23 +00:00
|
|
|
}
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
#[test]
|
|
|
|
fn newer_version_should_be_greater_than_earlier_version() -> Result<()> {
|
|
|
|
let source = TestSource::new().unwrap();
|
2021-05-16 19:15:07 +01:00
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret)?;
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
let source_file_relative_path = "some path";
|
2020-12-25 21:52:40 +00:00
|
|
|
let source_file_full_path = source.file_path(source_file_relative_path)?;
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, "old")?;
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
let old_item = newest_item(repository_path, secret, &source_file_full_path)?;
|
2020-11-28 14:29:23 +00:00
|
|
|
let old_version = old_item.version();
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, "new")?;
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
let new_item = newest_item(repository_path, secret, &source_file_full_path)?;
|
2020-11-28 14:29:23 +00:00
|
|
|
let new_version = new_item.version();
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
assert!(new_version > old_version);
|
2019-09-23 16:30:40 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
#[test]
|
|
|
|
fn restore_latest_version_by_default() -> Result<()> {
|
|
|
|
let source = TestSource::new().unwrap();
|
2021-05-16 19:15:07 +01:00
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret)?;
|
2019-09-23 12:18:18 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
let source_file_relative_path = "some path";
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, "old contents")?;
|
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, "newer contents")?;
|
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, source_file_relative_path, "newest contents")?;
|
2019-09-23 12:18:18 +01:00
|
|
|
|
2020-12-25 21:52:40 +00:00
|
|
|
let source_file_full_path = &source.file_path(source_file_relative_path)?;
|
2021-10-31 00:25:37 +01:00
|
|
|
assert_restored_file_contents(repository_path, secret, source_file_full_path, b"newest contents")
|
2020-11-28 14:29:23 +00:00
|
|
|
}
|
2019-09-23 12:18:18 +01:00
|
|
|
|
2020-11-28 14:29:23 +00:00
|
|
|
#[test]
|
|
|
|
fn forbid_backup_of_paths_within_repository() -> Result<()> {
|
2021-05-16 19:15:07 +01:00
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret)?;
|
|
|
|
let mut repository = Repository::open(repository_path, secret)?;
|
2020-12-25 21:52:40 +00:00
|
|
|
|
2021-10-22 21:20:53 +01:00
|
|
|
let error = backup::Engine::new(repository_path, &mut repository);
|
2020-11-28 14:29:23 +00:00
|
|
|
assert!(error.is_err());
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-30 20:41:06 +01:00
|
|
|
|
2021-10-30 22:07:28 +01:00
|
|
|
proptest! {
|
|
|
|
#[test]
|
|
|
|
fn allow_searching_by_filename(filename in "[a-zA-Z]{3,}"){
|
|
|
|
let source = TestSource::new().unwrap();
|
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret).unwrap();
|
2021-10-30 20:41:06 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, &filename, "some contents").unwrap();
|
2021-10-30 20:41:06 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
let repository = Repository::open(repository_path, secret).unwrap();
|
2021-10-30 20:41:06 +01:00
|
|
|
|
2021-10-30 22:07:28 +01:00
|
|
|
let second_file = repository.find_latest_by_path_fragment(&filename).unwrap().unwrap();
|
|
|
|
assert_eq!(second_file.original_source_path(), source.file_path(&filename).unwrap().as_os_str());
|
|
|
|
}
|
2021-10-30 20:41:06 +01:00
|
|
|
|
2021-10-30 22:07:28 +01:00
|
|
|
#[test]
|
2021-10-31 00:25:37 +01:00
|
|
|
fn not_leak_file_names_via_file_names(filename in "[a-zA-Z]{4,}"){
|
2021-10-30 22:07:28 +01:00
|
|
|
let source = TestSource::new().unwrap();
|
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
2021-10-31 00:25:37 +01:00
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret).unwrap();
|
2021-10-30 20:41:06 +01:00
|
|
|
|
2021-10-31 00:25:37 +01:00
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, &filename, "some contents").unwrap();
|
2021-10-30 22:07:28 +01:00
|
|
|
|
|
|
|
let walker = WalkDir::new(repository_path);
|
|
|
|
let matching_paths = walker
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
.filter(|e| e.path().as_os_str().to_string_lossy().contains(&filename));
|
|
|
|
|
|
|
|
assert_eq!(matching_paths.count(), 0);
|
|
|
|
}
|
2021-10-31 00:25:37 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_leak_file_names_via_file_contents(test_filename in "[a-zA-Z]{8,}"){
|
|
|
|
let source = TestSource::new().unwrap();
|
|
|
|
let dir = tempdir()?;
|
|
|
|
let repository_path = dir.path();
|
|
|
|
let secret = "some secret";
|
|
|
|
Repository::init(repository_path, secret).unwrap();
|
|
|
|
|
|
|
|
backup_file_with_text_contents(&source, repository_path, secret, &test_filename, "some contents").unwrap();
|
|
|
|
|
|
|
|
let all_repo_files = get_sorted_files_recursively(repository_path).unwrap();
|
2021-10-31 10:44:40 +00:00
|
|
|
assert!(!all_repo_files.is_empty());
|
2021-10-31 00:25:37 +01:00
|
|
|
|
|
|
|
for filepath in all_repo_files {
|
|
|
|
let mut file = File::open(&filepath).unwrap();
|
|
|
|
let filename = &filepath.as_os_str().to_string_lossy();
|
|
|
|
let mut contents = vec![];
|
|
|
|
file.read_to_end(&mut contents).unwrap();
|
|
|
|
let test_filename_bytes = test_filename.as_bytes();
|
|
|
|
let contains = contents.windows(test_filename_bytes.len()).any(move |sub_slice| sub_slice == test_filename_bytes);
|
|
|
|
assert!(!contains, "file {} in the repository directory contains plain text file name '{}' that was previously backed up", filename, test_filename);
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 22:07:28 +01:00
|
|
|
}
|
2020-11-28 14:29:23 +00:00
|
|
|
// TODO: encryption
|
|
|
|
// TODO: resume from sleep while backup in progress
|
2019-09-07 15:08:47 +01:00
|
|
|
}
|