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
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)