Files
PowerShell/tests/test-getcomputername.cpp
T

134 lines
3.6 KiB
C++
Raw Normal View History

//! @file test-getcomputername.cpp
//! @author Aaron Katz <v-aakatz@microsoft.com>
//! @brief Unit tests for GetComputerNameW
#include <string>
2015-08-17 17:02:40 -07:00
#include <cstring>
#include <vector>
#include <unistd.h>
#include <gtest/gtest.h>
#include <unicode/unistr.h>
#include "getcomputername.h"
//! Test fixture for GetComputerNameTest
2015-08-13 10:53:09 -07:00
class GetComputerNameTest : public ::testing::Test
{
protected:
DWORD lpnSize;
std::vector<WCHAR_T> lpBuffer;
BOOL result;
std::string expectedComputerName;
DWORD expectedSize;
//Get expected result from using linux call
2015-08-13 10:53:09 -07:00
GetComputerNameTest()
2015-08-17 17:02:40 -07:00
{
expectedComputerName.resize(HOST_NAME_MAX);
BOOL ret = gethostname(&expectedComputerName[0], expectedComputerName.length());
EXPECT_EQ(0, ret);
2015-08-17 17:02:40 -07:00
expectedSize = std::strlen(expectedComputerName.c_str()) + 1;
expectedComputerName.resize(expectedSize - 1);
}
2015-08-17 17:02:40 -07:00
//! Invokes GetComputerNameW with lpnSize and lpBuffer, saves result.
//!
//! @param size Assigns to lpnSize and allocates lpBuffer with
//! size number of null characters.
2015-08-13 10:53:09 -07:00
void TestWithSize(DWORD size)
{
lpnSize = size;
lpBuffer.assign(lpnSize, 0);
result = GetComputerNameW(&lpBuffer[0], &lpnSize);
}
2015-08-13 10:53:09 -07:00
void TestSuccess()
{
SCOPED_TRACE("");
//! Returns TRUE on success.
EXPECT_EQ(TRUE, result);
//! Sets lpnSize to number of WCHARs including null.
ASSERT_EQ(expectedSize, lpnSize);
// Read lpBuffer into UnicodeString (without null)
const char* begin = reinterpret_cast<char*>(&lpBuffer[0]);
icu::UnicodeString computername16(begin, (lpnSize-1)*sizeof(UChar), "UTF-16LE");
// Convert to UTF-8 for comparison
std::string computername;
computername16.toUTF8String(computername);
2015-08-17 17:02:40 -07:00
ASSERT_EQ(expectedSize, computername.length() + 1);
//! Returned computername(after conversion) is what was expected.
EXPECT_EQ(expectedComputerName, computername);
}
2015-08-13 10:53:09 -07:00
void TestInvalidParameter()
{
SCOPED_TRACE("");
2015-08-17 17:02:40 -07:00
// returns FALSE on failure
EXPECT_EQ(FALSE, result);
// sets errno to ERROR_INVALID_PARAMETER when lpBuffer is null
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
}
2015-08-13 10:53:09 -07:00
void TestInsufficientBuffer()
{
SCOPED_TRACE("");
2015-08-17 17:02:40 -07:00
// returns FALSE on failure
EXPECT_EQ(FALSE, result);
// sets errno to ERROR_INSUFFICIENT_BUFFER
EXPECT_EQ(errno, ERROR_INSUFFICIENT_BUFFER);
// sets lpnSize to length of username + null
2015-08-17 17:02:40 -07:00
ASSERT_EQ(expectedSize, lpnSize);
}
};
2015-08-13 10:53:09 -07:00
TEST_F(GetComputerNameTest, BufferAsNullButNotBufferSize)
{
lpnSize = 1;
result = GetComputerNameW(NULL, &lpnSize);
TestInvalidParameter();
// does not reset lpnSize
EXPECT_EQ(1, lpnSize);
2015-07-30 12:47:10 -07:00
}
2015-08-13 10:53:09 -07:00
TEST_F(GetComputerNameTest, BufferSizeAsNullButNotBuffer)
{
lpBuffer.push_back('\0');
result = GetComputerNameW(&lpBuffer[0], NULL);
TestInvalidParameter();
}
2015-08-13 10:53:09 -07:00
TEST_F(GetComputerNameTest, BufferSizeAsZero)
{
TestWithSize(0);
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
}
2015-08-13 10:53:09 -07:00
TEST_F(GetComputerNameTest, BufferSizeAsUserNameMinusOne)
{
// the buffer is also too small
2015-08-17 17:02:40 -07:00
TestWithSize(expectedSize-1);
TestInsufficientBuffer();
}
2015-08-13 10:53:09 -07:00
TEST_F(GetComputerNameTest, BufferSizeAsUserNamePlusOne)
{
// the buffer is exactly big enough
2015-08-17 17:02:40 -07:00
TestWithSize(expectedSize+1);
TestSuccess();
}
2015-08-13 10:53:09 -07:00
TEST_F(GetComputerNameTest, BufferSizeAsLoginNameMax)
{
// the buffer larger than needed
TestWithSize(HOST_NAME_MAX);
TestSuccess();
}