Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions up-subscription/src/handlers/fetch_subscribers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<SubscriberUUri>>();
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(_))));
}
}
48 changes: 16 additions & 32 deletions up-subscription/src/handlers/fetch_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(_))));
}
}
84 changes: 64 additions & 20 deletions up-subscription/src/handlers/register_for_notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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::<NotificationEvent>(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(_))));
}
}
24 changes: 4 additions & 20 deletions up-subscription/src/handlers/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
Loading
Loading