Skip to content

Commit

Permalink
Fix #230 (#235)
Browse files Browse the repository at this point in the history
* fix #230
  • Loading branch information
anweiss authored Feb 10, 2025
1 parent b28aa6b commit 924c288
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ simplelog = "0.12.1"
indoc = "2.0.1"
pretty_assertions = "1.2.0"
wasm-bindgen-test = "0.3.34"
hex = "0.4.3" # Add hex crate for tests

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
crossterm = { version = "0.28.1", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ pub enum Type2<'a> {
/// is the type of the tagged value
TaggedData {
/// Tag
tag: Option<usize>,
tag: Option<u64>,
/// Type
t: Type<'a>,
/// Span
Expand All @@ -1235,8 +1235,8 @@ pub enum Type2<'a> {
DataMajorType {
/// Major type
mt: u8,
/// Constraint
constraint: Option<usize>,
/// Constraint - Using u64 to support larger values in wasm32
constraint: Option<u64>,
/// Span
#[cfg(feature = "ast-span")]
span: Span,
Expand Down
6 changes: 3 additions & 3 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl<'a> Lexer<'a> {
{
Ok((
self.position,
Token::TAG(Some(t as u8), Some(self.read_number(idx)?.1 as usize)),
Token::TAG(Some(t as u8), Some(self.read_number(idx)?.1 as u64)),
))
}
}
Expand Down Expand Up @@ -956,7 +956,7 @@ impl<'a> Lexer<'a> {

#[cfg(not(target_arch = "wasm32"))]
{
Ok(Token::VALUE(Value::UINT(i)))
Ok(Token::VALUE(Value::UINT(i as usize)))
}

#[cfg(target_arch = "wasm32")]
Expand All @@ -966,7 +966,7 @@ impl<'a> Lexer<'a> {
}

#[cfg(not(target_arch = "wasm32"))]
fn read_number(&mut self, idx: usize) -> Result<(usize, usize)> {
fn read_number(&mut self, idx: usize) -> Result<(usize, u64)> {
let mut end_index = idx;

while let Some(&c) = self.peek_char() {
Expand Down
2 changes: 1 addition & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum Token<'a> {
/// Major type
Option<u8>,
/// Optional constraint
Option<usize>,
Option<u64>,
),

// Operators
Expand Down
8 changes: 4 additions & 4 deletions src/validator/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ where
Value::Bytes(b) => {
match mt {
2u8 => match constraint {
Some(c) if *c == b.len() => return Ok(()),
Some(c) if *c == b.len() as u64 => return Ok(()),
Some(c) => self.add_error(format!(
"expected byte string type with constraint {} (#{}.{}), got {:?}",
c, mt, c, self.cbor
Expand All @@ -2121,7 +2121,7 @@ where
Value::Text(t) => {
match mt {
3u8 => match constraint {
Some(c) if *c == t.len() => return Ok(()),
Some(c) if *c == t.len() as u64 => return Ok(()),
Some(c) => self.add_error(format!(
"expected text string type with constraint {} (#{}.{}), got {:?}",
c, mt, c, self.cbor
Expand All @@ -2139,7 +2139,7 @@ where
Value::Array(a) => {
match mt {
4u8 => match constraint {
Some(c) if *c == a.len() => return Ok(()),
Some(c) if *c == a.len() as u64 => return Ok(()),
Some(c) => self.add_error(format!(
"expected array type with constraint {} (#{}.{}), got {:?}",
c, mt, c, self.cbor
Expand All @@ -2157,7 +2157,7 @@ where
Value::Map(m) => {
match mt {
5u8 => match constraint {
Some(c) if *c == m.len() => return Ok(()),
Some(c) if *c == m.len() as u64 => return Ok(()),
Some(c) => self.add_error(format!(
"expected map type with constraint {} (#{}.{}), got {:?}",
c, mt, c, self.cbor
Expand Down
84 changes: 58 additions & 26 deletions tests/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#![cfg(feature = "cbor")]
#![cfg(not(target_arch = "wasm32"))]

use cddl::{self, validator::validate_cbor_from_slice};

use cddl::validator::validate_cbor_from_slice;
use ciborium::value::Value;
use serde::{Deserialize, Serialize};
use std::error::Error;

#[rustfmt::skip] // allow arbitrary indents for readability
#[rustfmt::skip]
pub mod cbor {
// example values from rfc7049 appendix A
pub const BOOL_FALSE: &[u8] = b"\xF4";
Expand Down Expand Up @@ -38,6 +39,29 @@ pub mod cbor {

}

// These data structures exist so that we can serialize some more complex
// beyond the RFC examples.
#[derive(Debug, Serialize, Deserialize)]
struct PersonStruct {
name: String,
age: u32,
}

#[derive(Debug, Serialize, Deserialize)]
struct PersonTuple(String, u32);

#[derive(Debug, Serialize, Deserialize)]
struct BackwardsTuple(u32, String);

#[derive(Debug, Serialize, Deserialize)]
struct LongTuple(String, u32, u32);

#[derive(Debug, Serialize, Deserialize)]
struct ShortTuple(String);

#[derive(Debug, Serialize, Deserialize)]
struct KitchenSink(String, u32, f64, bool);

#[test]
fn validate_cbor_bool() {
let cddl_input = r#"thing = true"#;
Expand Down Expand Up @@ -126,29 +150,6 @@ fn validate_cbor_array() {
validate_cbor_from_slice(cddl_input, cbor::ARRAY_123, None).unwrap();
}

// These data structures exist so that we can serialize some more complex
// beyond the RFC examples.
#[derive(Debug, Serialize, Deserialize)]
struct PersonStruct {
name: String,
age: u32,
}

#[derive(Debug, Serialize, Deserialize)]
struct PersonTuple(String, u32);

#[derive(Debug, Serialize, Deserialize)]
struct BackwardsTuple(u32, String);

#[derive(Debug, Serialize, Deserialize)]
struct LongTuple(String, u32, u32);

#[derive(Debug, Serialize, Deserialize)]
struct ShortTuple(String);

#[derive(Debug, Serialize, Deserialize)]
struct KitchenSink(String, u32, f64, bool);

#[test]
fn validate_cbor_group() {
let cddl_input = r#"thing = (* int)"#;
Expand Down Expand Up @@ -290,3 +291,34 @@ fn validate_cbor_map() {
let cddl_input = r#"thing = {x: int, y: int, z: int}"#;
validate_cbor_from_slice(cddl_input, cbor::ARRAY_123, None).unwrap_err();
}

#[test]
fn verify_large_tag_values() -> Result<(), Box<dyn Error>> {
let input = r#"
thing = #6.8386104246373017956(tstr) / #6.42(tstr)
"#;

// Test tag 42 (small tag value)
let test_str = "test";
let cbor = Value::Tag(42, Box::new(Value::Text(test_str.to_string())));
let mut bytes = Vec::new();
ciborium::ser::into_writer(&cbor, &mut bytes)?;
assert!(validate_cbor_from_slice(input, &bytes, None).is_ok());

// Test tag 8386104246373017956 (large tag value)
let cbor = Value::Tag(
8386104246373017956,
Box::new(Value::Text(test_str.to_string())),
);
let mut bytes = Vec::new();
ciborium::ser::into_writer(&cbor, &mut bytes)?;
assert!(validate_cbor_from_slice(input, &bytes, None).is_ok());

// Test wrong tag value - should fail
let cbor = Value::Tag(99, Box::new(Value::Text(test_str.to_string())));
let mut bytes = Vec::new();
ciborium::ser::into_writer(&cbor, &mut bytes)?;
assert!(validate_cbor_from_slice(input, &bytes, None).is_err());

Ok(())
}

0 comments on commit 924c288

Please sign in to comment.