Use expect instead of check in tests for accurate testing

This commit is contained in:
Sosthène Guédon
2022-12-16 11:34:46 +01:00
parent 7dd8b75441
commit 630482dd1b
2 changed files with 25 additions and 29 deletions
+13 -13
View File
@@ -15,10 +15,10 @@ use expectrl::{spawn, Eof, WaitStatus};
fn list() {
with_vsc(|| {
let mut p = spawn("piv-tool -n").unwrap();
p.check("Using reader with a card: Virtual PCD 00 00")
p.expect("Using reader with a card: Virtual PCD 00 00")
.unwrap();
p.check("Personal Identity Verification Card").unwrap();
p.check(Eof).unwrap();
p.expect("Personal Identity Verification Card").unwrap();
p.expect(Eof).unwrap();
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));
});
}
@@ -31,10 +31,10 @@ fn admin_mutual() {
.env("PIV_EXT_AUTH_KEY", "tests/default_admin_key")
.args(["-A", "M:9B:03"]);
let mut p = expectrl::session::Session::spawn(command).unwrap();
p.check("Using reader with a card: Virtual PCD 00 00")
p.expect("Using reader with a card: Virtual PCD 00 00")
.unwrap();
p.check("Personal Identity Verification Card").unwrap();
p.check(Eof).unwrap();
// p.expect("Personal Identity Verification Card").unwrap();
p.expect(Eof).unwrap();
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));
});
}
@@ -49,10 +49,10 @@ fn admin_card() {
.env("PIV_EXT_AUTH_KEY", "tests/default_admin_key")
.args(["-A", "A:9B:03"]);
let mut p = expectrl::session::Session::spawn(command).unwrap();
p.check("Using reader with a card: Virtual PCD 00 00")
p.expect("Using reader with a card: Virtual PCD 00 00")
.unwrap();
p.check("Personal Identity Verification Card").unwrap();
p.check(Eof).unwrap();
p.expect("Personal Identity Verification Card").unwrap();
p.expect(Eof).unwrap();
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));
});
}
@@ -65,9 +65,9 @@ fn generate_key() {
.env("PIV_EXT_AUTH_KEY", "tests/default_admin_key")
.args(["-A", "M:9B:03", "-G", "9A:11"]);
let mut p = expectrl::session::Session::spawn(command).unwrap();
p.check("Using reader with a card: Virtual PCD 00 00")
p.expect("Using reader with a card: Virtual PCD 00 00")
.unwrap();
p.check(Eof).unwrap();
p.expect(Eof).unwrap();
// Non zero exit code?
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 1));
});
@@ -77,9 +77,9 @@ fn generate_key() {
.env("PIV_EXT_AUTH_KEY", "tests/default_admin_key")
.args(["-A", "M:9B:03", "-G", "9A:07"]);
let mut p = expectrl::session::Session::spawn(command).unwrap();
p.check("Using reader with a card: Virtual PCD 00 00")
p.expect("Using reader with a card: Virtual PCD 00 00")
.unwrap();
p.check(Eof).unwrap();
p.expect(Eof).unwrap();
// Non zero exit code?
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 1));
});
+12 -16
View File
@@ -16,12 +16,12 @@ use std::io::Write;
fn list() {
with_vsc(|| {
let mut p = spawn("pivy-tool list").unwrap();
p.check(Regex("card: [0-9A-Z]*")).unwrap();
p.check("device: Virtual PCD 00 00").unwrap();
p.check("chuid: ok").unwrap();
p.check(Regex("guid: [0-9A-Z]*")).unwrap();
p.check("algos: 3DES AES256 ECCP256 (null) (null)").unwrap();
p.check(Eof).unwrap();
p.expect(Regex("card: [0-9A-Z]*")).unwrap();
p.expect("device: Virtual PCD 00 00").unwrap();
p.expect("chuid: ok").unwrap();
p.expect(Regex("guid: [0-9A-Z]*")).unwrap();
p.expect("algos: 3DES AES256 ECCP256 (null) (null)").unwrap();
p.expect(Eof).unwrap();
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));
});
}
@@ -30,13 +30,11 @@ fn list() {
fn generate() {
with_vsc(|| {
let mut p = spawn("pivy-tool -A 3des -K 010203040506070801020304050607080102030405060708 generate 9A -a eccp256 -P 123456").unwrap();
p.check("Touch button confirmation may be required.")
.unwrap();
p.check(Regex(
"^ecdsa-sha2-nistp256 (?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)? PIV_slot_9A@[A-F0-9]{20}$",
p.expect(Regex(
"ecdsa-sha2-nistp256 (?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)? PIV_slot_9A@[A-F0-9]{20}",
))
.unwrap();
p.check(Eof).unwrap();
p.expect(Eof).unwrap();
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));
});
}
@@ -45,13 +43,11 @@ fn generate() {
fn ecdh() {
with_vsc(|| {
let mut p = spawn("pivy-tool -A 3des -K 010203040506070801020304050607080102030405060708 generate 9A -a eccp256 -P 123456").unwrap();
p.check("Touch button confirmation may be required.")
.unwrap();
p.check(Regex(
"^ecdsa-sha2-nistp256 (?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)? PIV_slot_9A@[A-F0-9]{20}$",
p.expect(Regex(
"ecdsa-sha2-nistp256 (?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)? PIV_slot_9A@[A-F0-9]{20}",
))
.unwrap();
p.check(Eof).unwrap();
p.expect(Eof).unwrap();
assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));
let mut p = Command::new("pivy-tool")