blob: 3bd6df2ef58ed55bc05e819453c4a7c5631615cb [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
Igor Murashkina315f5c2015-07-31 17:35:52 -070023#include "base/out.h"
Andreas Gampe7848da42015-04-09 11:15:04 -070024#include "common_runtime_test.h"
25#include "scoped_thread_state_change.h"
26
Richard Uhlere5fed032015-03-18 08:21:11 -070027namespace art {
28
Andreas Gampe7848da42015-04-09 11:15:04 -070029class OatFileTest : public CommonRuntimeTest {
30};
31
32TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation) {
Richard Uhlere5fed032015-03-18 08:21:11 -070033 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
34 OatFile::ResolveRelativeEncodedDexLocation(
35 nullptr, "/data/app/foo/base.apk"));
36
37 EXPECT_EQ(std::string("/system/framework/base.apk"),
38 OatFile::ResolveRelativeEncodedDexLocation(
39 "/data/app/foo/base.apk", "/system/framework/base.apk"));
40
41 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
42 OatFile::ResolveRelativeEncodedDexLocation(
43 "/data/app/foo/base.apk", "base.apk"));
44
45 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
46 OatFile::ResolveRelativeEncodedDexLocation(
47 "/data/app/foo/base.apk", "foo/base.apk"));
48
49 EXPECT_EQ(std::string("/data/app/foo/base.apk:classes2.dex"),
50 OatFile::ResolveRelativeEncodedDexLocation(
51 "/data/app/foo/base.apk", "base.apk:classes2.dex"));
52
53 EXPECT_EQ(std::string("/data/app/foo/base.apk:classes11.dex"),
54 OatFile::ResolveRelativeEncodedDexLocation(
55 "/data/app/foo/base.apk", "base.apk:classes11.dex"));
56
57 EXPECT_EQ(std::string("base.apk"),
58 OatFile::ResolveRelativeEncodedDexLocation(
59 "/data/app/foo/sludge.apk", "base.apk"));
60
61 EXPECT_EQ(std::string("o/base.apk"),
62 OatFile::ResolveRelativeEncodedDexLocation(
63 "/data/app/foo/base.apk", "o/base.apk"));
64}
65
Andreas Gampe7848da42015-04-09 11:15:04 -070066static std::vector<const DexFile*> ToConstDexFiles(
67 const std::vector<std::unique_ptr<const DexFile>>& in) {
68 std::vector<const DexFile*> ret;
69 for (auto& d : in) {
70 ret.push_back(d.get());
71 }
72 return ret;
73}
74
75TEST_F(OatFileTest, DexFileDependencies) {
76 std::string error_msg;
77
78 // No dependencies.
Igor Murashkina315f5c2015-07-31 17:35:52 -070079 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies(nullptr, outof(error_msg))) << error_msg;
80 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies("", outof(error_msg))) << error_msg;
Andreas Gampe7848da42015-04-09 11:15:04 -070081
82 // Ill-formed dependencies.
Igor Murashkina315f5c2015-07-31 17:35:52 -070083 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc", outof(error_msg)));
84 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc*123*def", outof(error_msg)));
85 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc*def*", outof(error_msg)));
Andreas Gampe7848da42015-04-09 11:15:04 -070086
87 // Unsatisfiable dependency.
Igor Murashkina315f5c2015-07-31 17:35:52 -070088 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc*123*", outof(error_msg)));
Andreas Gampe7848da42015-04-09 11:15:04 -070089
90 // Load some dex files to be able to do a real test.
91 ScopedObjectAccess soa(Thread::Current());
92
93 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Main");
94 std::vector<const DexFile*> dex_files_const1 = ToConstDexFiles(dex_files1);
95 std::string encoding1 = OatFile::EncodeDexFileDependencies(dex_files_const1);
Igor Murashkina315f5c2015-07-31 17:35:52 -070096 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies(encoding1.c_str(), outof(error_msg)))
Andreas Gampe7848da42015-04-09 11:15:04 -070097 << error_msg << " " << encoding1;
98 std::vector<std::string> split1;
Igor Murashkina315f5c2015-07-31 17:35:52 -070099 EXPECT_TRUE(OatFile::GetDexLocationsFromDependencies(encoding1.c_str(), outof(split1)));
Andreas Gampe7848da42015-04-09 11:15:04 -0700100 ASSERT_EQ(split1.size(), 1U);
101 EXPECT_EQ(split1[0], dex_files_const1[0]->GetLocation());
102
103 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
104 EXPECT_GT(dex_files2.size(), 1U);
105 std::vector<const DexFile*> dex_files_const2 = ToConstDexFiles(dex_files2);
106 std::string encoding2 = OatFile::EncodeDexFileDependencies(dex_files_const2);
Igor Murashkina315f5c2015-07-31 17:35:52 -0700107 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies(encoding2.c_str(), outof(error_msg)))
Andreas Gampe7848da42015-04-09 11:15:04 -0700108 << error_msg << " " << encoding2;
109 std::vector<std::string> split2;
Igor Murashkina315f5c2015-07-31 17:35:52 -0700110 EXPECT_TRUE(OatFile::GetDexLocationsFromDependencies(encoding2.c_str(), outof(split2)));
Andreas Gampe7848da42015-04-09 11:15:04 -0700111 ASSERT_EQ(split2.size(), 2U);
112 EXPECT_EQ(split2[0], dex_files_const2[0]->GetLocation());
113 EXPECT_EQ(split2[1], dex_files_const2[1]->GetLocation());
114}
115
Richard Uhlere5fed032015-03-18 08:21:11 -0700116} // namespace art