xref: /aosp_15_r20/system/extras/memory_replay/tests/FileTest.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <malloc.h>
18*288bf522SAndroid Build Coastguard Worker #include <stdint.h>
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <string>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
23*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
24*288bf522SAndroid Build Coastguard Worker 
25*288bf522SAndroid Build Coastguard Worker #include <memory_trace/MemoryTrace.h>
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker #include "File.h"
28*288bf522SAndroid Build Coastguard Worker 
GetTestDirectory()29*288bf522SAndroid Build Coastguard Worker static std::string GetTestDirectory() {
30*288bf522SAndroid Build Coastguard Worker   return android::base::GetExecutableDirectory() + "/tests";
31*288bf522SAndroid Build Coastguard Worker }
32*288bf522SAndroid Build Coastguard Worker 
GetTestZip()33*288bf522SAndroid Build Coastguard Worker static std::string GetTestZip() {
34*288bf522SAndroid Build Coastguard Worker   return GetTestDirectory() + "/test.zip";
35*288bf522SAndroid Build Coastguard Worker }
36*288bf522SAndroid Build Coastguard Worker 
TEST(FileTest,zip_get_contents)37*288bf522SAndroid Build Coastguard Worker TEST(FileTest, zip_get_contents) {
38*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ("12345: malloc 0x1000 16\n12345: free 0x1000\n", ZipGetContents(GetTestZip().c_str()));
39*288bf522SAndroid Build Coastguard Worker }
40*288bf522SAndroid Build Coastguard Worker 
TEST(FileTest,zip_get_contents_bad_file)41*288bf522SAndroid Build Coastguard Worker TEST(FileTest, zip_get_contents_bad_file) {
42*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ("", ZipGetContents("/does/not/exist.zip"));
43*288bf522SAndroid Build Coastguard Worker }
44*288bf522SAndroid Build Coastguard Worker 
TEST(FileTest,get_unwind_info_from_zip_file)45*288bf522SAndroid Build Coastguard Worker TEST(FileTest, get_unwind_info_from_zip_file) {
46*288bf522SAndroid Build Coastguard Worker   // This will allocate, so do it before getting mallinfo.
47*288bf522SAndroid Build Coastguard Worker   std::string file_name = GetTestZip();
48*288bf522SAndroid Build Coastguard Worker 
49*288bf522SAndroid Build Coastguard Worker   size_t mallinfo_before = mallinfo().uordblks;
50*288bf522SAndroid Build Coastguard Worker   memory_trace::Entry* entries;
51*288bf522SAndroid Build Coastguard Worker   size_t num_entries;
52*288bf522SAndroid Build Coastguard Worker   GetUnwindInfo(file_name.c_str(), &entries, &num_entries);
53*288bf522SAndroid Build Coastguard Worker   size_t mallinfo_after = mallinfo().uordblks;
54*288bf522SAndroid Build Coastguard Worker 
55*288bf522SAndroid Build Coastguard Worker   // Verify no memory is allocated.
56*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(mallinfo_after, mallinfo_before);
57*288bf522SAndroid Build Coastguard Worker 
58*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(2U, num_entries);
59*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(12345, entries[0].tid);
60*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(memory_trace::MALLOC, entries[0].type);
61*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0x1000U, entries[0].ptr);
62*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(16U, entries[0].size);
63*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0U, entries[0].u.old_ptr);
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(12345, entries[1].tid);
66*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(memory_trace::FREE, entries[1].type);
67*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0x1000U, entries[1].ptr);
68*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0U, entries[1].size);
69*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0U, entries[1].u.old_ptr);
70*288bf522SAndroid Build Coastguard Worker 
71*288bf522SAndroid Build Coastguard Worker   FreeEntries(entries, num_entries);
72*288bf522SAndroid Build Coastguard Worker }
73*288bf522SAndroid Build Coastguard Worker 
TEST(FileTest,get_unwind_info_bad_zip_file)74*288bf522SAndroid Build Coastguard Worker TEST(FileTest, get_unwind_info_bad_zip_file) {
75*288bf522SAndroid Build Coastguard Worker   memory_trace::Entry* entries;
76*288bf522SAndroid Build Coastguard Worker   size_t num_entries;
77*288bf522SAndroid Build Coastguard Worker   EXPECT_DEATH(GetUnwindInfo("/does/not/exist.zip", &entries, &num_entries), "");
78*288bf522SAndroid Build Coastguard Worker }
79*288bf522SAndroid Build Coastguard Worker 
TEST(FileTest,get_unwind_info_from_text_file)80*288bf522SAndroid Build Coastguard Worker TEST(FileTest, get_unwind_info_from_text_file) {
81*288bf522SAndroid Build Coastguard Worker   // This will allocate, so do it before getting mallinfo.
82*288bf522SAndroid Build Coastguard Worker   std::string file_name = GetTestDirectory() + "/test.txt";
83*288bf522SAndroid Build Coastguard Worker 
84*288bf522SAndroid Build Coastguard Worker   size_t mallinfo_before = mallinfo().uordblks;
85*288bf522SAndroid Build Coastguard Worker   memory_trace::Entry* entries;
86*288bf522SAndroid Build Coastguard Worker   size_t num_entries;
87*288bf522SAndroid Build Coastguard Worker   GetUnwindInfo(file_name.c_str(), &entries, &num_entries);
88*288bf522SAndroid Build Coastguard Worker   size_t mallinfo_after = mallinfo().uordblks;
89*288bf522SAndroid Build Coastguard Worker 
90*288bf522SAndroid Build Coastguard Worker   // Verify no memory is allocated.
91*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(mallinfo_after, mallinfo_before);
92*288bf522SAndroid Build Coastguard Worker 
93*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(2U, num_entries);
94*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(98765, entries[0].tid);
95*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(memory_trace::MEMALIGN, entries[0].type);
96*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0xa000U, entries[0].ptr);
97*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(124U, entries[0].size);
98*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(16U, entries[0].u.align);
99*288bf522SAndroid Build Coastguard Worker 
100*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(98765, entries[1].tid);
101*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(memory_trace::FREE, entries[1].type);
102*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0xa000U, entries[1].ptr);
103*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0U, entries[1].size);
104*288bf522SAndroid Build Coastguard Worker   EXPECT_EQ(0U, entries[1].u.old_ptr);
105*288bf522SAndroid Build Coastguard Worker 
106*288bf522SAndroid Build Coastguard Worker   FreeEntries(entries, num_entries);
107*288bf522SAndroid Build Coastguard Worker }
108*288bf522SAndroid Build Coastguard Worker 
TEST(FileTest,get_unwind_info_bad_file)109*288bf522SAndroid Build Coastguard Worker TEST(FileTest, get_unwind_info_bad_file) {
110*288bf522SAndroid Build Coastguard Worker   memory_trace::Entry* entries;
111*288bf522SAndroid Build Coastguard Worker   size_t num_entries;
112*288bf522SAndroid Build Coastguard Worker   EXPECT_DEATH(GetUnwindInfo("/does/not/exist", &entries, &num_entries), "");
113*288bf522SAndroid Build Coastguard Worker }
114