Debian Codegraph @

tests/by-util/test_base64.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore unpadded, QUJD
7
8 #[cfg(unix)]
9 use uutests::at_and_ucmd;
10 use uutests::new_ucmd;
11
12 #[test]
13 fn test_version() {
14 new_ucmd!()
15 .arg("--version")
16 .succeeds()
17 .no_stderr()
18 .stdout_is(format!("base64 {}\n", uucore::crate_version!()));
19 }
20
21 #[test]
22 #[cfg(target_os = "linux")]
23 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
24 fn test_base64_non_utf8_paths() {
25 use std::os::unix::ffi::OsStringExt;
26 let (at, mut ucmd) = at_and_ucmd!();
27
28 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
29 std::fs::write(at.plus(&filename), b"hello world").unwrap();
30
31 ucmd.arg(&filename)
32 .succeeds()
33 .stdout_is("aGVsbG8gd29ybGQ=\n");
34 }
35
36 #[test]
37 fn test_encode() {
38 let input = "hello, world!";
39 new_ucmd!()
40 .pipe_in(input)
41 .succeeds()
42 .stdout_only("aGVsbG8sIHdvcmxkIQ==\n"); // spell-checker:disable-line
43
44 // Using '-' as our file
45 new_ucmd!()
46 .arg("-")
47 .pipe_in(input)
48 .succeeds()
49 .stdout_only("aGVsbG8sIHdvcmxkIQ==\n"); // spell-checker:disable-line
50 }
51
52 #[test]
53 fn test_encode_repeat_flags_later_wrap_10() {
54 let input = "hello, world!";
55 new_ucmd!()
56 .args(&["-ii", "-w15", "-w10"])
57 .pipe_in(input)
58 .succeeds()
59 .stdout_only("aGVsbG8sIH\ndvcmxkIQ==\n"); // spell-checker:disable-line
60 }
61
62 #[test]
63 fn test_encode_repeat_flags_later_wrap_15() {
64 let input = "hello, world!";
65 new_ucmd!()
66 .args(&["-ii", "-w10", "-w15"])
67 .pipe_in(input)
68 .succeeds()
69 .stdout_only("aGVsbG8sIHdvcmx\nkIQ==\n"); // spell-checker:disable-line
70 }
71
72 #[test]
73 fn test_decode_short() {
74 let input = "aQ";
75 new_ucmd!()
76 .args(&["--decode"])
77 .pipe_in(input)
78 .succeeds()
79 .stdout_only("i");
80 }
81
82 #[test]
83 fn test_multi_lines() {
84 let input = ["aQ\n\n\n", "a\nQ==\n\n\n"];
85 for i in input {
86 new_ucmd!()
87 .args(&["--decode"])
88 .pipe_in(i)
89 .succeeds()
90 .stdout_only("i");
91 }
92 }
93
94 #[test]
95 fn test_base64_encode_file() {
96 new_ucmd!()
97 .arg("input-simple.txt")
98 .succeeds()
99 .stdout_only("SGVsbG8sIFdvcmxkIQo=\n"); // spell-checker:disable-line
100 }
101
102 #[test]
103 fn test_decode() {
104 for decode_param in ["-d", "--decode", "--dec", "-D"] {
105 let input = "aGVsbG8sIHdvcmxkIQ=="; // spell-checker:disable-line
106 new_ucmd!()
107 .arg(decode_param)
108 .pipe_in(input)
109 .succeeds()
110 .stdout_only("hello, world!");
111 }
112 }
113
114 #[test]
115 fn test_decode_repeat_flags() {
116 let input = "aGVsbG8sIHdvcmxkIQ==\n"; // spell-checker:disable-line
117 new_ucmd!()
118 .args(&["-didiw80", "--wrap=17", "--wrap", "8"]) // spell-checker:disable-line
119 .pipe_in(input)
120 .succeeds()
121 .stdout_only("hello, world!");
122 }
123
124 #[test]
125 fn test_decode_padded_block_followed_by_unpadded_tail() {
126 new_ucmd!()
127 .arg("--decode")
128 .pipe_in("MTIzNA==MTIzNA")
129 .succeeds()
130 .stdout_only("12341234");
131 }
132
133 #[test]
134 fn test_decode_padded_block_followed_by_aligned_tail() {
135 new_ucmd!()
136 .arg("--decode")
137 .pipe_in("MTIzNA==QUJD")
138 .succeeds()
139 .stdout_only("1234ABC");
140 }
141
142 #[test]
143 fn test_decode_unpadded_stream_without_equals() {
144 new_ucmd!()
145 .arg("--decode")
146 .pipe_in("MTIzNA")
147 .succeeds()
148 .stdout_only("1234");
149 }
150
151 #[test]
152 fn test_garbage() {
153 let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line
154 new_ucmd!()
155 .arg("-d")
156 .pipe_in(input)
157 .fails()
158 .stderr_only("base64: error: invalid input\n");
159 }
160
161 #[test]
162 fn test_ignore_garbage() {
163 for ignore_garbage_param in ["-i", "--ignore-garbage", "--ig"] {
164 let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line
165 new_ucmd!()
166 .arg("-d")
167 .arg(ignore_garbage_param)
168 .pipe_in(input)
169 .succeeds()
170 .stdout_only("hello, world!");
171 }
172 }
173
174 #[test]
175 fn test_wrap() {
176 for wrap_param in ["-w", "--wrap", "--wr"] {
177 let input = "The quick brown fox jumps over the lazy dog.";
178 new_ucmd!()
179 .arg(wrap_param)
180 .arg("20")
181 .pipe_in(input)
182 .succeeds()
183 // spell-checker:disable-next-line
184 .stdout_only("VGhlIHF1aWNrIGJyb3du\nIGZveCBqdW1wcyBvdmVy\nIHRoZSBsYXp5IGRvZy4=\n");
185 }
186 let input = "hello, world";
187 new_ucmd!()
188 .args(&["--wrap", "0"])
189 .pipe_in(input)
190 .succeeds()
191 .stdout_only("aGVsbG8sIHdvcmxk"); // spell-checker:disable-line
192 new_ucmd!()
193 .args(&["--wrap", "30"])
194 .pipe_in(input)
195 .succeeds()
196 .stdout_only("aGVsbG8sIHdvcmxk\n"); // spell-checker:disable-line
197 }
198
199 #[test]
200 fn test_wrap_no_arg() {
201 for wrap_param in ["-w", "--wrap"] {
202 new_ucmd!()
203 .arg(wrap_param)
204 .fails()
205 .stderr_contains("error: a value is required for '--wrap <COLS>' but none was supplied")
206 .stderr_contains("For more information, try '--help'.")
207 .no_stdout();
208 }
209 }
210
211 #[test]
212 fn test_wrap_bad_arg() {
213 for wrap_param in ["-w", "--wrap"] {
214 new_ucmd!()
215 .arg(wrap_param)
216 .arg("b")
217 .fails()
218 .stderr_only("base64: invalid wrap size: 'b'\n");
219 }
220 }
221
222 #[test]
223 fn test_base64_extra_operand() {
224 // Expect a failure when multiple files are specified.
225 new_ucmd!()
226 .arg("a.txt")
227 .arg("b.txt")
228 .fails()
229 .usage_error("extra operand 'b.txt'");
230 }
231
232 #[test]
233 fn test_base64_file_not_found() {
234 new_ucmd!()
235 .arg("a.txt")
236 .fails()
237 .stderr_only("base64: a.txt: No such file or directory\n");
238 }
239
240 #[test]
241 fn test_no_repeated_trailing_newline() {
242 new_ucmd!()
243 .args(&["--wrap", "10", "--", "-"])
244 .pipe_in("The quick brown fox jumps over the lazy dog.")
245 .succeeds()
246 .stdout_only(
247 // cSpell:disable
248 "\
249 VGhlIHF1aW
250 NrIGJyb3du
251 IGZveCBqdW
252 1wcyBvdmVy
253 IHRoZSBsYX
254 p5IGRvZy4=
255 ",
256 // cSpell:enable
257 );
258 }
259
260 #[test]
261 fn test_wrap_default() {
262 const PIPE_IN: &str = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.";
263
264 new_ucmd!()
265 .args(&["--", "-"])
266 .pipe_in(PIPE_IN)
267 .succeeds()
268 .stdout_only(
269 // cSpell:disable
270 "\
271 VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gVGhlIHF1aWNrIGJy
272 b3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gVGhlIHF1aWNrIGJyb3duIGZveCBqdW1w
273 cyBvdmVyIHRoZSBsYXp5IGRvZy4=
274 ",
275 // cSpell:enable
276 );
277 }
278
279 #[test]
280 #[cfg(all(target_os = "linux", not(target_env = "musl")))]
281 #[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")]
282 fn test_read_error() {
283 new_ucmd!()
284 .arg("/proc/self/mem")
285 .fails()
286 .stderr_is("base64: read error: Input/output error\n");
287 }
288
289 #[test]
290 #[cfg(unix)]
291 fn test_base64_file_with_trailing_slash() {
292 let (at, mut ucmd) = at_and_ucmd!();
293 at.write("a", "b");
294
295 ucmd.arg("a/")
296 .fails()
297 .stderr_only("base64: a/: Not a directory\n");
298 }