From a2a9f1e331e07aa6a47ca2d75c2166dabb96ab22 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Thu, 21 Nov 2019 11:02:04 -0700 Subject: [PATCH] Truncate new keypair files (#7078) automerge --- sdk/src/signature.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sdk/src/signature.rs b/sdk/src/signature.rs index 0617fb2372..80edf27cda 100644 --- a/sdk/src/signature.rs +++ b/sdk/src/signature.rs @@ -168,6 +168,7 @@ pub fn write_keypair_file( } } .write(true) + .truncate(true) .create(true) .open(outfile)?; @@ -235,10 +236,28 @@ mod tests { #[test] fn test_write_keypair_file_overwrite_ok() { let outfile = tmp_file_path("test_write_keypair_file_overwrite_ok.json"); + write_keypair_file(&Keypair::new(), &outfile).unwrap(); write_keypair_file(&Keypair::new(), &outfile).unwrap(); } + #[test] + fn test_write_keypair_file_truncate() { + let outfile = tmp_file_path("test_write_keypair_file_truncate.json"); + + write_keypair_file(&Keypair::new(), &outfile).unwrap(); + read_keypair_file(&outfile).unwrap(); + + // Ensure outfile is truncated + { + let mut f = File::create(&outfile).unwrap(); + f.write_all(String::from_utf8([b'a'; 2048].to_vec()).unwrap().as_bytes()) + .unwrap(); + } + write_keypair_file(&Keypair::new(), &outfile).unwrap(); + read_keypair_file(&outfile).unwrap(); + } + #[test] fn test_keypair_from_seed() { let good_seed = vec![0; 32];