blob: a87552d5dca95440ff27eed6f00d0f8e9dc94fde [file] [log] [blame]
Calin Juravle87e2cb62017-06-13 21:48:45 -07001/*
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
20#include "class_loader_context.h"
21#include "common_runtime_test.h"
22
23#include "base/dchecked_vector.h"
24#include "base/stl_util.h"
25#include "class_linker.h"
26#include "dex_file.h"
27#include "handle_scope-inl.h"
28#include "mirror/class.h"
29#include "mirror/class_loader.h"
30#include "mirror/object-inl.h"
31#include "oat_file_assistant.h"
32#include "runtime.h"
33#include "scoped_thread_state_change-inl.h"
34#include "thread.h"
35#include "well_known_classes.h"
36
37namespace art {
38
39class ClassLoaderContextTest : public CommonRuntimeTest {
40 public:
41 void VerifyContextSize(ClassLoaderContext* context, size_t expected_size) {
42 ASSERT_TRUE(context != nullptr);
43 ASSERT_EQ(expected_size, context->class_loader_chain_.size());
44 }
45
46 void VerifyClassLoaderPCL(ClassLoaderContext* context,
47 size_t index,
Calin Juravle57d0acc2017-07-11 17:41:30 -070048 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070049 VerifyClassLoaderInfo(
50 context, index, ClassLoaderContext::kPathClassLoader, classpath);
51 }
52
53 void VerifyClassLoaderDLC(ClassLoaderContext* context,
54 size_t index,
Calin Juravle57d0acc2017-07-11 17:41:30 -070055 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070056 VerifyClassLoaderInfo(
57 context, index, ClassLoaderContext::kDelegateLastClassLoader, classpath);
58 }
59
Calin Juravle57d0acc2017-07-11 17:41:30 -070060 void VerifyClassLoaderPCLFromTestDex(ClassLoaderContext* context,
61 size_t index,
62 const std::string& test_name) {
63 VerifyClassLoaderFromTestDex(
64 context, index, ClassLoaderContext::kPathClassLoader, test_name);
65 }
66
67 void VerifyClassLoaderDLCFromTestDex(ClassLoaderContext* context,
68 size_t index,
69 const std::string& test_name) {
70 VerifyClassLoaderFromTestDex(
71 context, index, ClassLoaderContext::kDelegateLastClassLoader, test_name);
72 }
73
Calin Juravle87e2cb62017-06-13 21:48:45 -070074 void VerifyOpenDexFiles(
75 ClassLoaderContext* context,
76 size_t index,
77 std::vector<std::vector<std::unique_ptr<const DexFile>>*>& all_dex_files) {
78 ASSERT_TRUE(context != nullptr);
79 ASSERT_TRUE(context->dex_files_open_attempted_);
80 ASSERT_TRUE(context->dex_files_open_result_);
81 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
82 ASSERT_EQ(all_dex_files.size(), info.classpath.size());
83 size_t cur_open_dex_index = 0;
84 for (size_t k = 0; k < all_dex_files.size(); k++) {
85 std::vector<std::unique_ptr<const DexFile>>& dex_files_for_cp_elem = *(all_dex_files[k]);
86 for (size_t i = 0; i < dex_files_for_cp_elem.size(); i++) {
87 ASSERT_LT(cur_open_dex_index, info.opened_dex_files.size());
88
89 std::unique_ptr<const DexFile>& opened_dex_file =
90 info.opened_dex_files[cur_open_dex_index++];
91 std::unique_ptr<const DexFile>& expected_dex_file = dex_files_for_cp_elem[i];
92
93 ASSERT_EQ(expected_dex_file->GetLocation(), opened_dex_file->GetLocation());
94 ASSERT_EQ(expected_dex_file->GetLocationChecksum(), opened_dex_file->GetLocationChecksum());
95 ASSERT_EQ(info.classpath[k], opened_dex_file->GetBaseLocation());
96 }
97 }
98 }
99
Calin Juravle57d0acc2017-07-11 17:41:30 -0700100 std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader) {
101 return ClassLoaderContext::CreateContextForClassLoader(class_loader, nullptr);
102 }
103
Calin Juravle3f918642017-07-11 19:04:20 -0700104 std::unique_ptr<ClassLoaderContext> ParseContextWithChecksums(const std::string& context_spec) {
105 std::unique_ptr<ClassLoaderContext> context(new ClassLoaderContext());
106 if (!context->Parse(context_spec, /*parse_checksums*/ true)) {
107 return nullptr;
108 }
109 return context;
110 }
111
Calin Juravle57d0acc2017-07-11 17:41:30 -0700112 void VerifyContextForClassLoader(ClassLoaderContext* context) {
113 ASSERT_TRUE(context != nullptr);
114 ASSERT_TRUE(context->dex_files_open_attempted_);
115 ASSERT_TRUE(context->dex_files_open_result_);
116 ASSERT_FALSE(context->owns_the_dex_files_);
117 ASSERT_FALSE(context->special_shared_library_);
118 }
119
Calin Juravlec79470d2017-07-12 17:37:42 -0700120 void VerifyClassLoaderDexFiles(ScopedObjectAccess& soa,
121 Handle<mirror::ClassLoader> class_loader,
122 jclass type,
123 std::vector<const DexFile*>& expected_dex_files)
124 REQUIRES_SHARED(Locks::mutator_lock_) {
125 ASSERT_TRUE(class_loader->GetClass() == soa.Decode<mirror::Class>(type));
126
127 std::vector<const DexFile*> class_loader_dex_files = GetDexFiles(soa, class_loader);
128 ASSERT_EQ(expected_dex_files.size(), class_loader_dex_files.size());
129
130 for (size_t i = 0; i < expected_dex_files.size(); i++) {
131 ASSERT_EQ(expected_dex_files[i]->GetLocation(),
132 class_loader_dex_files[i]->GetLocation());
133 ASSERT_EQ(expected_dex_files[i]->GetLocationChecksum(),
134 class_loader_dex_files[i]->GetLocationChecksum());
135 }
136 }
137
Calin Juravle87e2cb62017-06-13 21:48:45 -0700138 private:
139 void VerifyClassLoaderInfo(ClassLoaderContext* context,
140 size_t index,
141 ClassLoaderContext::ClassLoaderType type,
Calin Juravle57d0acc2017-07-11 17:41:30 -0700142 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700143 ASSERT_TRUE(context != nullptr);
144 ASSERT_GT(context->class_loader_chain_.size(), index);
145 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
146 ASSERT_EQ(type, info.type);
147 std::vector<std::string> expected_classpath;
148 Split(classpath, ':', &expected_classpath);
149 ASSERT_EQ(expected_classpath, info.classpath);
150 }
Calin Juravle57d0acc2017-07-11 17:41:30 -0700151
152 void VerifyClassLoaderFromTestDex(ClassLoaderContext* context,
153 size_t index,
154 ClassLoaderContext::ClassLoaderType type,
155 const std::string& test_name) {
156 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(test_name.c_str());
157 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files;
158 all_dex_files.push_back(&dex_files);
159
160 VerifyClassLoaderInfo(context, index, type, GetTestDexFileName(test_name.c_str()));
161 VerifyOpenDexFiles(context, index, all_dex_files);
162 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700163};
164
165TEST_F(ClassLoaderContextTest, ParseValidContextPCL) {
166 std::unique_ptr<ClassLoaderContext> context =
167 ClassLoaderContext::Create("PCL[a.dex]");
168 VerifyContextSize(context.get(), 1);
169 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
170}
171
172TEST_F(ClassLoaderContextTest, ParseValidContextDLC) {
173 std::unique_ptr<ClassLoaderContext> context =
174 ClassLoaderContext::Create("DLC[a.dex]");
175 VerifyContextSize(context.get(), 1);
176 VerifyClassLoaderDLC(context.get(), 0, "a.dex");
177}
178
179TEST_F(ClassLoaderContextTest, ParseValidContextChain) {
180 std::unique_ptr<ClassLoaderContext> context =
181 ClassLoaderContext::Create("PCL[a.dex:b.dex];DLC[c.dex:d.dex];PCL[e.dex]");
182 VerifyContextSize(context.get(), 3);
183 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
184 VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex");
185 VerifyClassLoaderPCL(context.get(), 2, "e.dex");
186}
187
188TEST_F(ClassLoaderContextTest, ParseValidEmptyContextDLC) {
189 std::unique_ptr<ClassLoaderContext> context =
190 ClassLoaderContext::Create("DLC[]");
191 VerifyContextSize(context.get(), 1);
192 VerifyClassLoaderDLC(context.get(), 0, "");
193}
194
195TEST_F(ClassLoaderContextTest, ParseValidContextSpecialSymbol) {
196 std::unique_ptr<ClassLoaderContext> context =
197 ClassLoaderContext::Create(OatFile::kSpecialSharedLibrary);
198 VerifyContextSize(context.get(), 0);
199}
200
201TEST_F(ClassLoaderContextTest, ParseInvalidValidContexts) {
202 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("ABC[a.dex]"));
203 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL"));
204 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex"));
205 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCLa.dex]"));
206 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL{a.dex}"));
207 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex];DLC[b.dex"));
208}
209
210TEST_F(ClassLoaderContextTest, OpenInvalidDexFiles) {
211 std::unique_ptr<ClassLoaderContext> context =
212 ClassLoaderContext::Create("PCL[does_not_exist.dex]");
213 VerifyContextSize(context.get(), 1);
214 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, "."));
215}
216
217TEST_F(ClassLoaderContextTest, OpenValidDexFiles) {
218 std::string multidex_name = GetTestDexFileName("MultiDex");
219 std::vector<std::unique_ptr<const DexFile>> multidex_files = OpenTestDexFiles("MultiDex");
220 std::string myclass_dex_name = GetTestDexFileName("MyClass");
221 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
222 std::string dex_name = GetTestDexFileName("Main");
223 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Main");
224
225
226 std::unique_ptr<ClassLoaderContext> context =
227 ClassLoaderContext::Create(
228 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
229 "DLC[" + dex_name + "]");
230
231 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
232
233 VerifyContextSize(context.get(), 2);
234 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files0;
235 all_dex_files0.push_back(&multidex_files);
236 all_dex_files0.push_back(&myclass_dex_files);
237 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files1;
238 all_dex_files1.push_back(&dex_files);
239
240 VerifyOpenDexFiles(context.get(), 0, all_dex_files0);
241 VerifyOpenDexFiles(context.get(), 1, all_dex_files1);
242}
243
244TEST_F(ClassLoaderContextTest, OpenInvalidDexFilesMix) {
245 std::string dex_name = GetTestDexFileName("Main");
246 std::unique_ptr<ClassLoaderContext> context =
247 ClassLoaderContext::Create("PCL[does_not_exist.dex];DLC[" + dex_name + "]");
248 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, ""));
249}
250
251TEST_F(ClassLoaderContextTest, CreateClassLoader) {
252 std::string dex_name = GetTestDexFileName("Main");
253 std::unique_ptr<ClassLoaderContext> context =
254 ClassLoaderContext::Create("PCL[" + dex_name + "]");
255 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
256
257 std::vector<std::unique_ptr<const DexFile>> classpath_dex = OpenTestDexFiles("Main");
258 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
259
260 std::vector<const DexFile*> compilation_sources_raw =
261 MakeNonOwningPointerVector(compilation_sources);
262 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
263 ASSERT_TRUE(jclass_loader != nullptr);
264
265 ScopedObjectAccess soa(Thread::Current());
266
Calin Juravlec79470d2017-07-12 17:37:42 -0700267 StackHandleScope<1> hs(soa.Self());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700268 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
269 soa.Decode<mirror::ClassLoader>(jclass_loader));
270
271 ASSERT_TRUE(class_loader->GetClass() ==
272 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader));
273 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
274 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
275
Calin Juravlec79470d2017-07-12 17:37:42 -0700276 // For the first class loader the class path dex files must come first and then the
277 // compilation sources.
278 std::vector<const DexFile*> expected_classpath = MakeNonOwningPointerVector(classpath_dex);
279 for (auto& dex : compilation_sources_raw) {
280 expected_classpath.push_back(dex);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700281 }
282
Calin Juravlec79470d2017-07-12 17:37:42 -0700283 VerifyClassLoaderDexFiles(soa,
284 class_loader,
285 WellKnownClasses::dalvik_system_PathClassLoader,
286 expected_classpath);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700287}
288
Calin Juravle7b0648a2017-07-07 18:40:50 -0700289TEST_F(ClassLoaderContextTest, CreateClassLoaderWithEmptyContext) {
290 std::unique_ptr<ClassLoaderContext> context =
291 ClassLoaderContext::Create("");
292 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
293
294 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
295
296 std::vector<const DexFile*> compilation_sources_raw =
297 MakeNonOwningPointerVector(compilation_sources);
298 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
299 ASSERT_TRUE(jclass_loader != nullptr);
300
301 ScopedObjectAccess soa(Thread::Current());
302
Calin Juravlec79470d2017-07-12 17:37:42 -0700303 StackHandleScope<1> hs(soa.Self());
Calin Juravle7b0648a2017-07-07 18:40:50 -0700304 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
305 soa.Decode<mirror::ClassLoader>(jclass_loader));
306
Calin Juravlec79470d2017-07-12 17:37:42 -0700307 // An empty context should create a single PathClassLoader with only the compilation sources.
308 VerifyClassLoaderDexFiles(soa,
309 class_loader,
310 WellKnownClasses::dalvik_system_PathClassLoader,
311 compilation_sources_raw);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700312 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
313 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700314}
315
Calin Juravlec79470d2017-07-12 17:37:42 -0700316TEST_F(ClassLoaderContextTest, CreateClassLoaderWithComplexChain) {
317 // Setup the context.
318 std::vector<std::unique_ptr<const DexFile>> classpath_dex_a = OpenTestDexFiles("ForClassLoaderA");
319 std::vector<std::unique_ptr<const DexFile>> classpath_dex_b = OpenTestDexFiles("ForClassLoaderB");
320 std::vector<std::unique_ptr<const DexFile>> classpath_dex_c = OpenTestDexFiles("ForClassLoaderC");
321 std::vector<std::unique_ptr<const DexFile>> classpath_dex_d = OpenTestDexFiles("ForClassLoaderD");
322
323 std::string context_spec =
324 "PCL[" + CreateClassPath(classpath_dex_a) + ":" + CreateClassPath(classpath_dex_b) + "];" +
325 "DLC[" + CreateClassPath(classpath_dex_c) + "];" +
326 "PCL[" + CreateClassPath(classpath_dex_d) + "]";
327
328 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_spec);
329 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
330
331 // Setup the compilation sources.
332 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
333 std::vector<const DexFile*> compilation_sources_raw =
334 MakeNonOwningPointerVector(compilation_sources);
335
336 // Create the class loader.
337 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
338 ASSERT_TRUE(jclass_loader != nullptr);
339
340 // Verify the class loader.
341 ScopedObjectAccess soa(Thread::Current());
342
343 StackHandleScope<3> hs(soa.Self());
344 Handle<mirror::ClassLoader> class_loader_1 = hs.NewHandle(
345 soa.Decode<mirror::ClassLoader>(jclass_loader));
346
347 // Verify the first class loader
348
349 // For the first class loader the class path dex files must come first and then the
350 // compilation sources.
351 std::vector<const DexFile*> class_loader_1_dex_files =
352 MakeNonOwningPointerVector(classpath_dex_a);
353 for (auto& dex : classpath_dex_b) {
354 class_loader_1_dex_files.push_back(dex.get());
355 }
356 for (auto& dex : compilation_sources_raw) {
357 class_loader_1_dex_files.push_back(dex);
358 }
359 VerifyClassLoaderDexFiles(soa,
360 class_loader_1,
361 WellKnownClasses::dalvik_system_PathClassLoader,
362 class_loader_1_dex_files);
363
364 // Verify the second class loader
365 Handle<mirror::ClassLoader> class_loader_2 = hs.NewHandle(class_loader_1->GetParent());
366 std::vector<const DexFile*> class_loader_2_dex_files =
367 MakeNonOwningPointerVector(classpath_dex_c);
368 VerifyClassLoaderDexFiles(soa,
369 class_loader_2,
370 WellKnownClasses::dalvik_system_DelegateLastClassLoader,
371 class_loader_2_dex_files);
372
373 // Verify the third class loader
374 Handle<mirror::ClassLoader> class_loader_3 = hs.NewHandle(class_loader_2->GetParent());
375 std::vector<const DexFile*> class_loader_3_dex_files =
376 MakeNonOwningPointerVector(classpath_dex_d);
377 VerifyClassLoaderDexFiles(soa,
378 class_loader_3,
379 WellKnownClasses::dalvik_system_PathClassLoader,
380 class_loader_3_dex_files);
381 // The last class loader should have the BootClassLoader as a parent.
382 ASSERT_TRUE(class_loader_3->GetParent()->GetClass() ==
383 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
384}
385
386
Calin Juravle87e2cb62017-06-13 21:48:45 -0700387TEST_F(ClassLoaderContextTest, RemoveSourceLocations) {
388 std::unique_ptr<ClassLoaderContext> context =
389 ClassLoaderContext::Create("PCL[a.dex]");
390 dchecked_vector<std::string> classpath_dex;
391 classpath_dex.push_back("a.dex");
392 dchecked_vector<std::string> compilation_sources;
393 compilation_sources.push_back("src.dex");
394
395 // Nothing should be removed.
396 ASSERT_FALSE(context->RemoveLocationsFromClassPaths(compilation_sources));
397 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
398 // Classes should be removed.
399 ASSERT_TRUE(context->RemoveLocationsFromClassPaths(classpath_dex));
400 VerifyClassLoaderPCL(context.get(), 0, "");
401}
402
403TEST_F(ClassLoaderContextTest, EncodeInOatFile) {
404 std::string dex1_name = GetTestDexFileName("Main");
405 std::string dex2_name = GetTestDexFileName("MyClass");
406 std::unique_ptr<ClassLoaderContext> context =
407 ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]");
408 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
409
410 std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main");
411 std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MyClass");
412 std::string encoding = context->EncodeContextForOatFile("");
Calin Juravlec79470d2017-07-12 17:37:42 -0700413 std::string expected_encoding = "PCL[" + CreateClassPathWithChecksums(dex1) + ":" +
414 CreateClassPathWithChecksums(dex2) + "]";
Calin Juravle87e2cb62017-06-13 21:48:45 -0700415 ASSERT_EQ(expected_encoding, context->EncodeContextForOatFile(""));
416}
417
Calin Juravle57d0acc2017-07-11 17:41:30 -0700418// TODO(calin) add a test which creates the context for a class loader together with dex_elements.
419TEST_F(ClassLoaderContextTest, CreateContextForClassLoader) {
420 // The chain is
421 // ClassLoaderA (PathClassLoader)
422 // ^
423 // |
424 // ClassLoaderB (DelegateLastClassLoader)
425 // ^
426 // |
427 // ClassLoaderC (PathClassLoader)
428 // ^
429 // |
430 // ClassLoaderD (DelegateLastClassLoader)
431
432 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
433 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
434 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
435 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
436
437 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
438
439 VerifyContextForClassLoader(context.get());
440 VerifyContextSize(context.get(), 4);
441
442 VerifyClassLoaderDLCFromTestDex(context.get(), 0, "ForClassLoaderD");
443 VerifyClassLoaderPCLFromTestDex(context.get(), 1, "ForClassLoaderC");
444 VerifyClassLoaderDLCFromTestDex(context.get(), 2, "ForClassLoaderB");
445 VerifyClassLoaderPCLFromTestDex(context.get(), 3, "ForClassLoaderA");
446}
447
Calin Juravle3f918642017-07-11 19:04:20 -0700448TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatch) {
449 std::string context_spec = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890]";
450 std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec);
451
452 VerifyContextSize(context.get(), 2);
453 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
454 VerifyClassLoaderDLC(context.get(), 1, "c.dex");
455
456 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context_spec));
457
458 std::string wrong_class_loader_type = "PCL[a.dex*123:b.dex*456];PCL[c.dex*890]";
459 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_class_loader_type));
460
461 std::string wrong_class_loader_order = "DLC[c.dex*890];PCL[a.dex*123:b.dex*456]";
462 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_class_loader_order));
463
464 std::string wrong_classpath_order = "PCL[b.dex*456:a.dex*123];DLC[c.dex*890]";
465 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_classpath_order));
466
467 std::string wrong_checksum = "PCL[a.dex*999:b.dex*456];DLC[c.dex*890]";
468 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_checksum));
469
470 std::string wrong_extra_class_loader = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890];PCL[d.dex*321]";
471 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_extra_class_loader));
472
473 std::string wrong_extra_classpath = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890:d.dex*321]";
474 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_extra_classpath));
475
476 std::string wrong_spec = "PCL[a.dex*999:b.dex*456];DLC[";
477 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_spec));
478}
479
480TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncoding) {
481 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
482 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
483 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
484 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
485
486 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
487
488 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context->EncodeContextForOatFile("")));
489}
490
Calin Juravlea308a322017-07-18 16:51:51 -0700491TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncodingMultidex) {
492 jobject class_loader = LoadDexInPathClassLoader("MultiDex", nullptr);
493
494 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader);
495
496 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context->EncodeContextForOatFile("")));
497}
498
Calin Juravle87e2cb62017-06-13 21:48:45 -0700499} // namespace art