- return data from tests in a Vec
- run a test on a set of cards
This commit is contained in:
parent
3bba67fbcc
commit
6601d2d09b
1 changed files with 77 additions and 39 deletions
|
@ -1,52 +1,71 @@
|
||||||
// SPDX-FileCopyrightText: 2021 Heiko Schaefer <heiko@schaefer.name>
|
// SPDX-FileCopyrightText: 2021 Heiko Schaefer <heiko@schaefer.name>
|
||||||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use openpgp_card::apdu::PcscClient;
|
use openpgp_card::apdu::PcscClient;
|
||||||
use openpgp_card::card_app::CardApp;
|
use openpgp_card::card_app::CardApp;
|
||||||
use openpgp_card::CardClientBox;
|
use openpgp_card::CardClientBox;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn test(mut ca: CardApp) -> Result<()> {
|
#[derive(Debug)]
|
||||||
let res = ca.verify_pw3("12345678");
|
enum TestResult {
|
||||||
println!("res verify pw3 {:x?}", res);
|
Status([u8; 2]),
|
||||||
|
Text(String),
|
||||||
let check = ca.check_pw3();
|
|
||||||
println!("has pw3 been verified yet? {:x?}", check);
|
|
||||||
|
|
||||||
let res = ca.set_name("Admin<<Hello");
|
|
||||||
println!("set name res {:x?}", res);
|
|
||||||
res?.check_ok()?;
|
|
||||||
|
|
||||||
let cardholder = ca.get_cardholder_related_data()?;
|
|
||||||
println!("get name1: {}", cardholder.name.expect("name unset"));
|
|
||||||
|
|
||||||
let res = ca.verify_pw1("123456");
|
|
||||||
println!("res verify pw1 {:x?}", res);
|
|
||||||
|
|
||||||
let check = ca.check_pw3();
|
|
||||||
println!("has pw3 been verified yet? {:x?}", check);
|
|
||||||
|
|
||||||
let res = ca.set_name("There<<Hello");
|
|
||||||
println!("set name {:x?}", res);
|
|
||||||
|
|
||||||
let cardholder = ca.get_cardholder_related_data()?;
|
|
||||||
println!("get name2: {}", cardholder.name.expect("name unset"));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
type TestOutput = Vec<TestResult>;
|
||||||
let cards = PcscClient::list_cards()?;
|
|
||||||
|
|
||||||
// Ident of the OpenPGP Card that will be used for tests.
|
/// outputs:
|
||||||
let test_card_ident =
|
/// - verify pw3 + pin -> Status
|
||||||
env::var("TEST_CARD_IDENT").expect("TEST_CARD_IDENT is not set");
|
/// - verify pw3 (check) -> Status
|
||||||
|
/// - set name -> Status
|
||||||
|
/// - get name -> Text(name)
|
||||||
|
/// - verify pw1 + pin -> Status
|
||||||
|
/// - verify pw1 (check) -> Status
|
||||||
|
/// - set name -> Status
|
||||||
|
/// - get name -> Text(name)
|
||||||
|
fn test_verify(ca: &mut CardApp) -> Result<TestOutput> {
|
||||||
|
let mut out = vec![];
|
||||||
|
|
||||||
for card in cards {
|
let res = ca.verify_pw3("12345678")?;
|
||||||
|
out.push(TestResult::Status(res.status()));
|
||||||
|
|
||||||
|
let check = ca.check_pw3()?;
|
||||||
|
out.push(TestResult::Status(check.status()));
|
||||||
|
|
||||||
|
let res = ca.set_name("Admin<<Hello")?;
|
||||||
|
out.push(TestResult::Status(res.status()));
|
||||||
|
res.check_ok()?;
|
||||||
|
|
||||||
|
let cardholder = ca.get_cardholder_related_data()?;
|
||||||
|
out.push(TestResult::Text(cardholder.name.unwrap()));
|
||||||
|
|
||||||
|
let res = ca.verify_pw1("123456")?;
|
||||||
|
out.push(TestResult::Status(res.status()));
|
||||||
|
|
||||||
|
let check = ca.check_pw3()?;
|
||||||
|
out.push(TestResult::Status(check.status()));
|
||||||
|
|
||||||
|
let res = ca.set_name("There<<Hello")?;
|
||||||
|
out.push(TestResult::Status(res.status()));
|
||||||
|
res.check_ok()?;
|
||||||
|
|
||||||
|
let cardholder = ca.get_cardholder_related_data()?;
|
||||||
|
out.push(TestResult::Text(cardholder.name.unwrap()));
|
||||||
|
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_test(
|
||||||
|
cards: &[&str],
|
||||||
|
t: fn(&mut CardApp) -> Result<TestOutput>,
|
||||||
|
) -> Result<HashMap<String, TestOutput>> {
|
||||||
|
let mut out = HashMap::new();
|
||||||
|
|
||||||
|
for card in PcscClient::list_cards()? {
|
||||||
let card_client = Box::new(card) as CardClientBox;
|
let card_client = Box::new(card) as CardClientBox;
|
||||||
|
|
||||||
let mut ca = CardApp::new(card_client);
|
let mut ca = CardApp::new(card_client);
|
||||||
|
|
||||||
let res = ca.select()?;
|
let res = ca.select()?;
|
||||||
|
@ -55,13 +74,32 @@ fn main() -> Result<()> {
|
||||||
let ard = ca.get_app_data()?;
|
let ard = ca.get_app_data()?;
|
||||||
let app_id = CardApp::get_aid(&ard)?;
|
let app_id = CardApp::get_aid(&ard)?;
|
||||||
|
|
||||||
println!("Opened Card: ident {}", app_id.ident());
|
if cards.contains(&app_id.ident().as_str()) {
|
||||||
if app_id.ident() == test_card_ident {
|
println!("Running Test on {}:", app_id.ident());
|
||||||
println!("Running Test on {}", app_id.ident());
|
|
||||||
|
|
||||||
test(ca)?;
|
let res = t(&mut ca);
|
||||||
|
println!("{:x?}", res);
|
||||||
|
|
||||||
|
out.insert(app_id.ident(), res?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
// Ident of the OpenPGP Card that will be used for tests.
|
||||||
|
let test_card_ident =
|
||||||
|
env::var("TEST_CARD_IDENT").expect("TEST_CARD_IDENT is not set");
|
||||||
|
|
||||||
|
// list of card idents to runs the tests on
|
||||||
|
let cards = vec![
|
||||||
|
"0006:16019180", // Yubikey 5
|
||||||
|
"0005:0000A835", // FLOSS Card 3.4
|
||||||
|
"FFFE:57183146", // Rysim Gnuk (green)
|
||||||
|
];
|
||||||
|
|
||||||
|
let _verify_res = run_test(&cards, test_verify)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue