Fixing parsing passwords from file (#3367)
* fixing parsing passwords as inputs * Fixing typo in test Former-commit-id: b5e1b7fefc49b415e3d217f8e17bde32ee3ad51c
This commit is contained in:
parent
603c52f82d
commit
fb443dbe16
@ -15,9 +15,8 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use std::{io, env};
|
use std::{io, env};
|
||||||
use std::io::{Write, Read, BufReader, BufRead};
|
use std::io::{Write, BufReader, BufRead};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::path::Path;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use util::{clean_0x, U256, Uint, Address, path, CompactionProfile};
|
use util::{clean_0x, U256, Uint, Address, path, CompactionProfile};
|
||||||
use util::journaldb::Algorithm;
|
use util::journaldb::Algorithm;
|
||||||
@ -299,13 +298,11 @@ pub fn password_prompt() -> Result<String, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read a password from password file.
|
/// Read a password from password file.
|
||||||
pub fn password_from_file<P>(path: P) -> Result<String, String> where P: AsRef<Path> {
|
pub fn password_from_file(path: String) -> Result<String, String> {
|
||||||
let mut file = try!(File::open(path).map_err(|_| "Unable to open password file."));
|
let passwords = try!(passwords_from_files(vec![path]));
|
||||||
let mut file_content = String::new();
|
// use only first password from the file
|
||||||
match file.read_to_string(&mut file_content) {
|
passwords.get(0).map(String::to_owned)
|
||||||
Ok(_) => Ok(file_content.trim().into()),
|
.ok_or_else(|| "Password file seems to be empty.".to_owned())
|
||||||
Err(_) => Err("Unable to read password file.".into()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads passwords from files. Treats each line as a separate password.
|
/// Reads passwords from files. Treats each line as a separate password.
|
||||||
@ -314,7 +311,8 @@ pub fn passwords_from_files(files: Vec<String>) -> Result<Vec<String>, String> {
|
|||||||
let file = try!(File::open(filename).map_err(|_| format!("{} Unable to read password file. Ensure it exists and permissions are correct.", filename)));
|
let file = try!(File::open(filename).map_err(|_| format!("{} Unable to read password file. Ensure it exists and permissions are correct.", filename)));
|
||||||
let reader = BufReader::new(&file);
|
let reader = BufReader::new(&file);
|
||||||
let lines = reader.lines()
|
let lines = reader.lines()
|
||||||
.map(|l| l.unwrap())
|
.filter_map(|l| l.ok())
|
||||||
|
.map(|pwd| pwd.trim().to_owned())
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
Ok(lines)
|
Ok(lines)
|
||||||
}).collect::<Result<Vec<Vec<String>>, String>>();
|
}).collect::<Result<Vec<Vec<String>>, String>>();
|
||||||
@ -418,7 +416,20 @@ mod tests {
|
|||||||
let path = RandomTempPath::new();
|
let path = RandomTempPath::new();
|
||||||
let mut file = File::create(path.as_path()).unwrap();
|
let mut file = File::create(path.as_path()).unwrap();
|
||||||
file.write_all(b"a bc ").unwrap();
|
file.write_all(b"a bc ").unwrap();
|
||||||
assert_eq!(password_from_file(path).unwrap().as_bytes(), b"a bc");
|
assert_eq!(password_from_file(path.as_str().into()).unwrap().as_bytes(), b"a bc");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_password_multiline() {
|
||||||
|
let path = RandomTempPath::new();
|
||||||
|
let mut file = File::create(path.as_path()).unwrap();
|
||||||
|
file.write_all(br#" password with trailing whitespace
|
||||||
|
those passwords should be
|
||||||
|
ignored
|
||||||
|
but the first password is trimmed
|
||||||
|
|
||||||
|
"#).unwrap();
|
||||||
|
assert_eq!(&password_from_file(path.as_str().into()).unwrap(), "password with trailing whitespace");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user