1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#![cfg_attr(not(any(test, feature = "std")), no_std)]

#[macro_use]
extern crate delog;
// generate_macros!();

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Interface {
    Contact,
    Contactless,
}

pub type Data<const S: usize> = heapless::Vec<u8, S>;
pub type Result<T = ()> = core::result::Result<T, Status>;

pub mod aid;
pub mod command;
pub mod response;

pub use aid::{Aid, App};
pub use command::{Command, Instruction};
pub use response::{Response, Status};
pub mod tlv;

#[cfg(test)]
mod tests {
    use super::Command;
    use quickcheck::{Arbitrary, Gen};
    use quickcheck_macros::quickcheck;

    const COMMAND_SIZE: usize = 7609;

    #[derive(Clone, Debug)]
    struct Bytes<const N: usize>(Vec<u8>);

    impl<const N: usize> Arbitrary for Bytes<N> {
        fn arbitrary(g: &mut Gen) -> Self {
            let mut gen = Gen::new(g.size().min(N));
            Self(Arbitrary::arbitrary(&mut gen))
        }

        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
            Box::new(self.0.shrink().map(Self))
        }
    }

    #[quickcheck]
    fn parse_no_panic(data: Bytes<COMMAND_SIZE>) {
        let _command = Command::<COMMAND_SIZE>::try_from(&data.0);
    }

    #[quickcheck]
    fn parse_apdu(
        cla: u8,
        ins: u8,
        p1: u8,
        p2: u8,
        data: Bytes<{ u8::MAX as usize }>,
        le: Option<u8>,
    ) {
        let cla = if cla == u8::MAX { 0 } else { cla };

        let mut command = vec![cla, ins, p1, p2];
        if !data.0.is_empty() {
            command.push(data.0.len() as u8);
            command.extend_from_slice(&data.0);
        }
        if let Some(le) = le {
            command.push(le);
        }

        let command = Command::<COMMAND_SIZE>::try_from(&command).expect("unexpected error");
        assert_eq!(command.class().into_inner(), cla);
        assert_eq!(u8::from(command.instruction()), ins);
        assert_eq!(command.p1, p1);
        assert_eq!(command.p2, p2);
        assert!(!command.extended);
        assert_eq!(command.data().as_slice(), &data.0);
        assert_eq!(
            command.expected(),
            le.map(usize::from)
                .map(|val| if val == 0 { 256 } else { val })
                .unwrap_or_default()
        );
    }
}