use checkpointkit::{load, save, CheckpointError, ProgressTracker};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use tempfile::tempdir;
#[test]
fn break_save_empty_string_path() {
let state = 42;
let res = save(Path::new(""), &state);
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Saving to empty path should yield Io error");
}
#[test]
fn break_load_empty_string_path() {
let res: Result<Option<i32>, _> = load(Path::new(""));
match res {
Err(CheckpointError::Io { .. }) | Ok(None) => (),
_ => panic!("Loading from empty path should yield Io error or Ok(None)"),
}
}
#[test]
fn break_save_empty_state_struct() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Empty {}
let dir = tempdir().unwrap();
let path = dir.path().join("empty.json");
save(&path, &Empty {}).expect("Saving empty struct should succeed");
let loaded: Empty = load(&path).unwrap().unwrap();
assert_eq!(loaded, Empty {}, "Loaded empty struct should match");
}
#[test]
fn break_save_null_bytes_in_path() {
let state = 42;
let res = save(Path::new("null\0byte.json"), &state);
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Saving to path with null bytes should yield Io error");
}
#[test]
fn break_load_null_bytes_in_path() {
let res: Result<Option<i32>, _> = load(Path::new("null\0byte.json"));
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Loading from path with null bytes should yield Io error");
}
#[test]
fn break_progress_tracker_null_bytes_in_path() {
let res = ProgressTracker::new(100, Path::new("null\0byte.json"), Duration::from_secs(1));
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Progress tracker with null bytes in path should yield Io error");
}
#[test]
fn break_save_max_u64_state() {
let dir = tempdir().unwrap();
let path = dir.path().join("max_u64.json");
save(&path, &u64::MAX).expect("Saving u64::MAX should succeed");
let loaded: u64 = load(&path).unwrap().unwrap();
assert_eq!(loaded, u64::MAX, "Loaded value should match u64::MAX");
}
#[test]
fn break_progress_tracker_max_u64_total() {
let dir = tempdir().unwrap();
let path = dir.path().join("max_u64_progress.json");
let mut tracker = ProgressTracker::new(u64::MAX, &path, Duration::ZERO).unwrap();
let res = tracker.increment(1).unwrap();
assert_eq!(res, 1, "Incrementing from 0 should yield 1");
assert_eq!(tracker.total(), u64::MAX, "Total should be u64::MAX");
}
#[test]
fn break_progress_tracker_increment_past_max_u64() {
let dir = tempdir().unwrap();
let path = dir.path().join("overflow_progress.json");
let mut tracker = ProgressTracker::new(u64::MAX, &path, Duration::ZERO).unwrap();
let _ = tracker.increment(u64::MAX).unwrap();
let res = tracker.increment(1).unwrap();
assert_eq!(res, u64::MAX, "Incrementing past u64::MAX should cap at total");
}
#[test]
fn break_progress_tracker_max_u64_increment() {
let dir = tempdir().unwrap();
let path = dir.path().join("max_u64_inc.json");
let mut tracker = ProgressTracker::new(100, &path, Duration::ZERO).unwrap();
let res = tracker.increment(u64::MAX).unwrap();
assert_eq!(res, 100, "Incrementing by u64::MAX should cap at total");
}
#[test]
fn break_save_1mb_input() {
let dir = tempdir().unwrap();
let path = dir.path().join("1mb.json");
let state = "a".repeat(1024 * 1024);
save(&path, &state).expect("Saving 1MB string should succeed");
let loaded: String = load(&path).unwrap().unwrap();
assert_eq!(loaded, state, "Loaded string should match 1MB input");
}
#[test]
fn break_concurrent_saves() {
let dir = tempdir().unwrap();
let path = Arc::new(dir.path().join("concurrent.json"));
let mut handles = vec![];
let num_threads = 8;
for i in 0..num_threads {
let path = Arc::clone(&path);
handles.push(thread::spawn(move || {
let res = save(path.as_ref(), &i);
assert!(res.is_ok(), "Concurrent save should succeed");
}));
}
for handle in handles {
handle.join().unwrap();
}
let loaded: i32 = load(path.as_ref()).unwrap().unwrap();
assert!((0..num_threads).contains(&loaded), "Loaded value should be from one of the threads");
}
#[test]
fn break_load_malformed_json() {
let dir = tempdir().unwrap();
let path = dir.path().join("malformed.json");
fs::write(&path, "{").unwrap();
let res: Result<Option<i32>, _> = load(&path);
assert!(matches!(res, Err(CheckpointError::Serialize(_))), "Loading malformed JSON should yield Serialize error");
}
#[test]
fn break_progress_tracker_malformed_json() {
let dir = tempdir().unwrap();
let path = dir.path().join("malformed_progress.json");
fs::write(&path, "{").unwrap();
let res = ProgressTracker::new(100, &path, Duration::from_secs(1));
assert!(matches!(res, Err(CheckpointError::Serialize(_))), "ProgressTracker::new with malformed JSON should yield Serialize error");
}
#[test]
fn break_load_truncated_json() {
let dir = tempdir().unwrap();
let path = dir.path().join("truncated.json");
fs::write(&path, r#"{"total":100,"#).unwrap();
let res: Result<Option<HashMap<String, i32>>, _> = load(&path);
assert!(matches!(res, Err(CheckpointError::Serialize(_))), "Loading truncated JSON should yield Serialize error");
}
#[test]
fn break_save_unicode_bom() {
let dir = tempdir().unwrap();
let path = dir.path().join("bom.json");
let state = "\u{feff}hello";
save(&path, &state).expect("Saving string with BOM should succeed");
let loaded: String = load(&path).unwrap().unwrap();
assert_eq!(loaded, state, "Loaded string with BOM should match");
}
#[test]
fn break_save_overlong_unicode() {
let dir = tempdir().unwrap();
let path = dir.path().join("overlong.json");
let state = "A\u{0308}\u{0332}";
save(&path, &state).expect("Saving complex unicode should succeed");
let loaded: String = load(&path).unwrap().unwrap();
assert_eq!(loaded, state, "Loaded complex unicode should match");
}
#[test]
fn break_save_duplicate_keys() {
let dir = tempdir().unwrap();
let path = dir.path().join("dup_keys.json");
fs::write(&path, r#"{"a":1,"a":2}"#).unwrap();
let res: HashMap<String, i32> = load(&path).unwrap().unwrap();
assert_eq!(res.get("a"), Some(&2), "Duplicate keys should resolve to the last one (serde_json behavior)");
}
#[test]
fn break_save_nested_100k() {
let dir = tempdir().unwrap();
let path = dir.path().join("nested.json");
let mut state = Vec::new();
for i in 0..100_000 {
state.push(i);
}
save(&path, &state).expect("Saving 100k item vec should succeed");
let loaded: Vec<i32> = load(&path).unwrap().unwrap();
assert_eq!(loaded.len(), 100_000, "Loaded vec should have 100k items");
}
#[test]
fn break_path_traversal() {
let res = save(Path::new("../test.json"), &42);
assert!(matches!(res, Err(CheckpointError::PathTraversal(_))), "Path traversal should be blocked");
}
#[test]
fn break_load_path_traversal() {
let res: Result<Option<i32>, _> = load(Path::new("../test.json"));
assert!(matches!(res, Err(CheckpointError::PathTraversal(_))), "Loading with path traversal should be blocked");
}
#[test]
fn break_progress_tracker_path_traversal() {
let res = ProgressTracker::new(100, Path::new("../test.json"), Duration::from_secs(1));
assert!(matches!(res, Err(CheckpointError::PathTraversal(_))), "ProgressTracker with path traversal should be blocked");
}
#[test]
fn break_progress_tracker_increment_0() {
let dir = tempdir().unwrap();
let path = dir.path().join("inc0.json");
let mut tracker = ProgressTracker::new(100, &path, Duration::ZERO).unwrap();
let res = tracker.increment(0).unwrap();
assert_eq!(res, 0, "Incrementing by 0 should yield 0");
}
#[test]
fn break_progress_tracker_zero_total() {
let dir = tempdir().unwrap();
let path = dir.path().join("zero_total.json");
let mut tracker = ProgressTracker::new(0, &path, Duration::ZERO).unwrap();
let res = tracker.increment(1).unwrap();
assert_eq!(res, 0, "Incrementing with 0 total should cap at 0");
}
#[test]
fn break_progress_tracker_resume_zero_total() {
let dir = tempdir().unwrap();
let path = dir.path().join("zero_total_resume.json");
let mut tracker = ProgressTracker::new(0, &path, Duration::ZERO).unwrap();
let _ = tracker.increment(1).unwrap();
let res = tracker.resume().unwrap();
assert_eq!(res, 0, "Resuming with 0 total should yield 0");
}
#[test]
fn break_progress_tracker_new_with_larger_saved_progress() {
let dir = tempdir().unwrap();
let path = dir.path().join("larger_saved.json");
let mut tracker1 = ProgressTracker::new(100, &path, Duration::ZERO).unwrap();
let _ = tracker1.increment(50).unwrap();
let tracker2 = ProgressTracker::new(10, &path, Duration::ZERO).unwrap();
assert_eq!(tracker2.current(), 10, "New tracker should cap current at new total");
}
#[test]
fn break_save_empty_file_name() {
let dir = tempdir().unwrap();
let path = dir.path().to_path_buf(); let res = save(&path, &42);
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Saving to directory path should yield Io error");
}
#[test]
fn break_load_directory_path() {
let dir = tempdir().unwrap();
let path = dir.path().to_path_buf(); let res: Result<Option<i32>, _> = load(&path);
match res {
Err(CheckpointError::Io { .. } | CheckpointError::Serialize(_)) | Ok(None) => (),
_ => panic!("Loading from directory path should yield error or Ok(None)"),
}
}
#[test]
fn break_progress_tracker_directory_path() {
let dir = tempdir().unwrap();
let path = dir.path().to_path_buf(); let res = ProgressTracker::new(100, &path, Duration::from_secs(1));
match res {
Err(CheckpointError::Io { .. } | CheckpointError::Serialize(_)) => (),
_ => panic!("ProgressTracker::new on directory path should yield error"),
}
}
#[test]
fn break_save_read_only_parent() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let dir = tempdir().unwrap();
let path = dir.path().join("readonly").join("test.json");
fs::create_dir_all(dir.path().join("readonly")).unwrap();
let mut perms = fs::metadata(dir.path().join("readonly")).unwrap().permissions();
perms.set_mode(0o555);
fs::set_permissions(dir.path().join("readonly"), perms).unwrap();
let res = save(&path, &42);
let mut perms = fs::metadata(dir.path().join("readonly")).unwrap().permissions();
perms.set_mode(0o777);
fs::set_permissions(dir.path().join("readonly"), perms).unwrap();
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Saving to read-only dir should yield Io error");
}
}
#[test]
fn break_save_max_path_length() {
let dir = tempdir().unwrap();
let binding = dir.path().to_string_lossy();
let max_len = 255_usize.saturating_sub(binding.len()).saturating_sub(25);
let long_name = "a".repeat(max_len);
let path = dir.path().join(&long_name);
save(&path, &42).expect("Saving with valid filename length should succeed");
let loaded: i32 = load(&path).unwrap().unwrap();
assert_eq!(loaded, 42, "Loaded value should match");
}
#[test]
fn break_save_over_max_path_length() {
let dir = tempdir().unwrap();
let long_name = "a".repeat(1000); let path = dir.path().join(&long_name);
let res = save(&path, &42);
assert!(matches!(res, Err(CheckpointError::Io { .. })), "Saving with over max filename length should yield Io error");
}
#[test]
fn break_progress_tracker_interval_max() {
let dir = tempdir().unwrap();
let path = dir.path().join("interval_max.json");
let mut tracker = ProgressTracker::new(100, &path, Duration::MAX).unwrap();
let res = tracker.increment(1).unwrap();
assert_eq!(res, 1, "Incrementing with max interval should yield 1");
let loaded: Result<Option<HashMap<String, i32>>, _> = load(&path);
assert!(loaded.unwrap().is_none(), "Should not have saved due to max interval");
}