2015-08-13 09:47:10 -07:00
|
|
|
//! @file test-getcomputername.cpp
|
2015-10-27 15:33:25 -07:00
|
|
|
//! @author George Fleming <v-geflem@microsoft.com>
|
|
|
|
|
//! @brief Unit tests for GetComputerName
|
2015-04-03 10:22:05 -07:00
|
|
|
|
2015-08-13 09:47:10 -07:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
#include "getcomputername.h"
|
|
|
|
|
|
|
|
|
|
//! Test fixture for GetComputerNameTest
|
2015-08-13 10:53:09 -07:00
|
|
|
class GetComputerNameTest : public ::testing::Test
|
|
|
|
|
{
|
2015-08-04 14:38:53 -07:00
|
|
|
};
|
2015-04-03 10:22:05 -07:00
|
|
|
|
2015-11-04 20:30:52 -08:00
|
|
|
TEST_F(GetComputerNameTest, Success)
|
2015-08-13 10:53:09 -07:00
|
|
|
{
|
2015-12-21 10:15:16 -08:00
|
|
|
char expectedComputerName[_POSIX_HOST_NAME_MAX];
|
2015-11-02 16:40:28 -08:00
|
|
|
|
2015-11-04 20:30:52 -08:00
|
|
|
// the gethostname system call gets the nodename from uname
|
|
|
|
|
FILE *fPtr = popen("uname -n", "r");
|
2015-11-02 16:40:28 -08:00
|
|
|
ASSERT_TRUE(fPtr != NULL);
|
|
|
|
|
|
|
|
|
|
char *linePtr = fgets(expectedComputerName, sizeof(expectedComputerName), fPtr);
|
|
|
|
|
ASSERT_TRUE(linePtr != NULL);
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
2015-10-27 10:48:31 -07:00
|
|
|
ASSERT_STREQ(GetComputerName(), expectedComputerName);
|
2015-07-30 12:47:10 -07:00
|
|
|
}
|