Skip to content

Commit

Permalink
Use absolute paths when loading XML files (#4751)
Browse files Browse the repository at this point in the history
* Refs #10173: Add XMLLoadFile unittest

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Use absolute paths when loading XML files

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Fix windows.h INTERFACE name collision

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Avoid MINMAX windows warning as error in unittests

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Use DEFAULT_FASTDDS_PROFILES for windows

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Enable internal debug in test

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Force log info in the test

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #10173: Correct windows FASTDDS_DEFAULT PROFILES env and include
fastdds/log instead of fastrtps

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

---------

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
(cherry picked from commit 0919ff2)

# Conflicts:
#	include/fastrtps/xmlparser/XMLParserCommon.h
#	src/cpp/rtps/xmlparser/XMLParser.cpp
#	src/cpp/rtps/xmlparser/XMLParserCommon.cpp
#	src/cpp/rtps/xmlparser/XMLProfileManager.cpp
#	test/unittest/xmlparser/CMakeLists.txt
  • Loading branch information
Mario-DL authored and mergify[bot] committed May 22, 2024
1 parent 31a248f commit eb2f7b3
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 6 deletions.
12 changes: 12 additions & 0 deletions include/fastrtps/xmlparser/XMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ class XMLParser
const std::string& filename,
up_base_node_t& root);

/**
* Load a XML file.
* @param filename Name for the file to be loaded.
* @param root Root node.
* @param is_default Is the default XML file.
* @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case.
*/
static XMLP_ret loadXML(
const std::string& filename,
up_base_node_t& root,
bool is_default);

/**
* Load a XML data from buffer.
* @param data XML data to load.
Expand Down
8 changes: 8 additions & 0 deletions include/fastrtps/xmlparser/XMLParserCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ extern const char* SEND_BUFFER_SIZE;
extern const char* TTL;
extern const char* NON_BLOCKING_SEND;
extern const char* WHITE_LIST;
<<<<<<< HEAD:include/fastrtps/xmlparser/XMLParserCommon.h
=======
extern const char* NETWORK_INTERFACE;
extern const char* NETMASK_FILTER;
extern const char* NETWORK_INTERFACES;
extern const char* ALLOWLIST;
extern const char* BLOCKLIST;
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParserCommon.h
extern const char* MAX_MESSAGE_SIZE;
extern const char* MAX_INITIAL_PEERS_RANGE;
extern const char* KEEP_ALIVE_FREQUENCY;
Expand Down
12 changes: 12 additions & 0 deletions include/fastrtps/xmlparser/XMLProfileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ class XMLProfileManager
RTPS_DllAPI static XMLP_ret loadXMLFile(
const std::string& filename);

/**
* Load a profiles XML file.
* @param filename Name for the file to be loaded.
* @param is_default Flag to indicate if the file is a default profiles file.
* @return XMLP_ret::XML_OK if all profiles are correct, XMLP_ret::XML_NOK if some are and some are not,
* XMLP_ret::XML_ERROR in other case.
*/

static XMLP_ret loadXMLFile(
const std::string& filename,
bool is_default);

