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