• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
18 #include <stdio.h>
19 
20 #include "base/arena_allocator.h"
21 #include "base/common_art_test.h"
22 #include "base/unix_file/fd_file.h"
23 #include "dex/dex_file.h"
24 #include "profile/profile_boot_info.h"
25 
26 namespace art {
27 
28 class ProfileBootInfoTest : public CommonArtTest {
29  public:
SetUp()30   void SetUp() override {
31     CommonArtTest::SetUp();
32   }
33 };
34 
35 
TEST_F(ProfileBootInfoTest,LoadEmpty)36 TEST_F(ProfileBootInfoTest, LoadEmpty) {
37   ScratchFile profile;
38   std::vector<const DexFile*> dex_files;
39 
40   ProfileBootInfo loaded_info;
41   ASSERT_TRUE(loaded_info.IsEmpty());
42   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files));
43   ASSERT_TRUE(loaded_info.IsEmpty());
44 }
45 
TEST_F(ProfileBootInfoTest,OneMethod)46 TEST_F(ProfileBootInfoTest, OneMethod) {
47   ScratchFile profile;
48   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
49   std::vector<const DexFile*> dex_files = { dex.get() };
50 
51   ProfileBootInfo saved_info;
52   saved_info.Add(dex.get(), 0);
53   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
54   ASSERT_TRUE(profile.GetFile()->ResetOffset());
55 
56   ProfileBootInfo loaded_info;
57   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files));
58   ASSERT_EQ(loaded_info.GetDexFiles().size(), 1u);
59   ASSERT_STREQ(loaded_info.GetDexFiles()[0]->GetLocation().c_str(), dex->GetLocation().c_str());
60   ASSERT_EQ(loaded_info.GetMethods().size(), 1u);
61   ASSERT_EQ(loaded_info.GetMethods()[0].first, 0u);
62   ASSERT_EQ(loaded_info.GetMethods()[0].second, 0u);
63 }
64 
TEST_F(ProfileBootInfoTest,ManyDexFiles)65 TEST_F(ProfileBootInfoTest, ManyDexFiles) {
66   ScratchFile profile;
67   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
68   std::vector<const DexFile*> dex_files2;
69   dex_files2.reserve(dex_files.size());
70   for (const std::unique_ptr<const DexFile>& file : dex_files) {
71     dex_files2.push_back(file.get());
72   }
73 
74   ProfileBootInfo saved_info;
75   saved_info.Add(dex_files[0].get(), 42);
76   saved_info.Add(dex_files[1].get(), 108);
77   saved_info.Add(dex_files[1].get(), 54);
78   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
79   ASSERT_TRUE(profile.GetFile()->ResetOffset());
80 
81   ProfileBootInfo loaded_info;
82   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files2));
83   ASSERT_EQ(loaded_info.GetDexFiles().size(), 2u);
84   ASSERT_STREQ(loaded_info.GetDexFiles()[0]->GetLocation().c_str(),
85                dex_files[0]->GetLocation().c_str());
86   ASSERT_EQ(loaded_info.GetMethods().size(), 3u);
87   ASSERT_EQ(loaded_info.GetMethods()[0].first, 0u);
88   ASSERT_EQ(loaded_info.GetMethods()[0].second, 42u);
89   ASSERT_EQ(loaded_info.GetMethods()[1].first, 1u);
90   ASSERT_EQ(loaded_info.GetMethods()[1].second, 108u);
91   ASSERT_EQ(loaded_info.GetMethods()[2].first, 1u);
92   ASSERT_EQ(loaded_info.GetMethods()[2].second, 54u);
93 }
94 
TEST_F(ProfileBootInfoTest,LoadWrongDexFile)95 TEST_F(ProfileBootInfoTest, LoadWrongDexFile) {
96   ScratchFile profile;
97   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
98 
99   ProfileBootInfo saved_info;
100   saved_info.Add(dex.get(), 42);
101   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
102 
103 
104   ASSERT_TRUE(profile.GetFile()->ResetOffset());
105   ProfileBootInfo loaded_info;
106   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
107   std::vector<const DexFile*> dex_files2;
108   dex_files2.reserve(dex_files.size());
109   for (const std::unique_ptr<const DexFile>& file : dex_files) {
110     dex_files2.push_back(file.get());
111   }
112   ASSERT_FALSE(loaded_info.Load(profile.GetFd(), dex_files2));
113 }
114 
115 }  // namespace art
116