team-ljyzipelo

공개
107줄3.5 KB
1use super::*;
2use std::sync::atomic::{AtomicU64, Ordering};
3static NEXT: AtomicU64 = AtomicU64::new(0);
4struct Scratch(PathBuf);
5impl Scratch {
6 fn new() -> Self {
7 let root =
8 Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/rust-archive-worker/runs");
9 fs::create_dir_all(&root).unwrap();
10 let path = root.join(format!(
11 "sources-{}-{}",
12 std::process::id(),
13 NEXT.fetch_add(1, Ordering::Relaxed)
14 ));
15 fs::create_dir(&path).unwrap();
16 Self(path.canonicalize().unwrap())
17 }
18 fn source(&self, name: &str) -> Source {
19 Source {
20 path: NativePath::from_path(&self.0.join(name)),
21 archive_name: name.into(),
22 }
23 }
24}
25impl Drop for Scratch {
26 fn drop(&mut self) {
27 fs::remove_dir_all(&self.0).unwrap();
28 }
29}
30#[test]
31fn recursive_counts_and_source_bytes_honor_exact_quotas() {
32 let s = Scratch::new();
33 fs::create_dir(s.0.join("folder")).unwrap();
34 fs::create_dir(s.0.join("folder/empty")).unwrap();
35 fs::write(s.0.join("folder/file"), b"abc").unwrap();
36 for (entries, output, expected) in [
37 (3, 3, None),
38 (2, 3, Some(ErrorCode::Limit)),
39 (3, 2, Some(ErrorCode::Limit)),
40 ] {
41 let result = Plan::new(
42 &[s.source("folder")],
43 Limits {
44 entries,
45 output,
46 ..Limits::default()
47 },
48 &mut || Ok(()),
49 );
50 assert_eq!(result.as_ref().err().copied(), expected);
51 if let Ok(plan) = result {
52 assert_eq!(plan.items.len(), 3);
53 }
54 }
55}
56#[test]
57fn replaced_source_is_rejected_before_capture() {
58 let s = Scratch::new();
59 fs::write(s.0.join("file"), b"original").unwrap();
60 let plan = Plan::new(&[s.source("file")], Limits::default(), &mut || Ok(())).unwrap();
61 fs::rename(s.0.join("file"), s.0.join("previous")).unwrap();
62 fs::write(s.0.join("file"), b"different").unwrap();
63 assert!(matches!(
64 plan.items[0].open(),
65 Err(ErrorCode::SourceChanged)
66 ));
67}
68#[test]
69fn cancellation_stops_directory_enumeration() {
70 let s = Scratch::new();
71 fs::create_dir(s.0.join("folder")).unwrap();
72 for n in 0..20 {
73 fs::write(s.0.join(format!("folder/{n}")), b"x").unwrap();
74 }
75 let mut calls = 0;
76 let result = Plan::new(&[s.source("folder")], Limits::default(), &mut || {
77 calls += 1;
78 if calls == 5 {
79 Err(ErrorCode::None)
80 } else {
81 Ok(())
82 }
83 });
84 assert!(matches!(result, Err(ErrorCode::None)));
85}
86#[cfg(target_os = "linux")]
87#[test]
88fn pinned_directory_enumeration_does_not_follow_replacement_link() {
89 let s = Scratch::new();
90 fs::create_dir(s.0.join("folder")).unwrap();
91 fs::create_dir(s.0.join("outside")).unwrap();
92 fs::write(s.0.join("folder/original"), b"original").unwrap();
93 fs::write(s.0.join("outside/secret"), b"secret").unwrap();
94 let path = s.0.join("folder");
95 let input = Input::open(&path).unwrap();
96 fs::rename(&path, s.0.join("moved")).unwrap();
97 std::os::unix::fs::symlink(s.0.join("outside"), &path).unwrap();
98 let names = input
99 .children(&path)
100 .unwrap()
101 .map(|v| v.unwrap().file_name())
102 .collect::<Vec<_>>();
103 assert_eq!(names, vec![std::ffi::OsString::from("original")]);
104 assert!(input.open_child(&path, "original").is_ok());
105 assert!(input.open_child(&path, "secret").is_err());
106 assert!(matches!(Input::open(&path), Err(ErrorCode::UnsafePath)));
107}