Skip to content

Commit 06bfb99

Browse files
committed
fix: error coverage
1 parent fab0a4d commit 06bfb99

File tree

9 files changed

+175
-141
lines changed

9 files changed

+175
-141
lines changed

rust/catalyst-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ base64-url = "3.0.0"
3131
uuid = { version = "1.11.0", features = ["v4", "v7", "serde"] }
3232

3333
[dev-dependencies]
34+
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
3435
rand = "0.8.5"

rust/catalyst-types/src/conversion.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ pub fn vkey_from_bytes(bytes: &[u8]) -> Result<ed25519_dalek::VerifyingKey, VKey
6666
let mut ed25519 = [0u8; ed25519_dalek::PUBLIC_KEY_LENGTH];
6767
ed25519.copy_from_slice(bytes); // Can't panic because we already validated its size.
6868

69-
let pubkey = match ed25519_dalek::VerifyingKey::from_bytes(&ed25519) {
70-
Ok(key) => key,
71-
Err(err) => return Err(VKeyFromBytesError::ParseError { source: err }),
72-
};
73-
Ok(pubkey)
69+
ed25519_dalek::VerifyingKey::from_bytes(&ed25519)
70+
.map_err(|source| VKeyFromBytesError::ParseError { source })
7471
}

rust/catalyst-types/src/hashes.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ pub enum Blake2bHashError {
3636
/// The actual number of bytes in the provided input.
3737
actual: usize,
3838
},
39+
/// The input string is not a valid hex-encoded string.
40+
#[error("Invalid hex string: {source}")]
41+
InvalidHex {
42+
/// The underlying error from `hex`.
43+
#[from]
44+
source: hex::FromHexError,
45+
},
3946
}
4047

4148
/// data that is a blake2b [`struct@Hash`] of `BYTES` long.
@@ -133,10 +140,10 @@ impl<const BYTES: usize> fmt::Display for Blake2bHash<BYTES> {
133140
}
134141

135142
impl<const BYTES: usize> FromStr for Blake2bHash<BYTES> {
136-
type Err = hex::FromHexError;
143+
type Err = Blake2bHashError;
137144

138145
fn from_str(s: &str) -> Result<Self, Self::Err> {
139-
let hash: Hash<BYTES> = s.parse()?;
146+
let hash: Hash<BYTES> = s.parse().map_err(Blake2bHashError::from)?;
140147
Ok(hash.into())
141148
}
142149
}

rust/catalyst-types/src/kid_uri/authority.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

rust/catalyst-types/src/kid_uri/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
33
// cspell: words userinfo rngs Fftx csprng
44

5-
mod authority;
65
mod errors;
76
mod key_rotation;
8-
mod role0_pk;
97
mod role_index;
108

