Skip to content

Commit

Permalink
Update Server::setFQDN and add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: lbgracioso <[email protected]>
  • Loading branch information
lbgracioso committed Dec 11, 2023
1 parent 73916ab commit 1bbe3a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/cloysterhpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Server {
std::string m_fqdn; // TODO: Remove?

protected:
Server() = default;
Server(std::string_view hostname, OS& os, CPU& cpu,
std::list<Connection>&& connections,
std::optional<BMC> bmc = std::nullopt);
Expand Down Expand Up @@ -62,6 +61,7 @@ class Server {
void setBMC(const BMC& bmc);

virtual ~Server() = default;
Server() = default;
};

#endif // CLOYSTERHPC_SERVER_H_
48 changes: 32 additions & 16 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,7 @@ void Server::setFQDN(const std::string& fqdn)

// This pattern validates whether an FQDN is valid or not.
const std::regex fqdnPattern(
R"regex(^
( # Start of FQDN
( # Start of label (subdomain)
[a-zA-Z0-9] # First character of label
[a-zA-Z0-9\\-]* # Zero or more valid characters in label
[a-zA-Z0-9] # Last character of label
) # End of label
\. # Dot separator
)* # Zero or more labels
( # Start of last label (TLD)
[A-Za-z0-9] # First character of label (TLD)
[A-Za-z0-9\\-]* # Zero or more valid characters in label
[A-Za-z0-9] # Last character of label (TLD)
) # End of last label (TLD)
$)regex");
R"regex(^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-]*[a-zA-Z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9\\-]*[A-Za-z0-9])?$)regex");

if (!std::regex_match(fqdn, fqdnPattern))
throw std::runtime_error("Invalid FQDN format");
Expand Down Expand Up @@ -141,4 +127,34 @@ void Server::setBMC(const BMC& bmc) { m_bmc = bmc; }

const CPU& Server::getCPU() const noexcept { return m_cpu; }

void Server::setCPU(const CPU& cpu) { m_cpu = cpu; }
void Server::setCPU(const CPU& cpu) { m_cpu = cpu; }


#ifdef BUILD_TESTING
#include <doctest/doctest.h>
#else
#define DOCTEST_CONFIG_DISABLE
#include <doctest/doctest.h>
#endif

TEST_SUITE("Test FQDN")
{
TEST_CASE("FQDN Validation with Server::setFQDN") {
Server server;

SUBCASE("Valid FQDNs") {
CHECK_NOTHROW(server.setFQDN("example.com"));
CHECK_NOTHROW(server.setFQDN("subdomain.example.com"));
CHECK_NOTHROW(server.setFQDN("sub-domain.example.co.uk"));
}

SUBCASE("Invalid FQDNs") {
CHECK_THROWS(server.setFQDN("example")); // Missing TLD
CHECK_THROWS(server.setFQDN(".example.com")); // Leading dot
CHECK_THROWS(server.setFQDN("example.com.")); // Trailing dot
CHECK_THROWS(server.setFQDN("example..com")); // Double dot
CHECK_THROWS(server.setFQDN("example@com")); // Invalid character
CHECK_THROWS(server.setFQDN(std::string(256, 'a'))); // FQDN too long
}
}
}

0 comments on commit 1bbe3a0

Please sign in to comment.