diff --git a/up-subscription/src/handlers/fetch_subscribers.rs b/up-subscription/src/handlers/fetch_subscribers.rs index 60cb3f5..035732d 100644 --- a/up-subscription/src/handlers/fetch_subscribers.rs +++ b/up-subscription/src/handlers/fetch_subscribers.rs @@ -61,9 +61,10 @@ impl RequestHandler for FetchSubscribersRequestHandler { )); }; - // topic input validation // [impl->dsn~usubscription-fetch-subscribers-invalid-topic~1] - helpers::validate_uri(&topic)?; + helpers::validate_uri(&topic).map_err(|e| { + ServiceInvocationError::InvalidArgument(format!("Invalid topic uri '{topic}': {e}")) + })?; // Interact with subscription manager backend let (respond_to, receive_from) = oneshot::channel::>(); @@ -187,11 +188,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -216,11 +213,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -242,11 +235,7 @@ mod tests { .handle_request(RESOURCE_ID_FETCH_SUBSCRIBERS, &message_attributes, None) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -275,11 +264,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } // [utest->dsn~usubscription-fetch-subscribers-invalid-topic~1] @@ -329,10 +314,6 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } } diff --git a/up-subscription/src/handlers/fetch_subscriptions.rs b/up-subscription/src/handlers/fetch_subscriptions.rs index a747bf1..61ebe04 100644 --- a/up-subscription/src/handlers/fetch_subscriptions.rs +++ b/up-subscription/src/handlers/fetch_subscriptions.rs @@ -62,13 +62,21 @@ impl RequestHandler for FetchSubscriptionsRequestHandler { let request_kind = match request { Some(Request::Topic(topic)) => { // [impl->dsn~usubscription-fetch-subscriptions-invalid-topic~1] - helpers::validate_uri(&topic)?; + helpers::validate_uri(&topic).map_err(|e| { + ServiceInvocationError::InvalidArgument(format!( + "Invalid topic uri '{topic}': {e}" + )) + })?; RequestKind::Topic(topic) } Some(Request::Subscriber(subscriber)) => { if let Some(subscriber) = subscriber.uri.into_option() { // [impl->dsn~usubscription-fetch-subscriptions-invalid-subscriber~1] - helpers::validate_uri(&subscriber)?; + helpers::validate_uri(&subscriber).map_err(|e| { + ServiceInvocationError::InvalidArgument(format!( + "Invalid subscriber uri '{subscriber}': {e}" + )) + })?; RequestKind::Subscriber(subscriber) } else { return Err(ServiceInvocationError::InvalidArgument( @@ -235,11 +243,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -264,11 +268,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -290,11 +290,7 @@ mod tests { .handle_request(RESOURCE_ID_FETCH_SUBSCRIPTIONS, &message_attributes, None) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -323,11 +319,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } // [utest->dsn~usubscription-fetch-subscriptions-invalid-subscriber~1] @@ -386,11 +378,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } // [utest->dsn~usubscription-fetch-subscriptions-invalid-topic~1] @@ -444,10 +432,6 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } } diff --git a/up-subscription/src/handlers/register_for_notifications.rs b/up-subscription/src/handlers/register_for_notifications.rs index c35bb02..fa86afe 100644 --- a/up-subscription/src/handlers/register_for_notifications.rs +++ b/up-subscription/src/handlers/register_for_notifications.rs @@ -58,6 +58,10 @@ impl RequestHandler for RegisterNotificationsRequestHandler { "No topic defined in request".to_string(), )); }; + // [impl->dsn~usubscription-register-notifications-invalid-topic~1] + helpers::validate_uri(topic).map_err(|e| { + ServiceInvocationError::InvalidArgument(format!("Invalid topic uri '{topic}': {e}")) + })?; // Interact with notification manager backend let se = NotificationEvent::AddNotifyee { @@ -84,8 +88,11 @@ impl RequestHandler for RegisterNotificationsRequestHandler { #[cfg(test)] mod tests { use super::*; + use test_case::test_case; use tokio::sync::mpsc::{self}; + use up_rust::UUri; + use crate::{helpers, tests::test_lib}; // [utest->dsn~usubscription-register-notifications-protobuf~1] @@ -162,11 +169,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -192,11 +195,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -221,11 +220,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -253,10 +248,59 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); + } + + // [utest->dsn~usubscription-register-notifications-invalid-topic~1] + #[test_case(UUri::default(); "Bad topic UUri")] + #[test_case(UUri { + authority_name: String::from("*"), + ue_id: test_lib::helpers::TOPIC_LOCAL1_ID, + ue_version_major: test_lib::helpers::TOPIC_LOCAL1_VERSION as u32, + resource_id: test_lib::helpers::TOPIC_LOCAL1_RESOURCE as u32, + ..Default::default() + }; "Wildcard authority in topic UUri")] + #[test_case(UUri { + authority_name: test_lib::helpers::LOCAL_AUTHORITY.into(), + ue_id: 0xFFFF_0000, + ue_version_major: test_lib::helpers::TOPIC_LOCAL1_VERSION as u32, + resource_id: test_lib::helpers::TOPIC_LOCAL1_RESOURCE as u32, + ..Default::default() + }; "Wildcard entity id in topic UUri")] + #[test_case(UUri { + authority_name: test_lib::helpers::LOCAL_AUTHORITY.into(), + ue_id: test_lib::helpers::TOPIC_LOCAL1_ID, + ue_version_major: test_lib::helpers::TOPIC_LOCAL1_VERSION as u32, + resource_id: 0x0000_FFFF, + ..Default::default() + }; "Wildcard resource id in topic UUri")] + #[tokio::test] + async fn test_invalid_topic_uri(topic: UUri) { + helpers::init_once(); + + // create request and other required object(s) + let notification_request = NotificationsRequest { + topic: Some(topic).into(), + ..Default::default() + }; + let request_payload = UPayload::try_from_protobuf(notification_request.clone()).unwrap(); + let message_attributes = UAttributes { + source: Some(test_lib::helpers::subscriber_uri1()).into(), + ..Default::default() + }; + let (notification_sender, _) = mpsc::channel::(1); + + // create and spawn off handler, to make all the asnync goodness work + let request_handler = RegisterNotificationsRequestHandler::new(notification_sender); + + let result = request_handler + .handle_request( + RESOURCE_ID_REGISTER_FOR_NOTIFICATIONS, + &message_attributes, + Some(request_payload), + ) + .await; + + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } } diff --git a/up-subscription/src/handlers/subscribe.rs b/up-subscription/src/handlers/subscribe.rs index 2e64996..f879195 100644 --- a/up-subscription/src/handlers/subscribe.rs +++ b/up-subscription/src/handlers/subscribe.rs @@ -206,11 +206,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -235,11 +231,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -261,11 +253,7 @@ mod tests { .handle_request(RESOURCE_ID_SUBSCRIBE, &message_attributes, None) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -294,11 +282,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] diff --git a/up-subscription/src/handlers/unregister_for_notifications.rs b/up-subscription/src/handlers/unregister_for_notifications.rs index 3a59ca3..b7753a6 100644 --- a/up-subscription/src/handlers/unregister_for_notifications.rs +++ b/up-subscription/src/handlers/unregister_for_notifications.rs @@ -155,11 +155,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -185,11 +181,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -214,11 +206,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -246,10 +234,6 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } } diff --git a/up-subscription/src/handlers/unsubscribe.rs b/up-subscription/src/handlers/unsubscribe.rs index ae7476a..4c9dc28 100644 --- a/up-subscription/src/handlers/unsubscribe.rs +++ b/up-subscription/src/handlers/unsubscribe.rs @@ -180,11 +180,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -209,11 +205,7 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -235,11 +227,7 @@ mod tests { .handle_request(RESOURCE_ID_UNSUBSCRIBE, &message_attributes, None) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } #[tokio::test] @@ -268,10 +256,6 @@ mod tests { ) .await; - assert!(result.is_err()); - match result.unwrap_err() { - ServiceInvocationError::InvalidArgument(_) => {} - _ => panic!("Wrong error type"), - } + assert!(result.is_err_and(|err| matches!(err, ServiceInvocationError::InvalidArgument(_)))); } }