119
use std::{

rust/catalyst-types/src/kid_uri/role0_pk.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

rust/catalyst-types/src/uuid/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub use uuid_v7::UuidV7 as V7;
1212
/// Invalid Doc Type UUID
1313
pub const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
1414

15+
/// CBOR tag for UUID content.
16+
pub const UUID_CBOR_TAG: u64 = 37;
17+
1518
/// Errors that can occur when decoding CBOR-encoded UUIDs.
1619
#[derive(Debug, Error)]
1720
pub enum CborUuidError {
@@ -31,9 +34,6 @@ pub enum CborUuidError {
3134
},
3235
}
3336

34-
/// CBOR tag for UUID content.
35-
pub const UUID_CBOR_TAG: u64 = 37;
36-
3737
/// Decode `CBOR` encoded `UUID`.
3838
pub(crate) fn decode_cbor_uuid(val: &coset::cbor::Value) -> Result<uuid::Uuid, CborUuidError> {
3939
let Some((UUID_CBOR_TAG, coset::cbor::Value::Bytes(bytes))) = val.as_tag() else {

rust/catalyst-types/src/uuid/uuid_v4.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,81 @@ impl From<uuid::Uuid> for UuidV4 {
6868
Self { uuid }
6969
}
7070
}
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use coset::cbor::Value;
75+
use uuid::Uuid;
76+
77+
use super::*;
78+
use crate::uuid::UUID_CBOR_TAG;
79+
80+
#[test]
81+
fn test_invalid_uuid() {
82+
let invalid_uuid = UuidV4::invalid();
83+
assert!(!invalid_uuid.is_valid(), "Invalid UUID should not be valid");
84+
assert_eq!(
85+
invalid_uuid.uuid(),
86+
INVALID_UUID,
87+
"Invalid UUID should match INVALID_UUID"
88+
);
89+
}
90+
91+
#[test]
92+
fn test_valid_uuid() {
93+
let valid_uuid = UuidV4::from(Uuid::new_v4());
94+
assert!(valid_uuid.is_valid(), "Valid UUID should be valid");
95+
}
96+
97+
#[test]
98+
fn test_invalid_version_uuid() {
99+
let invalid_version_uuid = UuidV4::from(Uuid::from_u128(0)); // Zero UUID is not valid.
100+
assert!(
101+
!invalid_version_uuid.is_valid(),
102+
"Zero UUID should not be valid"
103+
);
104+
}
105+
106+
#[test]
107+
fn test_display_trait() {
108+
let valid_uuid = UuidV4::from(Uuid::new_v4());
109+
assert_eq!(
110+
format!("{}", valid_uuid),
111+
valid_uuid.uuid().to_string(),
112+
"Display implementation should match UUID string"
113+
);
114+
}
115+
116+
#[test]
117+
fn test_try_from_cbor_valid_uuid() {
118+
let uuid = Uuid::new_v4();
119+
let cbor_value = Value::Tag(
120+
UUID_CBOR_TAG,
121+
Box::new(Value::Bytes(uuid.as_bytes().to_vec())),
122+
);
123+
let result = UuidV4::try_from(&cbor_value);
124+
125+
assert!(
126+
result.is_ok(),
127+
"Should successfully parse valid UUID from CBOR"
128+
);
129+
let uuid_v4 = result.unwrap();
130+
assert!(uuid_v4.is_valid(), "Parsed UUIDv4 should be valid");
131+
assert_eq!(
132+
uuid_v4.uuid(),
133+
uuid,
134+
"Parsed UUID should match original UUID"
135+
);
136+
}
137+
138+
#[test]
139+
fn test_try_from_cbor_invalid_uuid() {
140+
let cbor_value = Value::Bytes(vec![0; 16]); // Zeroed-out UUID bytes
141+
let result = UuidV4::try_from(&cbor_value);
142+
143+
assert!(
144+
result.is_err(),
145+
"Should fail to parse invalid UUID from CBOR"
146+
);
147+
}
148+
}

rust/catalyst-types/src/uuid/uuid_v7.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,85 @@ impl From<uuid::Uuid> for UuidV7 {
6868
Self { uuid }
6969
}
7070
}
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use coset::cbor::Value;
75+
use uuid::Uuid;
76+
77+
use super::*;
78+
use crate::uuid::UUID_CBOR_TAG;
79+
80+
#[test]
81+
fn test_invalid_uuid() {
82+
let invalid_uuid = UuidV7::invalid();
83+
assert!(!invalid_uuid.is_valid(), "Invalid UUID should not be valid");
84+
assert_eq!(
85+
invalid_uuid.uuid(),
86+
INVALID_UUID,
87+
"Invalid UUID should match INVALID_UUID"
88+
);
89+
}
90+
91+
#[test]
92+
fn test_valid_uuid() {
93+
// Generate a UUIDv7 (this assumes you have access to a valid UUIDv7 library)
94+
let valid_uuid = UuidV7::from(
95+
Uuid::try_parse("017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1").unwrap(), // Example UUIDv7
96+
);
97+
assert!(valid_uuid.is_valid(), "Valid UUID should be valid");
98+
}
99+
100+
#[test]
101+
fn test_invalid_version_uuid() {
102+
let invalid_version_uuid = UuidV7::from(Uuid::from_u128(0)); // Zero UUID is not valid.
103+
assert!(
104+
!invalid_version_uuid.is_valid(),
105+
"Zero UUID should not be valid"
106+
);
107+
}
108+
109+
#[test]
110+
fn test_display_trait() {
111+
let valid_uuid =
112+
UuidV7::from(Uuid::try_parse("017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1").unwrap());
113+
assert_eq!(
114+
format!("{}", valid_uuid),
115+
valid_uuid.uuid().to_string(),
116+
"Display implementation should match UUID string"
117+
);
118+
}
119+
120+
#[test]
121+
fn test_try_from_cbor_valid_uuid() {
122+
let uuid = Uuid::try_parse("017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1").unwrap();
123+
let cbor_value = Value::Tag(
124+
UUID_CBOR_TAG,
125+
Box::new(Value::Bytes(uuid.as_bytes().to_vec())),
126+
);
127+
let result = UuidV7::try_from(&cbor_value);
128+
129+
assert!(
130+
result.is_ok(),
131+
"Should successfully parse valid UUID from CBOR"
132+
);
133+
let uuid_v7 = result.unwrap();
134+
assert!(uuid_v7.is_valid(), "Parsed UUIDv7 should be valid");
135+
assert_eq!(
136+
uuid_v7.uuid(),
137+
uuid,
138+
"Parsed UUID should match original UUID"
139+
);
140+
}
141+
142+
#[test]
143+
fn test_try_from_cbor_invalid_uuid() {
144+
let cbor_value = Value::Bytes(vec![0; 16]); // Zeroed-out UUID bytes
145+
let result = UuidV7::try_from(&cbor_value);
146+
147+
assert!(
148+
result.is_err(),
149+
"Should fail to parse invalid UUID from CBOR"
150+
);
151+
}
152+
}

0 commit comments

Comments
 (0)