Skip to content

Commit

Permalink
allow/deny conf: fix doc and empty lists management (fix #14) (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
JEnoch authored Nov 20, 2023
1 parent 8f1526b commit 5722313
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 11 deletions.
20 changes: 9 additions & 11 deletions DEFAULT_CONFIG.json5
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@
// shm_enabled: false,

////
//// allow: Sets of 1 or more regular expression per ROS interface kind matching the interface names that must be routed via zenoh.
//// By default, all interfaces are allowed.
//// If both 'allow' and 'deny' are set an interface will be allowed if it matches only the expression in 'allow' set.
////
//// allow / deny: Specify the lists of ROS 2 interfaces that are allowed or denied to be routed over Zenoh.
//// Each element of the lists is a regular expression that must match the full interface name.
//// You cannot set both 'allow' and 'deny' in the same configuration.
//// If neither 'allow' nor 'deny' are set, all interfaces are allowed.
//// Use 'allow' to allow only the specified interfaces. If an interface type is set to an empty list
//// or is not specified at all, it means that NO such interface is allowed.
//// Use 'deny' to allow all except the specified interfaces. If an interface type is set to an empty list
//// or is not specified at all, it means that ALL such interface are allowed.
// allow: {
// publishers: [".*/laser_scan", "/tf", ".*/pose"],
// subscribers: [".*/cmd_vel"],
Expand All @@ -64,12 +68,6 @@
// action_servers: [".*/rotate_absolute"],
// action_clients: [],
// },

////
//// deny: Sets of 1 or more regular expression per ROS interface kind matching the interface names that must NOT be routed via zenoh.
//// By default, no interface are denied.
//// If both 'allow' and 'deny' are set an interface will be allowed if it matches only the expression in 'allow' set.
////
// deny: {
// publishers: ["/rosout", "/parameter_events"],
// subscribers: ["/rosout"],
Expand All @@ -80,7 +78,7 @@
// },

////
//// pub_max_frequencies: Specifies a list of maximum frequency of publications routing over zenoh for a set of Publishers.
//// pub_max_frequencies: Specify a list of maximum frequency of publications routing over zenoh for a set of Publishers.
//// The strings must have the format "<regex>=<float>":
//// - "regex" is a regular expression matching a Publisher interface name
//// - "float" is the maximum frequency in Hertz;
Expand Down
155 changes: 155 additions & 0 deletions zenoh-plugin-ros2dds/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ impl<'de> Visitor<'de> for RegexVisitor {
while let Some(s) = seq.next_element::<String>()? {
vec.push(format!("^{s}$"));
}
if vec.is_empty() {
return Ok(None);
};

let s: String = vec.join("|");
Regex::new(&s)
.map(Some)
Expand Down Expand Up @@ -377,3 +381,154 @@ where
}
seq.end()
}

mod tests {

#[test]
fn test_allowance() {
use super::*;

let allow: Allowance = serde_json::from_str(
r#"{
"allow": {
"publishers": ["/tf", ".*/pose"],
"subscribers": [],
"service_servers": [".*"],
"action_servers": [".*/rotate_absolute"],
"action_clients": [ "" ]
}
}"#,
)
.unwrap();
println!("allow: {}", serde_json::to_string(&allow).unwrap());

assert!(matches!(
allow,
Allowance::Allow(ROS2InterfacesRegex {
publishers: Some(_),
subscribers: None,
service_servers: Some(_),
service_clients: None,
action_servers: Some(_),
action_clients: Some(_),
})
));

assert!(allow.is_publisher_allowed("/tf"));
assert!(allow.is_publisher_allowed("/x/y/pose"));
assert!(!allow.is_publisher_allowed("/abc/rotate_absolute"));
assert!(!allow.is_publisher_allowed("/cmd_vel"));
assert!(!allow.is_service_cli_allowed("/some_pseudo_random_name"));

assert!(!allow.is_subscriber_allowed("/tf"));
assert!(!allow.is_subscriber_allowed("/x/y/pose"));
assert!(!allow.is_publisher_allowed("/abc/rotate_absolute"));
assert!(!allow.is_subscriber_allowed("/cmd_vel"));
assert!(!allow.is_service_cli_allowed("/some_pseudo_random_name"));

assert!(allow.is_service_srv_allowed("/tf"));
assert!(allow.is_service_srv_allowed("/x/y/pose"));
assert!(allow.is_service_srv_allowed("/abc/rotate_absolute"));
assert!(allow.is_service_srv_allowed("/cmd_vel"));
assert!(allow.is_service_srv_allowed("/some_pseudo_random_name"));

assert!(!allow.is_service_cli_allowed("/tf"));
assert!(!allow.is_service_cli_allowed("/x/y/pose"));
assert!(!allow.is_service_cli_allowed("/abc/rotate_absolute"));
assert!(!allow.is_service_cli_allowed("/cmd_vel"));
assert!(!allow.is_service_cli_allowed("/some_pseudo_random_name"));

assert!(!allow.is_action_srv_allowed("/tf"));
assert!(!allow.is_action_srv_allowed("/x/y/pose"));
assert!(allow.is_action_srv_allowed("/abc/rotate_absolute"));
assert!(!allow.is_action_srv_allowed("/cmd_vel"));
assert!(!allow.is_action_srv_allowed("/some_pseudo_random_name"));

assert!(!allow.is_action_cli_allowed("/tf"));
assert!(!allow.is_action_cli_allowed("/x/y/pose"));
assert!(!allow.is_action_cli_allowed("/abc/rotate_absolute"));
assert!(!allow.is_action_cli_allowed("/cmd_vel"));
assert!(!allow.is_action_cli_allowed("/some_pseudo_random_name"));

let deny: Allowance = serde_json::from_str(
r#"{
"deny": {
"publishers": ["/tf", ".*/pose"],
"subscribers": [],
"service_servers": [".*"],
"action_servers": [".*/rotate_absolute"],
"action_clients": [ "" ]
}
}"#,
)
.unwrap();
println!("deny: {}", serde_json::to_string(&allow).unwrap());

