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 
23 #include "common_runtime_test.h"
24 #include "dexopt_test.h"
25 #include "scoped_thread_state_change-inl.h"
26 #include "vdex_file.h"
27 
28 namespace art {
29 
30 class OatFileTest : public DexoptTest {
31 };
32 
TEST_F(OatFileTest,LoadOat)33 TEST_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;
43   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
44                                                    oat_location.c_str(),
45                                                    oat_location.c_str(),
46                                                    /*executable=*/ false,
47                                                    /*low_4gb=*/ false,
48                                                    dex_location,
49                                                    &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 
TEST_F(OatFileTest,ChangingMultiDexUncompressed)56 TEST_F(OatFileTest, ChangingMultiDexUncompressed) {
57   std::string dex_location = GetScratchDir() + "/MultiDexUncompressedAligned.jar";
58 
59   Copy(GetTestDexFileName("MultiDexUncompressedAligned"), dex_location);
60   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   {
69     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
70                                                      oat_location.c_str(),
71                                                      oat_location.c_str(),
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