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   for (const std::unique_ptr<const DexFile>& file : dex_files) {
70     dex_files2.push_back(file.get());
71   }
72 
73   ProfileBootInfo saved_info;
74   saved_info.Add(dex_files[0].get(), 42);
75   saved_info.Add(dex_files[1].get(), 108);
76   saved_info.Add(dex_files[1].get(), 54);
77   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
78   ASSERT_TRUE(profile.GetFile()->ResetOffset());
79 
80   ProfileBootInfo loaded_info;
81   ASSERT_TRUE(loaded_info.Load(profile.GetFd(), dex_files2));
82   ASSERT_EQ(loaded_info.GetDexFiles().size(), 2u);
83   ASSERT_STREQ(loaded_info.GetDexFiles()[0]->GetLocation().c_str(),
84                dex_files[0]->GetLocation().c_str());
85   ASSERT_EQ(loaded_info.GetMethods().size(), 3u);
86   ASSERT_EQ(loaded_info.GetMethods()[0].first, 0u);
87   ASSERT_EQ(loaded_info.GetMethods()[0].second, 42u);
88   ASSERT_EQ(loaded_info.GetMethods()[1].first, 1u);
89   ASSERT_EQ(loaded_info.GetMethods()[1].second, 108u);
90   ASSERT_EQ(loaded_info.GetMethods()[2].first, 1u);
91   ASSERT_EQ(loaded_info.GetMethods()[2].second, 54u);
92 }
93 
TEST_F(ProfileBootInfoTest,LoadWrongDexFile)94 TEST_F(ProfileBootInfoTest, LoadWrongDexFile) {
95   ScratchFile profile;
96   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
97 
98   ProfileBootInfo saved_info;
99   saved_info.Add(dex.get(), 42);
100   ASSERT_TRUE(saved_info.Save(profile.GetFd()));
101 
102 
103   ASSERT_TRUE(profile.GetFile()->ResetOffset());
104   ProfileBootInfo loaded_info;
105   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
106   std::vector<const DexFile*> dex_files2;
107   for (const std::unique_ptr<const DexFile>& file : dex_files) {
108     dex_files2.push_back(file.get());
109   }
110   ASSERT_FALSE(loaded_info.Load(profile.GetFd(), dex_files2));
111 }
112 
113 }  // namespace art
114