1 /****************************************************************************** 2 * 3 * Copyright 2014 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #include <gtest/gtest.h> 20 21 #include "osi/include/allocation_tracker.h" 22 23 void allocation_tracker_uninit(void); 24 25 static const allocator_id_t allocator_id = 5; 26 27 TEST(AllocationTrackerTest, test_uninit_no_bad_effects) { 28 void* dummy_allocation = malloc(4); 29 30 // Ensure uninitialized state (previous tests may have called init) 31 allocation_tracker_uninit(); 32 33 EXPECT_EQ(4U, allocation_tracker_resize_for_canary(4)); 34 allocation_tracker_notify_alloc(allocator_id, dummy_allocation, 4); 35 EXPECT_EQ(0U, allocation_tracker_expect_no_allocations()); // should not have 36 // registered an 37 // allocation 38 allocation_tracker_notify_free(allocator_id, dummy_allocation); 39 EXPECT_EQ(0U, allocation_tracker_expect_no_allocations()); 40 41 free(dummy_allocation); 42 } 43 44 TEST(AllocationTrackerTest, test_canaries_on) { 45 allocation_tracker_uninit(); 46 allocation_tracker_init(); 47 48 size_t with_canary_size = allocation_tracker_resize_for_canary(4); 49 EXPECT_TRUE(with_canary_size > 4); 50 51 void* dummy_allocation = malloc(with_canary_size); 52 void* useable_ptr = 53 allocation_tracker_notify_alloc(allocator_id, dummy_allocation, 4); 54 EXPECT_TRUE(useable_ptr > dummy_allocation); 55 EXPECT_EQ(4U, allocation_tracker_expect_no_allocations()); // should have 56 // registered the 57 // allocation 58 void* freeable_ptr = 59 allocation_tracker_notify_free(allocator_id, useable_ptr); 60 EXPECT_EQ(dummy_allocation, freeable_ptr); 61 EXPECT_EQ(0U, allocation_tracker_expect_no_allocations()); 62 63 free(dummy_allocation); 64 } 65