Repository: google/OpenSK
File: src/ctap/hid/mod.rs (Rust)

Code before:
        let mut response = CtapHid::split_message(message);
        let mut expected_packet = [0x00; 64];
        expected_packet[..9]
            .copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x02, 0x99, 0x99]);
        assert_eq!(response.next(), Some(expected_packet));
        assert_eq!(response.next(), None);
    }

    #[test]
    fn test_split_message_too_large() {
        let payload = vec![0xFF; 7609 + 1];
        let message = Message {
            cid: [0x12, 0x34, 0x56, 0x78],
            cmd: CtapHidCommand::Cbor,
            payload,
        };
        let mut response = CtapHid::split_message(message);
        let mut expected_packet = [0x00; 64];
        expected_packet[..8].copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x01, 0xF2]);
        assert_eq!(response.next(), Some(expected_packet));
        assert_eq!(response.next(), None);
    }

    #[test]
    fn test_keepalive() {
        for status in [KeepaliveStatus::Processing, KeepaliveStatus::UpNeeded].iter() {
            let cid = [0x12, 0x34, 0x56, 0x78];
            let mut response = CtapHid::keepalive(cid, *status);
            let mut expected_packet = [0x00; 64];
            expected_packet[..8].copy_from_slice(&[
                0x12,
                0x34,
                0x56,
                0x78,
                0xBB,
                0x00,
                0x01,
                *status as u8,
            ]);
            assert_eq!(response.next(), Some(expected_packet));
            assert_eq!(response.next(), None);
        }
    }

    #[test]
    fn test_process_single_packet() {
        let cid = [0x12, 0x34, 0x56, 0x78];
        let mut packet = [0x00; 64];
        packet[..4].copy_from_slice(&cid);
        packet[4..9].copy_from_slice(&[0x81, 0x00, 0x02, 0x99, 0x99]);
        let (processed_cid, processed_packet) = CtapHid::process_single_packet(&packet);

Review comment (suggestion):
nit/fyi: it's possible to write `&status` here (so once) instead of `*status` at each usage occurrences below (when the type is copy).

Code after:
        let mut response = CtapHid::split_message(message);
        let mut expected_packet = [0x00; 64];
        expected_packet[..9]
            .copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x02, 0x99, 0x99]);
        assert_eq!(response.next(), Some(expected_packet));
        assert_eq!(response.next(), None);
    }

    #[test]
    fn test_split_message_too_large() {
        let payload = vec![0xFF; 7609 + 1];
        let message = Message {
            cid: [0x12, 0x34, 0x56, 0x78],
            cmd: CtapHidCommand::Cbor,
            payload,
        };
        let mut response = CtapHid::split_message(message);
        let mut expected_packet = [0x00; 64];
        expected_packet[..8].copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x01, 0xF2]);
        assert_eq!(response.next(), Some(expected_packet));
        assert_eq!(response.next(), None);
    }

    #[test]
    fn test_keepalive() {
        for &status in [KeepaliveStatus::Processing, KeepaliveStatus::UpNeeded].iter() {
            let cid = [0x12, 0x34, 0x56, 0x78];
            let mut response = CtapHid::keepalive(cid, status);
            let mut expected_packet = [0x00; 64];
            expected_packet[..8].copy_from_slice(&[
                0x12,
                0x34,
                0x56,
                0x78,
                0xBB,
                0x00,
                0x01,
                status as u8,
            ]);
            assert_eq!(response.next(), Some(expected_packet));
            assert_eq!(response.next(), None);
        }
    }

    #[test]
    fn test_process_single_packet() {
        let cid = [0x12, 0x34, 0x56, 0x78];
        let mut packet = [0x00; 64];
        packet[..4].copy_from_slice(&cid);
        packet[4..9].copy_from_slice(&[0x81, 0x00, 0x02, 0x99, 0x99]);
        let (processed_cid, processed_packet) = CtapHid::process_single_packet(&packet);