| 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 |
#ifdef __cplusplus |
| 5 |
#include <cstdlib> |
| 6 |
#include <cstddef> |
| 7 |
#else |
| 8 |
#include <stdlib.h> |
| 9 |
#include <stddef.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
#include "azure_macro_utils/macro_utils.h" |
| 13 |
#include "testrunnerswitcher.h" |
| 14 |
|
| 15 |
void* my_gballoc_malloc(size_t size) |
| 16 |
{ |
| 17 |
return malloc(size); |
| 18 |
} |
| 19 |
|
| 20 |
void* my_gballoc_calloc(size_t nmemb, size_t size) |
| 21 |
{ |
| 22 |
return calloc(nmemb, size); |
| 23 |
} |
| 24 |
|
| 25 |
void my_gballoc_free(void* ptr) |
| 26 |
{ |
| 27 |
free(ptr); |
| 28 |
} |
| 29 |
|
| 30 |
#define ENABLE_MOCKS |
| 31 |
#include "umock_c/umock_c.h" |
| 32 |
#include "azure_c_shared_utility/buffer_.h" |
| 33 |
#include "azure_c_shared_utility/gballoc.h" |
| 34 |
|
| 35 |
#undef ENABLE_MOCKS |
| 36 |
#include "azure_c_shared_utility/constbuffer.h" |
| 37 |
|
| 38 |
static TEST_MUTEX_HANDLE g_testByTest; |
| 39 |
|
| 40 |
static const char* buffer1 = "le buffer no 1"; |
| 41 |
static const char* buffer2 = NULL; |
| 42 |
static const char* buffer3 = "three"; |
| 43 |
|
| 44 |
#define BUFFER1_HANDLE (BUFFER_HANDLE)1 |
| 45 |
#define BUFFER1_u_char ((unsigned char*)buffer1) |
| 46 |
#define BUFFER1_length strlen(buffer1) |
| 47 |
|
| 48 |
#define BUFFER2_HANDLE (BUFFER_HANDLE)2 |
| 49 |
#define BUFFER2_u_char ((unsigned char*)buffer2) |
| 50 |
#define BUFFER2_length ((size_t)0) |
| 51 |
|
| 52 |
#define BUFFER3_HANDLE (BUFFER_HANDLE)3 |
| 53 |
#define BUFFER3_u_char ((unsigned char*)buffer3) |
| 54 |
#define BUFFER3_length ((size_t)0) |
| 55 |
|
| 56 |
unsigned char* my_BUFFER_u_char(BUFFER_HANDLE handle) |
| 57 |
{ |
| 58 |
unsigned char* result; |
| 59 |
if (handle == BUFFER1_HANDLE) |
| 60 |
{ |
| 61 |
result = BUFFER1_u_char; |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
result = NULL; |
| 66 |
ASSERT_FAIL("who am I?"); |
| 67 |
} |
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
static size_t my_BUFFER_length(BUFFER_HANDLE handle) |
| 72 |
{ |
| 73 |
size_t result; |
| 74 |
if (handle == BUFFER1_HANDLE) |
| 75 |
{ |
| 76 |
result = BUFFER1_length; |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
result = 0; |
| 81 |
ASSERT_FAIL("who am I?"); |
| 82 |
} |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
MOCK_FUNCTION_WITH_CODE(, void, test_free_func, void*, context) |
| 87 |
MOCK_FUNCTION_END() |
| 88 |
|
| 89 |
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES) |
| 90 |
|
| 91 |
static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code) |
| 92 |
{ |
| 93 |
ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code)); |
| 94 |
} |
| 95 |
|
| 96 |
BEGIN_TEST_SUITE(constbuffer_unittests) |
| 97 |
|
| 98 |
TEST_SUITE_INITIALIZE(setsBufferTempSize) |
| 99 |
{ |
| 100 |
g_testByTest = TEST_MUTEX_CREATE(); |
| 101 |
ASSERT_IS_NOT_NULL(g_testByTest); |
| 102 |
|
| 103 |
umock_c_init(on_umock_c_error); |
| 104 |
|
| 105 |
REGISTER_UMOCK_ALIAS_TYPE(BUFFER_HANDLE, void*); |
| 106 |
|
| 107 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc); |
| 108 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc); |
| 109 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free); |
| 110 |
REGISTER_GLOBAL_MOCK_HOOK(BUFFER_u_char, my_BUFFER_u_char); |
| 111 |
REGISTER_GLOBAL_MOCK_HOOK(BUFFER_length, my_BUFFER_length); |
| 112 |
} |
| 113 |
|
| 114 |
TEST_SUITE_CLEANUP(TestClassCleanup) |
| 115 |
{ |
| 116 |
umock_c_deinit(); |
| 117 |
|
| 118 |
TEST_MUTEX_DESTROY(g_testByTest); |
| 119 |
} |
| 120 |
|
| 121 |
TEST_FUNCTION_INITIALIZE(f) |
| 122 |
{ |
| 123 |
if (TEST_MUTEX_ACQUIRE(g_testByTest)) |
| 124 |
{ |
| 125 |
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework"); |
| 126 |
} |
| 127 |
|
| 128 |
umock_c_reset_all_calls(); |
| 129 |
} |
| 130 |
|
| 131 |
TEST_FUNCTION_CLEANUP(cleans) |
| 132 |
{ |
| 133 |
TEST_MUTEX_RELEASE(g_testByTest); |
| 134 |
} |
| 135 |
|
| 136 |
/* CONSTBUFFER_Create */ |
| 137 |
|
| 138 |
/*Tests_SRS_CONSTBUFFER_02_001: [If source is NULL and size is different than 0 then CONSTBUFFER_Create shall fail and return NULL.]*/ |
| 139 |
TEST_FUNCTION(CONSTBUFFER_Create_with_invalid_args_fails) |
| 140 |
{ |
| 141 |
///arrange |
| 142 |
|
| 143 |
///act |
| 144 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_Create(NULL, 1); |
| 145 |
|
| 146 |
///assert |
| 147 |
ASSERT_IS_NULL(handle); |
| 148 |
|
| 149 |
///cleanup |
| 150 |
} |
| 151 |
|
| 152 |
/*Tests_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/ |
| 153 |
/*Tests_SRS_CONSTBUFFER_02_004: [Otherwise CONSTBUFFER_Create shall return a non-NULL handle.]*/ |
| 154 |
TEST_FUNCTION(CONSTBUFFER_Create_succeeds) |
| 155 |
{ |
| 156 |
///arrange |
| 157 |
CONSTBUFFER_HANDLE handle; |
| 158 |
const CONSTBUFFER* content; |
| 159 |
|
| 160 |
///act |
| 161 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 162 |
.IgnoreAllArguments(); |
| 163 |
|
| 164 |
handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 165 |
|
| 166 |
///assert |
| 167 |
ASSERT_IS_NOT_NULL(handle); |
| 168 |
/*testing the "copy"*/ |
| 169 |
content = CONSTBUFFER_GetContent(handle); |
| 170 |
ASSERT_ARE_EQUAL(size_t, BUFFER1_length, content->size); |
| 171 |
ASSERT_ARE_EQUAL(int, 0, memcmp(BUFFER1_u_char, content->buffer, BUFFER1_length)); |
| 172 |
/*testing that it is a copy and not a pointer assignment*/ |
| 173 |
ASSERT_ARE_NOT_EQUAL(void_ptr, BUFFER1_u_char, content->buffer); |
| 174 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 175 |
|
| 176 |
///cleanup |
| 177 |
CONSTBUFFER_DecRef(handle); |
| 178 |
} |
| 179 |
|
| 180 |
/* CONSTBUFFER_CreateFromBuffer */ |
| 181 |
|
| 182 |
/*Tests_SRS_CONSTBUFFER_02_009: [Otherwise, CONSTBUFFER_CreateFromBuffer shall return a non-NULL handle.]*/ |
| 183 |
/*Tests_SRS_CONSTBUFFER_02_007: [Otherwise, CONSTBUFFER_CreateFromBuffer shall copy the content of buffer.]*/ |
| 184 |
TEST_FUNCTION(CONSTBUFFER_CreateFromBuffer_succeeds) |
| 185 |
{ |
| 186 |
///arrange |
| 187 |
CONSTBUFFER_HANDLE handle; |
| 188 |
const CONSTBUFFER* content; |
| 189 |
|
| 190 |
///act |
| 191 |
STRICT_EXPECTED_CALL(BUFFER_length(BUFFER1_HANDLE)); |
| 192 |
STRICT_EXPECTED_CALL(BUFFER_u_char(BUFFER1_HANDLE)); |
| 193 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 194 |
.IgnoreAllArguments(); |
| 195 |
|
| 196 |
handle = CONSTBUFFER_CreateFromBuffer(BUFFER1_HANDLE); |
| 197 |
|
| 198 |
///assert |
| 199 |
ASSERT_IS_NOT_NULL(handle); |
| 200 |
/*testing the "copy"*/ |
| 201 |
content = CONSTBUFFER_GetContent(handle); |
| 202 |
ASSERT_ARE_EQUAL(size_t, BUFFER1_length, content->size); |
| 203 |
ASSERT_ARE_EQUAL(int, 0, memcmp(BUFFER1_u_char, content->buffer, BUFFER1_length)); |
| 204 |
/*testing that it is a copy and not a pointer assignment*/ |
| 205 |
ASSERT_ARE_NOT_EQUAL(void_ptr, BUFFER1_u_char, content->buffer); |
| 206 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 207 |
|
| 208 |
///cleanup |
| 209 |
CONSTBUFFER_DecRef(handle); |
| 210 |
} |
| 211 |
|
| 212 |
/*Tests_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.]*/ |
| 213 |
TEST_FUNCTION(CONSTBUFFER_CreateFromBuffer_fails_when_malloc_fails) |
| 214 |
{ |
| 215 |
///arrange |
| 216 |
CONSTBUFFER_HANDLE handle; |
| 217 |
|
| 218 |
///act |
| 219 |
STRICT_EXPECTED_CALL(BUFFER_length(BUFFER1_HANDLE)); |
| 220 |
STRICT_EXPECTED_CALL(BUFFER_u_char(BUFFER1_HANDLE)); |
| 221 |
|
| 222 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 223 |
.IgnoreAllArguments() |
| 224 |
.SetReturn(NULL); |
| 225 |
|
| 226 |
|
| 227 |
handle = CONSTBUFFER_CreateFromBuffer(BUFFER1_HANDLE); |
| 228 |
|
| 229 |
///assert |
| 230 |
ASSERT_IS_NULL(handle); |
| 231 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 232 |
|
| 233 |
///cleanup |
| 234 |
CONSTBUFFER_DecRef(handle); |
| 235 |
} |
| 236 |
|
| 237 |
/*Tests_SRS_CONSTBUFFER_02_006: [If buffer is NULL then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.]*/ |
| 238 |
TEST_FUNCTION(CONSTBUFFER_CreateFromBuffer_with_NULL_fails) |
| 239 |
{ |
| 240 |
///arrange |
| 241 |
|
| 242 |
///act |
| 243 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_CreateFromBuffer(NULL); |
| 244 |
|
| 245 |
///assert |
| 246 |
ASSERT_IS_NULL(handle); |
| 247 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 248 |
|
| 249 |
///cleanup |
| 250 |
CONSTBUFFER_DecRef(handle); |
| 251 |
} |
| 252 |
|
| 253 |
/*Tests_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/ |
| 254 |
TEST_FUNCTION(CONSTBUFFER_Create_fails_when_malloc_fails) |
| 255 |
{ |
| 256 |
///arrange |
| 257 |
CONSTBUFFER_HANDLE handle; |
| 258 |
|
| 259 |
///act |
| 260 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 261 |
.IgnoreAllArguments() |
| 262 |
.SetReturn(NULL); |
| 263 |
|
| 264 |
handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 265 |
|
| 266 |
///assert |
| 267 |
ASSERT_IS_NULL(handle); |
| 268 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 269 |
|
| 270 |
///cleanup |
| 271 |
} |
| 272 |
|
| 273 |
/*Tests_SRS_CONSTBUFFER_02_005: [The non-NULL handle returned by CONSTBUFFER_Create shall have its ref count set to "1".]*/ |
| 274 |
/*Tests_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_DecRef shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/ |
| 275 |
TEST_FUNCTION(CONSTBUFFER_Create_is_ref_counted_1) |
| 276 |
{ |
| 277 |
///arrange |
| 278 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 279 |
umock_c_reset_all_calls(); |
| 280 |
///act |
| 281 |
|
| 282 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 283 |
|
| 284 |
CONSTBUFFER_DecRef(handle); |
| 285 |
|
| 286 |
///assert |
| 287 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 288 |
|
| 289 |
///cleanup |
| 290 |
} |
| 291 |
|
| 292 |
/*Tests_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/ |
| 293 |
TEST_FUNCTION(CONSTBUFFER_Create_from_0_size_succeeds_1) |
| 294 |
{ |
| 295 |
///arrange |
| 296 |
CONSTBUFFER_HANDLE handle; |
| 297 |
const CONSTBUFFER* content; |
| 298 |
|
| 299 |
///act |
| 300 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 301 |
.IgnoreAllArguments(); |
| 302 |
|
| 303 |
handle = CONSTBUFFER_Create(BUFFER2_u_char, BUFFER2_length); |
| 304 |
|
| 305 |
///assert |
| 306 |
ASSERT_IS_NOT_NULL(handle); |
| 307 |
/*testing the "copy"*/ |
| 308 |
content = CONSTBUFFER_GetContent(handle); |
| 309 |
ASSERT_ARE_EQUAL(size_t, BUFFER2_length, content->size); |
| 310 |
/*testing that it is a copy and not a pointer assignment*/ |
| 311 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 312 |
|
| 313 |
///cleanup |
| 314 |
CONSTBUFFER_DecRef(handle); |
| 315 |
} |
| 316 |
|
| 317 |
/*Tests_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/ |
| 318 |
/*Tests_SRS_CONSTBUFFER_02_009: [Otherwise, CONSTBUFFER_CreateFromBuffer shall return a non-NULL handle.]*/ |
| 319 |
TEST_FUNCTION(CONSTBUFFER_Create_from_0_size_succeeds_2) |
| 320 |
{ |
| 321 |
///arrange |
| 322 |
CONSTBUFFER_HANDLE handle; |
| 323 |
const CONSTBUFFER* content; |
| 324 |
|
| 325 |
///act |
| 326 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 327 |
.IgnoreAllArguments(); |
| 328 |
|
| 329 |
handle = CONSTBUFFER_Create(BUFFER3_u_char, BUFFER3_length); |
| 330 |
|
| 331 |
///assert |
| 332 |
ASSERT_IS_NOT_NULL(handle); |
| 333 |
/*testing the "copy"*/ |
| 334 |
content = CONSTBUFFER_GetContent(handle); |
| 335 |
ASSERT_ARE_EQUAL(size_t, BUFFER3_length, content->size); |
| 336 |
/*testing that it is a copy and not a pointer assignment*/ |
| 337 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 338 |
|
| 339 |
///cleanup |
| 340 |
CONSTBUFFER_DecRef(handle); |
| 341 |
} |
| 342 |
|
| 343 |
/* CONSTBUFFER_CreateWithMoveMemory */ |
| 344 |
|
| 345 |
/* Tests_SRS_CONSTBUFFER_01_001: [ If source is NULL and size is different than 0 then CONSTBUFFER_CreateWithMoveMemory shall fail and return NULL. ]*/ |
| 346 |
TEST_FUNCTION(CONSTBUFFER_CreateWithMoveMemory_with_invalid_args_fails) |
| 347 |
{ |
| 348 |
///arrange |
| 349 |
|
| 350 |
///act |
| 351 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_CreateWithMoveMemory(NULL, 1); |
| 352 |
|
| 353 |
///assert |
| 354 |
ASSERT_IS_NULL(handle); |
| 355 |
|
| 356 |
///cleanup |
| 357 |
} |
| 358 |
|
| 359 |
/* Tests_SRS_CONSTBUFFER_01_002: [ CONSTBUFFER_CreateWithMoveMemory shall store the source and size and return a non-NULL handle to the newly created const buffer. ]*/ |
| 360 |
TEST_FUNCTION(CONSTBUFFER_CreateWithMoveMemory_succeeds) |
| 361 |
{ |
| 362 |
///arrange |
| 363 |
CONSTBUFFER_HANDLE handle; |
| 364 |
const CONSTBUFFER* content; |
| 365 |
unsigned char* test_buffer = (unsigned char* )my_gballoc_malloc(2); |
| 366 |
test_buffer[0] = 42; |
| 367 |
test_buffer[1] = 43; |
| 368 |
|
| 369 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 370 |
.IgnoreAllArguments(); |
| 371 |
|
| 372 |
///act |
| 373 |
handle = CONSTBUFFER_CreateWithMoveMemory(test_buffer, 2); |
| 374 |
|
| 375 |
///assert |
| 376 |
ASSERT_IS_NOT_NULL(handle); |
| 377 |
/*testing the "storage"*/ |
| 378 |
content = CONSTBUFFER_GetContent(handle); |
| 379 |
ASSERT_ARE_EQUAL(size_t, 2, content->size); |
| 380 |
ASSERT_ARE_EQUAL(void_ptr, test_buffer, content->buffer, "same buffer should be returned"); |
| 381 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 382 |
|
| 383 |
///cleanup |
| 384 |
CONSTBUFFER_DecRef(handle); |
| 385 |
} |
| 386 |
|
| 387 |
/* Tests_SRS_CONSTBUFFER_01_002: [ CONSTBUFFER_CreateWithMoveMemory shall store the source and size and return a non-NULL handle to the newly created const buffer. ]*/ |
| 388 |
/* Tests_SRS_CONSTBUFFER_01_004: [ If source is non-NULL and size is 0, the source pointer shall be owned (and freed) by the newly created instance of const buffer. ]*/ |
| 389 |
TEST_FUNCTION(CONSTBUFFER_CreateWithMoveMemory_with_0_size_succeeds) |
| 390 |
{ |
| 391 |
///arrange |
| 392 |
CONSTBUFFER_HANDLE handle; |
| 393 |
const CONSTBUFFER* content; |
| 394 |
unsigned char* test_buffer = (unsigned char* )my_gballoc_malloc(2); |
| 395 |
test_buffer[0] = 42; |
| 396 |
test_buffer[1] = 43; |
| 397 |
|
| 398 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 399 |
.IgnoreAllArguments(); |
| 400 |
|
| 401 |
///act |
| 402 |
handle = CONSTBUFFER_CreateWithMoveMemory(test_buffer, 0); |
| 403 |
|
| 404 |
///assert |
| 405 |
ASSERT_IS_NOT_NULL(handle); |
| 406 |
/*testing the "storage"*/ |
| 407 |
content = CONSTBUFFER_GetContent(handle); |
| 408 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 409 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 410 |
|
| 411 |
///cleanup |
| 412 |
CONSTBUFFER_DecRef(handle); |
| 413 |
} |
| 414 |
|
| 415 |
/* Tests_SRS_CONSTBUFFER_01_002: [ CONSTBUFFER_CreateWithMoveMemory shall store the source and size and return a non-NULL handle to the newly created const buffer. ]*/ |
| 416 |
TEST_FUNCTION(CONSTBUFFER_CreateWithMoveMemory_with_NULL_source_and_0_size_succeeds) |
| 417 |
{ |
| 418 |
///arrange |
| 419 |
CONSTBUFFER_HANDLE handle; |
| 420 |
const CONSTBUFFER* content; |
| 421 |
|
| 422 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 423 |
.IgnoreAllArguments(); |
| 424 |
|
| 425 |
///act |
| 426 |
handle = CONSTBUFFER_CreateWithMoveMemory(NULL, 0); |
| 427 |
|
| 428 |
///assert |
| 429 |
ASSERT_IS_NOT_NULL(handle); |
| 430 |
/*testing the "storage"*/ |
| 431 |
content = CONSTBUFFER_GetContent(handle); |
| 432 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 433 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 434 |
|
| 435 |
///cleanup |
| 436 |
CONSTBUFFER_DecRef(handle); |
| 437 |
} |
| 438 |
|
| 439 |
/* Tests_SRS_CONSTBUFFER_01_005: [ If any error occurs, CONSTBUFFER_CreateWithMoveMemory shall fail and return NULL. ]*/ |
| 440 |
TEST_FUNCTION(when_malloc_fails_CONSTBUFFER_CreateWithMoveMemory_fails) |
| 441 |
{ |
| 442 |
///arrange |
| 443 |
CONSTBUFFER_HANDLE handle; |
| 444 |
unsigned char* test_buffer = (unsigned char* )my_gballoc_malloc(2); |
| 445 |
test_buffer[0] = 42; |
| 446 |
test_buffer[1] = 43; |
| 447 |
|
| 448 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 449 |
.IgnoreAllArguments() |
| 450 |
.SetReturn(NULL); |
| 451 |
|
| 452 |
///act |
| 453 |
handle = CONSTBUFFER_CreateWithMoveMemory(test_buffer, 2); |
| 454 |
|
| 455 |
///assert |
| 456 |
ASSERT_IS_NULL(handle); |
| 457 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 458 |
|
| 459 |
///cleanup |
| 460 |
free(test_buffer); |
| 461 |
} |
| 462 |
|
| 463 |
/* CONSTBUFFER_CreateWithCustomFree */ |
| 464 |
|
| 465 |
/* Tests_SRS_CONSTBUFFER_01_006: [ If source is NULL and size is different than 0 then CONSTBUFFER_CreateWithCustomFree shall fail and return NULL. ]*/ |
| 466 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_with_invalid_args_fails) |
| 467 |
{ |
| 468 |
///arrange |
| 469 |
|
| 470 |
///act |
| 471 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_CreateWithCustomFree(NULL, 1, test_free_func, (void*)0x4242); |
| 472 |
|
| 473 |
///assert |
| 474 |
ASSERT_IS_NULL(handle); |
| 475 |
} |
| 476 |
|
| 477 |
/* Tests_SRS_CONSTBUFFER_01_013: [ If customFreeFunc is NULL, CONSTBUFFER_CreateWithCustomFree shall fail and return NULL. ]*/ |
| 478 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_with_NULL_customFreeFunc_fails) |
| 479 |
{ |
| 480 |
///arrange |
| 481 |
CONSTBUFFER_HANDLE handle; |
| 482 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 483 |
test_buffer[0] = 42; |
| 484 |
test_buffer[1] = 43; |
| 485 |
|
| 486 |
///act |
| 487 |
handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 2, NULL, test_buffer); |
| 488 |
|
| 489 |
///assert |
| 490 |
ASSERT_IS_NULL(handle); |
| 491 |
|
| 492 |
/// cleanup |
| 493 |
my_gballoc_free(test_buffer); |
| 494 |
} |
| 495 |
|
| 496 |
/* Tests_SRS_CONSTBUFFER_01_008: [ CONSTBUFFER_CreateWithCustomFree shall store the source and size and return a non-NULL handle to the newly created const buffer. ]*/ |
| 497 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_succeeds) |
| 498 |
{ |
| 499 |
///arrange |
| 500 |
CONSTBUFFER_HANDLE handle; |
| 501 |
const CONSTBUFFER* content; |
| 502 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 503 |
test_buffer[0] = 42; |
| 504 |
test_buffer[1] = 43; |
| 505 |
|
| 506 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 507 |
.IgnoreAllArguments(); |
| 508 |
|
| 509 |
///act |
| 510 |
handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 2, free, test_buffer); |
| 511 |
|
| 512 |
///assert |
| 513 |
ASSERT_IS_NOT_NULL(handle); |
| 514 |
/*testing the "storage"*/ |
| 515 |
content = CONSTBUFFER_GetContent(handle); |
| 516 |
ASSERT_ARE_EQUAL(size_t, 2, content->size); |
| 517 |
ASSERT_ARE_EQUAL(void_ptr, test_buffer, content->buffer, "same buffer should be returned"); |
| 518 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 519 |
|
| 520 |
///cleanup |
| 521 |
CONSTBUFFER_DecRef(handle); |
| 522 |
} |
| 523 |
|
| 524 |
/* Tests_SRS_CONSTBUFFER_01_014: [ customFreeFuncContext shall be allowed to be NULL. ]*/ |
| 525 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_succeedswith_NULL_free_function_context) |
| 526 |
{ |
| 527 |
///arrange |
| 528 |
CONSTBUFFER_HANDLE handle; |
| 529 |
const CONSTBUFFER* content; |
| 530 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 531 |
test_buffer[0] = 42; |
| 532 |
test_buffer[1] = 43; |
| 533 |
|
| 534 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 535 |
.IgnoreAllArguments(); |
| 536 |
|
| 537 |
///act |
| 538 |
handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 2, test_free_func, NULL); |
| 539 |
|
| 540 |
///assert |
| 541 |
ASSERT_IS_NOT_NULL(handle); |
| 542 |
/*testing the "storage"*/ |
| 543 |
content = CONSTBUFFER_GetContent(handle); |
| 544 |
ASSERT_ARE_EQUAL(size_t, 2, content->size); |
| 545 |
ASSERT_ARE_EQUAL(void_ptr, test_buffer, content->buffer, "same buffer should be returned"); |
| 546 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 547 |
|
| 548 |
///cleanup |
| 549 |
CONSTBUFFER_DecRef(handle); |
| 550 |
my_gballoc_free(test_buffer); |
| 551 |
} |
| 552 |
|
| 553 |
/* Tests_SRS_CONSTBUFFER_01_008: [ CONSTBUFFER_CreateWithCustomFree shall store the source and size and return a non-NULL handle to the newly created const buffer. ]*/ |
| 554 |
/* Tests_SRS_CONSTBUFFER_01_007: [ If source is non-NULL and size is 0, the source pointer shall be owned (and freed) by the newly created instance of const buffer. ]*/ |
| 555 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_with_0_size_succeeds) |
| 556 |
{ |
| 557 |
///arrange |
| 558 |
CONSTBUFFER_HANDLE handle; |
| 559 |
const CONSTBUFFER* content; |
| 560 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 561 |
test_buffer[0] = 42; |
| 562 |
test_buffer[1] = 43; |
| 563 |
|
| 564 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 565 |
.IgnoreAllArguments(); |
| 566 |
|
| 567 |
///act |
| 568 |
handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 0, free, test_buffer); |
| 569 |
|
| 570 |
///assert |
| 571 |
ASSERT_IS_NOT_NULL(handle); |
| 572 |
/*testing the "storage"*/ |
| 573 |
content = CONSTBUFFER_GetContent(handle); |
| 574 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 575 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 576 |
|
| 577 |
///cleanup |
| 578 |
CONSTBUFFER_DecRef(handle); |
| 579 |
} |
| 580 |
|
| 581 |
/* Tests_SRS_CONSTBUFFER_01_008: [ CONSTBUFFER_CreateWithCustomFree shall store the source and size and return a non-NULL handle to the newly created const buffer. ]*/ |
| 582 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_with_NULL_source_and_0_size_succeeds) |
| 583 |
{ |
| 584 |
///arrange |
| 585 |
CONSTBUFFER_HANDLE handle; |
| 586 |
const CONSTBUFFER* content; |
| 587 |
|
| 588 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 589 |
.IgnoreAllArguments(); |
| 590 |
|
| 591 |
///act |
| 592 |
handle = CONSTBUFFER_CreateWithCustomFree(NULL, 0, free, NULL); |
| 593 |
|
| 594 |
///assert |
| 595 |
ASSERT_IS_NOT_NULL(handle); |
| 596 |
/*testing the "storage"*/ |
| 597 |
content = CONSTBUFFER_GetContent(handle); |
| 598 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 599 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 600 |
|
| 601 |
///cleanup |
| 602 |
CONSTBUFFER_DecRef(handle); |
| 603 |
} |
| 604 |
|
| 605 |
/* Tests_SRS_CONSTBUFFER_01_011: [ If any error occurs, CONSTBUFFER_CreateWithMoveMemory shall fail and return NULL. ]*/ |
| 606 |
TEST_FUNCTION(when_malloc_fails_CONSTBUFFER_CreateWithCustomFree_fails) |
| 607 |
{ |
| 608 |
///arrange |
| 609 |
CONSTBUFFER_HANDLE handle; |
| 610 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 611 |
test_buffer[0] = 42; |
| 612 |
test_buffer[1] = 43; |
| 613 |
|
| 614 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 615 |
.IgnoreAllArguments() |
| 616 |
.SetReturn(NULL); |
| 617 |
|
| 618 |
///act |
| 619 |
handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 2, free, test_buffer); |
| 620 |
|
| 621 |
///assert |
| 622 |
ASSERT_IS_NULL(handle); |
| 623 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 624 |
|
| 625 |
///cleanup |
| 626 |
free(test_buffer); |
| 627 |
} |
| 628 |
|
| 629 |
/* CONSTBUFFER_GetContent */ |
| 630 |
|
| 631 |
/*Tests_SRS_CONSTBUFFER_02_011: [If constbufferHandle is NULL then CONSTBUFFER_GetContent shall return NULL.]*/ |
| 632 |
TEST_FUNCTION(CONSTBUFFER_GetContent_with_NULL_returns_NULL) |
| 633 |
{ |
| 634 |
///arrange |
| 635 |
|
| 636 |
///act |
| 637 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(NULL); |
| 638 |
|
| 639 |
///assert |
| 640 |
ASSERT_IS_NULL(content); |
| 641 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 642 |
|
| 643 |
///cleanup |
| 644 |
} |
| 645 |
|
| 646 |
/*Tests_SRS_CONSTBUFFER_02_012: [Otherwise, CONSTBUFFER_GetContent shall return a const CONSTBUFFER* that matches byte by byte the original bytes used to created the const buffer and has the same length.]*/ |
| 647 |
TEST_FUNCTION(CONSTBUFFER_GetContent_succeeds_1) |
| 648 |
{ |
| 649 |
///arrange |
| 650 |
CONSTBUFFER_HANDLE handle; |
| 651 |
const CONSTBUFFER* content; |
| 652 |
|
| 653 |
handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 654 |
umock_c_reset_all_calls(); |
| 655 |
|
| 656 |
///act |
| 657 |
content = CONSTBUFFER_GetContent(handle); |
| 658 |
|
| 659 |
///assert |
| 660 |
ASSERT_IS_NOT_NULL(content); |
| 661 |
/*testing the "copy"*/ |
| 662 |
ASSERT_ARE_EQUAL(size_t, BUFFER1_length, content->size); |
| 663 |
ASSERT_ARE_EQUAL(int, 0, memcmp(BUFFER1_u_char, content->buffer, BUFFER1_length)); |
| 664 |
/*testing that it is a copy and not a pointer assignment*/ |
| 665 |
ASSERT_ARE_NOT_EQUAL(void_ptr, BUFFER1_u_char, content->buffer); |
| 666 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 667 |
|
| 668 |
///cleanup |
| 669 |
CONSTBUFFER_DecRef(handle); |
| 670 |
} |
| 671 |
|
| 672 |
/*Tests_SRS_CONSTBUFFER_02_012: [Otherwise, CONSTBUFFER_GetContent shall return a const CONSTBUFFER* that matches byte by byte the original bytes used to created the const buffer and has the same length.]*/ |
| 673 |
TEST_FUNCTION(CONSTBUFFER_GetContent_succeeds_2) |
| 674 |
{ |
| 675 |
///arrange |
| 676 |
CONSTBUFFER_HANDLE handle; |
| 677 |
const CONSTBUFFER* content; |
| 678 |
handle = CONSTBUFFER_Create(NULL, 0); |
| 679 |
umock_c_reset_all_calls(); |
| 680 |
|
| 681 |
///act |
| 682 |
content = CONSTBUFFER_GetContent(handle); |
| 683 |
|
| 684 |
///assert |
| 685 |
ASSERT_IS_NOT_NULL(content); |
| 686 |
/*testing the "copy"*/ |
| 687 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 688 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 689 |
|
| 690 |
///cleanup |
| 691 |
CONSTBUFFER_DecRef(handle); |
| 692 |
} |
| 693 |
|
| 694 |
/*Tests_SRS_CONSTBUFFER_02_025: [ If handle is NULL then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 695 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_with_handle_NULL_fails) |
| 696 |
{ |
| 697 |
///arrange |
| 698 |
|
| 699 |
///act |
| 700 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(NULL, 0, 0); |
| 701 |
|
| 702 |
///assert |
| 703 |
ASSERT_IS_NULL(result); |
| 704 |
} |
| 705 |
|
| 706 |
/*Tests_SRS_CONSTBUFFER_02_033: [ If offset is greater than handles's size then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 707 |
/*Tests_SRS_CONSTBUFFER_02_027: [ If offset + size exceed handles's size then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 708 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_with_offset_greater_than_handle_s_size_fails) |
| 709 |
{ |
| 710 |
///arrange |
| 711 |
CONSTBUFFER_HANDLE origin; |
| 712 |
const char source[] = "source"; |
| 713 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 714 |
.IgnoreAllArguments(); |
| 715 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 716 |
ASSERT_IS_NOT_NULL(origin); |
| 717 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 718 |
umock_c_reset_all_calls(); |
| 719 |
|
| 720 |
///act |
| 721 |
CONSTBUFFER_HANDLE result; |
| 722 |
result = CONSTBUFFER_CreateFromOffsetAndSize(origin, sizeof(source) + 1, 0); |
| 723 |
ASSERT_IS_NULL(result); |
| 724 |
result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 0, sizeof(source) + 1); |
| 725 |
ASSERT_IS_NULL(result); |
| 726 |
result = CONSTBUFFER_CreateFromOffsetAndSize(origin, sizeof(source), 1); |
| 727 |
ASSERT_IS_NULL(result); |
| 728 |
result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 1, sizeof(source)); |
| 729 |
ASSERT_IS_NULL(result); |
| 730 |
|
| 731 |
///cleanup |
| 732 |
CONSTBUFFER_DecRef(origin); |
| 733 |
} |
| 734 |
|
| 735 |
/*Tests_SRS_CONSTBUFFER_02_032: [ If there are any failures then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 736 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_with_offset_plus_size_equal_to_SIZE_MAX_fail) |
| 737 |
{ |
| 738 |
///arrange |
| 739 |
CONSTBUFFER_HANDLE origin; |
| 740 |
const char source[] = "source"; |
| 741 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 742 |
.IgnoreAllArguments(); |
| 743 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 744 |
ASSERT_IS_NOT_NULL(origin); |
| 745 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 746 |
umock_c_reset_all_calls(); |
| 747 |
|
| 748 |
///act |
| 749 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, sizeof(source) - 1, SIZE_MAX- sizeof(source) + 2); |
| 750 |
|
| 751 |
///assert |
| 752 |
ASSERT_IS_NULL(result); |
| 753 |
|
| 754 |
///cleanup |
| 755 |
CONSTBUFFER_DecRef(origin); |
| 756 |
} |
| 757 |
|
| 758 |
/*Tests_SRS_CONSTBUFFER_02_027: [ If offset + size exceed handles's size then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 759 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_with_offset_plus_size_exceed_handle_size_fails_2) |
| 760 |
{ |
| 761 |
///arrange |
| 762 |
CONSTBUFFER_HANDLE origin; |
| 763 |
const char source[] = "source"; |
| 764 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 765 |
.IgnoreAllArguments(); |
| 766 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 767 |
ASSERT_IS_NOT_NULL(origin); |
| 768 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 769 |
umock_c_reset_all_calls(); |
| 770 |
|
| 771 |
///act |
| 772 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, sizeof(source)-1, 2); |
| 773 |
|
| 774 |
///assert |
| 775 |
ASSERT_IS_NULL(result); |
| 776 |
|
| 777 |
///cleanup |
| 778 |
CONSTBUFFER_DecRef(origin); |
| 779 |
} |
| 780 |
|
| 781 |
/*Tests_SRS_CONSTBUFFER_02_028: [ CONSTBUFFER_CreateFromOffsetAndSize shall allocate memory for a new CONSTBUFFER_HANDLE's content. ]*/ |
| 782 |
/*Tests_SRS_CONSTBUFFER_02_029: [ CONSTBUFFER_CreateFromOffsetAndSize shall set the ref count of the newly created CONSTBUFFER_HANDLE to the initial value. ]*/ |
| 783 |
/*Tests_SRS_CONSTBUFFER_02_030: [ CONSTBUFFER_CreateFromOffsetAndSize shall increment the reference count of handle. ]*/ |
| 784 |
/*Tests_SRS_CONSTBUFFER_02_031: [ CONSTBUFFER_CreateFromOffsetAndSize shall succeed and return a non-NULL value. ]*/ |
| 785 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_succeeds_1) |
| 786 |
{ |
| 787 |
///arrange |
| 788 |
CONSTBUFFER_HANDLE origin; |
| 789 |
const char source[] = "source"; |
| 790 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 791 |
.IgnoreAllArguments(); |
| 792 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 793 |
ASSERT_IS_NOT_NULL(origin); |
| 794 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 795 |
umock_c_reset_all_calls(); |
| 796 |
|
| 797 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 798 |
.IgnoreAllArguments(); |
| 799 |
|
| 800 |
///act |
| 801 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 0, sizeof(source)); |
| 802 |
|
| 803 |
///assert |
| 804 |
ASSERT_IS_NOT_NULL(result); |
| 805 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 806 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(result); |
| 807 |
ASSERT_ARE_EQUAL(size_t, sizeof(source), content->size); |
| 808 |
ASSERT_IS_TRUE(memcmp(content->buffer, source, sizeof(source)) == 0); |
| 809 |
|
| 810 |
///cleanup |
| 811 |
CONSTBUFFER_DecRef(origin); |
| 812 |
CONSTBUFFER_DecRef(result); |
| 813 |
} |
| 814 |
|
| 815 |
/*Tests_SRS_CONSTBUFFER_02_028: [ CONSTBUFFER_CreateFromOffsetAndSize shall allocate memory for a new CONSTBUFFER_HANDLE's content. ]*/ |
| 816 |
/*Tests_SRS_CONSTBUFFER_02_029: [ CONSTBUFFER_CreateFromOffsetAndSize shall set the ref count of the newly created CONSTBUFFER_HANDLE to the initial value. ]*/ |
| 817 |
/*Tests_SRS_CONSTBUFFER_02_030: [ CONSTBUFFER_CreateFromOffsetAndSize shall increment the reference count of handle. ]*/ |
| 818 |
/*Tests_SRS_CONSTBUFFER_02_031: [ CONSTBUFFER_CreateFromOffsetAndSize shall succeed and return a non-NULL value. ]*/ |
| 819 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_succeeds_2) |
| 820 |
{ |
| 821 |
///arrange |
| 822 |
CONSTBUFFER_HANDLE origin; |
| 823 |
const char source[] = "source"; |
| 824 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 825 |
.IgnoreAllArguments(); |
| 826 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 827 |
ASSERT_IS_NOT_NULL(origin); |
| 828 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 829 |
umock_c_reset_all_calls(); |
| 830 |
|
| 831 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 832 |
.IgnoreAllArguments(); |
| 833 |
|
| 834 |
///act |
| 835 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 0, 0); |
| 836 |
|
| 837 |
///assert |
| 838 |
ASSERT_IS_NOT_NULL(result); |
| 839 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 840 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(result); |
| 841 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 842 |
|
| 843 |
///cleanup |
| 844 |
CONSTBUFFER_DecRef(origin); |
| 845 |
CONSTBUFFER_DecRef(result); |
| 846 |
} |
| 847 |
|
| 848 |
/*Tests_SRS_CONSTBUFFER_02_028: [ CONSTBUFFER_CreateFromOffsetAndSize shall allocate memory for a new CONSTBUFFER_HANDLE's content. ]*/ |
| 849 |
/*Tests_SRS_CONSTBUFFER_02_029: [ CONSTBUFFER_CreateFromOffsetAndSize shall set the ref count of the newly created CONSTBUFFER_HANDLE to the initial value. ]*/ |
| 850 |
/*Tests_SRS_CONSTBUFFER_02_030: [ CONSTBUFFER_CreateFromOffsetAndSize shall increment the reference count of handle. ]*/ |
| 851 |
/*Tests_SRS_CONSTBUFFER_02_031: [ CONSTBUFFER_CreateFromOffsetAndSize shall succeed and return a non-NULL value. ]*/ |
| 852 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_succeeds_3) |
| 853 |
{ |
| 854 |
///arrange |
| 855 |
CONSTBUFFER_HANDLE origin; |
| 856 |
const char source[] = "source"; |
| 857 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 858 |
.IgnoreAllArguments(); |
| 859 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 860 |
ASSERT_IS_NOT_NULL(origin); |
| 861 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 862 |
umock_c_reset_all_calls(); |
| 863 |
|
| 864 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 865 |
.IgnoreAllArguments(); |
| 866 |
|
| 867 |
///act |
| 868 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, sizeof(source) - 1, 1); |
| 869 |
|
| 870 |
///assert |
| 871 |
ASSERT_IS_NOT_NULL(result); |
| 872 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 873 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(result); |
| 874 |
ASSERT_ARE_EQUAL(size_t, 1, content->size); |
| 875 |
|
| 876 |
///cleanup |
| 877 |
CONSTBUFFER_DecRef(origin); |
| 878 |
CONSTBUFFER_DecRef(result); |
| 879 |
} |
| 880 |
|
| 881 |
/*Tests_SRS_CONSTBUFFER_02_028: [ CONSTBUFFER_CreateFromOffsetAndSize shall allocate memory for a new CONSTBUFFER_HANDLE's content. ]*/ |
| 882 |
/*Tests_SRS_CONSTBUFFER_02_029: [ CONSTBUFFER_CreateFromOffsetAndSize shall set the ref count of the newly created CONSTBUFFER_HANDLE to the initial value. ]*/ |
| 883 |
/*Tests_SRS_CONSTBUFFER_02_030: [ CONSTBUFFER_CreateFromOffsetAndSize shall increment the reference count of handle. ]*/ |
| 884 |
/*Tests_SRS_CONSTBUFFER_02_031: [ CONSTBUFFER_CreateFromOffsetAndSize shall succeed and return a non-NULL value. ]*/ |
| 885 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_succeeds_4) |
| 886 |
{ |
| 887 |
///arrange |
| 888 |
CONSTBUFFER_HANDLE origin; |
| 889 |
const char source[] = "source"; |
| 890 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 891 |
.IgnoreAllArguments(); |
| 892 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 893 |
ASSERT_IS_NOT_NULL(origin); |
| 894 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 895 |
umock_c_reset_all_calls(); |
| 896 |
|
| 897 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 898 |
.IgnoreAllArguments(); |
| 899 |
|
| 900 |
///act |
| 901 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 1, sizeof(source) - 1); |
| 902 |
|
| 903 |
///assert |
| 904 |
ASSERT_IS_NOT_NULL(result); |
| 905 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 906 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(result); |
| 907 |
ASSERT_ARE_EQUAL(size_t, sizeof(source)-1, content->size); |
| 908 |
ASSERT_IS_TRUE(memcmp(content->buffer, source+1, sizeof(source)-1) == 0); |
| 909 |
|
| 910 |
///cleanup |
| 911 |
CONSTBUFFER_DecRef(origin); |
| 912 |
CONSTBUFFER_DecRef(result); |
| 913 |
} |
| 914 |
|
| 915 |
/*Tests_SRS_CONSTBUFFER_02_028: [ CONSTBUFFER_CreateFromOffsetAndSize shall allocate memory for a new CONSTBUFFER_HANDLE's content. ]*/ |
| 916 |
/*Tests_SRS_CONSTBUFFER_02_029: [ CONSTBUFFER_CreateFromOffsetAndSize shall set the ref count of the newly created CONSTBUFFER_HANDLE to the initial value. ]*/ |
| 917 |
/*Tests_SRS_CONSTBUFFER_02_030: [ CONSTBUFFER_CreateFromOffsetAndSize shall increment the reference count of handle. ]*/ |
| 918 |
/*Tests_SRS_CONSTBUFFER_02_031: [ CONSTBUFFER_CreateFromOffsetAndSize shall succeed and return a non-NULL value. ]*/ |
| 919 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_succeeds_5) |
| 920 |
{ |
| 921 |
///arrange |
| 922 |
CONSTBUFFER_HANDLE origin; |
| 923 |
const char source[] = "source"; |
| 924 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 925 |
.IgnoreAllArguments(); |
| 926 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 927 |
ASSERT_IS_NOT_NULL(origin); |
| 928 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 929 |
umock_c_reset_all_calls(); |
| 930 |
|
| 931 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 932 |
.IgnoreAllArguments(); |
| 933 |
|
| 934 |
///act |
| 935 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, sizeof(source), 0); |
| 936 |
|
| 937 |
///assert |
| 938 |
ASSERT_IS_NOT_NULL(result); |
| 939 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 940 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(result); |
| 941 |
ASSERT_ARE_EQUAL(size_t, 0, content->size); |
| 942 |
|
| 943 |
///cleanup |
| 944 |
CONSTBUFFER_DecRef(origin); |
| 945 |
CONSTBUFFER_DecRef(result); |
| 946 |
} |
| 947 |
|
| 948 |
/*Tests_SRS_CONSTBUFFER_02_032: [ If there are any failures then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 949 |
TEST_FUNCTION(CONSTBUFFER_CreateFromOffsetAndSize_when_malloc_fails_it_fails) |
| 950 |
{ |
| 951 |
///arrange |
| 952 |
CONSTBUFFER_HANDLE origin; |
| 953 |
const char source[] = "source"; |
| 954 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 955 |
.IgnoreAllArguments(); |
| 956 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 957 |
ASSERT_IS_NOT_NULL(origin); |
| 958 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 959 |
umock_c_reset_all_calls(); |
| 960 |
|
| 961 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 962 |
.IgnoreAllArguments() |
| 963 |
.SetReturn(NULL); |
| 964 |
|
| 965 |
///act |
| 966 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 1, sizeof(source) - 1); |
| 967 |
|
| 968 |
///assert |
| 969 |
ASSERT_IS_NULL(result); |
| 970 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 971 |
|
| 972 |
///cleanup |
| 973 |
CONSTBUFFER_DecRef(origin); |
| 974 |
} |
| 975 |
|
| 976 |
/*Tests_SRS_CONSTBUFFER_02_024: [ If the constbufferHandle was created by calling CONSTBUFFER_CreateFromOffsetAndSize then CONSTBUFFER_DecRef shall decrement the ref count of the original handle passed to CONSTBUFFER_CreateFromOffsetAndSize. ]*/ |
| 977 |
/*Tests_SRS_CONSTBUFFER_02_032: [ If there are any failures then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 978 |
TEST_FUNCTION(CONSTBUFFER_DecRef_for_CONSTBUFFER_CreateFromOffsetAndSize_succeeds) |
| 979 |
{ |
| 980 |
///arrange |
| 981 |
CONSTBUFFER_HANDLE origin; |
| 982 |
const char source[] = "source"; |
| 983 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 984 |
.IgnoreAllArguments(); |
| 985 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 986 |
ASSERT_IS_NOT_NULL(origin); |
| 987 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 988 |
umock_c_reset_all_calls(); |
| 989 |
|
| 990 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 991 |
.IgnoreAllArguments(); |
| 992 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 1, sizeof(source) - 1); |
| 993 |
ASSERT_IS_NOT_NULL(result); |
| 994 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 995 |
umock_c_reset_all_calls(); |
| 996 |
|
| 997 |
STRICT_EXPECTED_CALL(gballoc_free(result)); |
| 998 |
|
| 999 |
///act |
| 1000 |
CONSTBUFFER_DecRef(result); |
| 1001 |
|
| 1002 |
///assert - origin should pretty much be still accesible |
| 1003 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1004 |
|
| 1005 |
const CONSTBUFFER* content = CONSTBUFFER_GetContent(origin); |
| 1006 |
ASSERT_ARE_EQUAL(size_t, sizeof(source), content->size); |
| 1007 |
ASSERT_IS_TRUE(memcmp(content->buffer, source, sizeof(source)) == 0); |
| 1008 |
|
| 1009 |
///cleanup |
| 1010 |
CONSTBUFFER_DecRef(origin); |
| 1011 |
} |
| 1012 |
|
| 1013 |
/*Tests_SRS_CONSTBUFFER_02_024: [ If the constbufferHandle was created by calling CONSTBUFFER_CreateFromOffsetAndSize then CONSTBUFFER_DecRef shall decrement the ref count of the original handle passed to CONSTBUFFER_CreateFromOffsetAndSize. ]*/ |
| 1014 |
/*Tests_SRS_CONSTBUFFER_02_032: [ If there are any failures then CONSTBUFFER_CreateFromOffsetAndSize shall fail and return NULL. ]*/ |
| 1015 |
TEST_FUNCTION(CONSTBUFFER_DecRef_for_CONSTBUFFER_CreateFromOffsetAndSize_succeeds_2) |
| 1016 |
{ |
| 1017 |
///arrange |
| 1018 |
CONSTBUFFER_HANDLE origin; |
| 1019 |
const char source[] = "source"; |
| 1020 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1021 |
.IgnoreAllArguments(); |
| 1022 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 1023 |
ASSERT_IS_NOT_NULL(origin); |
| 1024 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1025 |
umock_c_reset_all_calls(); |
| 1026 |
|
| 1027 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1028 |
.IgnoreAllArguments(); |
| 1029 |
CONSTBUFFER_HANDLE result = CONSTBUFFER_CreateFromOffsetAndSize(origin, 1, sizeof(source) - 1); |
| 1030 |
ASSERT_IS_NOT_NULL(result); |
| 1031 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1032 |
umock_c_reset_all_calls(); |
| 1033 |
|
| 1034 |
/*note: no expected calls - after the below call both buffers are at ref count == 1*/ |
| 1035 |
|
| 1036 |
///act |
| 1037 |
CONSTBUFFER_DecRef(origin); |
| 1038 |
|
| 1039 |
///assert - origin should pretty much be still accesible |
| 1040 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1041 |
|
| 1042 |
const CONSTBUFFER * content = CONSTBUFFER_GetContent(origin); |
| 1043 |
ASSERT_ARE_EQUAL(size_t, sizeof(source), content->size); |
| 1044 |
ASSERT_IS_TRUE(memcmp(content->buffer, source, sizeof(source)) == 0); |
| 1045 |
|
| 1046 |
const CONSTBUFFER* contentResult = CONSTBUFFER_GetContent(result); |
| 1047 |
ASSERT_ARE_EQUAL(size_t, sizeof(source)-1, contentResult->size); |
| 1048 |
ASSERT_IS_TRUE(memcmp(contentResult->buffer, source+ 1, sizeof(source)-1) == 0); |
| 1049 |
|
| 1050 |
///cleanup |
| 1051 |
CONSTBUFFER_DecRef(result); |
| 1052 |
} |
| 1053 |
|
| 1054 |
/*Tests_SRS_CONSTBUFFER_02_024: [ If the constbufferHandle was created by calling CONSTBUFFER_CreateFromOffsetAndSize then CONSTBUFFER_DecRef shall decrement the ref count of the original handle passed to CONSTBUFFER_CreateFromOffsetAndSize. ]*/ |
| 1055 |
TEST_FUNCTION(CONSTBUFFER_DecRef_for_CONSTBUFFER_CreateFromOffsetAndSize_succeeds_3) |
| 1056 |
{ |
| 1057 |
///arrange |
| 1058 |
CONSTBUFFER_HANDLE origin; |
| 1059 |
const char source[] = "source"; |
| 1060 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1061 |
.IgnoreAllArguments(); |
| 1062 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 1063 |
ASSERT_IS_NOT_NULL(origin); |
| 1064 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1065 |
umock_c_reset_all_calls(); |
| 1066 |
|
| 1067 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1068 |
.IgnoreAllArguments(); |
| 1069 |
CONSTBUFFER_HANDLE result1 = CONSTBUFFER_CreateFromOffsetAndSize(origin, 0, 2); |
| 1070 |
ASSERT_IS_NOT_NULL(result1); |
| 1071 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1072 |
umock_c_reset_all_calls(); |
| 1073 |
|
| 1074 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1075 |
.IgnoreAllArguments(); |
| 1076 |
CONSTBUFFER_HANDLE result2 = CONSTBUFFER_CreateFromOffsetAndSize(result1, 1, 1); |
| 1077 |
ASSERT_IS_NOT_NULL(result2); |
| 1078 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1079 |
umock_c_reset_all_calls(); |
| 1080 |
|
| 1081 |
CONSTBUFFER_DecRef(origin); |
| 1082 |
umock_c_reset_all_calls(); |
| 1083 |
|
| 1084 |
/*note: no expected calls - after the below call all buffers are at ref count == 1*/ |
| 1085 |
|
| 1086 |
///act |
| 1087 |
CONSTBUFFER_DecRef(result1); /*at this time result 2 has a ref to result1, which has a ref to origin. Nothing is freed*/ |
| 1088 |
|
| 1089 |
///assert |
| 1090 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1091 |
|
| 1092 |
///cleanup |
| 1093 |
CONSTBUFFER_DecRef(result2); /*triggers the release of result1, which triggers the release of origin*/ |
| 1094 |
} |
| 1095 |
|
| 1096 |
/*Tests_SRS_CONSTBUFFER_02_024: [ If the constbufferHandle was created by calling CONSTBUFFER_CreateFromOffsetAndSize then CONSTBUFFER_DecRef shall decrement the ref count of the original handle passed to CONSTBUFFER_CreateFromOffsetAndSize. ]*/ |
| 1097 |
TEST_FUNCTION(CONSTBUFFER_DecRef_for_CONSTBUFFER_CreateFromOffsetAndSize_succeeds_4) |
| 1098 |
{ |
| 1099 |
///arrange |
| 1100 |
CONSTBUFFER_HANDLE origin; |
| 1101 |
const char source[] = "source"; |
| 1102 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1103 |
.IgnoreAllArguments(); |
| 1104 |
origin = CONSTBUFFER_Create((const unsigned char*)source, sizeof(source)); |
| 1105 |
ASSERT_IS_NOT_NULL(origin); |
| 1106 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1107 |
umock_c_reset_all_calls(); |
| 1108 |
|
| 1109 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1110 |
.IgnoreAllArguments(); |
| 1111 |
CONSTBUFFER_HANDLE result1 = CONSTBUFFER_CreateFromOffsetAndSize(origin, 0, 2); |
| 1112 |
ASSERT_IS_NOT_NULL(result1); |
| 1113 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1114 |
umock_c_reset_all_calls(); |
| 1115 |
|
| 1116 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 1117 |
.IgnoreAllArguments(); |
| 1118 |
CONSTBUFFER_HANDLE result2 = CONSTBUFFER_CreateFromOffsetAndSize(result1, 1, 1); |
| 1119 |
ASSERT_IS_NOT_NULL(result2); |
| 1120 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1121 |
umock_c_reset_all_calls(); |
| 1122 |
|
| 1123 |
CONSTBUFFER_DecRef(origin); |
| 1124 |
CONSTBUFFER_DecRef(result1); /*at this time result 2 has a ref to result1, which has a ref to origin. Nothing is freed*/ |
| 1125 |
umock_c_reset_all_calls(); |
| 1126 |
|
| 1127 |
/*note: no expected calls - after the below call all buffers are at ref count == 1*/ |
| 1128 |
|
| 1129 |
STRICT_EXPECTED_CALL(gballoc_free(origin)); |
| 1130 |
STRICT_EXPECTED_CALL(gballoc_free(result1)); |
| 1131 |
STRICT_EXPECTED_CALL(gballoc_free(result2)); |
| 1132 |
|
| 1133 |
///act |
| 1134 |
CONSTBUFFER_DecRef(result2); /*triggers the release of result1, which triggers the release of origin*/ |
| 1135 |
|
| 1136 |
///assert |
| 1137 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1138 |
|
| 1139 |
///cleanup - nothing left |
| 1140 |
|
| 1141 |
} |
| 1142 |
|
| 1143 |
/* CONSTBUFFER_IncRef */ |
| 1144 |
|
| 1145 |
/*Tests_SRS_CONSTBUFFER_02_013: [If constbufferHandle is NULL then CONSTBUFFER_IncRef shall return.]*/ |
| 1146 |
TEST_FUNCTION(CONSTBUFFER_IncRef_with_NULL_returns_NULL) |
| 1147 |
{ |
| 1148 |
///arrange |
| 1149 |
|
| 1150 |
///act |
| 1151 |
CONSTBUFFER_IncRef(NULL); |
| 1152 |
|
| 1153 |
///assert |
| 1154 |
} |
| 1155 |
|
| 1156 |
/*Tests_SRS_CONSTBUFFER_02_014: [Otherwise, CONSTBUFFER_IncRef shall increment the reference count.]*/ |
| 1157 |
TEST_FUNCTION(CONSTBUFFER_IncRef_increments_ref_count_1) |
| 1158 |
{ |
| 1159 |
///arrange |
| 1160 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 1161 |
umock_c_reset_all_calls(); |
| 1162 |
|
| 1163 |
///act |
| 1164 |
CONSTBUFFER_IncRef(handle); |
| 1165 |
|
| 1166 |
///assert |
| 1167 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1168 |
|
| 1169 |
///cleanup |
| 1170 |
CONSTBUFFER_DecRef(handle); |
| 1171 |
CONSTBUFFER_DecRef(handle); |
| 1172 |
} |
| 1173 |
|
| 1174 |
/*Tests_SRS_CONSTBUFFER_02_014: [Otherwise, CONSTBUFFER_IncRef shall increment the reference count and return constbufferHandle.]*/ |
| 1175 |
/*Tests_SRS_CONSTBUFFER_02_016: [Otherwise, CONSTBUFFER_DecRef shall decrement the refcount on the constbufferHandle handle.]*/ |
| 1176 |
/*Tests_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_DecRef shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/ |
| 1177 |
TEST_FUNCTION(CONSTBUFFER_IncRef_increments_ref_count_2) |
| 1178 |
{ |
| 1179 |
///arrange |
| 1180 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 1181 |
CONSTBUFFER_IncRef(handle); |
| 1182 |
umock_c_reset_all_calls(); |
| 1183 |
|
| 1184 |
///act |
| 1185 |
CONSTBUFFER_DecRef(handle); /*only a dec_Ref is expected here, so no effects*/ |
| 1186 |
|
| 1187 |
///assert |
| 1188 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1189 |
|
| 1190 |
///cleanup |
| 1191 |
CONSTBUFFER_DecRef(handle); |
| 1192 |
} |
| 1193 |
|
| 1194 |
/*Tests_SRS_CONSTBUFFER_02_014: [Otherwise, CONSTBUFFER_IncRef shall increment the reference count and return constbufferHandle.]*/ |
| 1195 |
/*Tests_SRS_CONSTBUFFER_02_016: [Otherwise, CONSTBUFFER_DecRef shall decrement the refcount on the constbufferHandle handle.]*/ |
| 1196 |
/*Tests_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_DecRef shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/ |
| 1197 |
TEST_FUNCTION(CONSTBUFFER_IncRef_increments_ref_count_3) |
| 1198 |
{ |
| 1199 |
///arrange |
| 1200 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_Create(BUFFER1_u_char, BUFFER1_length); |
| 1201 |
CONSTBUFFER_IncRef(handle); |
| 1202 |
CONSTBUFFER_DecRef(handle); /*only a dec_Ref is expected here, so no effects*/ |
| 1203 |
umock_c_reset_all_calls(); |
| 1204 |
|
| 1205 |
///act |
| 1206 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1207 |
CONSTBUFFER_DecRef(handle); |
| 1208 |
|
| 1209 |
///assert |
| 1210 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1211 |
|
| 1212 |
///cleanup |
| 1213 |
} |
| 1214 |
|
| 1215 |
/* CONSTBUFFER_DecRef */ |
| 1216 |
|
| 1217 |
/*Tests_SRS_CONSTBUFFER_02_015: [If constbufferHandle is NULL then CONSTBUFFER_DecRef shall do nothing.]*/ |
| 1218 |
TEST_FUNCTION(CONSTBUFFER_DecRef_with_NULL_argument_does_nothing) |
| 1219 |
{ |
| 1220 |
///arrange |
| 1221 |
|
| 1222 |
///act |
| 1223 |
CONSTBUFFER_DecRef(NULL); |
| 1224 |
|
| 1225 |
///assert |
| 1226 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1227 |
|
| 1228 |
///cleanup |
| 1229 |
} |
| 1230 |
|
| 1231 |
/*Tests_SRS_CONSTBUFFER_02_010: [The non-NULL handle returned by CONSTBUFFER_CreateFromBuffer shall have its ref count set to "1".]*/ |
| 1232 |
/*Tests_SRS_CONSTBUFFER_02_005: [The non-NULL handle returned by CONSTBUFFER_Create shall have its ref count set to "1".]*/ |
| 1233 |
/*Tests_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_DecRef shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/ |
| 1234 |
TEST_FUNCTION(CONSTBUFFER_CreateFromBuffer_is_ref_counted_1) |
| 1235 |
{ |
| 1236 |
///arrange |
| 1237 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_CreateFromBuffer(BUFFER1_HANDLE); |
| 1238 |
umock_c_reset_all_calls(); |
| 1239 |
///act |
| 1240 |
|
| 1241 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1242 |
|
| 1243 |
CONSTBUFFER_DecRef(handle); |
| 1244 |
|
| 1245 |
///assert |
| 1246 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1247 |
|
| 1248 |
///cleanup |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* Tests_SRS_CONSTBUFFER_01_010: [ The non-NULL handle returned by CONSTBUFFER_CreateWithCustomFree shall have its ref count set to 1. ]*/ |
| 1252 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_is_ref_counted_1) |
| 1253 |
{ |
| 1254 |
///arrange |
| 1255 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 1256 |
test_buffer[0] = 42; |
| 1257 |
test_buffer[1] = 43; |
| 1258 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 2, free, test_buffer); |
| 1259 |
umock_c_reset_all_calls(); |
| 1260 |
///act |
| 1261 |
|
| 1262 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1263 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1264 |
|
| 1265 |
CONSTBUFFER_DecRef(handle); |
| 1266 |
|
| 1267 |
///assert |
| 1268 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* Tests_SRS_CONSTBUFFER_01_009: [ CONSTBUFFER_CreateWithCustomFree shall store customFreeFunc and customFreeFuncContext in order to use them to free the memory when the CONST buffer resources are freed. ]*/ |
| 1272 |
/* Tests_SRS_CONSTBUFFER_01_012: [ If the buffer was created by calling CONSTBUFFER_CreateWithCustomFree, the customFreeFunc function shall be called to free the memory, while passed customFreeFuncContext as argument. ]*/ |
| 1273 |
TEST_FUNCTION(CONSTBUFFER_CreateWithCustomFree_with_custom_free_function_calls_the_custom_free_func) |
| 1274 |
{ |
| 1275 |
///arrange |
| 1276 |
unsigned char* test_buffer = (unsigned char*)my_gballoc_malloc(2); |
| 1277 |
test_buffer[0] = 42; |
| 1278 |
test_buffer[1] = 43; |
| 1279 |
CONSTBUFFER_HANDLE handle = CONSTBUFFER_CreateWithCustomFree(test_buffer, 2, test_free_func, (void*)0x4242); |
| 1280 |
umock_c_reset_all_calls(); |
| 1281 |
///act |
| 1282 |
|
| 1283 |
STRICT_EXPECTED_CALL(test_free_func((void*)0x4242)); |
| 1284 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1285 |
|
| 1286 |
CONSTBUFFER_DecRef(handle); |
| 1287 |
|
| 1288 |
///assert |
| 1289 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1290 |
|
| 1291 |
// cleanup |
| 1292 |
my_gballoc_free(test_buffer); |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* Tests_SRS_CONSTBUFFER_01_003: [ The non-NULL handle returned by CONSTBUFFER_CreateWithMoveMemory shall have its ref count set to "1". ]*/ |
| 1296 |
/*Tests_SRS_CONSTBUFFER_02_017: [If the refcount reaches zero, then CONSTBUFFER_DecRef shall deallocate all resources used by the CONSTBUFFER_HANDLE.]*/ |
| 1297 |
TEST_FUNCTION(CONSTBUFFER_CreateWithMoveMemory_is_ref_counted_1) |
| 1298 |
{ |
| 1299 |
///arrange |
| 1300 |
CONSTBUFFER_HANDLE handle; |
| 1301 |
unsigned char* test_buffer = (unsigned char* )my_gballoc_malloc(2); |
| 1302 |
test_buffer[0] = 42; |
| 1303 |
test_buffer[1] = 43; |
| 1304 |
handle = CONSTBUFFER_CreateWithMoveMemory(test_buffer, 2); |
| 1305 |
umock_c_reset_all_calls(); |
| 1306 |
|
| 1307 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1308 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 1309 |
|
| 1310 |
///act |
| 1311 |
CONSTBUFFER_DecRef(handle); |
| 1312 |
|
| 1313 |
///assert |
| 1314 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 1315 |
} |
| 1316 |
|
| 1317 |
/*Tests_SRS_CONSTBUFFER_02_018: [ If left is NULL and right is NULL then CONSTBUFFER_HANDLE_contain_same shall return true. ]*/ |
| 1318 |
TEST_FUNCTION(CONSTBUFFER_HANDLE_contain_same_with_left_NULL_and_right_NULL_returns_true) |
| 1319 |
{ |
| 1320 |
///arrange |
| 1321 |
bool result; |
| 1322 |
|
| 1323 |
///act |
| 1324 |
result = CONSTBUFFER_HANDLE_contain_same(NULL, NULL); |
| 1325 |
|
| 1326 |
///assert |
| 1327 |
ASSERT_IS_TRUE(result); |
| 1328 |
} |
| 1329 |
|
| 1330 |
/*Tests_SRS_CONSTBUFFER_02_019: [ If left is NULL and right is not NULL then CONSTBUFFER_HANDLE_contain_same shall return false. ]*/ |
| 1331 |
TEST_FUNCTION(CONSTBUFFER_HANDLE_contain_same_with_left_NULL_and_right_not_NULL_returns_false) |
| 1332 |
{ |
| 1333 |
///arrange |
| 1334 |
bool result; |
| 1335 |
unsigned char rightSource = 'r'; |
| 1336 |
CONSTBUFFER_HANDLE right = CONSTBUFFER_Create(&rightSource, sizeof(rightSource)); |
| 1337 |
ASSERT_IS_NOT_NULL(right); |
| 1338 |
|
| 1339 |
///act |
| 1340 |
result = CONSTBUFFER_HANDLE_contain_same(NULL, right); |
| 1341 |
|
| 1342 |
///assert |
| 1343 |
ASSERT_IS_FALSE(result); |
| 1344 |
|
| 1345 |
///clean |
| 1346 |
CONSTBUFFER_DecRef(right); |
| 1347 |
} |
| 1348 |
|
| 1349 |
/*Tests_SRS_CONSTBUFFER_02_020: [ If left is not NULL and right is NULL then CONSTBUFFER_HANDLE_contain_same shall return false. ]*/ |
| 1350 |
TEST_FUNCTION(CONSTBUFFER_HANDLE_contain_same_with_left_not_NULL_and_right_NULL_returns_false) |
| 1351 |
{ |
| 1352 |
///arrange |
| 1353 |
bool result; |
| 1354 |
unsigned char leftSource = 'l'; |
| 1355 |
CONSTBUFFER_HANDLE left = CONSTBUFFER_Create(&leftSource, sizeof(leftSource)); |
| 1356 |
ASSERT_IS_NOT_NULL(left); |
| 1357 |
|
| 1358 |
///act |
| 1359 |
result = CONSTBUFFER_HANDLE_contain_same(left, NULL); |
| 1360 |
|
| 1361 |
///assert |
| 1362 |
ASSERT_IS_FALSE(result); |
| 1363 |
|
| 1364 |
///clean |
| 1365 |
CONSTBUFFER_DecRef(left); |
| 1366 |
} |
| 1367 |
|
| 1368 |
/*Tests_SRS_CONSTBUFFER_02_021: [ If left's size is different than right's size then CONSTBUFFER_HANDLE_contain_same shall return false. ]*/ |
| 1369 |
TEST_FUNCTION(CONSTBUFFER_HANDLE_contain_same_with_left_and_right_sizes_not_equal_returns_false) |
| 1370 |
{ |
| 1371 |
///arrange |
| 1372 |
bool result; |
| 1373 |
unsigned char leftSource = 'l'; |
| 1374 |
CONSTBUFFER_HANDLE left = CONSTBUFFER_Create(&leftSource, sizeof(leftSource)); |
| 1375 |
ASSERT_IS_NOT_NULL(left); |
| 1376 |
|
| 1377 |
unsigned char rightSource[2] = { 'r', 'r' }; |
| 1378 |
CONSTBUFFER_HANDLE right = CONSTBUFFER_Create(rightSource, sizeof(rightSource)); |
| 1379 |
ASSERT_IS_NOT_NULL(right); |
| 1380 |
|
| 1381 |
///act |
| 1382 |
result = CONSTBUFFER_HANDLE_contain_same(left, right); |
| 1383 |
|
| 1384 |
///assert |
| 1385 |
ASSERT_IS_FALSE(result); |
| 1386 |
|
| 1387 |
///clean |
| 1388 |
CONSTBUFFER_DecRef(left); |
| 1389 |
CONSTBUFFER_DecRef(right); |
| 1390 |
} |
| 1391 |
|
| 1392 |
/*Tests_SRS_CONSTBUFFER_02_022: [ If left's buffer is contains different bytes than rights's buffer then CONSTBUFFER_HANDLE_contain_same shall return false. ]*/ |
| 1393 |
TEST_FUNCTION(CONSTBUFFER_HANDLE_contain_same_with_left_and_right_content_not_equal_returns_false) |
| 1394 |
{ |
| 1395 |
///arrange |
| 1396 |
bool result; |
| 1397 |
unsigned char leftSource[2] = { 'l', 'l' }; |
| 1398 |
CONSTBUFFER_HANDLE left = CONSTBUFFER_Create(leftSource, sizeof(leftSource)); |
| 1399 |
ASSERT_IS_NOT_NULL(left); |
| 1400 |
|
| 1401 |
unsigned char rightSource[2] = { 'r', 'r' }; |
| 1402 |
CONSTBUFFER_HANDLE right = CONSTBUFFER_Create(rightSource, sizeof(rightSource)); |
| 1403 |
ASSERT_IS_NOT_NULL(right); |
| 1404 |
|
| 1405 |
///act |
| 1406 |
result = CONSTBUFFER_HANDLE_contain_same(left, right); |
| 1407 |
|
| 1408 |
///assert |
| 1409 |
ASSERT_IS_FALSE(result); |
| 1410 |
|
| 1411 |
///clean |
| 1412 |
CONSTBUFFER_DecRef(left); |
| 1413 |
CONSTBUFFER_DecRef(right); |
| 1414 |
} |
| 1415 |
|
| 1416 |
/*Tests_SRS_CONSTBUFFER_02_023: [ CONSTBUFFER_HANDLE_contain_same shall return true. ]*/ |
| 1417 |
TEST_FUNCTION(CONSTBUFFER_HANDLE_contain_same_with_left_and_right_same_returns_true) |
| 1418 |
{ |
| 1419 |
///arrange |
| 1420 |
bool result; |
| 1421 |
unsigned char leftSource[2] = { '1', '2' }; |
| 1422 |
CONSTBUFFER_HANDLE left = CONSTBUFFER_Create(leftSource, sizeof(leftSource)); |
| 1423 |
ASSERT_IS_NOT_NULL(left); |
| 1424 |
|
| 1425 |
unsigned char rightSource[2] = { '1', '2' }; |
| 1426 |
CONSTBUFFER_HANDLE right = CONSTBUFFER_Create(rightSource, sizeof(rightSource)); |
| 1427 |
ASSERT_IS_NOT_NULL(right); |
| 1428 |
|
| 1429 |
///act |
| 1430 |
result = CONSTBUFFER_HANDLE_contain_same(left, right); |
| 1431 |
|
| 1432 |
///assert |
| 1433 |
ASSERT_IS_TRUE(result); |
| 1434 |
|
| 1435 |
///clean |
| 1436 |
CONSTBUFFER_DecRef(left); |
| 1437 |
CONSTBUFFER_DecRef(right); |
| 1438 |
} |
| 1439 |
END_TEST_SUITE(constbuffer_unittests) |