Improve GetFullyQualfiedName() test

This commit is contained in:
Andrew Schwartzmeyer
2016-09-12 15:01:18 -07:00
parent b86b1f870a
commit e7537b7ef4
2 changed files with 17 additions and 14 deletions
@@ -11,15 +11,7 @@
//! @brief GetFullyQualifiedName retrieves the fully qualified dns name of the host
//!
//! @exception errno Passes these errors via errno to GetLastError:
//! - ERROR_INVALID_FUNCTION: getlogin_r() returned an unrecognized error code (from GetComputerName)
//! - ERROR_INVALID_ADDRESS: buffer is an invalid address (from GetComputerName)
//! - ERROR_GEN_FAILURE: buffer not large enough (from GetComputerName)
//! - ERROR_BAD_NET_NAME: Cannot determine network short name
//!
//! @retval username as UTF-8 string, or null if unsuccessful
//!
char *GetFullyQualifiedName()
{
errno = 0;
@@ -12,19 +12,30 @@
TEST(GetFullyQualifiedNameTest, ValidateLinuxGetFullyQualifiedDomainName)
{
std::string actual(GetFullyQualifiedName());
std::string hostname(GetComputerName());
char *hostname = GetComputerName();
ASSERT_STRNE(NULL, hostname);
// this might be fail
errno = 0;
char *actual = GetFullyQualifiedName();
int fqdnErrno = errno;
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
EXPECT_FALSE(getaddrinfo(hostname.c_str(), "http", &hints, &info));
errno = 0;
if (getaddrinfo(hostname, "http", &hints, &info) != 0)
{
// test that getaddrinfo failed the same way
EXPECT_EQ(fqdnErrno, errno);
goto exit;
}
// Compare canonical name to FQDN
EXPECT_STREQ(info->ai_canonname, actual.c_str());
EXPECT_STREQ(info->ai_canonname, actual);
freeaddrinfo(info);
exit:
free(hostname);
}