1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <gtest/gtest.h> 18 #include <stdint.h> 19 #include <string.h> 20 21 #include "Action.h" 22 #include "Pointers.h" 23 24 TEST(ActionTest, malloc) { 25 uint8_t memory[Action::MaxActionSize()]; 26 const char* line = "1024"; 27 Action* action = Action::CreateAction(0x1234, "malloc", line, memory); 28 ASSERT_TRUE(action != NULL); 29 ASSERT_FALSE(action->DoesFree()); 30 ASSERT_FALSE(action->EndThread()); 31 32 Pointers pointers(1); 33 action->Execute(&pointers); 34 void* pointer = pointers.Remove(0x1234); 35 ASSERT_TRUE(pointer != nullptr); 36 free(pointer); 37 } 38 39 TEST(ActionTest, malloc_malformed) { 40 uint8_t memory[128]; 41 const char* line = ""; 42 Action* action = Action::CreateAction(0x1234, "malloc", line, memory); 43 ASSERT_FALSE(action != NULL); 44 } 45 46 TEST(ActionTest, free) { 47 uint8_t memory[128]; 48 const char* line = ""; 49 Action* action = Action::CreateAction(0x1234, "free", line, memory); 50 ASSERT_TRUE(action != NULL); 51 ASSERT_TRUE(action->DoesFree()); 52 ASSERT_FALSE(action->EndThread()); 53 54 Pointers pointers(1); 55 pointers.Add(0x1234, malloc(10)); 56 action->Execute(&pointers); 57 } 58 59 TEST(ActionTest, calloc) { 60 uint8_t memory[128]; 61 const char* line = "100 10"; 62 Action* action = Action::CreateAction(0x1234, "calloc", line, memory); 63 ASSERT_TRUE(action != NULL); 64 ASSERT_FALSE(action->DoesFree()); 65 ASSERT_FALSE(action->EndThread()); 66 67 Pointers pointers(1); 68 action->Execute(&pointers); 69 void* pointer = pointers.Remove(0x1234); 70 ASSERT_TRUE(pointer != nullptr); 71 free(pointer); 72 } 73 74 TEST(ActionTest, free_zero) { 75 uint8_t memory[128]; 76 const char* line = ""; 77 Action* action = Action::CreateAction(0, "free", line, memory); 78 ASSERT_TRUE(action != NULL); 79 ASSERT_FALSE(action->DoesFree()); 80 ASSERT_FALSE(action->EndThread()); 81 // Should be a nop. 82 action->Execute(nullptr); 83 } 84 85 TEST(ActionTest, calloc_malformed) { 86 uint8_t memory[128]; 87 const char* line1 = "100"; 88 Action* action = Action::CreateAction(0x1234, "calloc", line1, memory); 89 ASSERT_FALSE(action != NULL); 90 91 const char* line2 = ""; 92 action = Action::CreateAction(0x1234, "calloc", line2, memory); 93 ASSERT_FALSE(action != NULL); 94 } 95 96 TEST(ActionTest, realloc) { 97 uint8_t memory[128]; 98 const char* line = "0xabcd 100"; 99 Action* action = Action::CreateAction(0x1234, "realloc", line, memory); 100 ASSERT_TRUE(action != NULL); 101 ASSERT_TRUE(action->DoesFree()); 102 ASSERT_FALSE(action->EndThread()); 103 104 Pointers pointers(1); 105 pointers.Add(0xabcd, malloc(10)); 106 action->Execute(&pointers); 107 void* pointer = pointers.Remove(0x1234); 108 ASSERT_TRUE(pointer != nullptr); 109 free(pointer); 110 111 const char* null_line = "0x0 100"; 112 action = Action::CreateAction(0x1234, "realloc", null_line, memory); 113 ASSERT_FALSE(action->DoesFree()); 114 ASSERT_FALSE(action->EndThread()); 115 116 action->Execute(&pointers); 117 pointer = pointers.Remove(0x1234); 118 ASSERT_TRUE(pointer != nullptr); 119 free(pointer); 120 } 121 122 TEST(ActionTest, realloc_malformed) { 123 uint8_t memory[128]; 124 const char* line1 = "0x100"; 125 Action* action = Action::CreateAction(0x1234, "realloc", line1, memory); 126 ASSERT_FALSE(action != NULL); 127 128 const char* line2 = ""; 129 action = Action::CreateAction(0x1234, "realloc", line2, memory); 130 ASSERT_FALSE(action != NULL); 131 } 132 133 TEST(ActionTest, memalign) { 134 uint8_t memory[128]; 135 const char* line = "16 300"; 136 Action* action = Action::CreateAction(0x1234, "memalign", line, memory); 137 ASSERT_TRUE(action != NULL); 138 ASSERT_FALSE(action->DoesFree()); 139 ASSERT_FALSE(action->EndThread()); 140 141 Pointers pointers(1); 142 action->Execute(&pointers); 143 void* pointer = pointers.Remove(0x1234); 144 ASSERT_TRUE(pointer != nullptr); 145 free(pointer); 146 } 147 148 TEST(ActionTest, memalign_malformed) { 149 uint8_t memory[128]; 150 const char* line1 = "100"; 151 Action* action = Action::CreateAction(0x1234, "memalign", line1, memory); 152 ASSERT_FALSE(action != NULL); 153 154 const char* line2 = ""; 155 action = Action::CreateAction(0x1234, "memalign", line2, memory); 156 ASSERT_FALSE(action != NULL); 157 } 158 159 TEST(ActionTest, endthread) { 160 uint8_t memory[128]; 161 const char* line = ""; 162 Action* action = Action::CreateAction(0x0, "thread_done", line, memory); 163 ASSERT_TRUE(action != NULL); 164 ASSERT_FALSE(action->DoesFree()); 165 ASSERT_TRUE(action->EndThread()); 166 167 action->Execute(nullptr); 168 } 169