Fix test of FQDN
We can't use `hostname --fqdn` or any other shell tool because there is no POSIX compatible way to do this short of system calls. Unfortunately this means the test practically reimplements the original code, but oh well.
This commit is contained in:
parent
5e0728e48e
commit
7146cc0b86
1 changed files with 21 additions and 15 deletions
|
@ -4,6 +4,10 @@
|
|||
|
||||
#include <gtest/gtest.h>
|
||||
#include "getfullyqualifiedname.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <string>
|
||||
|
||||
//! Test fixture for GetComputerNameTest
|
||||
class GetFullyQualifiedNameTest : public ::testing::Test
|
||||
|
@ -12,23 +16,25 @@ class GetFullyQualifiedNameTest : public ::testing::Test
|
|||
|
||||
TEST_F(GetFullyQualifiedNameTest, ValidateLinuxGetFullyQualifiedDomainName)
|
||||
{
|
||||
char expectedComputerName[_POSIX_HOST_NAME_MAX];
|
||||
std::string actual(GetFullyQualifiedName());
|
||||
|
||||
//Get expected result from using linux command
|
||||
std::string hostname(_POSIX_HOST_NAME_MAX, 0);
|
||||
ASSERT_FALSE(gethostname(&hostname[0], hostname.length()));
|
||||
// trim null characters from string
|
||||
hostname = std::string(hostname.c_str());
|
||||
|
||||
FILE *fPtr = popen("hostname --fqdn", "r");
|
||||
ASSERT_TRUE(fPtr != NULL);
|
||||
struct addrinfo hints, *info;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_CANONNAME;
|
||||
ASSERT_FALSE(getaddrinfo(hostname.c_str(), "http", &hints, &info));
|
||||
|
||||
char *linePtr = fgets(expectedComputerName, sizeof(expectedComputerName), fPtr);
|
||||
ASSERT_TRUE(linePtr != NULL);
|
||||
// Compare hostname part of FQDN
|
||||
ASSERT_EQ(hostname, actual.substr(0, hostname.length()));
|
||||
|
||||
// There's a tendency to have \n at end of fgets string, so remove it before compare
|
||||
size_t sz = strlen(expectedComputerName);
|
||||
if (sz > 0 && expectedComputerName[sz - 1] == '\n')
|
||||
{
|
||||
expectedComputerName[sz - 1] = '\0';
|
||||
}
|
||||
pclose(fPtr);
|
||||
// Compare canonical name to FQDN
|
||||
ASSERT_EQ(info->ai_canonname, actual);
|
||||
|
||||
ASSERT_STREQ(GetFullyQualifiedName(), expectedComputerName);
|
||||
freeaddrinfo(info);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue