Added test for buffer not having enough space to write computer name

This commit is contained in:
Aaron 2015-07-30 13:47:31 -07:00
parent 385fb20cdf
commit da4d4d7a21
2 changed files with 7 additions and 4 deletions

View file

@ -5,8 +5,8 @@
BOOL GetComputerName(LPTSTR name, LPDWORD len) BOOL GetComputerName(LPTSTR name, LPDWORD len)
{ {
errno = 0; errno = 0;
size_t len2 = *len;
int host = gethostname(name, HOST_NAME_MAX); int host = gethostname(name, len2);
if(host == 0) if(host == 0)
{ {
return TRUE; return TRUE;

View file

@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "getcomputername.h" #include "getcomputername.h"
#include <iostream>
TEST(GetComputerName,simple) TEST(GetComputerName,simple)
{ {
@ -26,11 +27,13 @@ TEST(GetComputerName,simple)
ASSERT_EQ(hostnameString,hostnameStringTest); ASSERT_EQ(hostnameString,hostnameStringTest);
} }
TEST(GetComputerName,buffertosmall) TEST(GetComputerName,bufferttoosmall)
{ {
char hostname[HOST_NAME_MAX]; char hostname[HOST_NAME_MAX];
std::string hostnameFunctionTest; std::string hostnameFunctionTest;
DWORD hostSize = 0; DWORD hostSize = 0;
BOOL getComputerName = GetComputerName(&hostnameFunctionTest[0], &hostSize); BOOL getComputerName = GetComputerName(&hostnameFunctionTest[0], &hostSize);
ASSERT_TRUE(getComputerName != 0); ASSERT_TRUE(getComputerName == 0);
char buffer[ 256 ];
EXPECT_EQ(errno, ERROR_BUFFER_OVERFLOW);
} }