/**
* Load a profiles XML string.
* @param data Buffer containing the data.
Expand Down
167 changes: 167 additions & 0 deletions src/cpp/rtps/xmlparser/XMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include <iostream>
#include <string>
#include <unordered_map>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32

#include <fastrtps/xmlparser/XMLParserCommon.h>
#include <fastrtps/xmlparser/XMLTree.h>
Expand All @@ -43,7 +48,36 @@ namespace xmlparser {
XMLP_ret XMLParser::loadDefaultXMLFile(
up_base_node_t& root)
{
<<<<<<< HEAD:src/cpp/rtps/xmlparser/XMLParser.cpp
return loadXML(DEFAULT_FASTRTPS_PROFILES, root);
=======
// Use absolute path to ensure that the file is loaded only once
#ifdef _WIN32
char current_directory[MAX_PATH];
if (GetCurrentDirectory(MAX_PATH, current_directory) == 0)
{
EPROSIMA_LOG_ERROR(XMLPARSER, "GetCurrentDirectory failed " << GetLastError());
}
else
{
strcat_s(current_directory, MAX_PATH, DEFAULT_FASTDDS_PROFILES);
return loadXML(current_directory, root, true);
}
#else
char current_directory[PATH_MAX];
if (getcwd(current_directory, PATH_MAX) == NULL)
{
EPROSIMA_LOG_ERROR(XMLPARSER, "getcwd failed " << std::strerror(errno));
}
else
{
strcat(current_directory, "/");
strcat(current_directory, DEFAULT_FASTDDS_PROFILES);
return loadXML(current_directory, root, true);
}
#endif // _WIN32
return XMLP_ret::XML_ERROR;
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParser.cpp
}

XMLP_ret XMLParser::parseXML(
Expand Down Expand Up @@ -367,7 +401,59 @@ XMLP_ret XMLParser::parseXMLTransportData(
return ret;
}
}
<<<<<<< HEAD:src/cpp/rtps/xmlparser/XMLParser.cpp
else
=======
}

XMLProfileManager::insertTransportById(sId, pDescriptor);
return ret;
}

XMLP_ret XMLParser::validateXMLTransportElements(
tinyxml2::XMLElement& p_root)
{
XMLP_ret ret = XMLP_ret::XML_OK;
for (tinyxml2::XMLElement* p_aux0 = p_root.FirstChildElement(); p_aux0 != nullptr;
p_aux0 = p_aux0->NextSiblingElement())
{
const char* name = p_aux0->Name();
if (!(strcmp(name, TRANSPORT_ID) == 0 ||
strcmp(name, TYPE) == 0 ||
strcmp(name, SEND_BUFFER_SIZE) == 0 ||
strcmp(name, RECEIVE_BUFFER_SIZE) == 0 ||
strcmp(name, MAX_MESSAGE_SIZE) == 0 ||
strcmp(name, MAX_INITIAL_PEERS_RANGE) == 0 ||
strcmp(name, WHITE_LIST) == 0 ||
strcmp(name, NETMASK_FILTER) == 0 ||
strcmp(name, NETWORK_INTERFACES) == 0 ||
strcmp(name, TTL) == 0 ||
strcmp(name, NON_BLOCKING_SEND) == 0 ||
strcmp(name, UDP_OUTPUT_PORT) == 0 ||
strcmp(name, TCP_WAN_ADDR) == 0 ||
strcmp(name, KEEP_ALIVE_FREQUENCY) == 0 ||
strcmp(name, KEEP_ALIVE_TIMEOUT) == 0 ||
strcmp(name, MAX_LOGICAL_PORT) == 0 ||
strcmp(name, LOGICAL_PORT_RANGE) == 0 ||
strcmp(name, LOGICAL_PORT_INCREMENT) == 0 ||
strcmp(name, LISTENING_PORTS) == 0 ||
strcmp(name, CALCULATE_CRC) == 0 ||
strcmp(name, CHECK_CRC) == 0 ||
strcmp(name, KEEP_ALIVE_THREAD) == 0 ||
strcmp(name, ACCEPT_THREAD) == 0 ||
strcmp(name, ENABLE_TCP_NODELAY) == 0 ||
strcmp(name, TCP_NEGOTIATION_TIMEOUT) == 0 ||
strcmp(name, TLS) == 0 ||
strcmp(name, SEGMENT_SIZE) == 0 ||
strcmp(name, PORT_QUEUE_CAPACITY) == 0 ||
strcmp(name, HEALTHY_CHECK_TIMEOUT_MS) == 0 ||
strcmp(name, RTPS_DUMP_FILE) == 0 ||
strcmp(name, DEFAULT_RECEPTION_THREADS) == 0 ||
strcmp(name, RECEPTION_THREADS) == 0 ||
strcmp(name, DUMP_THREAD) == 0 ||
strcmp(name, PORT_OVERFLOW_POLICY) == 0 ||
strcmp(name, SEGMENT_OVERFLOW_POLICY) == 0))
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParser.cpp
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Invalid transport type: '" << sType << "'");
return XMLP_ret::XML_ERROR;
Expand Down Expand Up @@ -481,7 +567,11 @@ XMLP_ret XMLParser::parseXMLCommonTransportData(
p_aux1 != nullptr; p_aux1 = p_aux1->NextSiblingElement())
{
address = p_aux1->Name();
<<<<<<< HEAD:src/cpp/rtps/xmlparser/XMLParser.cpp
if (strcmp(address, ADDRESS) == 0)
=======
if (strcmp(address, ADDRESS) == 0 || strcmp(address, NETWORK_INTERFACE) == 0)
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParser.cpp
{
const char* text = p_aux1->GetText();
if (nullptr != text)
Expand Down Expand Up @@ -510,7 +600,72 @@ XMLP_ret XMLParser::parseXMLCommonTransportData(
strcmp(name, HEALTHY_CHECK_TIMEOUT_MS) == 0 || strcmp(name, HEALTHY_CHECK_TIMEOUT_MS) == 0 ||
strcmp(name, RTPS_DUMP_FILE) == 0)
{
<<<<<<< HEAD:src/cpp/rtps/xmlparser/XMLParser.cpp
// Parsed outside of this method
=======
std::string netmask_filter_str;
if (XMLP_ret::XML_OK != getXMLString(p_aux0, &netmask_filter_str, 0))
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Invalid element found into 'netmask_filter'.");
return XMLP_ret::XML_ERROR;
}

try
{
p_transport->netmask_filter = fastdds::rtps::network::netmask_filter::string_to_netmask_filter_kind(
netmask_filter_str);
}
catch (const std::invalid_argument& e)
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Invalid element found into 'netmask_filter' : " << e.what());
return XMLP_ret::XML_ERROR;
}
}
else if (strcmp(name, NETWORK_INTERFACES) == 0)
{
if (XMLP_ret::XML_OK != parseXMLInterfaces(p_aux0, p_transport))
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Failed to parse 'interfaces' element.");
return XMLP_ret::XML_ERROR;
}
}
}
return XMLP_ret::XML_OK;
}

XMLP_ret XMLParser::parseXMLInterfaces(
tinyxml2::XMLElement* p_root,
std::shared_ptr<fastdds::rtps::SocketTransportDescriptor> p_transport)
{
/*
<xs:complexType name="interfacesType">
<xs:all>
<xs:element name="allowlist" type="allowlistType" minOccurs="0" maxOccurs="1"/>
<xs:element name="blocklist" type="blocklistType" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
*/
tinyxml2::XMLElement* p_aux0 = nullptr;
const char* name = nullptr;
for (p_aux0 = p_root->FirstChildElement(); p_aux0 != nullptr; p_aux0 = p_aux0->NextSiblingElement())
{
name = p_aux0->Name();
if (strcmp(name, ALLOWLIST) == 0)
{
if (XMLP_ret::XML_OK != parseXMLAllowlist(p_aux0, p_transport))
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Failed to parse 'allowlist'.");
return XMLP_ret::XML_ERROR;
}
}
else if (strcmp(name, BLOCKLIST) == 0)
{
if (XMLP_ret::XML_OK != parseXMLBlocklist(p_aux0, p_transport))
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Failed to parse 'blocklist'.");
return XMLP_ret::XML_ERROR;
}
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParser.cpp
}
else
{
Expand Down Expand Up @@ -1663,6 +1818,14 @@ XMLP_ret XMLParser::parseXMLConsumer(
XMLP_ret XMLParser::loadXML(
const std::string& filename,
up_base_node_t& root)
{
return loadXML(filename, root, false);
}

XMLP_ret XMLParser::loadXML(
const std::string& filename,
up_base_node_t& root,
bool is_default)
{
if (filename.empty())
{
Expand All @@ -1673,7 +1836,11 @@ XMLP_ret XMLParser::loadXML(
tinyxml2::XMLDocument xmlDoc;
if (tinyxml2::XMLError::XML_SUCCESS != xmlDoc.LoadFile(filename.c_str()))
{
<<<<<<< HEAD:src/cpp/rtps/xmlparser/XMLParser.cpp
if (filename != std::string(DEFAULT_FASTRTPS_PROFILES))
=======
if (!is_default)
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParser.cpp
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Error opening '" << filename << "'");
}
Expand Down
8 changes: 8 additions & 0 deletions src/cpp/rtps/xmlparser/XMLParserCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ const char* SEND_BUFFER_SIZE = "sendBufferSize";
const char* TTL = "TTL";
const char* NON_BLOCKING_SEND = "non_blocking_send";
const char* WHITE_LIST = "interfaceWhiteList";
<<<<<<< HEAD:src/cpp/rtps/xmlparser/XMLParserCommon.cpp
=======
const char* NETWORK_INTERFACE = "interface";
const char* NETMASK_FILTER = "netmask_filter";
const char* NETWORK_INTERFACES = "interfaces";
const char* ALLOWLIST = "allowlist";
const char* BLOCKLIST = "blocklist";
>>>>>>> 0919ff294 (Use absolute paths when loading XML files (#4751)):src/cpp/xmlparser/XMLParserCommon.cpp
const char* MAX_MESSAGE_SIZE = "maxMessageSize";
const char* MAX_INITIAL_PEERS_RANGE = "maxInitialPeersRange";
const char* KEEP_ALIVE_FREQUENCY = "keep_alive_frequency_ms";
Expand Down
Loading

0 comments on commit eb2f7b3

Please # to comment.