anweiss / cddl

Concise data definition language (RFC 8610) implementation and JSON and CBOR validator in Rust
https://cddl.anweiss.tech
MIT License
90 stars 12 forks source link

Fail to validate nested array #218

Open gregorydemay opened 7 months ago

gregorydemay commented 7 months ago
  1. Tested with cddl = "0.9.4"
  2. Cannot handle a CDDL schema involving nested array, e.g., [0, [0]]. Note that the not-nested schema [0, 0] seems to work fine.

Example

    #[test]
    fn should_validate_nested_array() {
        let schema = r#"root = [0, 0]"#;
        let cbor_bytes = [0x82, 0x00, 0x00];
        let result = cddl::validate_cbor_from_slice(schema, &cbor_bytes, None);
        assert!( //PASSES!
            result.is_ok(),
            "Validation of {:x?} failed: {:?}",
            cbor_bytes,
            result
        );

        let schema = r#"root = [0, [0]]"#;
        let cbor_bytes = [0x82, 0x00, 0x81, 0x00];
        let result = cddl::validate_cbor_from_slice(schema, &cbor_bytes, None);
        assert!( //FAILS!
            result.is_ok(),
            "Validation of {:x?} failed: {:?}",
            cbor_bytes,
            result
        );
    }

The first assertion passes, while the second one returns the following error

Validation of [82, 0, 81, 0] failed: Err(Validation([ValidationError { reason: "expected array with length 1, got 2", cddl_location: "", cbor_location: "", is_multi_type_choice: false, is_multi_group_choice: false, is_group_to_choice_enum: false, type_group_name_entry: None }]))
anweiss commented 5 months ago

Thanks for reporting @gregorydemay. This is going to take a little time for me to fix. The array validation logic I have implemented is a bit of a mess and doesn't properly validate nested arrays.