2018-05-30 23:57:13 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use grin_chain as chain;
|
|
|
|
use grin_core as core;
|
|
|
|
|
|
|
|
use grin_store as store;
|
|
|
|
use grin_util as util;
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2018-08-03 05:16:16 +03:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::fs::{self, File, OpenOptions};
|
2018-09-12 14:17:36 +03:00
|
|
|
use std::iter::FromIterator;
|
2018-08-03 05:16:16 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-05-30 23:57:13 +03:00
|
|
|
use std::sync::Arc;
|
2018-11-05 17:56:45 +03:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::chain::store::ChainStore;
|
|
|
|
use crate::chain::txhashset;
|
|
|
|
use crate::core::core::BlockHeader;
|
|
|
|
use crate::util::file;
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
fn clean_output_dir(dir_name: &str) {
|
|
|
|
let _ = fs::remove_dir_all(dir_name);
|
|
|
|
}
|
|
|
|
|
2018-08-03 05:16:16 +03:00
|
|
|
#[test]
|
|
|
|
fn test_unexpected_zip() {
|
2018-11-05 17:56:45 +03:00
|
|
|
let now = SystemTime::now();
|
|
|
|
let rand = now.duration_since(UNIX_EPOCH).unwrap().subsec_micros();
|
|
|
|
|
2018-08-03 05:16:16 +03:00
|
|
|
let db_root = format!(".grin_txhashset_zip");
|
|
|
|
clean_output_dir(&db_root);
|
|
|
|
let db_env = Arc::new(store::new_env(db_root.clone()));
|
|
|
|
let chain_store = ChainStore::new(db_env).unwrap();
|
|
|
|
let store = Arc::new(chain_store);
|
|
|
|
txhashset::TxHashSet::open(db_root.clone(), store.clone(), None).unwrap();
|
|
|
|
// First check if everything works out of the box
|
2018-11-05 17:56:45 +03:00
|
|
|
assert!(txhashset::zip_read(db_root.clone(), &BlockHeader::default(), Some(rand)).is_ok());
|
|
|
|
let zip_path = Path::new(&db_root).join(format!("txhashset_snapshot_{}.zip", rand));
|
2018-08-03 05:16:16 +03:00
|
|
|
let zip_file = File::open(&zip_path).unwrap();
|
|
|
|
assert!(txhashset::zip_write(db_root.clone(), zip_file, &BlockHeader::default()).is_ok());
|
|
|
|
// Remove temp txhashset dir
|
2018-11-05 17:56:45 +03:00
|
|
|
fs::remove_dir_all(Path::new(&db_root).join(format!("txhashset_zip_{}", rand))).unwrap();
|
2018-08-03 05:16:16 +03:00
|
|
|
// Then add strange files in the original txhashset folder
|
|
|
|
write_file(db_root.clone());
|
2018-11-05 17:56:45 +03:00
|
|
|
assert!(txhashset::zip_read(db_root.clone(), &BlockHeader::default(), Some(rand)).is_ok());
|
2018-08-03 05:16:16 +03:00
|
|
|
// Check that the temp dir dos not contains the strange files
|
2018-11-05 17:56:45 +03:00
|
|
|
let txhashset_zip_path = Path::new(&db_root).join(format!("txhashset_zip_{}", rand));
|
2018-09-12 14:17:36 +03:00
|
|
|
assert!(txhashset_contains_expected_files(
|
2018-11-05 17:56:45 +03:00
|
|
|
format!("txhashset_zip_{}", rand),
|
2018-09-12 14:17:36 +03:00
|
|
|
txhashset_zip_path.clone()
|
|
|
|
));
|
2018-11-05 17:56:45 +03:00
|
|
|
fs::remove_dir_all(Path::new(&db_root).join(format!("txhashset_zip_{}", rand))).unwrap();
|
2018-08-03 05:16:16 +03:00
|
|
|
|
|
|
|
let zip_file = File::open(zip_path).unwrap();
|
|
|
|
assert!(txhashset::zip_write(db_root.clone(), zip_file, &BlockHeader::default()).is_ok());
|
|
|
|
// Check that the txhashset dir dos not contains the strange files
|
|
|
|
let txhashset_path = Path::new(&db_root).join("txhashset");
|
2018-09-12 14:17:36 +03:00
|
|
|
assert!(txhashset_contains_expected_files(
|
|
|
|
"txhashset".to_string(),
|
|
|
|
txhashset_path.clone()
|
|
|
|
));
|
2018-08-03 05:16:16 +03:00
|
|
|
fs::remove_dir_all(Path::new(&db_root).join("txhashset")).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-09-12 14:17:36 +03:00
|
|
|
fn write_file(db_root: String) {
|
2018-08-03 05:16:16 +03:00
|
|
|
OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
2018-09-12 14:17:36 +03:00
|
|
|
.open(
|
|
|
|
Path::new(&db_root)
|
|
|
|
.join("txhashset")
|
|
|
|
.join("kernel")
|
|
|
|
.join("strange0"),
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-03 05:16:16 +03:00
|
|
|
OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
2018-09-12 14:17:36 +03:00
|
|
|
.open(Path::new(&db_root).join("txhashset").join("strange1"))
|
|
|
|
.unwrap();
|
2018-08-03 05:16:16 +03:00
|
|
|
fs::create_dir(Path::new(&db_root).join("txhashset").join("strange_dir")).unwrap();
|
|
|
|
OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
2018-09-12 14:17:36 +03:00
|
|
|
.open(
|
|
|
|
Path::new(&db_root)
|
|
|
|
.join("txhashset")
|
|
|
|
.join("strange_dir")
|
|
|
|
.join("strange2"),
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-09-12 14:17:36 +03:00
|
|
|
fs::create_dir(
|
|
|
|
Path::new(&db_root)
|
|
|
|
.join("txhashset")
|
|
|
|
.join("strange_dir")
|
|
|
|
.join("strange_subdir"),
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-03 05:16:16 +03:00
|
|
|
OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
2018-09-12 14:17:36 +03:00
|
|
|
.open(
|
|
|
|
Path::new(&db_root)
|
|
|
|
.join("txhashset")
|
|
|
|
.join("strange_dir")
|
|
|
|
.join("strange_subdir")
|
|
|
|
.join("strange3"),
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-03 05:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn txhashset_contains_expected_files(dirname: String, path_buf: PathBuf) -> bool {
|
|
|
|
let list_zip_files = file::list_files(path_buf.into_os_string().into_string().unwrap());
|
|
|
|
let zip_files_hashset: HashSet<_> = HashSet::from_iter(list_zip_files.iter().cloned());
|
2018-09-12 14:17:36 +03:00
|
|
|
let expected_files = vec![
|
|
|
|
dirname,
|
|
|
|
"output".to_string(),
|
|
|
|
"rangeproof".to_string(),
|
|
|
|
"kernel".to_string(),
|
|
|
|
"pmmr_hash.bin".to_string(),
|
|
|
|
"pmmr_data.bin".to_string(),
|
|
|
|
];
|
2018-08-03 05:16:16 +03:00
|
|
|
let expected_files_hashset = HashSet::from_iter(expected_files.iter().cloned());
|
2018-09-12 14:17:36 +03:00
|
|
|
let intersection: HashSet<_> = zip_files_hashset
|
|
|
|
.difference(&expected_files_hashset)
|
|
|
|
.collect();
|
2019-02-25 09:57:56 +03:00
|
|
|
intersection.is_empty()
|
2018-09-12 14:17:36 +03:00
|
|
|
}
|