From 6601d2d09b4c424f4b7875018ecc5c7e4193046e Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Tue, 13 Jul 2021 20:36:45 +0200 Subject: [PATCH] - return data from tests in a Vec - run a test on a set of cards --- card-functionality/src/main.rs | 116 ++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 39 deletions(-) diff --git a/card-functionality/src/main.rs b/card-functionality/src/main.rs index e059f3c..ec702e0 100644 --- a/card-functionality/src/main.rs +++ b/card-functionality/src/main.rs @@ -1,52 +1,71 @@ // SPDX-FileCopyrightText: 2021 Heiko Schaefer // SPDX-License-Identifier: MIT OR Apache-2.0 -use anyhow::Result; +use anyhow::{anyhow, Result}; use std::env; use openpgp_card::apdu::PcscClient; use openpgp_card::card_app::CardApp; use openpgp_card::CardClientBox; +use std::collections::HashMap; -fn test(mut ca: CardApp) -> Result<()> { - let res = ca.verify_pw3("12345678"); - println!("res verify pw3 {:x?}", res); - - let check = ca.check_pw3(); - println!("has pw3 been verified yet? {:x?}", check); - - let res = ca.set_name("Admin< Result<()> { - let cards = PcscClient::list_cards()?; +type TestOutput = Vec; - // 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"); +/// outputs: +/// - verify pw3 + pin -> Status +/// - 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 { + 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< Result, +) -> Result> { + let mut out = HashMap::new(); + + for card in PcscClient::list_cards()? { let card_client = Box::new(card) as CardClientBox; - let mut ca = CardApp::new(card_client); let res = ca.select()?; @@ -55,13 +74,32 @@ fn main() -> Result<()> { let ard = ca.get_app_data()?; let app_id = CardApp::get_aid(&ard)?; - println!("Opened Card: ident {}", app_id.ident()); - if app_id.ident() == test_card_ident { - println!("Running Test on {}", app_id.ident()); + if cards.contains(&app_id.ident().as_str()) { + 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(()) }