team-ljyzipelo

공개
211줄6.3 KB
1use super::support::*;
2use zipelo_rust_memory_zip_create::{
3 Checkpoint, Failure, Limits, MAX_BYTES, MAX_INPUTS, Method, create,
4};
5
6#[test]
7fn caller_input_exact_and_one_over() {
8 for method in [Method::Stored, Method::Deflate] {
9 let mut input = small(method);
10 input.limits.input = 3;
11 assert_zip32(&run(input), 1);
12 let mut input = small(method);
13 input.limits.input = 2;
14 fail_before_output(input, Failure::Budget);
15 }
16}
17
18#[test]
19fn aggregate_input_not_just_each_entry_is_bounded() {
20 let make = || {
21 request(
22 Method::Stored,
23 vec![entry("a", vec![1, 2]), entry("b", vec![3, 4])],
24 )
25 };
26 let mut input = make();
27 input.limits.input = 4;
28 assert_zip32(&run(input), 2);
29 let mut input = make();
30 input.limits.input = 3;
31 fail_before_output(input, Failure::Budget);
32}
33
34#[test]
35fn hard_input_limit_and_larger_caller_cannot_bypass() {
36 let mut input = request(
37 Method::Deflate,
38 vec![entry("a", vec![0; MAX_BYTES as usize])],
39 );
40 input.limits.input = u64::MAX;
41 assert_zip32(&run(input), 1);
42 let mut input = request(
43 Method::Deflate,
44 vec![entry("a", vec![0; MAX_BYTES as usize + 1])],
45 );
46 input.limits.input = u64::MAX;
47 fail_before_output(input, Failure::Budget);
48}
49
50#[test]
51fn zero_input_budget_allows_empty_file_only() {
52 let mut input = request(Method::Stored, vec![entry("a", vec![])]);
53 input.limits.input = 0;
54 assert_zip32(&run(input), 1);
55 let mut input = small(Method::Stored);
56 input.limits.input = 0;
57 fail_before_output(input, Failure::Budget);
58}
59
60#[test]
61fn artifact_counts_as_one_additional_entry() {
62 let mut input = small(Method::Stored);
63 input.limits.entries = 2;
64 assert_zip32(&run(input), 1);
65 let mut input = small(Method::Stored);
66 input.limits.entries = 1;
67 fail_before_output(input, Failure::Budget);
68 let mut input = small(Method::Stored);
69 input.limits.entries = 0;
70 fail_before_output(input, Failure::Budget);
71}
72
73#[test]
74fn hard_input_count_1024_and_one_over() {
75 let make = |count| {
76 request(
77 Method::Stored,
78 (0..count)
79 .map(|i| entry(format!("file-{i}"), vec![]))
80 .collect(),
81 )
82 };
83 let mut input = make(MAX_INPUTS);
84 input.limits.entries = usize::MAX;
85 assert_zip32(&run(input), MAX_INPUTS);
86 let mut input = make(MAX_INPUTS + 1);
87 input.limits.entries = usize::MAX;
88 fail_before_output(input, Failure::Budget);
89}
90
91#[test]
92fn output_exact_and_one_byte_over_caller_budget() {
93 for method in [Method::Stored, Method::Deflate] {
94 let expected = run(small(method));
95 let mut input = small(method);
96 input.limits.output = expected.len() as u64;
97 assert_eq!(run(input), expected);
98 let mut input = small(method);
99 input.limits.output = expected.len() as u64 - 1;
100 assert_eq!(create(input, |_| Ok(())), Err(Failure::Budget));
101 }
102}
103
104#[test]
105fn stored_output_hard_limit_exact_and_one_over() {
106 assert_eq!(
107 run(request(Method::Stored, vec![entry("a", vec![])])).len(),
108 100
109 );
110 for excess in [0, 1] {
111 let mut input = request(
112 Method::Stored,
113 vec![entry("a", vec![0; MAX_BYTES as usize - 100 + excess])],
114 );
115 input.limits.output = u64::MAX;
116 let result = create(input, |_| Ok(()));
117 if excess == 0 {
118 assert_eq!(result.unwrap().len() as u64, MAX_BYTES);
119 } else {
120 assert_eq!(result, Err(Failure::Budget));
121 }
122 }
123}
124
125#[test]
126fn processed_is_input_plus_zip_without_rewrite_double_charge() {
127 for method in [Method::Stored, Method::Deflate] {
128 let expected = run(small(method));
129 let total = 3 + expected.len() as u64;
130 let mut input = small(method);
131 input.limits.processed = total;
132 let mut patch_seen = false;
133 let result = create(input, |point| {
134 if let Checkpoint::Write {
135 offset,
136 extent,
137 len,
138 } = point
139 {
140 patch_seen |= len > 0 && offset < extent;
141 }
142 Ok(())
143 });
144 assert_eq!(result.unwrap(), expected);
145 assert!(patch_seen);
146 let mut input = small(method);
147 input.limits.processed = total - 1;
148 assert_eq!(create(input, |_| Ok(())), Err(Failure::Budget));
149 }
150}
151
152#[test]
153fn processed_budget_covers_all_input_before_writer_creation() {
154 let mut input = small(Method::Stored);
155 input.limits.processed = 2;
156 fail_before_output(input, Failure::Budget);
157 let mut input = small(Method::Stored);
158 input.limits.processed = 3;
159 assert_eq!(create(input, |_| Ok(())), Err(Failure::Budget));
160}
161
162#[test]
163fn central_directory_overflow_is_checked_before_write_and_return() {
164 for method in [Method::Stored, Method::Deflate] {
165 let bytes = run(small(method));
166 let central = assert_zip32(&bytes, 1) as u64;
167 let mut input = small(method);
168 input.limits.output = central + 1;
169 let mut after_finish = false;
170 let mut rejected_write = false;
171 let mut returned = false;
172 let result = create(input, |point| {
173 match point {
174 Checkpoint::Finish => after_finish = true,
175 Checkpoint::Write { offset, len, .. } if after_finish && offset >= central => {
176 rejected_write |= offset + len as u64 > central + 1;
177 }
178 Checkpoint::Return => returned = true,
179 _ => (),
180 }
181 Ok(())
182 });
183 assert_eq!(result, Err(Failure::Budget));
184 assert!(after_finish && rejected_write && !returned);
185 }
186}
187
188#[test]
189fn zero_output_or_processed_budget_cannot_publish_empty_file() {
190 for output_zero in [true, false] {
191 let mut input = request(Method::Stored, vec![entry("a", vec![])]);
192 if output_zero {
193 input.limits.output = 0;
194 } else {
195 input.limits.processed = 0;
196 }
197 assert_eq!(create(input, |_| Ok(())), Err(Failure::Budget));
198 }
199}
200
201#[test]
202fn maximal_caller_values_do_not_overflow_small_creation() {
203 let mut input = small(Method::Stored);
204 input.limits = Limits {
205 input: u64::MAX,
206 output: u64::MAX,
207 entries: usize::MAX,
208 processed: u64::MAX,
209 };
210 assert_zip32(&run(input), 1);
211}