From 1d7acc7f0d496e8bb78385a59ee63c7eb225a719 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 18 Jul 2026 13:47:08 -0700 Subject: [PATCH] infe: return errors for missing content types --- src/meta/iinf.rs | 68 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/src/meta/iinf.rs b/src/meta/iinf.rs index f2d65570..ae98e8d5 100644 --- a/src/meta/iinf.rs +++ b/src/meta/iinf.rs @@ -66,12 +66,11 @@ impl AtomExt for ItemInfoEntry { (self.item_id as u16).encode(buf)?; self.item_protection_index.encode(buf)?; self.item_name.as_str().encode(buf)?; - self.content_type.clone().unwrap().as_str().encode(buf)?; - self.content_encoding - .clone() - .unwrap_or("".to_string()) - .as_str() - .encode(buf)?; + let content_type = self.content_type.as_deref().ok_or(Error::MissingContent( + "content_type required for infe version 0", + ))?; + content_type.encode(buf)?; + self.content_encoding.as_deref().unwrap_or("").encode(buf)?; if version == ItemInfoEntryVersion::V1 { return Err(Error::Unsupported("infe version 1 extensions")); } @@ -85,12 +84,11 @@ impl AtomExt for ItemInfoEntry { Some(self.item_type).encode(buf)?; self.item_name.as_str().encode(buf)?; if self.item_type == Some(FourCC::new(b"mime")) { - self.content_type.clone().unwrap().as_str().encode(buf)?; - self.content_encoding - .clone() - .unwrap_or("".to_string()) - .as_str() - .encode(buf)?; + let content_type = self.content_type.as_deref().ok_or(Error::MissingContent( + "content_type required with 'mime' item_type", + ))?; + content_type.encode(buf)?; + self.content_encoding.as_deref().unwrap_or("").encode(buf)?; } else if self.item_type == Some(FourCC::new(b"uri ")) { let item_uri_type = self.item_uri_type.as_ref().ok_or(Error::MissingContent( "item_uri_type required with 'uri ' item_type", @@ -321,6 +319,52 @@ mod tests { )); } + /// Rejects a version 0 item info entry when its required content type is missing. + #[test] + fn test_iinf_encode_v0_without_content_type_returns_error() { + let iinf = Iinf { + item_infos: vec![ItemInfoEntry { + item_id: 1, + item_protection_index: 0, + item_type: None, + item_name: "Item".to_string(), + content_type: None, + content_encoding: None, + item_uri_type: None, + item_not_in_presentation: false, + }], + }; + let mut buf = Vec::new(); + + assert!(matches!( + iinf.encode(&mut buf), + Err(Error::MissingContent(_)) + )); + } + + /// Rejects a `mime` item info entry when its required content type is missing. + #[test] + fn test_iinf_encode_mime_without_content_type_returns_error() { + let iinf = Iinf { + item_infos: vec![ItemInfoEntry { + item_id: 1, + item_protection_index: 0, + item_type: Some(FourCC::new(b"mime")), + item_name: "Item".to_string(), + content_type: None, + content_encoding: None, + item_uri_type: None, + item_not_in_presentation: false, + }], + }; + let mut buf = Vec::new(); + + assert!(matches!( + iinf.encode(&mut buf), + Err(Error::MissingContent(_)) + )); + } + /// Version 1 `infe` entries are reported as unsupported, not panicked. #[test] fn test_iinf_decode_unsupported_infe_v1_returns_error() {