ActionTest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <gtest/gtest.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include "Action.h"
  20. #include "Pointers.h"
  21. TEST(ActionTest, malloc) {
  22. uint8_t memory[Action::MaxActionSize()];
  23. const char* line = "1024";
  24. Action* action = Action::CreateAction(0x1234, "malloc", line, memory);
  25. ASSERT_TRUE(action != NULL);
  26. ASSERT_FALSE(action->DoesFree());
  27. ASSERT_FALSE(action->EndThread());
  28. Pointers pointers(1);
  29. action->Execute(&pointers);
  30. void* pointer = pointers.Remove(0x1234);
  31. ASSERT_TRUE(pointer != nullptr);
  32. free(pointer);
  33. }
  34. TEST(ActionTest, malloc_malformed) {
  35. uint8_t memory[128];
  36. const char* line = "";
  37. Action* action = Action::CreateAction(0x1234, "malloc", line, memory);
  38. ASSERT_FALSE(action != NULL);
  39. }
  40. TEST(ActionTest, free) {
  41. uint8_t memory[128];
  42. const char* line = "";
  43. Action* action = Action::CreateAction(0x1234, "free", line, memory);
  44. ASSERT_TRUE(action != NULL);
  45. ASSERT_TRUE(action->DoesFree());
  46. ASSERT_FALSE(action->EndThread());
  47. Pointers pointers(1);
  48. pointers.Add(0x1234, malloc(10));
  49. action->Execute(&pointers);
  50. }
  51. TEST(ActionTest, calloc) {
  52. uint8_t memory[128];
  53. const char* line = "100 10";
  54. Action* action = Action::CreateAction(0x1234, "calloc", line, memory);
  55. ASSERT_TRUE(action != NULL);
  56. ASSERT_FALSE(action->DoesFree());
  57. ASSERT_FALSE(action->EndThread());
  58. Pointers pointers(1);
  59. action->Execute(&pointers);
  60. void* pointer = pointers.Remove(0x1234);
  61. ASSERT_TRUE(pointer != nullptr);
  62. free(pointer);
  63. }
  64. TEST(ActionTest, free_zero) {
  65. uint8_t memory[128];
  66. const char* line = "";
  67. Action* action = Action::CreateAction(0, "free", line, memory);
  68. ASSERT_TRUE(action != NULL);
  69. ASSERT_FALSE(action->DoesFree());
  70. ASSERT_FALSE(action->EndThread());
  71. // Should be a nop.
  72. action->Execute(nullptr);
  73. }
  74. TEST(ActionTest, calloc_malformed) {
  75. uint8_t memory[128];
  76. const char* line1 = "100";
  77. Action* action = Action::CreateAction(0x1234, "calloc", line1, memory);
  78. ASSERT_FALSE(action != NULL);
  79. const char* line2 = "";
  80. action = Action::CreateAction(0x1234, "calloc", line2, memory);
  81. ASSERT_FALSE(action != NULL);
  82. }
  83. TEST(ActionTest, realloc) {
  84. uint8_t memory[128];
  85. const char* line = "0xabcd 100";
  86. Action* action = Action::CreateAction(0x1234, "realloc", line, memory);
  87. ASSERT_TRUE(action != NULL);
  88. ASSERT_TRUE(action->DoesFree());
  89. ASSERT_FALSE(action->EndThread());
  90. Pointers pointers(1);
  91. pointers.Add(0xabcd, malloc(10));
  92. action->Execute(&pointers);
  93. void* pointer = pointers.Remove(0x1234);
  94. ASSERT_TRUE(pointer != nullptr);
  95. free(pointer);
  96. const char* null_line = "0x0 100";
  97. action = Action::CreateAction(0x1234, "realloc", null_line, memory);
  98. ASSERT_FALSE(action->DoesFree());
  99. ASSERT_FALSE(action->EndThread());
  100. action->Execute(&pointers);
  101. pointer = pointers.Remove(0x1234);
  102. ASSERT_TRUE(pointer != nullptr);
  103. free(pointer);
  104. }
  105. TEST(ActionTest, realloc_malformed) {
  106. uint8_t memory[128];
  107. const char* line1 = "0x100";
  108. Action* action = Action::CreateAction(0x1234, "realloc", line1, memory);
  109. ASSERT_FALSE(action != NULL);
  110. const char* line2 = "";
  111. action = Action::CreateAction(0x1234, "realloc", line2, memory);
  112. ASSERT_FALSE(action != NULL);
  113. }
  114. TEST(ActionTest, memalign) {
  115. uint8_t memory[128];
  116. const char* line = "16 300";
  117. Action* action = Action::CreateAction(0x1234, "memalign", line, memory);
  118. ASSERT_TRUE(action != NULL);
  119. ASSERT_FALSE(action->DoesFree());
  120. ASSERT_FALSE(action->EndThread());
  121. Pointers pointers(1);
  122. action->Execute(&pointers);
  123. void* pointer = pointers.Remove(0x1234);
  124. ASSERT_TRUE(pointer != nullptr);
  125. free(pointer);
  126. }
  127. TEST(ActionTest, memalign_malformed) {
  128. uint8_t memory[128];
  129. const char* line1 = "100";
  130. Action* action = Action::CreateAction(0x1234, "memalign", line1, memory);
  131. ASSERT_FALSE(action != NULL);
  132. const char* line2 = "";
  133. action = Action::CreateAction(0x1234, "memalign", line2, memory);
  134. ASSERT_FALSE(action != NULL);
  135. }
  136. TEST(ActionTest, endthread) {
  137. uint8_t memory[128];
  138. const char* line = "";
  139. Action* action = Action::CreateAction(0x0, "thread_done", line, memory);
  140. ASSERT_TRUE(action != NULL);
  141. ASSERT_FALSE(action->DoesFree());
  142. ASSERT_TRUE(action->EndThread());
  143. action->Execute(nullptr);
  144. }