xref: /aosp_15_r20/art/libprofile/profile/profile_boot_info_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/arena_allocator.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/common_art_test.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/unix_file/fd_file.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
24*795d594fSAndroid Build Coastguard Worker #include "profile/profile_boot_info.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker class ProfileBootInfoTest : public CommonArtTest {
29*795d594fSAndroid Build Coastguard Worker  public:
SetUp()30*795d594fSAndroid Build Coastguard Worker   void SetUp() override {
31*795d594fSAndroid Build Coastguard Worker     CommonArtTest::SetUp();
32*795d594fSAndroid Build Coastguard Worker   }
33*795d594fSAndroid Build Coastguard Worker };
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileBootInfoTest,LoadEmpty)36*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileBootInfoTest, LoadEmpty) {
37*795d594fSAndroid Build Coastguard Worker   ScratchFile profile;
38*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> dex_files;
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo loaded_info;
41*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(loaded_info.IsEmpty());
42*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files));
43*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(loaded_info.IsEmpty());
44*795d594fSAndroid Build Coastguard Worker }
45*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileBootInfoTest,OneMethod)46*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileBootInfoTest, OneMethod) {
47*795d594fSAndroid Build Coastguard Worker   ScratchFile profile;
48*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
49*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> dex_files = { dex.get() };
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo saved_info;
52*795d594fSAndroid Build Coastguard Worker   saved_info.Add(dex.get(), 0);
53*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
54*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(profile.GetFile()->ResetOffset());
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo loaded_info;
57*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files));
58*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetDexFiles().size(), 1u);
59*795d594fSAndroid Build Coastguard Worker   ASSERT_STREQ(loaded_info.GetDexFiles()[0]->GetLocation().c_str(), dex->GetLocation().c_str());
60*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods().size(), 1u);
61*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[0].first, 0u);
62*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[0].second, 0u);
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileBootInfoTest,ManyDexFiles)65*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileBootInfoTest, ManyDexFiles) {
66*795d594fSAndroid Build Coastguard Worker   ScratchFile profile;
67*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
68*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> dex_files2;
69*795d594fSAndroid Build Coastguard Worker   dex_files2.reserve(dex_files.size());
70*795d594fSAndroid Build Coastguard Worker   for (const std::unique_ptr<const DexFile>& file : dex_files) {
71*795d594fSAndroid Build Coastguard Worker     dex_files2.push_back(file.get());
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo saved_info;
75*795d594fSAndroid Build Coastguard Worker   saved_info.Add(dex_files[0].get(), 42);
76*795d594fSAndroid Build Coastguard Worker   saved_info.Add(dex_files[1].get(), 108);
77*795d594fSAndroid Build Coastguard Worker   saved_info.Add(dex_files[1].get(), 54);
78*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
79*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(profile.GetFile()->ResetOffset());
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo loaded_info;
82*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files2));
83*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetDexFiles().size(), 2u);
84*795d594fSAndroid Build Coastguard Worker   ASSERT_STREQ(loaded_info.GetDexFiles()[0]->GetLocation().c_str(),
85*795d594fSAndroid Build Coastguard Worker                dex_files[0]->GetLocation().c_str());
86*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods().size(), 3u);
87*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[0].first, 0u);
88*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[0].second, 42u);
89*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[1].first, 1u);
90*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[1].second, 108u);
91*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[2].first, 1u);
92*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(loaded_info.GetMethods()[2].second, 54u);
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileBootInfoTest,LoadWrongDexFile)95*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileBootInfoTest, LoadWrongDexFile) {
96*795d594fSAndroid Build Coastguard Worker   ScratchFile profile;
97*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo saved_info;
100*795d594fSAndroid Build Coastguard Worker   saved_info.Add(dex.get(), 42);
101*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(profile.GetFile()->ResetOffset());
105*795d594fSAndroid Build Coastguard Worker   ProfileBootInfo loaded_info;
106*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
107*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> dex_files2;
108*795d594fSAndroid Build Coastguard Worker   dex_files2.reserve(dex_files.size());
109*795d594fSAndroid Build Coastguard Worker   for (const std::unique_ptr<const DexFile>& file : dex_files) {
110*795d594fSAndroid Build Coastguard Worker     dex_files2.push_back(file.get());
111*795d594fSAndroid Build Coastguard Worker   }
112*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(loaded_info.Load(profile.GetFd(), dex_files2));
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker 
115*795d594fSAndroid Build Coastguard Worker }  // namespace art
116