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