assert!(matches!(
deny,
Allowance::Deny(ROS2InterfacesRegex {
publishers: Some(_),
subscribers: None,
service_servers: Some(_),
service_clients: None,
action_servers: Some(_),
action_clients: Some(_),
})
));

assert!(!deny.is_publisher_allowed("/tf"));
assert!(!deny.is_publisher_allowed("/x/y/pose"));
assert!(deny.is_publisher_allowed("/abc/rotate_absolute"));
assert!(deny.is_publisher_allowed("/cmd_vel"));
assert!(deny.is_service_cli_allowed("/some_pseudo_random_name"));

assert!(deny.is_subscriber_allowed("/tf"));
assert!(deny.is_subscriber_allowed("/x/y/pose"));
assert!(deny.is_publisher_allowed("/abc/rotate_absolute"));
assert!(deny.is_subscriber_allowed("/cmd_vel"));
assert!(deny.is_service_cli_allowed("/some_pseudo_random_name"));

assert!(!deny.is_service_srv_allowed("/tf"));
assert!(!deny.is_service_srv_allowed("/x/y/pose"));
assert!(!deny.is_service_srv_allowed("/abc/rotate_absolute"));
assert!(!deny.is_service_srv_allowed("/cmd_vel"));
assert!(!deny.is_service_srv_allowed("/some_pseudo_random_name"));

assert!(deny.is_service_cli_allowed("/tf"));
assert!(deny.is_service_cli_allowed("/x/y/pose"));
assert!(deny.is_service_cli_allowed("/abc/rotate_absolute"));
assert!(deny.is_service_cli_allowed("/cmd_vel"));
assert!(deny.is_service_cli_allowed("/some_pseudo_random_name"));

assert!(deny.is_action_srv_allowed("/tf"));
assert!(deny.is_action_srv_allowed("/x/y/pose"));
assert!(!deny.is_action_srv_allowed("/abc/rotate_absolute"));
assert!(deny.is_action_srv_allowed("/cmd_vel"));
assert!(deny.is_action_srv_allowed("/some_pseudo_random_name"));

assert!(deny.is_action_cli_allowed("/tf"));
assert!(deny.is_action_cli_allowed("/x/y/pose"));
assert!(deny.is_action_cli_allowed("/abc/rotate_absolute"));
assert!(deny.is_action_cli_allowed("/cmd_vel"));
assert!(deny.is_action_cli_allowed("/some_pseudo_random_name"));

let invalid = serde_json::from_str::<Allowance>(
r#"{
"allow": {
"publishers": ["/tf", ".*/pose"],
"subscribers": [],
"service_servers": [".*"],
"action_servers": [".*/rotate_absolute"],
"action_clients": [ "" ]
},
"deny": {
"subscribers": ["/tf", ".*/pose"],
"service_clients": [".*"],
"action_servers": [""],
"action_clients": [ ".*/rotate_absolute" ]
},
}"#,
);
assert!(invalid.is_err());
}
}

0 comments on commit 5722313

Please sign in to comment.