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

Code before:
        assert_eq!(created_cbor, cbor_packed_attestation_statement);
    }

    #[test]
    fn test_from_into_cose_key_cbor() {
        for algorithm in &[ECDH_ALGORITHM, ES256_ALGORITHM] {
            let cbor_value = cbor_map! {
                1 => EC2_KEY_TYPE,
                3 => algorithm,
                -1 => P_256_CURVE,
                -2 => [0u8; 32],
                -3 => [0u8; 32],
            };
            let cose_key = CoseKey::try_from(cbor_value.clone()).unwrap();
            let created_cbor_value = cbor::Value::from(cose_key);
            assert_eq!(created_cbor_value, cbor_value);
        }

        let cbor_value = cbor_map! {
            1 => EC2_KEY_TYPE,
            // unknown algorithm
            3 => 0,
            -1 => P_256_CURVE,
            -2 => [0u8; 32],
            -3 => [0u8; 32],
        };
        assert_eq!(
            CoseKey::try_from(cbor_value),
            Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM)
        );
        let cbor_value = cbor_map! {
            // unknown type
            1 => 0,
            3 => ECDH_ALGORITHM,
            -1 => P_256_CURVE,
            -2 => [0u8; 32],
            -3 => [0u8; 32],
        };
        assert_eq!(
            CoseKey::try_from(cbor_value),
            Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM)
        );
        let cbor_value = cbor_map! {
            1 => EC2_KEY_TYPE,
            3 => ECDH_ALGORITHM,
            // unknown curve
            -1 => 0,
            -2 => [0u8; 32],
            -3 => [0u8; 32],
        };
        assert_eq!(

Review comment (bug):
Nit: It's usually better to have many small tests than one large test, so that:
- if the first assertion fails, it doesn't block the rest of the test cases from running,
- it's clear at first glance what failed and what succeeded, allowing easier debugging and bug fixing if some test failed.

Code after:
            x5c: Some(vec![vec![0x5C, 0x5C, 0x5C, 0x5C]]),
            ecdaa_key_id: Some(vec![0xEC, 0xDA, 0x1D]),
        };
        let created_cbor: cbor::Value = packed_attestation_statement.into();
        assert_eq!(created_cbor, cbor_packed_attestation_statement);
    }

    #[test]
    fn test_from_into_cose_key_cbor() {
        for algorithm in &[CoseKey::ECDH_ALGORITHM, ES256_ALGORITHM] {
            let cbor_value = cbor_map! {
                1 => CoseKey::EC2_KEY_TYPE,
                3 => algorithm,
                -1 => CoseKey::P_256_CURVE,
                -2 => [0u8; 32],
                -3 => [0u8; 32],
            };
            let cose_key = CoseKey::try_from(cbor_value.clone()).unwrap();
            let created_cbor_value = cbor::Value::from(cose_key);
            assert_eq!(created_cbor_value, cbor_value);
        }
    }

    #[test]
    fn test_cose_key_unknown_algorithm() {
        let cbor_value = cbor_map! {
            1 => CoseKey::EC2_KEY_TYPE,
            // unknown algorithm
            3 => 0,
            -1 => CoseKey::P_256_CURVE,
            -2 => [0u8; 32],
            -3 => [0u8; 32],
        };
        assert_eq!(
            CoseKey::try_from(cbor_value),
            Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM)
        );
    }

    #[test]
    fn test_cose_key_unknown_type() {
        let cbor_value = cbor_map! {
            // unknown type
            1 => 0,
            3 => CoseKey::ECDH_ALGORITHM,
            -1 => CoseKey::P_256_CURVE,
            -2 => [0u8; 32],
            -3 => [0u8; 32],
        };
        assert_eq!(
            CoseKey::try_from(cbor_value),