Skip to content

Commit

Permalink
ros2GH-23 Improve santization of topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Idel-SI authored and anhosi committed Sep 5, 2018
1 parent 21911c9 commit 4f98d17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
17 changes: 16 additions & 1 deletion rosbag2/src/rosbag2/rosbag2_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,23 @@ std::map<std::string, std::string> Rosbag2Node::sanitize_topics_and_types(
"Topic '%s' has several types associated. Only topics with one type are supported.",
element.first.c_str());
return true;
} else {
char type_separator = '/';
auto sep_position_back = element.second[0].find_last_of(type_separator);
auto sep_position_front = element.second[0].find_first_of(type_separator);
if (sep_position_back == std::string::npos ||
sep_position_back != sep_position_front ||
sep_position_back == 0 ||
sep_position_back == element.second[0].length() - 1)
{
RCUTILS_LOG_ERROR_NAMED(
"rosbag2",
"Topic '%s' has non-ROS type %s . Only ROS topics are supported.",
element.first.c_str(), element.second[0].c_str());
return true;
}
return false;
}
return false;
});

std::map<std::string, std::string> topics_and_types_to_record;
Expand Down
30 changes: 29 additions & 1 deletion rosbag2/test/rosbag2/rosbag2_rosbag_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <gmock/gmock.h>

#include <future>
#include <map>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -140,7 +141,7 @@ TEST_F(RosBag2NodeFixture,
EXPECT_THAT(topics_and_types.begin()->second, StrEq("std_msgs/String"));
}

TEST_F(RosBag2NodeFixture, get_topics_with_types_returns_multiple_topics_for_multiple_inputs) {
TEST_F(RosBag2NodeFixture, get_topics_with_types_returns_only_specified_topics) {
std::string first_topic("/string_topic");
std::string second_topic("/other_topic");
std::string third_topic("/wrong_topic");
Expand Down Expand Up @@ -177,3 +178,30 @@ TEST_F(RosBag2NodeFixture, get_all_topics_with_types_returns_all_topics)
EXPECT_THAT(
topics_and_types.find("/parameter_events")->second, StrEq("rcl_interfaces/ParameterEvent"));
}

TEST_F(RosBag2NodeFixture, sanitize_topics_and_types_deletes_topics_with_multiple_types)
{
std::map<std::string, std::vector<std::string>> topics_and_types = {
{"/topic", {"package1/type1"}},
{"/topic2", {"package1/type1", "package2/type2"}}};

auto sanitized_topics_and_types = node_->sanitize_topics_and_types(topics_and_types);

ASSERT_THAT(sanitized_topics_and_types, SizeIs(1));
EXPECT_THAT(sanitized_topics_and_types.find("/topic")->second, StrEq("package1/type1"));
}

TEST_F(RosBag2NodeFixture, sanitize_topics_and_types_deletes_topics_with_wrong_type_format)
{
std::map<std::string, std::vector<std::string>> topics_and_types = {
{"/topic", {"package1/type1"}},
{"/topic2", {"package1/"}},
{"/topic3", {"package1/package2/type"}},
{"/topic4", {"/type"}}
};

auto sanitized_topics_and_types = node_->sanitize_topics_and_types(topics_and_types);

ASSERT_THAT(sanitized_topics_and_types, SizeIs(1));
EXPECT_THAT(sanitized_topics_and_types.find("/topic")->second, StrEq("package1/type1"));
}

0 comments on commit 4f98d17

Please sign in to comment.