Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 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 <gtest/gtest.h> |
| 22 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 23 | #include "common_runtime_test.h" |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame^] | 24 | #include "dexopt_test.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame^] | 26 | #include "vdex_file.h" |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 27 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame^] | 30 | class OatFileTest : public DexoptTest { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation) { |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 34 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 35 | OatFile::ResolveRelativeEncodedDexLocation( |
| 36 | nullptr, "/data/app/foo/base.apk")); |
| 37 | |
| 38 | EXPECT_EQ(std::string("/system/framework/base.apk"), |
| 39 | OatFile::ResolveRelativeEncodedDexLocation( |
| 40 | "/data/app/foo/base.apk", "/system/framework/base.apk")); |
| 41 | |
| 42 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 43 | OatFile::ResolveRelativeEncodedDexLocation( |
| 44 | "/data/app/foo/base.apk", "base.apk")); |
| 45 | |
| 46 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 47 | OatFile::ResolveRelativeEncodedDexLocation( |
| 48 | "/data/app/foo/base.apk", "foo/base.apk")); |
| 49 | |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 50 | EXPECT_EQ(std::string("/data/app/foo/base.apk!classes2.dex"), |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 51 | OatFile::ResolveRelativeEncodedDexLocation( |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 52 | "/data/app/foo/base.apk", "base.apk!classes2.dex")); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 53 | |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 54 | EXPECT_EQ(std::string("/data/app/foo/base.apk!classes11.dex"), |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 55 | OatFile::ResolveRelativeEncodedDexLocation( |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 56 | "/data/app/foo/base.apk", "base.apk!classes11.dex")); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 57 | |
| 58 | EXPECT_EQ(std::string("base.apk"), |
| 59 | OatFile::ResolveRelativeEncodedDexLocation( |
| 60 | "/data/app/foo/sludge.apk", "base.apk")); |
| 61 | |
| 62 | EXPECT_EQ(std::string("o/base.apk"), |
| 63 | OatFile::ResolveRelativeEncodedDexLocation( |
| 64 | "/data/app/foo/base.apk", "o/base.apk")); |
| 65 | } |
| 66 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame^] | 67 | TEST_F(OatFileTest, LoadOat) { |
| 68 | std::string dex_location = GetScratchDir() + "/LoadOat.jar"; |
| 69 | |
| 70 | Copy(GetDexSrc1(), dex_location); |
| 71 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
| 72 | |
| 73 | std::string oat_location; |
| 74 | std::string error_msg; |
| 75 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 76 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 77 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(), |
| 78 | oat_location.c_str(), |
| 79 | nullptr, |
| 80 | nullptr, |
| 81 | false, |
| 82 | /*low_4gb*/false, |
| 83 | dex_location.c_str(), |
| 84 | &error_msg)); |
| 85 | ASSERT_TRUE(odex_file.get() != nullptr); |
| 86 | |
| 87 | // Check that the vdex file was loaded in the reserved space of odex file. |
| 88 | EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin()); |
| 89 | } |
| 90 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 91 | } // namespace art |