Skip to content

Commit

Permalink
Improve testability of new code
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman authored and ReenigneArcher committed Aug 25, 2024
1 parent b752aa2 commit a7b43d3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
22 changes: 12 additions & 10 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,31 @@ namespace net {

/**
* @brief Returns a string for use as the instance name for mDNS.
* @param hostname The hostname to use for instance name generation.
* @return Hostname-based instance name or "Sunshine" if hostname is invalid.
*/
std::string
mdns_instance_name() {
std::string hostname = boost::asio::ip::host_name();
mdns_instance_name(const std::string_view &hostname) {
// Start with the unmodified hostname
std::string instancename { hostname.data(), hostname.size() };

// Truncate to 63 characters per RFC 6763 section 7.2.
if (hostname.size() > 63) {
hostname.resize(63);
if (instancename.size() > 63) {
instancename.resize(63);
}

for (auto i = 0; i < hostname.size(); i++) {
for (auto i = 0; i < instancename.size(); i++) {
// Replace any spaces with dashes
if (hostname[i] == ' ') {
hostname[i] = '-';
if (instancename[i] == ' ') {
instancename[i] = '-';
}
else if (!std::isalnum(hostname[i]) && hostname[i] != '-') {
else if (!std::isalnum(instancename[i]) && instancename[i] != '-') {
// Stop at the first invalid character
hostname.resize(i);
instancename.resize(i);
break;
}
}

return !hostname.empty() ? hostname : "Sunshine";
return !instancename.empty() ? instancename : "Sunshine";
}
} // namespace net
3 changes: 2 additions & 1 deletion src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ namespace net {

/**
* @brief Returns a string for use as the instance name for mDNS.
* @param hostname The hostname to use for instance name generation.
* @return Hostname-based instance name or "Sunshine" if hostname is invalid.
*/
std::string
mdns_instance_name();
mdns_instance_name(const std::string_view &hostname);
} // namespace net
2 changes: 1 addition & 1 deletion src/platform/linux/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ namespace platf::publish {
return nullptr;
}

auto instance_name = net::mdns_instance_name();
auto instance_name = net::mdns_instance_name(boost::asio::ip::host_name());
name.reset(avahi::strdup(instance_name.c_str()));

client.reset(
Expand Down
5 changes: 3 additions & 2 deletions src/platform/windows/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ namespace platf::publish {
auto alarm = safe::make_alarm<PDNS_SERVICE_INSTANCE>();

std::wstring domain { SERVICE_TYPE_DOMAIN.data(), SERVICE_TYPE_DOMAIN.size() };
std::wstring name = from_utf8(net::mdns_instance_name() + '.') + domain;

auto host = from_utf8(boost::asio::ip::host_name() + ".local");
auto hostname = boost::asio::ip::host_name();
auto name = from_utf8(net::mdns_instance_name(hostname) + '.') + domain;
auto host = from_utf8(hostname + ".local");

DNS_SERVICE_INSTANCE instance {};
instance.pszInstanceName = name.data();
Expand Down
27 changes: 15 additions & 12 deletions tests/unit/test_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@

#include "../tests_common.h"

TEST(MdnsInstanceNameTests, ValidLength) {
auto name = net::mdns_instance_name();
struct MdnsInstanceNameTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};

// The instance name must be 63 characters or less
EXPECT_LT(name.size(), 64);
TEST_P(MdnsInstanceNameTest, Run) {
auto [input, expected] = GetParam();
ASSERT_EQ(net::mdns_instance_name(input), expected);
}

TEST(MdnsInstanceNameTests, ValidCharacters) {
auto name = net::mdns_instance_name();

// The string must not contain invalid hostname characters
for (const char& c : name) {
EXPECT_TRUE(std::isalnum(c) || c == '-');
}
}
INSTANTIATE_TEST_SUITE_P(
MdnsInstanceNameTests,
MdnsInstanceNameTest,
testing::Values(
std::make_tuple("shortname-123", "shortname-123"),
std::make_tuple("space 123", "space-123"),
std::make_tuple("hostname.domain.test", "hostname"),
std::make_tuple("&", "Sunshine"),
std::make_tuple("", "Sunshine"),
std::make_tuple("😁", "Sunshine"),
std::make_tuple(std::string(128, 'a'), std::string(63, 'a'))));

0 comments on commit a7b43d3

Please sign in to comment.