| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#ifndef MOCKCALLARGUMENT_H |
| 5 |
#define MOCKCALLARGUMENT_H |
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include "stdafx.h" |
| 10 |
#include "mockvalue.h" |
| 11 |
#include "mockcallargumentbase.h" |
| 12 |
#include "micromockexception.h" |
| 13 |
#include "stddef.h" |
| 14 |
|
| 15 |
template<typename T> |
| 16 |
class CMockCallArgument : |
| 17 |
public CMockValue<T>, public CMockCallArgumentBase |
| 18 |
{ |
| 19 |
public: |
| 20 |
CMockCallArgument(_In_ T argValue, _In_ bool IsIgnoreable = true) : |
| 21 |
CMockValue<T>(argValue), |
| 22 |
m_Ignored(false) |
| 23 |
{ |
| 24 |
m_IsIgnoreable = IsIgnoreable; |
| 25 |
} |
| 26 |
|
| 27 |
virtual ~CMockCallArgument() |
| 28 |
{ |
| 29 |
for (size_t i = 0; i < m_BufferValidations.size(); i++) |
| 30 |
{ |
| 31 |
void* buffer = m_BufferValidations[i].m_Buffer; |
| 32 |
if (buffer != NULL) |
| 33 |
{ |
| 34 |
free(buffer); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
for (size_t i = 0; i < m_CopyOutArgumentBuffers.size(); i++) |
| 39 |
{ |
| 40 |
void* buffer = m_CopyOutArgumentBuffers[i].m_Buffer; |
| 41 |
if (buffer != NULL) |
| 42 |
{ |
| 43 |
free(buffer); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// TODO: this should be fixed to be const |
| 49 |
bool operator==(_In_ CMockCallArgument<T>& rhs) |
| 50 |
{ |
| 51 |
bool result = true; |
| 52 |
|
| 53 |
if (m_BufferValidations.size() > 0 && |
| 54 |
rhs.m_BufferValidations.size() == 0) |
| 55 |
{ |
| 56 |
return rhs == *this; |
| 57 |
} |
| 58 |
|
| 59 |
if (rhs.m_BufferValidations.size() > 0) |
| 60 |
{ |
| 61 |
UINT8* argBufferValidationMask = rhs.CreateBufferValidationMask(); |
| 62 |
|
| 63 |
if (NULL != argBufferValidationMask) |
| 64 |
{ |
| 65 |
FormatArgumentBufferString(rhs.m_BufferValidations, *((UINT8**)&this->CMockValue<T>::m_Value)); |
| 66 |
rhs.FormatArgumentBufferString(rhs.m_BufferValidations, argBufferValidationMask); |
| 67 |
free(argBufferValidationMask); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_ALLOCATION_FAILURE, |
| 72 |
_T("Failed allocating buffer validation mask"))); |
| 73 |
} |
| 74 |
|
| 75 |
result = result && ValidateArgumentBuffer(rhs.m_BufferValidations); |
| 76 |
} |
| 77 |
|
| 78 |
if (result && |
| 79 |
!m_Ignored && |
| 80 |
!rhs.m_Ignored) |
| 81 |
{ |
| 82 |
// simply invoke the CMockValue == operator to take advantage of |
| 83 |
// any specializations defined in tests |
| 84 |
result = *((CMockValue<T>*)this) == (*(CMockValue<T>*)&rhs); |
| 85 |
} |
| 86 |
|
| 87 |
return result; |
| 88 |
} |
| 89 |
|
| 90 |
bool ValidateArgumentBuffer(_In_ const std::vector<BUFFER_ARGUMENT_DATA>& bufferValidations) const |
| 91 |
{ |
| 92 |
bool result = true; |
| 93 |
|
| 94 |
for (size_t i = 0; i < bufferValidations.size(); i++) |
| 95 |
{ |
| 96 |
UINT8* argBuffer = *((UINT8**)&this->CMockValue<T>::m_Value); |
| 97 |
if (NULL == argBuffer) |
| 98 |
{ |
| 99 |
result = false; |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
result &= (memcmp(argBuffer + bufferValidations[i].m_Offset, bufferValidations[i].m_Buffer, |
| 104 |
bufferValidations[i].m_ByteCount) == 0); |
| 105 |
} |
| 106 |
|
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
virtual std::tstring ToString() const |
| 111 |
{ |
| 112 |
if (m_ArgumentAsString.length() > 0) |
| 113 |
{ |
| 114 |
return m_ArgumentAsString; |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
return this->CMockValue<T>::ToString(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
virtual void CopyOutArgumentDataFrom(_In_ const CMockCallArgumentBase* sourceMockCallArgument) |
| 123 |
{ |
| 124 |
const CMockCallArgument<T>* argument = (const CMockCallArgument<T>*)(sourceMockCallArgument); |
| 125 |
if (NULL != argument) |
| 126 |
{ |
| 127 |
for (size_t i = 0; i < argument->m_CopyOutArgumentBuffers.size(); i++) |
| 128 |
{ |
| 129 |
UINT8* argBuffer = *((UINT8**)&this->m_OriginalValue); |
| 130 |
if (NULL != argBuffer) |
| 131 |
{ |
| 132 |
memmove(argBuffer + argument->m_CopyOutArgumentBuffers[i].m_Offset, |
| 133 |
argument->m_CopyOutArgumentBuffers[i].m_Buffer, |
| 134 |
argument->m_CopyOutArgumentBuffers[i].m_ByteCount); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
virtual bool EqualTo(_In_ const CMockCallArgumentBase* right) |
| 141 |
{ |
| 142 |
return (*this == *(CMockCallArgument<T>*)(const CMockCallArgument<T>*)(right)); |
| 143 |
} |
| 144 |
|
| 145 |
void AddBufferValidation(const void* expectedBuffer, _In_ size_t bytesToValidate, _In_ size_t byteOffset = 0) |
| 146 |
{ |
| 147 |
if ((NULL != expectedBuffer) && |
| 148 |
(bytesToValidate > 0)) |
| 149 |
{ |
| 150 |
for (size_t i = byteOffset; i < byteOffset + bytesToValidate; i++) |
| 151 |
{ |
| 152 |
UINT8 argBufferValidateByte = 0; |
| 153 |
if ((GetArgBufferValidationExpectedByte(i, &argBufferValidateByte) == true) && |
| 154 |
(argBufferValidateByte != ((UINT8*)expectedBuffer)[i - byteOffset])) |
| 155 |
{ |
| 156 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_VALIDATE_BUFFERS, |
| 157 |
_T("Validate argument buffer specified 2 times for the same byte"))); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
BUFFER_ARGUMENT_DATA argumentValidationData; |
| 162 |
|
| 163 |
argumentValidationData.m_Buffer = malloc(bytesToValidate); |
| 164 |
if (NULL != argumentValidationData.m_Buffer) |
| 165 |
{ |
| 166 |
(void)memcpy(argumentValidationData.m_Buffer, expectedBuffer, bytesToValidate); |
| 167 |
} |
| 168 |
|
| 169 |
argumentValidationData.m_ByteCount = bytesToValidate; |
| 170 |
argumentValidationData.m_Offset = byteOffset; |
| 171 |
|
| 172 |
m_BufferValidations.push_back(argumentValidationData); |
| 173 |
|
| 174 |
// ignore the argument |
| 175 |
SetIgnored(true); |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_VALIDATE_BUFFERS, |
| 180 |
_T("Invalid arguments for AddBufferValidation."))); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
virtual void AddCopyOutArgumentBuffer(const void* injectedBuffer, _In_ size_t bytesToCopy, _In_ size_t byteOffset = 0) |
| 185 |
{ |
| 186 |
BUFFER_ARGUMENT_DATA outArgumentCopyData; |
| 187 |
|
| 188 |
outArgumentCopyData.m_Buffer = malloc(bytesToCopy); |
| 189 |
if (NULL != outArgumentCopyData.m_Buffer) |
| 190 |
{ |
| 191 |
(void)memcpy(outArgumentCopyData.m_Buffer, injectedBuffer, bytesToCopy); |
| 192 |
} |
| 193 |
outArgumentCopyData.m_ByteCount = bytesToCopy; |
| 194 |
outArgumentCopyData.m_Offset = byteOffset; |
| 195 |
|
| 196 |
m_CopyOutArgumentBuffers.push_back(outArgumentCopyData); |
| 197 |
|
| 198 |
// ignore the argument |
| 199 |
SetIgnored(true); |
| 200 |
} |
| 201 |
|
| 202 |
virtual void SetIgnored(_In_ bool ignored) |
| 203 |
{ |
| 204 |
if (m_IsIgnoreable == true) |
| 205 |
{ |
| 206 |
m_Ignored = ignored; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
private: |
| 211 |
void FormatArgumentBufferString(_In_ std::vector<BUFFER_ARGUMENT_DATA>& bufferValidations, _In_ const UINT8* buffer) |
| 212 |
{ |
| 213 |
size_t pos = 0; |
| 214 |
std::tostringstream strStream; |
| 215 |
|
| 216 |
strStream << std::hex << std::uppercase; |
| 217 |
|
| 218 |
if (NULL != buffer) |
| 219 |
{ |
| 220 |
sort(bufferValidations.begin(), bufferValidations.end()); |
| 221 |
|
| 222 |
strStream << _T("["); |
| 223 |
|
| 224 |
for (size_t i = 0; i < bufferValidations.size(); i++) |
| 225 |
{ |
| 226 |
// fill with ".." |
| 227 |
while (pos < bufferValidations[i].m_Offset) |
| 228 |
{ |
| 229 |
if (pos > 0) |
| 230 |
{ |
| 231 |
strStream << _T(" "); |
| 232 |
} |
| 233 |
|
| 234 |
strStream << _T(".."); |
| 235 |
pos++; |
| 236 |
} |
| 237 |
|
| 238 |
// put the actual bytes in |
| 239 |
while (pos < bufferValidations[i].m_ByteCount + bufferValidations[i].m_Offset) |
| 240 |
{ |
| 241 |
if (pos > 0) |
| 242 |
{ |
| 243 |
strStream << _T(" "); |
| 244 |
} |
| 245 |
|
| 246 |
strStream << std::setfill(_T('0')) << std::setw(2) << (unsigned int)buffer[pos]; |
| 247 |
pos++; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
strStream << _T("]"); |
| 252 |
|
| 253 |
m_ArgumentAsString = strStream.str(); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_ARGUMENT, |
| 258 |
_T("NULL buffer argument attempted to be used for argument validation."))); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
bool GetArgBufferValidationExpectedByte(_In_ size_t pos, UINT8* buffer) const |
| 263 |
{ |
| 264 |
bool result = false; |
| 265 |
|
| 266 |
for (size_t i = 0; i < m_BufferValidations.size(); i++) |
| 267 |
{ |
| 268 |
if ((m_BufferValidations[i].m_Offset <= pos) && |
| 269 |
(pos < m_BufferValidations[i].m_Offset + m_BufferValidations[i].m_ByteCount)) |
| 270 |
{ |
| 271 |
*buffer = ((UINT8*)m_BufferValidations[i].m_Buffer)[pos - m_BufferValidations[i].m_Offset]; |
| 272 |
result = true; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
return result; |
| 277 |
} |
| 278 |
|
| 279 |
UINT8* CreateBufferValidationMask() |
| 280 |
{ |
| 281 |
UINT8* result = NULL; |
| 282 |
|
| 283 |
if (m_BufferValidations.size() > 0) |
| 284 |
{ |
| 285 |
size_t bufferSize; |
| 286 |
|
| 287 |
sort(m_BufferValidations.begin(), m_BufferValidations.end()); |
| 288 |
bufferSize = m_BufferValidations[m_BufferValidations.size() - 1].m_Offset + m_BufferValidations[m_BufferValidations.size() - 1].m_ByteCount; |
| 289 |
|
| 290 |
result = (UINT8*)malloc(bufferSize); |
| 291 |
if (NULL != result) |
| 292 |
{ |
| 293 |
for (size_t i = 0; i < m_BufferValidations.size(); i++) |
| 294 |
{ |
| 295 |
(void)memcpy(result + m_BufferValidations[i].m_Offset, |
| 296 |
m_BufferValidations[i].m_Buffer, m_BufferValidations[i].m_ByteCount); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return result; |
| 302 |
} |
| 303 |
|
| 304 |
std::vector<BUFFER_ARGUMENT_DATA> m_BufferValidations; |
| 305 |
std::vector<BUFFER_ARGUMENT_DATA> m_CopyOutArgumentBuffers; |
| 306 |
std::tstring m_ArgumentAsString; |
| 307 |
bool m_Ignored; |
| 308 |
bool m_IsIgnoreable; /*used for "time is never ignoreable"*/ |
| 309 |
}; |
| 310 |
|
| 311 |
#endif // MOCKCALLARGUMENT_H |