Skip to content

Commit

Permalink
Added rust-version in Cargo.toml (fixes issue conradkleinespel#73) an…
Browse files Browse the repository at this point in the history
…d updated string cleanup to handle ctrl+u (fixes issue conradkleinespel#70)
  • Loading branch information
LSchallot committed Sep 8, 2022
1 parent 80929ab commit 9fc8978
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ homepage = "https://github.com/conradkleinespel/rpassword"
repository = "https://github.com/conradkleinespel/rpassword"
documentation = "https://docs.rs/rpassword/"
readme = "README.md"
rust-version = "1.60"
keywords = ["read", "password", "security", "pass", "getpass"]
edition = "2018"

Expand Down
6 changes: 3 additions & 3 deletions src/rpassword/all.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rutil::fix_new_line::fix_new_line;
use crate::rutil::fix_line_issues::fix_line_issues;
use crate::rutil::print_tty::{print_tty, print_writer};
use crate::rutil::safe_string::SafeString;
use std::io::{BufRead, Write};
Expand Down Expand Up @@ -199,7 +199,7 @@ mod windows {

std::mem::drop(hidden_input);

super::fix_new_line(password.into_inner())
super::fix_line_issues(password.into_inner())
}
}

Expand All @@ -215,7 +215,7 @@ pub fn read_password_from_bufread(reader: &mut impl BufRead) -> std::io::Result<
let mut password = SafeString::new();
reader.read_line(&mut password)?;

fix_new_line(password.into_inner())
fix_line_issues(password.into_inner())
}

/// Prompts on the TTY and then reads a password from anything that implements BufRead
Expand Down
2 changes: 1 addition & 1 deletion src/rutil.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod atty;
pub mod fix_new_line;
pub mod fix_line_issues;
pub mod print_tty;
pub mod safe_string;
#[cfg(feature = "serde")]
Expand Down
27 changes: 27 additions & 0 deletions src/rutil/fix_line_issues.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Normalizes the return of `read_line()` in the context of a CLI application
pub fn fix_line_issues(mut line: String) -> std::io::Result<String> {
if !line.ends_with('\n') {
return Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"unexpected end of file",
));
}

// Remove the \n from the line.
line.pop();

// Remove the \r from the line if present
if line.ends_with('\r') {
line.pop();
}

if line.contains("") {
let vec: Vec<&str> = line.split("").collect();
line = match vec.last() {
None => String::new(),
Some(i) => i.to_string()
}
}

Ok(line)
}
13 changes: 13 additions & 0 deletions tests/no-terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ fn mock_input_lf() -> Cursor<&'static [u8]> {
Cursor::new(&b"A mocked response.\nAnother mocked response.\n"[..])
}

fn mock_input_ctrl_u() -> Cursor<&'static [u8]> {
Cursor::new(&b"A mocked response.Another mocked response.\n"[..])
}

#[test]
fn can_read_from_redirected_input_many_times() {
close_stdin();
Expand All @@ -55,3 +59,12 @@ fn can_read_from_redirected_input_many_times() {
let response = crate::read_password_from_bufread(&mut reader_lf).unwrap();
assert_eq!(response, "Another mocked response.");
}

#[test]
fn can_read_from_input_ctrl_u() {
close_stdin();

let mut reader_ctrl_u = crate::mock_input_ctrl_u();
let response = crate::read_password_from_bufread(&mut reader_ctrl_u).unwrap();
assert_eq!(response, "Another mocked response.");
}

0 comments on commit 9fc8978

Please sign in to comment.