use object_store::local::LocalFileSystem;
use std::sync::Arc;
use wal_backup::storage::{BucketStore, ObjectStoreBucket};
#[tokio::test]
async fn test_prefix_traversal_in_prefix_rejected() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let result = ObjectStoreBucket::new(store, "../escape".to_string());
assert!(
result.is_err(),
"path traversal in prefix should be rejected"
);
}
#[tokio::test]
async fn test_prefix_null_byte_in_prefix_rejected() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let result = ObjectStoreBucket::new(store, "safe\0".to_string());
assert!(result.is_err(), "null byte in prefix should be rejected");
}
#[tokio::test]
async fn test_get_missing_key_returns_none() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "test".to_string()).expect("bucket"));
let result = bucket.get("nonexistent-key").await;
assert!(result.is_ok());
assert!(result.unwrap().is_none(), "missing key should return None");
}
#[tokio::test]
async fn test_exists_missing_key_returns_false() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "test".to_string()).expect("bucket"));
let result = bucket.exists("nonexistent-key").await;
assert!(result.is_ok());
assert!(!result.unwrap(), "missing key should not exist");
}
#[tokio::test]
async fn test_list_with_no_matching_prefix_returns_empty() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "test".to_string()).expect("bucket"));
let result = bucket.list("nonexistent-prefix").await;
assert!(result.is_ok());
assert!(
result.unwrap().is_empty(),
"no matching prefix should return empty list"
);
}
#[tokio::test]
async fn test_path_traversal_key_rejected() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "safe".to_string()).expect("bucket"));
let result = bucket
.put("../../etc/passwd", bytes::Bytes::from(b"oops".as_slice()))
.await;
assert!(result.is_err(), "path traversal key should be rejected");
let result2 = bucket.get("..\0hidden").await;
assert!(result2.is_err(), "null byte in key should be rejected");
}
#[tokio::test]
async fn test_empty_prefix_rejects_traversal() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "".to_string()).expect("bucket"));
let result = bucket
.put("../escape", bytes::Bytes::from(b"data".as_slice()))
.await;
assert!(
result.is_err(),
"path traversal in key should be rejected even with empty prefix"
);
}
#[tokio::test]
async fn test_valid_key_accepted() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "test".to_string()).expect("bucket"));
let result = bucket
.put(
"wal/00000000-00000001.segment",
bytes::Bytes::from(b"data".as_slice()),
)
.await;
assert!(result.is_ok(), "valid key with slashes should be accepted");
}
#[tokio::test]
async fn test_put_get_roundtrip() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "roundtrip".to_string()).expect("bucket"));
let data = bytes::Bytes::from(b"hello world".as_slice());
bucket.put("test_key", data.clone()).await.expect("put");
let result = bucket.get("test_key").await.expect("get");
assert_eq!(result, Some(data));
}
#[tokio::test]
async fn test_exists_returns_true_for_existing_key() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket =
Arc::new(ObjectStoreBucket::new(store, "exists_test".to_string()).expect("bucket"));
bucket
.put("existing_key", bytes::Bytes::from(b"data".as_slice()))
.await
.expect("put");
assert!(bucket.exists("existing_key").await.expect("exists"));
}
#[tokio::test]
async fn test_delete_removes_key() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket =
Arc::new(ObjectStoreBucket::new(store, "delete_test".to_string()).expect("bucket"));
bucket
.put("to_delete", bytes::Bytes::from(b"data".as_slice()))
.await
.expect("put");
assert!(bucket.exists("to_delete").await.expect("exists before"));
bucket.delete("to_delete").await.expect("delete");
assert!(!bucket.exists("to_delete").await.expect("exists after"));
assert!(bucket.get("to_delete").await.expect("get").is_none());
}
#[tokio::test]
async fn test_list_returns_stored_keys() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "list_test".to_string()).expect("bucket"));
bucket
.put("wal/seg1.segment", bytes::Bytes::from(b"a".as_slice()))
.await
.expect("put1");
bucket
.put("wal/seg2.segment", bytes::Bytes::from(b"b".as_slice()))
.await
.expect("put2");
bucket
.put("manifest.json", bytes::Bytes::from(b"{}".as_slice()))
.await
.expect("put3");
let keys = bucket.list("wal/").await.expect("list");
assert!(keys.contains(&"wal/seg1.segment".to_string()));
assert!(keys.contains(&"wal/seg2.segment".to_string()));
assert!(!keys.contains(&"manifest.json".to_string()));
}
#[tokio::test]
async fn test_empty_prefix_valid_key() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "".to_string()).expect("bucket"));
bucket
.put("valid_key", bytes::Bytes::from(b"data".as_slice()))
.await
.expect("put with empty prefix");
let result = bucket.get("valid_key").await.expect("get");
assert!(result.is_some());
}
#[test]
fn test_build_s3_store_returns_arc_objectstore() {
use wal_backup::storage::object_store_util::build_s3_store;
let result = build_s3_store(
"http://localhost:9000",
"us-east-1",
"test_access_key",
"test_secret_key",
"test-bucket",
);
assert!(
result.is_ok(),
"build_s3_store should succeed with valid parameters: {:?}",
result
);
}
#[test]
#[ignore = "depends on AWS env vars; run manually with `cargo test -- --ignored`. Mutates process env so must run serially."]
fn test_build_s3_store_from_env_missing_vars_returns_err() {
use wal_backup::storage::object_store_util::build_s3_store_from_env;
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = LOCK.lock().expect("env test lock");
std::env::remove_var("AWS_ACCESS_KEY_ID");
std::env::remove_var("AWS_SECRET_ACCESS_KEY");
std::env::remove_var("AWS_DEFAULT_REGION");
std::env::remove_var("AWS_REGION");
std::env::remove_var("AWS_ENDPOINT_URL");
let result = build_s3_store_from_env("test-bucket");
assert!(
result.is_err(),
"build_s3_store_from_env without env vars should fail"
);
}
#[tokio::test]
async fn test_absolute_key_rejected() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let bucket = Arc::new(ObjectStoreBucket::new(store, "test".to_string()).expect("bucket"));
let result = bucket.put("/etc/passwd", bytes::Bytes::from("data")).await;
assert!(result.is_err(), "absolute key should be rejected");
}
#[tokio::test]
async fn test_absolute_prefix_rejected() {
let tmp = tempfile::tempdir().expect("tmp dir");
let bucket_dir = tmp.path().join("bucket");
std::fs::create_dir_all(&bucket_dir).expect("create bucket dir");
let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir).expect("store"));
let result = ObjectStoreBucket::new(store, "/absolute-prefix".to_string());
assert!(result.is_err(), "absolute prefix should be rejected");
}