From 5e61f0ad50c6309d10502ffa1f5783b35dc72b08 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 8 Jun 2026 12:10:21 +0200 Subject: [PATCH] Add tests for enum counts and conversions --- Cargo.toml | 1 + src/ctap2/get_info.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/webauthn.rs | 11 +++++++++++ 3 files changed, 50 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ddae0a6..c6a496e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ ciborium = "0.2" hex = "0.4" hex-literal = "0.4.1" serde_test = "1.0.176" +strum = { version = "0.28", features = ["derive"] } [features] std = [] diff --git a/src/ctap2/get_info.rs b/src/ctap2/get_info.rs index 90e1229..857048a 100644 --- a/src/ctap2/get_info.rs +++ b/src/ctap2/get_info.rs @@ -258,6 +258,7 @@ impl ResponseBuilder { } #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[cfg_attr(test, derive(strum::EnumCount, strum::VariantArray))] #[non_exhaustive] #[serde(into = "&str", try_from = "&str")] pub enum Version { @@ -308,6 +309,7 @@ impl TryFrom<&str> for Version { pub const EXTENSION_COUNT: usize = 7; #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[cfg_attr(test, derive(strum::EnumCount, strum::VariantArray))] #[non_exhaustive] #[serde(into = "&str", try_from = "&str")] pub enum Extension { @@ -364,6 +366,7 @@ impl TryFrom<&str> for Extension { pub const TRANSPORT_COUNT: usize = 3; #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[cfg_attr(test, derive(strum::EnumCount, strum::VariantArray))] #[non_exhaustive] #[serde(into = "&str", try_from = "&str")] pub enum Transport { @@ -526,7 +529,42 @@ pub struct Certifications { #[cfg(test)] mod tests { use super::*; + use core::fmt::Debug; use serde_test::{assert_ser_tokens, assert_tokens, Token}; + use strum::{EnumCount, VariantArray}; + + fn test_enum(count: usize) + where + T: EnumCount + + VariantArray + + Into<&'static str> + + for<'a> TryFrom<&'a str> + + PartialEq + + Debug + + Copy, + { + assert_eq!(count, T::COUNT); + + for variant in T::VARIANTS { + let variant_str: &str = (*variant).into(); + assert_eq!(Some(*variant), T::try_from(variant_str).ok()); + } + } + + #[test] + fn test_version() { + test_enum::(VERSION_COUNT); + } + + #[test] + fn test_transport() { + test_enum::(TRANSPORT_COUNT); + } + + #[test] + fn test_extension() { + test_enum::(EXTENSION_COUNT); + } #[test] fn test_serde_version() { diff --git a/src/webauthn.rs b/src/webauthn.rs index ec9eb37..59a908c 100644 --- a/src/webauthn.rs +++ b/src/webauthn.rs @@ -137,6 +137,7 @@ impl PublicKeyCredentialUserEntity { #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] +#[cfg_attr(test, derive(strum::EnumCount, strum::VariantArray))] #[non_exhaustive] pub enum KnownPublicKeyCredentialParameters { ES256, @@ -289,6 +290,16 @@ pub struct PublicKeyCredentialDescriptorRef<'a> { #[cfg(test)] mod tests { use super::*; + use strum::{EnumCount as _, VariantArray as _}; + + #[test] + fn test_known_cred_params() { + assert_eq!(KnownPublicKeyCredentialParameters::COUNT, COUNT_KNOWN_ALGS); + assert_eq!( + KnownPublicKeyCredentialParameters::ALL, + KnownPublicKeyCredentialParameters::VARIANTS + ); + } #[test] fn test_truncate() {