blob: a88553ca7b7b74b0faebe48ebfbd3c0a30884750 [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"
24#include "scoped_thread_state_change.h"
25
Richard Uhlere5fed032015-03-18 08:21:11 -070026namespace art {
27
Andreas Gampe7848da42015-04-09 11:15:04 -070028class OatFileTest : public CommonRuntimeTest {
29};
30
31TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation) {
Richard Uhlere5fed032015-03-18 08:21:11 -070032 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
33 OatFile::ResolveRelativeEncodedDexLocation(
34 nullptr, "/data/app/foo/base.apk"));
35
36 EXPECT_EQ(std::string("/system/framework/base.apk"),
37 OatFile::ResolveRelativeEncodedDexLocation(
38 "/data/app/foo/base.apk", "/system/framework/base.apk"));
39
40 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
41 OatFile::ResolveRelativeEncodedDexLocation(
42 "/data/app/foo/base.apk", "base.apk"));
43
44 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
45 OatFile::ResolveRelativeEncodedDexLocation(
46 "/data/app/foo/base.apk", "foo/base.apk"));
47
48 EXPECT_EQ(std::string("/data/app/foo/base.apk:classes2.dex"),
49 OatFile::ResolveRelativeEncodedDexLocation(
50 "/data/app/foo/base.apk", "base.apk:classes2.dex"));
51
52 EXPECT_EQ(std::string("/data/app/foo/base.apk:classes11.dex"),
53 OatFile::ResolveRelativeEncodedDexLocation(
54 "/data/app/foo/base.apk", "base.apk:classes11.dex"));
55
56 EXPECT_EQ(std::string("base.apk"),
57 OatFile::ResolveRelativeEncodedDexLocation(
58 "/data/app/foo/sludge.apk", "base.apk"));
59
60 EXPECT_EQ(std::string("o/base.apk"),
61 OatFile::ResolveRelativeEncodedDexLocation(
62 "/data/app/foo/base.apk", "o/base.apk"));
63}
64
Andreas Gampe7848da42015-04-09 11:15:04 -070065static std::vector<const DexFile*> ToConstDexFiles(
66 const std::vector<std::unique_ptr<const DexFile>>& in) {
67 std::vector<const DexFile*> ret;
68 for (auto& d : in) {
69 ret.push_back(d.get());
70 }
71 return ret;
72}
73
74TEST_F(OatFileTest, DexFileDependencies) {
75 std::string error_msg;
76
77 // No dependencies.
Igor Murashkinb1d8c312015-08-04 11:18:43 -070078 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies(nullptr, &error_msg)) << error_msg;
79 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies("", &error_msg)) << error_msg;
Andreas Gampe7848da42015-04-09 11:15:04 -070080
81 // Ill-formed dependencies.
Igor Murashkinb1d8c312015-08-04 11:18:43 -070082 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc", &error_msg));
83 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc*123*def", &error_msg));
84 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc*def*", &error_msg));
Andreas Gampe7848da42015-04-09 11:15:04 -070085
86 // Unsatisfiable dependency.
Igor Murashkinb1d8c312015-08-04 11:18:43 -070087 EXPECT_FALSE(OatFile::CheckStaticDexFileDependencies("abc*123*", &error_msg));
Andreas Gampe7848da42015-04-09 11:15:04 -070088
89 // Load some dex files to be able to do a real test.
90 ScopedObjectAccess soa(Thread::Current());
91
92 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Main");
93 std::vector<const DexFile*> dex_files_const1 = ToConstDexFiles(dex_files1);
94 std::string encoding1 = OatFile::EncodeDexFileDependencies(dex_files_const1);
Igor Murashkinb1d8c312015-08-04 11:18:43 -070095 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies(encoding1.c_str(), &error_msg))
Andreas Gampe7848da42015-04-09 11:15:04 -070096 << error_msg << " " << encoding1;
97 std::vector<std::string> split1;
Igor Murashkinb1d8c312015-08-04 11:18:43 -070098 EXPECT_TRUE(OatFile::GetDexLocationsFromDependencies(encoding1.c_str(), &split1));
Andreas Gampe7848da42015-04-09 11:15:04 -070099 ASSERT_EQ(split1.size(), 1U);
100 EXPECT_EQ(split1[0], dex_files_const1[0]->GetLocation());
101
102 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
103 EXPECT_GT(dex_files2.size(), 1U);
104 std::vector<const DexFile*> dex_files_const2 = ToConstDexFiles(dex_files2);
105 std::string encoding2 = OatFile::EncodeDexFileDependencies(dex_files_const2);
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700106 EXPECT_TRUE(OatFile::CheckStaticDexFileDependencies(encoding2.c_str(), &error_msg))
Andreas Gampe7848da42015-04-09 11:15:04 -0700107 << error_msg << " " << encoding2;
108 std::vector<std::string> split2;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700109 EXPECT_TRUE(OatFile::GetDexLocationsFromDependencies(encoding2.c_str(), &split2));
Andreas Gampe7848da42015-04-09 11:15:04 -0700110 ASSERT_EQ(split2.size(), 2U);
111 EXPECT_EQ(split2[0], dex_files_const2[0]->GetLocation());
112 EXPECT_EQ(split2[1], dex_files_const2[1]->GetLocation());
113}
114
Richard Uhlere5fed032015-03-18 08:21:11 -0700115} // namespace art