blob: 8222a8aaaefa59f437780dc5157696dee95ca4ee [file] [log] [blame]
Richard Uhlere5fed032015-03-18 08:21:11 -07001/*
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 <gtest/gtest.h>
22
Andreas Gampe7848da42015-04-09 11:15:04 -070023#include "common_runtime_test.h"
David Srbeckyec2cdf42017-12-08 16:21:25 +000024#include "dexopt_test.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
David Srbeckyec2cdf42017-12-08 16:21:25 +000026#include "vdex_file.h"
Andreas Gampe7848da42015-04-09 11:15:04 -070027
Richard Uhlere5fed032015-03-18 08:21:11 -070028namespace art {
29
David Srbeckyec2cdf42017-12-08 16:21:25 +000030class OatFileTest : public DexoptTest {
Andreas Gampe7848da42015-04-09 11:15:04 -070031};
32
David Srbeckyec2cdf42017-12-08 16:21:25 +000033TEST_F(OatFileTest, LoadOat) {
34 std::string dex_location = GetScratchDir() + "/LoadOat.jar";
35
36 Copy(GetDexSrc1(), dex_location);
37 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
38
39 std::string oat_location;
40 std::string error_msg;
41 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
42 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +010043 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +010044 oat_location.c_str(),
David Srbeckyec2cdf42017-12-08 16:21:25 +000045 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +010046 /*executable=*/ false,
47 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +000048 dex_location,
David Srbeckyec2cdf42017-12-08 16:21:25 +000049 &error_msg));
50 ASSERT_TRUE(odex_file.get() != nullptr);
51
52 // Check that the vdex file was loaded in the reserved space of odex file.
53 EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin());
54}
55
Andreas Gampefc604a72018-02-08 15:43:37 -080056TEST_F(OatFileTest, ChangingMultiDexUncompressed) {
David Srbecky8c8f1482020-02-05 20:17:10 +000057 std::string dex_location = GetScratchDir() + "/MultiDexUncompressedAligned.jar";
Andreas Gampefc604a72018-02-08 15:43:37 -080058
David Srbecky8c8f1482020-02-05 20:17:10 +000059 Copy(GetTestDexFileName("MultiDexUncompressedAligned"), dex_location);
Andreas Gampefc604a72018-02-08 15:43:37 -080060 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
61
62 std::string oat_location;
63 std::string error_msg;
64 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
65 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
66
67 // Ensure we can load that file. Just a precondition.
68 {
Vladimir Markof4efa9e2018-10-17 14:12:45 +010069 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +010070 oat_location.c_str(),
Andreas Gampefc604a72018-02-08 15:43:37 -080071 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +010072 /*executable=*/ false,
73 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +000074 dex_location,
Andreas Gampefc604a72018-02-08 15:43:37 -080075 &error_msg));
76 ASSERT_TRUE(odex_file != nullptr);
77 ASSERT_EQ(2u, odex_file->GetOatDexFiles().size());
78 }
79
80 // Now replace the source.
David Srbecky8c8f1482020-02-05 20:17:10 +000081 Copy(GetTestDexFileName("MainUncompressedAligned"), dex_location);
Andreas Gampefc604a72018-02-08 15:43:37 -080082
83 // And try to load again.
Vladimir Markof4efa9e2018-10-17 14:12:45 +010084 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Vladimir Markoc09cd052018-08-23 16:36:36 +010085 oat_location,
86 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010087 /*executable=*/ false,
88 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +000089 dex_location,
Andreas Gampefc604a72018-02-08 15:43:37 -080090 &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
Richard Uhlere5fed032015-03-18 08:21:11 -070096} // namespace art