Skip to content

Commit

Permalink
Refs #22056: Add machine_id() getter test
Browse files Browse the repository at this point in the history
Signed-off-by: cferreiragonz <[email protected]>
  • Loading branch information
cferreiragonz committed Nov 5, 2024
1 parent 755c05a commit 03bfe8d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/cpp/utils/Host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Host
if (machine_id_ == "")
{
EPROSIMA_LOG_WARNING(UTILS, "Cannot get machine id. Failing back to IP based ID");
machine_id_ = std::to_string(id_);
machine_id_ = "";
}
}

Expand All @@ -179,7 +179,7 @@ class Host
CFStringGetCString(uuidCf, buf, 255, kCFStringEncodingMacRoman);
CFRelease(uuidCf);
return std::string(buf, 255);
#else
#elif defined(_POSIX_SOURCE)
int fd = open("/etc/machine-id", O_RDONLY);
if (fd == -1) {
return "";
Expand All @@ -194,6 +194,8 @@ class Host
}

return std::string(buffer, 32);
#else
return "";
#endif // if defined(_WIN32)
}

Expand Down
1 change: 1 addition & 0 deletions test/unittest/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(SYSTEMINFOTESTS_SOURCE
${PROJECT_SOURCE_DIR}/src/cpp/rtps/transport/network/NetworkInterfaceWithFilter.cpp
${PROJECT_SOURCE_DIR}/src/cpp/utils/IPFinder.cpp
${PROJECT_SOURCE_DIR}/src/cpp/utils/IPLocator.cpp
${PROJECT_SOURCE_DIR}/src/cpp/utils/md5.cpp
${PROJECT_SOURCE_DIR}/src/cpp/utils/SystemInfo.cpp)

set(TREETESTS_SOURCE
Expand Down
7 changes: 7 additions & 0 deletions test/unittest/utils/SystemInfoTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>

#include <fastcdr/cdr/fixed_size_string.hpp>
#include <rtps/attributes/ServerAttributes.hpp>
#include <utils/SystemInfo.hpp>

Expand Down Expand Up @@ -160,6 +161,12 @@ TEST_F(SystemInfoTests, GetUsernameTest)
EXPECT_FALSE(username.empty());
}

TEST_F(SystemInfoTests, GetMachineId)
{
eprosima::fastcdr::string_255 machine_id = eprosima::SystemInfo::instance().machine_id();
EXPECT_GT(machine_id.size(), 0u);
}

/**
* This test checks the file_exists static method of the SystemInfo class
*/
Expand Down

0 comments on commit 03bfe8d

Please sign in to comment.