|
2 | 2 | // |
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | | -// spell-checker:ignore parenb parmrk ixany iuclc onlcr icanon noflsh econl igpar ispeed ospeed |
| 5 | +// spell-checker:ignore parenb parmrk ixany iuclc onlcr icanon noflsh econl igpar ispeed ospeed NCCS nonhex gstty |
6 | 6 |
|
7 | | -use uutests::new_ucmd; |
8 | | -use uutests::util::pty_path; |
| 7 | +use uutests::util::{expected_result, pty_path}; |
| 8 | +use uutests::{at_and_ts, new_ucmd, unwrap_or_return}; |
| 9 | + |
| 10 | +/// Normalize stderr by replacing the full binary path with just the utility name |
| 11 | +/// This allows comparison between GNU (which shows "stty" or "gstty") and ours (which shows full path) |
| 12 | +fn normalize_stderr(stderr: &str) -> String { |
| 13 | + // Replace patterns like "Try 'gstty --help'" or "Try '/path/to/stty --help'" with "Try 'stty --help'" |
| 14 | + let re = regex::Regex::new(r"Try '[^']*(?:g)?stty --help'").unwrap(); |
| 15 | + re.replace_all(stderr, "Try 'stty --help'").to_string() |
| 16 | +} |
9 | 17 |
|
10 | 18 | #[test] |
11 | 19 | fn test_invalid_arg() { |
@@ -406,3 +414,172 @@ fn test_columns_env_wrapping() { |
406 | 414 | ); |
407 | 415 | } |
408 | 416 | } |
| 417 | + |
| 418 | +// Tests for saved state parsing and restoration |
| 419 | +#[test] |
| 420 | +#[cfg(unix)] |
| 421 | +fn test_save_and_restore() { |
| 422 | + let (path, _controller, _replica) = pty_path(); |
| 423 | + let saved = new_ucmd!() |
| 424 | + .args(&["--save", "--file", &path]) |
| 425 | + .succeeds() |
| 426 | + .stdout_move_str(); |
| 427 | + |
| 428 | + let saved = saved.trim(); |
| 429 | + assert!(saved.contains(':')); |
| 430 | + |
| 431 | + new_ucmd!().args(&["--file", &path, saved]).succeeds(); |
| 432 | +} |
| 433 | + |
| 434 | +#[test] |
| 435 | +#[cfg(unix)] |
| 436 | +fn test_save_with_g_flag() { |
| 437 | + let (path, _controller, _replica) = pty_path(); |
| 438 | + let saved = new_ucmd!() |
| 439 | + .args(&["-g", "--file", &path]) |
| 440 | + .succeeds() |
| 441 | + .stdout_move_str(); |
| 442 | + |
| 443 | + let saved = saved.trim(); |
| 444 | + assert!(saved.contains(':')); |
| 445 | + |
| 446 | + new_ucmd!().args(&["--file", &path, saved]).succeeds(); |
| 447 | +} |
| 448 | + |
| 449 | +#[test] |
| 450 | +#[cfg(unix)] |
| 451 | +fn test_save_restore_after_change() { |
| 452 | + let (path, _controller, _replica) = pty_path(); |
| 453 | + let saved = new_ucmd!() |
| 454 | + .args(&["--save", "--file", &path]) |
| 455 | + .succeeds() |
| 456 | + .stdout_move_str(); |
| 457 | + |
| 458 | + let saved = saved.trim(); |
| 459 | + |
| 460 | + new_ucmd!() |
| 461 | + .args(&["--file", &path, "intr", "^A"]) |
| 462 | + .succeeds(); |
| 463 | + |
| 464 | + new_ucmd!().args(&["--file", &path, saved]).succeeds(); |
| 465 | + |
| 466 | + new_ucmd!() |
| 467 | + .args(&["--file", &path]) |
| 468 | + .succeeds() |
| 469 | + .stdout_str_check(|s| !s.contains("intr = ^A")); |
| 470 | +} |
| 471 | + |
| 472 | +// These tests both validate what we expect each input to return and their error codes |
| 473 | +// and also use the GNU coreutils results to validate our results match expectations |
| 474 | +#[test] |
| 475 | +#[cfg(unix)] |
| 476 | +fn test_saved_state_valid_formats() { |
| 477 | + let (path, _controller, _replica) = pty_path(); |
| 478 | + let (_at, ts) = at_and_ts!(); |
| 479 | + |
| 480 | + // Generate valid saved state from the actual terminal |
| 481 | + let saved = unwrap_or_return!(expected_result(&ts, &["-g", "--file", &path])).stdout_move_str(); |
| 482 | + let saved = saved.trim(); |
| 483 | + |
| 484 | + let result = ts.ucmd().args(&["--file", &path, saved]).run(); |
| 485 | + |
| 486 | + result.success().no_stderr(); |
| 487 | + |
| 488 | + let exp_result = unwrap_or_return!(expected_result(&ts, &["--file", &path, saved])); |
| 489 | + let normalized_stderr = normalize_stderr(result.stderr_str()); |
| 490 | + result |
| 491 | + .stdout_is(exp_result.stdout_str()) |
| 492 | + .code_is(exp_result.code()); |
| 493 | + assert_eq!(normalized_stderr, exp_result.stderr_str()); |
| 494 | +} |
| 495 | + |
| 496 | +#[test] |
| 497 | +#[cfg(unix)] |
| 498 | +fn test_saved_state_invalid_formats() { |
| 499 | + let (path, _controller, _replica) = pty_path(); |
| 500 | + let (_at, ts) = at_and_ts!(); |
| 501 | + |
| 502 | + let num_cc = nix::libc::NCCS; |
| 503 | + |
| 504 | + // Build test strings with platform-specific counts |
| 505 | + let cc_zeros = vec!["0"; num_cc].join(":"); |
| 506 | + let cc_with_invalid = if num_cc > 0 { |
| 507 | + let mut parts = vec!["1c"; num_cc]; |
| 508 | + parts[0] = "100"; // First control char > 255 |
| 509 | + parts.join(":") |
| 510 | + } else { |
| 511 | + String::new() |
| 512 | + }; |
| 513 | + let cc_with_space = if num_cc > 0 { |
| 514 | + let mut parts = vec!["1c"; num_cc]; |
| 515 | + parts[0] = "1c "; // Space in hex |
| 516 | + parts.join(":") |
| 517 | + } else { |
| 518 | + String::new() |
| 519 | + }; |
| 520 | + let cc_with_nonhex = if num_cc > 0 { |
| 521 | + let mut parts = vec!["1c"; num_cc]; |
| 522 | + parts[0] = "xyz"; // Non-hex |
| 523 | + parts.join(":") |
| 524 | + } else { |
| 525 | + String::new() |
| 526 | + }; |
| 527 | + let cc_with_empty = if num_cc > 0 { |
| 528 | + let mut parts = vec!["1c"; num_cc]; |
| 529 | + parts[0] = ""; // Empty |
| 530 | + parts.join(":") |
| 531 | + } else { |
| 532 | + String::new() |
| 533 | + }; |
| 534 | + |
| 535 | + // Cannot test single value since it would be interpreted as baud rate |
| 536 | + let invalid_states = vec![ |
| 537 | + "500:5:4bf".to_string(), // fewer than expected parts |
| 538 | + "500:5:4bf:8a3b".to_string(), // only 4 parts |
| 539 | + format!("500:5:{}:8a3b:{}", cc_zeros, "extra"), // too many parts |
| 540 | + format!("500::4bf:8a3b:{}", cc_zeros), // empty hex value in flags |
| 541 | + format!("500:5:4bf:8a3b:{}", cc_with_empty), // empty hex value in cc |
| 542 | + format!("500:5:4bf:8a3b:{}", cc_with_nonhex), // non-hex characters |
| 543 | + format!("500:5:4bf:8a3b:{}", cc_with_space), // space in hex value |
| 544 | + format!("500:5:4bf:8a3b:{}", cc_with_invalid), // control char > 255 |
| 545 | + ]; |
| 546 | + |
| 547 | + for state in &invalid_states { |
| 548 | + let result = ts.ucmd().args(&["--file", &path, state]).run(); |
| 549 | + |
| 550 | + result.failure().stderr_contains("invalid argument"); |
| 551 | + |
| 552 | + let exp_result = unwrap_or_return!(expected_result(&ts, &["--file", &path, state])); |
| 553 | + let normalized_stderr = normalize_stderr(result.stderr_str()); |
| 554 | + let exp_normalized_stderr = normalize_stderr(exp_result.stderr_str()); |
| 555 | + result |
| 556 | + .stdout_is(exp_result.stdout_str()) |
| 557 | + .code_is(exp_result.code()); |
| 558 | + assert_eq!(normalized_stderr, exp_normalized_stderr); |
| 559 | + } |
| 560 | +} |
| 561 | + |
| 562 | +#[test] |
| 563 | +#[cfg(unix)] |
| 564 | +#[ignore = "Fails because the implementation of print state is not correctly printing flags on certain platforms"] |
| 565 | +fn test_saved_state_with_control_chars() { |
| 566 | + let (path, _controller, _replica) = pty_path(); |
| 567 | + let (_at, ts) = at_and_ts!(); |
| 568 | + |
| 569 | + // Build a valid saved state with platform-specific number of control characters |
| 570 | + let num_cc = nix::libc::NCCS; |
| 571 | + let cc_values: Vec<String> = (1..=num_cc).map(|_| format!("{:x}", 0)).collect(); |
| 572 | + let saved_state = format!("500:5:4bf:8a3b:{}", cc_values.join(":")); |
| 573 | + |
| 574 | + ts.ucmd().args(&["--file", &path, &saved_state]).succeeds(); |
| 575 | + |
| 576 | + let result = ts.ucmd().args(&["-g", "--file", &path]).run(); |
| 577 | + |
| 578 | + result.success().stdout_contains(":"); |
| 579 | + |
| 580 | + let exp_result = unwrap_or_return!(expected_result(&ts, &["-g", "--file", &path])); |
| 581 | + result |
| 582 | + .stdout_is(exp_result.stdout_str()) |
| 583 | + .stderr_is(exp_result.stderr_str()) |
| 584 | + .code_is(exp_result.code()); |
| 585 | +} |
0 commit comments