xref: /aosp_15_r20/system/extras/memory_replay/tests/FileTest.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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 <malloc.h>
18 #include <stdint.h>
19 
20 #include <string>
21 
22 #include <android-base/file.h>
23 #include <gtest/gtest.h>
24 
25 #include <memory_trace/MemoryTrace.h>
26 
27 #include "File.h"
28 
GetTestDirectory()29 static std::string GetTestDirectory() {
30   return android::base::GetExecutableDirectory() + "/tests";
31 }
32 
GetTestZip()33 static std::string GetTestZip() {
34   return GetTestDirectory() + "/test.zip";
35 }
36 
TEST(FileTest,zip_get_contents)37 TEST(FileTest, zip_get_contents) {
38   EXPECT_EQ("12345: malloc 0x1000 16\n12345: free 0x1000\n", ZipGetContents(GetTestZip().c_str()));
39 }
40 
TEST(FileTest,zip_get_contents_bad_file)41 TEST(FileTest, zip_get_contents_bad_file) {
42   EXPECT_EQ("", ZipGetContents("/does/not/exist.zip"));
43 }
44 
TEST(FileTest,get_unwind_info_from_zip_file)45 TEST(FileTest, get_unwind_info_from_zip_file) {
46   // This will allocate, so do it before getting mallinfo.
47   std::string file_name = GetTestZip();
48 
49   size_t mallinfo_before = mallinfo().uordblks;
50   memory_trace::Entry* entries;
51   size_t num_entries;
52   GetUnwindInfo(file_name.c_str(), &entries, &num_entries);
53   size_t mallinfo_after = mallinfo().uordblks;
54 
55   // Verify no memory is allocated.
56   EXPECT_EQ(mallinfo_after, mallinfo_before);
57 
58   ASSERT_EQ(2U, num_entries);
59   EXPECT_EQ(12345, entries[0].tid);
60   EXPECT_EQ(memory_trace::MALLOC, entries[0].type);
61   EXPECT_EQ(0x1000U, entries[0].ptr);
62   EXPECT_EQ(16U, entries[0].size);
63   EXPECT_EQ(0U, entries[0].u.old_ptr);
64 
65   EXPECT_EQ(12345, entries[1].tid);
66   EXPECT_EQ(memory_trace::FREE, entries[1].type);
67   EXPECT_EQ(0x1000U, entries[1].ptr);
68   EXPECT_EQ(0U, entries[1].size);
69   EXPECT_EQ(0U, entries[1].u.old_ptr);
70 
71   FreeEntries(entries, num_entries);
72 }
73 
TEST(FileTest,get_unwind_info_bad_zip_file)74 TEST(FileTest, get_unwind_info_bad_zip_file) {
75   memory_trace::Entry* entries;
76   size_t num_entries;
77   EXPECT_DEATH(GetUnwindInfo("/does/not/exist.zip", &entries, &num_entries), "");
78 }
79 
TEST(FileTest,get_unwind_info_from_text_file)80 TEST(FileTest, get_unwind_info_from_text_file) {
81   // This will allocate, so do it before getting mallinfo.
82   std::string file_name = GetTestDirectory() + "/test.txt";
83 
84   size_t mallinfo_before = mallinfo().uordblks;
85   memory_trace::Entry* entries;
86   size_t num_entries;
87   GetUnwindInfo(file_name.c_str(), &entries, &num_entries);
88   size_t mallinfo_after = mallinfo().uordblks;
89 
90   // Verify no memory is allocated.
91   EXPECT_EQ(mallinfo_after, mallinfo_before);
92 
93   ASSERT_EQ(2U, num_entries);
94   EXPECT_EQ(98765, entries[0].tid);
95   EXPECT_EQ(memory_trace::MEMALIGN, entries[0].type);
96   EXPECT_EQ(0xa000U, entries[0].ptr);
97   EXPECT_EQ(124U, entries[0].size);
98   EXPECT_EQ(16U, entries[0].u.align);
99 
100   EXPECT_EQ(98765, entries[1].tid);
101   EXPECT_EQ(memory_trace::FREE, entries[1].type);
102   EXPECT_EQ(0xa000U, entries[1].ptr);
103   EXPECT_EQ(0U, entries[1].size);
104   EXPECT_EQ(0U, entries[1].u.old_ptr);
105 
106   FreeEntries(entries, num_entries);
107 }
108 
TEST(FileTest,get_unwind_info_bad_file)109 TEST(FileTest, get_unwind_info_bad_file) {
110   memory_trace::Entry* entries;
111   size_t num_entries;
112   EXPECT_DEATH(GetUnwindInfo("/does/not/exist", &entries, &num_entries), "");
113 }
114