| 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 |
#[cfg(unix)] |
| 7 |
use std::fs::File; |
| 8 |
#[cfg(unix)] |
| 9 |
use std::fs::{Permissions, set_permissions}; |
| 10 |
#[cfg(unix)] |
| 11 |
use std::io::{Read as _, Write as _}; |
| 12 |
#[cfg(target_os = "linux")] |
| 13 |
use std::os::unix::ffi::OsStrExt; |
| 14 |
#[cfg(unix)] |
| 15 |
use std::os::unix::fs::PermissionsExt; |
| 16 |
#[cfg(unix)] |
| 17 |
use uutests::util::pty_path; |
| 18 |
#[cfg(unix)] |
| 19 |
use uutests::{at_and_ucmd, new_ucmd}; |
| 20 |
|
| 21 |
#[cfg(unix)] |
| 22 |
fn run_more_with_pty( |
| 23 |
args: &[&str], |
| 24 |
file: &str, |
| 25 |
content: &str, |
| 26 |
) -> (uutests::util::UChild, File, String) { |
| 27 |
let (path, mut controller, _replica) = pty_path(); |
| 28 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 29 |
at.write(file, content); |
| 30 |
|
| 31 |
let mut child = ucmd |
| 32 |
.set_stdin(File::open(&path).unwrap()) |
| 33 |
.set_stdout(File::create(&path).unwrap()) |
| 34 |
.args(args) |
| 35 |
.arg(file) |
| 36 |
.run_no_wait(); |
| 37 |
|
| 38 |
child.delay(200); |
| 39 |
let mut output = vec![0u8; 1024]; |
| 40 |
let n = controller.read(&mut output).unwrap(); |
| 41 |
let output_str = String::from_utf8_lossy(&output[..n]).to_string(); |
| 42 |
|
| 43 |
(child, controller, output_str) |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(unix)] |
| 47 |
fn quit_more(controller: &mut File, mut child: uutests::util::UChild) { |
| 48 |
controller.write_all(b"q").unwrap(); |
| 49 |
child.delay(50); |
| 50 |
} |
| 51 |
|
| 52 |
#[cfg(unix)] |
| 53 |
#[test] |
| 54 |
fn test_no_arg() { |
| 55 |
let (path, _controller, _replica) = pty_path(); |
| 56 |
new_ucmd!() |
| 57 |
.set_stdin(File::open(&path).unwrap()) |
| 58 |
.set_stdout(File::create(&path).unwrap()) |
| 59 |
.fails() |
| 60 |
.stderr_contains("more: bad usage"); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
#[cfg(unix)] |
| 65 |
fn test_valid_arg() { |
| 66 |
let args_list: Vec<&[&str]> = vec![ |
| 67 |
&["-c"], |
| 68 |
&["--clean-print"], |
| 69 |
&["-p"], |
| 70 |
&["--print-over"], |
| 71 |
&["-s"], |
| 72 |
&["--squeeze"], |
| 73 |
&["-u"], |
| 74 |
&["--plain"], |
| 75 |
&["-n", "10"], |
| 76 |
&["--lines", "0"], |
| 77 |
&["--number", "0"], |
| 78 |
&["-F", "10"], |
| 79 |
&["--from-line", "0"], |
| 80 |
&["-P", "something"], |
| 81 |
&["--pattern", "-1"], |
| 82 |
&["-f"], |
| 83 |
&["--logical"], |
| 84 |
&["-l"], |
| 85 |
&["--no-pause"], |
| 86 |
]; |
| 87 |
for args in args_list { |
| 88 |
test_alive(args); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
#[cfg(unix)] |
| 93 |
fn test_alive(args: &[&str]) { |
| 94 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 95 |
let (path, mut controller, _replica) = pty_path(); |
| 96 |
|
| 97 |
let content = "test content"; |
| 98 |
let file = "test_file"; |
| 99 |
at.write(file, content); |
| 100 |
|
| 101 |
let mut child = ucmd |
| 102 |
.set_stdin(File::open(&path).unwrap()) |
| 103 |
.set_stdout(File::create(&path).unwrap()) |
| 104 |
.args(args) |
| 105 |
.arg(file) |
| 106 |
.run_no_wait(); |
| 107 |
|
| 108 |
// wait for more to start and display the file |
| 109 |
child.delay(100); |
| 110 |
|
| 111 |
assert!(child.is_alive(), "Command should still be alive"); |
| 112 |
|
| 113 |
// cleanup |
| 114 |
controller.write_all(b"q").unwrap(); |
| 115 |
child.delay(50); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
#[cfg(unix)] |
| 120 |
fn test_invalid_arg() { |
| 121 |
let (path, _controller, _replica) = pty_path(); |
| 122 |
new_ucmd!() |
| 123 |
.set_stdin(File::open(&path).unwrap()) |
| 124 |
.set_stdout(File::create(&path).unwrap()) |
| 125 |
.arg("--invalid") |
| 126 |
.fails(); |
| 127 |
|
| 128 |
let (path, _controller, _replica) = pty_path(); |
| 129 |
new_ucmd!() |
| 130 |
.set_stdin(File::open(&path).unwrap()) |
| 131 |
.set_stdout(File::create(&path).unwrap()) |
| 132 |
.arg("--lines") |
| 133 |
.arg("-10") |
| 134 |
.fails(); |
| 135 |
|
| 136 |
let (path, _controller, _replica) = pty_path(); |
| 137 |
new_ucmd!() |
| 138 |
.set_stdin(File::open(&path).unwrap()) |
| 139 |
.set_stdout(File::create(&path).unwrap()) |
| 140 |
.arg("--from-line") |
| 141 |
.arg("-10") |
| 142 |
.fails(); |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
#[cfg(unix)] |
| 147 |
fn test_file_arg() { |
| 148 |
// Directory as argument |
| 149 |
let (path, _controller, _replica) = pty_path(); |
| 150 |
new_ucmd!() |
| 151 |
.set_stdin(File::open(&path).unwrap()) |
| 152 |
.set_stdout(File::create(&path).unwrap()) |
| 153 |
.arg(".") |
| 154 |
.succeeds() |
| 155 |
.stderr_contains("'.' is a directory."); |
| 156 |
|
| 157 |
// Single argument errors |
| 158 |
let (path, _controller, _replica) = pty_path(); |
| 159 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 160 |
at.mkdir_all("folder"); |
| 161 |
ucmd.set_stdin(File::open(&path).unwrap()) |
| 162 |
.set_stdout(File::create(&path).unwrap()) |
| 163 |
.arg("folder") |
| 164 |
.succeeds() |
| 165 |
.stderr_contains("is a directory"); |
| 166 |
|
| 167 |
let (path, _controller, _replica) = pty_path(); |
| 168 |
new_ucmd!() |
| 169 |
.set_stdin(File::open(&path).unwrap()) |
| 170 |
.set_stdout(File::create(&path).unwrap()) |
| 171 |
.arg("nonexistent_file") |
| 172 |
.succeeds() |
| 173 |
.stderr_contains("No such file or directory"); |
| 174 |
|
| 175 |
// Multiple nonexistent files |
| 176 |
let (path, _controller, _replica) = pty_path(); |
| 177 |
new_ucmd!() |
| 178 |
.set_stdin(File::open(&path).unwrap()) |
| 179 |
.set_stdout(File::create(&path).unwrap()) |
| 180 |
.arg("file2") |
| 181 |
.arg("file3") |
| 182 |
.succeeds() |
| 183 |
.stderr_contains("file2") |
| 184 |
.stderr_contains("file3"); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
#[cfg(unix)] |
| 189 |
fn test_invalid_file_perms() { |
| 190 |
let (path, _controller, _replica) = pty_path(); |
| 191 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 192 |
let permissions = Permissions::from_mode(0o244); |
| 193 |
at.make_file("invalid-perms.txt"); |
| 194 |
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); |
| 195 |
ucmd.set_stdin(File::open(&path).unwrap()) |
| 196 |
.set_stdout(File::create(&path).unwrap()) |
| 197 |
.arg("invalid-perms.txt") |
| 198 |
.succeeds() |
| 199 |
.stderr_contains("permission denied"); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
#[cfg(target_os = "linux")] |
| 204 |
fn test_more_non_utf8_paths() { |
| 205 |
let (path, _controller, _replica) = pty_path(); |
| 206 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 207 |
let file_name = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt"); |
| 208 |
// Create test file with normal name first |
| 209 |
at.write( |
| 210 |
&file_name.to_string_lossy(), |
| 211 |
"test content for non-UTF-8 file", |
| 212 |
); |
| 213 |
|
| 214 |
// Test that more can handle non-UTF-8 filenames without crashing |
| 215 |
ucmd.set_stdin(File::open(&path).unwrap()) |
| 216 |
.set_stdout(File::create(&path).unwrap()) |
| 217 |
.arg(file_name) |
| 218 |
.succeeds(); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
#[cfg(unix)] |
| 223 |
fn test_basic_display() { |
| 224 |
let (child, mut controller, output) = |
| 225 |
run_more_with_pty(&[], "test.txt", "line1\nline2\nline3\n"); |
| 226 |
assert!(output.contains("line1")); |
| 227 |
quit_more(&mut controller, child); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
#[cfg(unix)] |
| 232 |
fn test_squeeze_blank_lines() { |
| 233 |
let (child, mut controller, output) = |
| 234 |
run_more_with_pty(&["-s"], "test.txt", "line1\n\n\n\nline2\n"); |
| 235 |
assert!(output.contains("line1")); |
| 236 |
quit_more(&mut controller, child); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
#[cfg(unix)] |
| 241 |
fn test_pattern_search() { |
| 242 |
let (child, mut controller, output) = run_more_with_pty( |
| 243 |
&["-P", "target"], |
| 244 |
"test.txt", |
| 245 |
"foo\nbar\nbaz\ntarget\nend\n", |
| 246 |
); |
| 247 |
assert!(output.contains("target")); |
| 248 |
assert!(!output.contains("foo")); |
| 249 |
quit_more(&mut controller, child); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
#[cfg(unix)] |
| 254 |
fn test_from_line_option() { |
| 255 |
let (child, mut controller, output) = |
| 256 |
run_more_with_pty(&["-F", "2"], "test.txt", "line1\nline2\nline3\nline4\n"); |
| 257 |
assert!(output.contains("line2")); |
| 258 |
assert!(!output.contains("line1")); |
| 259 |
quit_more(&mut controller, child); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
#[cfg(unix)] |
| 264 |
fn test_lines_option_max_u16_no_overflow() { |
| 265 |
// Regression test for https://github.com/uutils/coreutils/pull/12970 |
| 266 |
// When -n is u16::MAX (65535), adding 1 for the banner line used to |
| 267 |
// overflow. Verify that more starts and displays content without crashing. |
| 268 |
let (child, mut controller, output) = |
| 269 |
run_more_with_pty(&["-n", "65535"], "test.txt", "line1\nline2\nline3\n"); |
| 270 |
assert!(output.contains("line1")); |
| 271 |
quit_more(&mut controller, child); |
| 272 |
} |