1 /*
2 * Copyright (C) 2017 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 <gtest/gtest.h>
18
19 #include "dexopt_test.h"
20
21 namespace art {
22 namespace gc {
23 namespace space {
24
TEST_F(DexoptTest,ValidateOatFile)25 TEST_F(DexoptTest, ValidateOatFile) {
26 std::string dex1 = GetScratchDir() + "/Dex1.jar";
27 std::string multidex1 = GetScratchDir() + "/MultiDex1.jar";
28 std::string dex2 = GetScratchDir() + "/Dex2.jar";
29 std::string oat_location = GetScratchDir() + "/Oat.oat";
30
31 Copy(GetDexSrc1(), dex1);
32 Copy(GetMultiDexSrc1(), multidex1);
33 Copy(GetDexSrc2(), dex2);
34
35 std::string error_msg;
36 std::vector<std::string> args;
37 args.push_back("--dex-file=" + dex1);
38 args.push_back("--dex-file=" + multidex1);
39 args.push_back("--dex-file=" + dex2);
40 args.push_back("--oat-file=" + oat_location);
41 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
42
43 std::unique_ptr<OatFile> oat(OatFile::Open(oat_location.c_str(),
44 oat_location.c_str(),
45 nullptr,
46 nullptr,
47 false,
48 /*low_4gb*/false,
49 nullptr,
50 &error_msg));
51 ASSERT_TRUE(oat != nullptr) << error_msg;
52
53 // Originally all the dex checksums should be up to date.
54 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
55
56 // Invalidate the dex1 checksum.
57 Copy(GetDexSrc2(), dex1);
58 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
59
60 // Restore the dex1 checksum.
61 Copy(GetDexSrc1(), dex1);
62 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
63
64 // Invalidate the non-main multidex checksum.
65 Copy(GetMultiDexSrc2(), multidex1);
66 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
67
68 // Restore the multidex checksum.
69 Copy(GetMultiDexSrc1(), multidex1);
70 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
71
72 // Invalidate the dex2 checksum.
73 Copy(GetDexSrc1(), dex2);
74 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
75
76 // restore the dex2 checksum.
77 Copy(GetDexSrc2(), dex2);
78 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
79
80 // Replace the multidex file with a non-multidex file.
81 Copy(GetDexSrc1(), multidex1);
82 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
83
84 // Restore the multidex file
85 Copy(GetMultiDexSrc1(), multidex1);
86 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
87
88 // Replace dex1 with a multidex file.
89 Copy(GetMultiDexSrc1(), dex1);
90 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
91
92 // Restore the dex1 file.
93 Copy(GetDexSrc1(), dex1);
94 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
95
96 // Remove the dex2 file.
97 EXPECT_EQ(0, unlink(dex2.c_str()));
98 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
99
100 // Restore the dex2 file.
101 Copy(GetDexSrc2(), dex2);
102 EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
103
104 // Remove the multidex file.
105 EXPECT_EQ(0, unlink(multidex1.c_str()));
106 EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
107 }
108
109 } // namespace space
110 } // namespace gc
111 } // namespace art
112