1 /*
2  * Copyright (C) 2015 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 "oat_file.h"
18 
19 #include <string>
20 
21 #include "common_runtime_test.h"
22 #include "dexopt_test.h"
23 #include "gtest/gtest.h"
24 #include "scoped_thread_state_change-inl.h"
25 #include "vdex_file.h"
26 
27 namespace art HIDDEN {
28 
29 class OatFileTest : public DexoptTest {};
30 
TEST_F(OatFileTest,LoadOat)31 TEST_F(OatFileTest, LoadOat) {
32   std::string dex_location = GetScratchDir() + "/LoadOat.jar";
33 
34   Copy(GetDexSrc1(), dex_location);
35   GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
36 
37   std::string oat_location;
38   std::string error_msg;
39   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
40       dex_location, kRuntimeISA, &oat_location, &error_msg))
41       << error_msg;
42   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
43                                                    oat_location,
44                                                    oat_location,
45                                                    /*executable=*/false,
46                                                    /*low_4gb=*/false,
47                                                    dex_location,
48                                                    &error_msg));
49   ASSERT_TRUE(odex_file.get() != nullptr);
50 
51   // Check that the vdex file was loaded in the reserved space of odex file.
52   EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin());
53 }
54 
TEST_F(OatFileTest,ChangingMultiDexUncompressed)55 TEST_F(OatFileTest, ChangingMultiDexUncompressed) {
56   std::string dex_location = GetScratchDir() + "/MultiDexUncompressedAligned.jar";
57 
58   Copy(GetTestDexFileName("MultiDexUncompressedAligned"), dex_location);
59   GenerateOatForTest(dex_location.c_str(), CompilerFilter::kVerify);
60 
61   std::string oat_location;
62   std::string error_msg;
63   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
64       dex_location, kRuntimeISA, &oat_location, &error_msg))
65       << error_msg;
66 
67   // Ensure we can load that file. Just a precondition.
68   {
69     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
70                                                      oat_location,
71                                                      oat_location,
72                                                      /*executable=*/false,
73                                                      /*low_4gb=*/false,
74                                                      dex_location,
75                                                      &error_msg));
76     ASSERT_TRUE(odex_file != nullptr);
77     ASSERT_EQ(2u, odex_file->GetOatDexFiles().size());
78   }
79 
80   // Now replace the source.
81   Copy(GetTestDexFileName("MainUncompressedAligned"), dex_location);
82 
83   // And try to load again.
84   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
85                                                    oat_location,
86                                                    oat_location,
87                                                    /*executable=*/false,
88                                                    /*low_4gb=*/false,
89                                                    dex_location,
90                                                    &error_msg));
91   EXPECT_TRUE(odex_file == nullptr);
92   EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1"))
93       << error_msg;
94 }
95 
96 }  // namespace art
97