zalloc.c (3030B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include <CUnit/CUnit.h> 21#include <guacamole/mem.h> 22#include <stdint.h> 23 24/** 25 * Test which verifies that guac_mem_zalloc() returns NULL for all inputs involving 26 * at least one zero value. 27 */ 28void test_mem__zalloc_fail_zero() { 29 30 CU_ASSERT_PTR_NULL(guac_mem_zalloc(0)); 31 CU_ASSERT_PTR_NULL(guac_mem_zalloc(0, 0)); 32 CU_ASSERT_PTR_NULL(guac_mem_zalloc(0, 0, 0)); 33 CU_ASSERT_PTR_NULL(guac_mem_zalloc(0, 0, 0, 0)); 34 CU_ASSERT_PTR_NULL(guac_mem_zalloc(0, 0, 0, 0, 0)); 35 36 CU_ASSERT_PTR_NULL(guac_mem_zalloc(1, 0)); 37 CU_ASSERT_PTR_NULL(guac_mem_zalloc(3, 2, 0)); 38 CU_ASSERT_PTR_NULL(guac_mem_zalloc(5, 0, 8, 9)); 39 CU_ASSERT_PTR_NULL(guac_mem_zalloc(99, 99, 99, 0, 99)); 40 41} 42 43/** 44 * Returns whether all bytes within the given memory region are zero. 45 * 46 * @param ptr 47 * The first byte of the memory region to test. 48 * 49 * @param length 50 * The number of bytes within the memory region. 51 * 52 * @returns 53 * Non-zero if all bytes within the memory region have the value of zero, 54 * zero otherwise. 55 */ 56static int is_all_zeroes(void* ptr, size_t length) { 57 58 int result = 0; 59 60 unsigned char* current = (unsigned char*) ptr; 61 for (size_t i = 0; i < length; i++) 62 result |= *(current++); 63 64 return !result; 65 66} 67 68/** 69 * Test which verifies that guac_mem_zalloc() successfully allocates blocks of 70 * memory for inputs that can reasonably be expected to succeed, and that each 71 * block is zeroed out. 72 */ 73void test_mem__zalloc_success() { 74 75 void* ptr; 76 77 ptr = guac_mem_zalloc(123); 78 CU_ASSERT_PTR_NOT_NULL(ptr); 79 CU_ASSERT(is_all_zeroes(ptr, 123)); 80 guac_mem_free(ptr); 81 82 ptr = guac_mem_zalloc(123, 456); 83 CU_ASSERT_PTR_NOT_NULL(ptr); 84 CU_ASSERT(is_all_zeroes(ptr, 123 * 456)); 85 guac_mem_free(ptr); 86 87 ptr = guac_mem_zalloc(123, 456, 789); 88 CU_ASSERT_PTR_NOT_NULL(ptr); 89 CU_ASSERT(is_all_zeroes(ptr, 123 * 456 * 789)); 90 guac_mem_free(ptr); 91 92} 93 94/** 95 * Test which verifies that guac_mem_zalloc() fails to allocate blocks of memory 96 * that exceed the capacity of a size_t. 97 */ 98void test_mem__zalloc_fail_large() { 99 CU_ASSERT_PTR_NULL(guac_mem_zalloc(123, 456, SIZE_MAX)); 100 CU_ASSERT_PTR_NULL(guac_mem_zalloc(SIZE_MAX / 2, SIZE_MAX / 2)); 101} 102