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

Code before:
            cid,
            cmd: CtapHid::COMMAND_KEEPALIVE,
            payload: vec![status_code],
        })
        .unwrap()
    }

    #[cfg(feature = "with_ctap1")]
    fn ctap1_error_message(
        cid: ChannelID,
        error_code: ctap1::Ctap1StatusCode,
    ) -> HidPacketIterator {
        // This unwrap is safe because the payload length is 2 <= 7609 bytes
        let code: u16 = error_code.into();
        CtapHid::split_message(Message {
            cid,
            cmd: CtapHid::COMMAND_MSG,
            payload: code.to_be_bytes().to_vec(),
        })
        .unwrap()
    }

    #[cfg(feature = "with_ctap1")]
    fn ctap1_success_message(cid: ChannelID, payload: &[u8]) -> HidPacketIterator {
        let mut response = payload.to_vec();
        let code: u16 = ctap1::Ctap1StatusCode::SW_INTERNAL_EXCEPTION.into();
        response.extend_from_slice(&code.to_be_bytes());
        CtapHid::split_message(Message {
            cid,
            cmd: CtapHid::COMMAND_MSG,
            payload: response,
        })
        .unwrap()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crypto::rng256::ThreadRng256;

    const CLOCK_FREQUENCY_HZ: usize = 32768;
    // Except for tests for timeouts (done in ctap1.rs), transactions are time independant.
    const DUMMY_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ);
    const DUMMY_TIMESTAMP: Timestamp<isize> = Timestamp::from_ms(0);

    fn process_messages<CheckUserPresence>(
        ctap_hid: &mut CtapHid,
        ctap_state: &mut CtapState<ThreadRng256, CheckUserPresence>,
        request: Vec<Message>,
    ) -> Option<Vec<Message>>

Review comment (suggestion):
Here we need to either change the name or the error code.
A function called `ctap1_success_message` shouldn't expect an `INTERNAL_EXCEPTION` error code.

Code after:
            cid,
            cmd: CtapHid::COMMAND_KEEPALIVE,
            payload: vec![status_code],
        })
        .unwrap()
    }

    #[cfg(feature = "with_ctap1")]
    fn ctap1_error_message(
        cid: ChannelID,
        error_code: ctap1::Ctap1StatusCode,
    ) -> HidPacketIterator {
        // This unwrap is safe because the payload length is 2 <= 7609 bytes
        let code: u16 = error_code.into();
        CtapHid::split_message(Message {
            cid,
            cmd: CtapHid::COMMAND_MSG,
            payload: code.to_be_bytes().to_vec(),
        })
        .unwrap()
    }

    #[cfg(feature = "with_ctap1")]
    fn ctap1_success_message(cid: ChannelID, payload: &[u8]) -> HidPacketIterator {
        let mut response = payload.to_vec();
        let code: u16 = ctap1::Ctap1StatusCode::SW_SUCCESS.into();
        response.extend_from_slice(&code.to_be_bytes());
        CtapHid::split_message(Message {
            cid,
            cmd: CtapHid::COMMAND_MSG,
            payload: response,
        })
        .unwrap()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crypto::rng256::ThreadRng256;

    const CLOCK_FREQUENCY_HZ: usize = 32768;
    // Except for tests for timeouts (done in ctap1.rs), transactions are time independant.
    const DUMMY_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ);
    const DUMMY_TIMESTAMP: Timestamp<isize> = Timestamp::from_ms(0);

    fn process_messages<CheckUserPresence>(
        ctap_hid: &mut CtapHid,
        ctap_state: &mut CtapState<ThreadRng256, CheckUserPresence>,
        request: Vec<Message>,
    ) -> Option<Vec<Message>>