1 /*
2  * Copyright (C) 2019 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 <stdint.h>
18 
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 
23 #include "Alloc.h"
24 
TEST(AllocTest,malloc_valid)25 TEST(AllocTest, malloc_valid) {
26   std::string line = "1234: malloc 0xabd0000 20";
27   AllocEntry entry;
28   AllocGetData(line, &entry);
29   EXPECT_EQ(MALLOC, entry.type);
30   EXPECT_EQ(1234, entry.tid);
31   EXPECT_EQ(0xabd0000U, entry.ptr);
32   EXPECT_EQ(20U, entry.size);
33   EXPECT_EQ(0U, entry.u.align);
34 }
35 
TEST(AllocTest,malloc_invalid)36 TEST(AllocTest, malloc_invalid) {
37   std::string line = "1234: malloc 0xabd0000";
38   AllocEntry entry;
39   EXPECT_DEATH(AllocGetData(line, &entry), "");
40 
41   line = "1234: malloc";
42   EXPECT_DEATH(AllocGetData(line, &entry), "");
43 }
44 
TEST(AllocTest,free_valid)45 TEST(AllocTest, free_valid) {
46   std::string line = "1235: free 0x5000";
47   AllocEntry entry;
48   AllocGetData(line, &entry);
49   EXPECT_EQ(FREE, entry.type);
50   EXPECT_EQ(1235, entry.tid);
51   EXPECT_EQ(0x5000U, entry.ptr);
52   EXPECT_EQ(0U, entry.size);
53   EXPECT_EQ(0U, entry.u.align);
54 }
55 
TEST(AllocTest,free_invalid)56 TEST(AllocTest, free_invalid) {
57   std::string line = "1234: free";
58   AllocEntry entry;
59   EXPECT_DEATH(AllocGetData(line, &entry), "");
60 }
61 
TEST(AllocTest,calloc_valid)62 TEST(AllocTest, calloc_valid) {
63   std::string line = "1236: calloc 0x8000 50 30";
64   AllocEntry entry;
65   AllocGetData(line, &entry);
66   EXPECT_EQ(CALLOC, entry.type);
67   EXPECT_EQ(1236, entry.tid);
68   EXPECT_EQ(0x8000U, entry.ptr);
69   EXPECT_EQ(30U, entry.size);
70   EXPECT_EQ(50U, entry.u.n_elements);
71 }
72 
TEST(AllocTest,calloc_invalid)73 TEST(AllocTest, calloc_invalid) {
74   std::string line = "1236: calloc 0x8000 50";
75   AllocEntry entry;
76   EXPECT_DEATH(AllocGetData(line, &entry), "");
77 
78   line = "1236: calloc 0x8000";
79   EXPECT_DEATH(AllocGetData(line, &entry), "");
80 
81   line = "1236: calloc";
82   EXPECT_DEATH(AllocGetData(line, &entry), "");
83 }
84 
TEST(AllocTest,realloc_valid)85 TEST(AllocTest, realloc_valid) {
86   std::string line = "1237: realloc 0x9000 0x4000 80";
87   AllocEntry entry;
88   AllocGetData(line, &entry);
89   EXPECT_EQ(REALLOC, entry.type);
90   EXPECT_EQ(1237, entry.tid);
91   EXPECT_EQ(0x9000U, entry.ptr);
92   EXPECT_EQ(80U, entry.size);
93   EXPECT_EQ(0x4000U, entry.u.old_ptr);
94 }
95 
TEST(AllocTest,realloc_invalid)96 TEST(AllocTest, realloc_invalid) {
97   std::string line = "1237: realloc 0x9000 0x4000";
98   AllocEntry entry;
99   EXPECT_DEATH(AllocGetData(line, &entry), "");
100 
101   line = "1237: realloc 0x9000";
102   EXPECT_DEATH(AllocGetData(line, &entry), "");
103 
104   line = "1237: realloc";
105   EXPECT_DEATH(AllocGetData(line, &entry), "");
106 }
107 
TEST(AllocTest,memalign_valid)108 TEST(AllocTest, memalign_valid) {
109   std::string line = "1238: memalign 0xa000 16 89";
110   AllocEntry entry;
111   AllocGetData(line, &entry);
112   EXPECT_EQ(MEMALIGN, entry.type);
113   EXPECT_EQ(1238, entry.tid);
114   EXPECT_EQ(0xa000U, entry.ptr);
115   EXPECT_EQ(89U, entry.size);
116   EXPECT_EQ(16U, entry.u.align);
117 }
118 
TEST(AllocTest,memalign_invalid)119 TEST(AllocTest, memalign_invalid) {
120   std::string line = "1238: memalign 0xa000 16";
121   AllocEntry entry;
122   EXPECT_DEATH(AllocGetData(line, &entry), "");
123 
124   line = "1238: memalign 0xa000";
125   EXPECT_DEATH(AllocGetData(line, &entry), "");
126 
127   line = "1238: memalign";
128   EXPECT_DEATH(AllocGetData(line, &entry), "");
129 }
130 
TEST(AllocTest,thread_done_valid)131 TEST(AllocTest, thread_done_valid) {
132   std::string line = "1239: thread_done 0x0";
133   AllocEntry entry;
134   AllocGetData(line, &entry);
135   EXPECT_EQ(THREAD_DONE, entry.type);
136   EXPECT_EQ(1239, entry.tid);
137   EXPECT_EQ(0U, entry.ptr);
138   EXPECT_EQ(0U, entry.size);
139   EXPECT_EQ(0U, entry.u.old_ptr);
140 }
141 
TEST(AllocTest,thread_done_invalid)142 TEST(AllocTest, thread_done_invalid) {
143   std::string line = "1240: thread_done";
144   AllocEntry entry;
145   EXPECT_DEATH(AllocGetData(line, &entry), "");
146 }
147