Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate QoS profile values are not negative. #483

Merged
merged 15 commits into from
Aug 5, 2020
5 changes: 5 additions & 0 deletions ros2bag/ros2bag/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def dict_to_duration(time_dict: Optional[Dict[str, int]]) -> Duration:
"""Convert a QoS duration profile from YAML into an rclpy Duration."""
if time_dict:
try:
if (Duration(seconds=time_dict['sec'], nanoseconds=time_dict['nsec']) <
Duration(seconds=0)):
raise ValueError('Time duration may not be a negative value.')
return Duration(seconds=time_dict['sec'], nanoseconds=time_dict['nsec'])
except KeyError:
raise ValueError(
Expand All @@ -61,6 +64,8 @@ def interpret_dict_as_qos_profile(qos_profile_dict: Dict) -> QoSProfile:
elif policy_key in _QOS_POLICY_FROM_SHORT_NAME:
new_profile_dict[policy_key] = _QOS_POLICY_FROM_SHORT_NAME[policy_key](policy_value)
elif policy_key in _VALUE_KEYS:
if policy_value < 0:
raise ValueError('`{}` may not be a negative value.'.format(policy_key))
new_profile_dict[policy_key] = policy_value
else:
raise ValueError('Unexpected key `{}` for QoS profile.'.format(policy_key))
Expand Down
14 changes: 14 additions & 0 deletions ros2bag/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ def test_convert_yaml_to_qos_profile(self):
assert qos_profiles[topic_name_2].avoid_ros_namespace_conventions == expected_convention
assert qos_profiles[topic_name_2].history == \
QoSHistoryPolicy.RMW_QOS_POLICY_HISTORY_KEEP_ALL

def test_interpret_dict_as_qos_profile_negative(self):
qos_dict = {'history': 'keep_all', 'depth': -1}
with self.assertRaises(ValueError):
interpret_dict_as_qos_profile(qos_dict)
qos_dict = {'history': 'keep_all', 'deadline': {'sec': -1, 'nsec': -1}}
with self.assertRaises(ValueError):
interpret_dict_as_qos_profile(qos_dict)
qos_dict = {'history': 'keep_all', 'lifespan': {'sec': -1, 'nsec': -1}}
with self.assertRaises(ValueError):
interpret_dict_as_qos_profile(qos_dict)
qos_dict = {'history': 'keep_all', 'liveliness_lease_duration': {'sec': -1, 'nsec': -1}}
with self.assertRaises(ValueError):
interpret_dict_as_qos_profile(qos_dict)