team-ljyzipelo

공개
230줄5.6 KB
1use crate::support::*;
2use publication::{
3 Error, Limits, MAX_BYTES, MAX_IO_BYTES, MAX_IO_CALLS, Publication, Request, Stop, Usage,
4};
5use std::{ffi::OsStr, path::Path, time::Duration};
6
7fn boundary(length: usize) {
8 let sandbox = Sandbox::new();
9 let bytes = pattern(length);
10 let before = bytes.clone();
11 let parent = sandbox.parent();
12 let report = publish(&parent, OsStr::new("output"), &bytes, Limits::default());
13 committed(report, &parent, OsStr::new("output"), &before);
14 assert_eq!(bytes, before);
15 sandbox.finish();
16}
17
18#[test]
19fn empty() {
20 boundary(0);
21}
22#[test]
23fn one() {
24 boundary(1);
25}
26#[test]
27fn chunk_minus_one() {
28 boundary(65_535);
29}
30#[test]
31fn chunk() {
32 boundary(65_536);
33}
34#[test]
35fn chunk_plus_one() {
36 boundary(65_537);
37}
38#[test]
39fn maximum_minus_one() {
40 boundary(16_777_215);
41}
42#[test]
43fn maximum() {
44 boundary(16_777_216);
45}
46
47#[test]
48fn invalid_limits_precede_paths_and_callback() {
49 for limits in [
50 Limits {
51 max_bytes: MAX_BYTES + 1,
52 ..Limits::default()
53 },
54 Limits {
55 max_io_bytes: MAX_IO_BYTES + 1,
56 ..Limits::default()
57 },
58 Limits {
59 max_io_calls: MAX_IO_CALLS + 1,
60 ..Limits::default()
61 },
62 Limits {
63 max_duration: Duration::ZERO,
64 ..Limits::default()
65 },
66 Limits {
67 max_duration: Duration::from_secs(301),
68 ..Limits::default()
69 },
70 ] {
71 let (task, cancel) = Publication::new();
72 let report = task.publish(
73 Request {
74 parent: Path::new("invalid"),
75 leaf: OsStr::new(""),
76 bytes: &[],
77 limits,
78 },
79 |_| -> Result<(), Stop<()>> { panic!("callback must not run") },
80 );
81 let (error, usage, closed) = not_committed(report);
82 assert!(matches!(error, Error::InvalidLimits));
83 assert_eq!(usage, Usage::default());
84 cleanup(&closed, 0);
85 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
86 }
87}
88
89#[test]
90fn oversized_input_rejected_without_io() {
91 let bytes = pattern(MAX_BYTES as usize + 1);
92 let report = publish(
93 Path::new("invalid"),
94 OsStr::new("output"),
95 &bytes,
96 Limits::default(),
97 );
98 let (error, usage, closed) = not_committed(report);
99 assert!(matches!(error, Error::LimitExceeded));
100 assert_eq!(usage, Usage::default());
101 cleanup(&closed, 0);
102}
103
104#[test]
105fn zero_byte_limit_accepts_empty_with_exact_eof_budget() {
106 let sandbox = Sandbox::new();
107 let parent = sandbox.parent();
108 let limits = Limits {
109 max_bytes: 0,
110 max_io_bytes: 1,
111 max_io_calls: 1,
112 ..Limits::default()
113 };
114 committed(
115 publish(&parent, OsStr::new("output"), &[], limits),
116 &parent,
117 OsStr::new("output"),
118 &[],
119 );
120 sandbox.finish();
121}
122
123#[test]
124fn zero_byte_limit_rejects_payload_before_path_validation() {
125 let (error, usage, closed) = not_committed(publish(
126 Path::new("invalid"),
127 OsStr::new("output"),
128 b"x",
129 Limits {
130 max_bytes: 0,
131 ..Limits::default()
132 },
133 ));
134 assert!(matches!(error, Error::LimitExceeded));
135 assert_eq!(usage, Usage::default());
136 cleanup(&closed, 0);
137}
138
139fn empty_budget(limits: Limits) {
140 let sandbox = Sandbox::new();
141 let (error, usage, closed) = not_committed(publish(
142 &sandbox.parent(),
143 OsStr::new("output"),
144 &[],
145 limits,
146 ));
147 assert!(matches!(error, Error::LimitExceeded));
148 assert_eq!(usage.open_calls, 2);
149 assert_eq!(usage.metadata_calls, 3);
150 assert_eq!(usage.sync_calls, 1);
151 assert_eq!(
152 usage.write_calls + usage.read_calls + usage.reserved_io_bytes + usage.link_calls,
153 0
154 );
155 cleanup(&closed, 2);
156 assert!(names(&sandbox.parent()).is_empty());
157 sandbox.finish();
158}
159
160#[test]
161fn zero_io_budget_fails_eof_without_refund() {
162 empty_budget(Limits {
163 max_io_bytes: 0,
164 ..Limits::default()
165 });
166}
167
168#[test]
169fn zero_call_budget_is_valid_but_cannot_probe_eof() {
170 empty_budget(Limits {
171 max_io_calls: 0,
172 ..Limits::default()
173 });
174}
175
176#[test]
177fn exact_io_and_call_budget_includes_eof() {
178 let sandbox = Sandbox::new();
179 let parent = sandbox.parent();
180 let bytes = pattern(65_537);
181 let limits = Limits {
182 max_io_bytes: 131_075,
183 max_io_calls: 5,
184 ..Limits::default()
185 };
186 committed(
187 publish(&parent, OsStr::new("output"), &bytes, limits),
188 &parent,
189 OsStr::new("output"),
190 &bytes,
191 );
192 sandbox.finish();
193}
194
195fn last_eof_limit(limits: Limits) {
196 let sandbox = Sandbox::new();
197 let (error, usage, closed) = not_committed(publish(
198 &sandbox.parent(),
199 OsStr::new("output"),
200 &pattern(65_537),
201 limits,
202 ));
203 assert!(matches!(error, Error::LimitExceeded));
204 assert_eq!(usage.write_calls, 2);
205 assert_eq!(usage.read_calls, 2);
206 assert_eq!(usage.reserved_io_bytes, 131_074);
207 assert_eq!(usage.actual_written_bytes, 65_537);
208 assert_eq!(usage.actual_read_bytes, 65_537);
209 assert_eq!(usage.link_calls, 0);
210 assert_eq!(usage.sync_calls, 1);
211 cleanup(&closed, 2);
212 assert!(names(&sandbox.parent()).is_empty());
213 sandbox.finish();
214}
215
216#[test]
217fn one_less_io_budget_stops_before_last_eof() {
218 last_eof_limit(Limits {
219 max_io_bytes: 131_074,
220 ..Limits::default()
221 });
222}
223
224#[test]
225fn one_less_call_budget_stops_before_last_eof() {
226 last_eof_limit(Limits {
227 max_io_calls: 4,
228 ..Limits::default()
229 });
230}