diff options
author | 2018-02-08 15:44:50 -0800 | |
---|---|---|
committer | 2018-02-09 10:59:57 -0800 | |
commit | 139512d4b110b087ea5439852f9f53e90e3d5a33 (patch) | |
tree | 6372bda67a55ba5bc505def17eb87b942827cf4b | |
parent | cd4ddeea1c33d5f9b7758d116451bd06f1b81aff (diff) |
Split dex_file_loader_test
Into DexFileLoader and ArtDexFileLoader portions. This is the last test
refactoring before moving libdexfile out of the runtime directory.
Bug: 22322814
Test: make -j 50 test-art-host
Change-Id: I7568a7b444fb110f64614f2066df6eb5ab3a5c69
-rw-r--r-- | build/Android.gtest.mk | 2 | ||||
-rw-r--r-- | runtime/Android.bp | 3 | ||||
-rw-r--r-- | runtime/dex/art_dex_file_loader_test.cc | 308 | ||||
-rw-r--r-- | runtime/dex/code_item_accessors-no_art-inl.h | 23 | ||||
-rw-r--r-- | runtime/dex/dex_file_loader_test.cc (renamed from runtime/dex/dex_file_test.cc) | 482 |
5 files changed, 420 insertions, 398 deletions
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk index 894b33b00a..3ab0b1f30c 100644 --- a/build/Android.gtest.mk +++ b/build/Android.gtest.mk @@ -131,6 +131,7 @@ $(ART_TEST_TARGET_GTEST_VerifierDepsMulti_DEX): $(ART_TEST_GTEST_VerifierDepsMul $(HOST_OUT_EXECUTABLES)/smali assemble --output $@ $(filter %.smali,$^) # Dex file dependencies for each gtest. +ART_GTEST_art_dex_file_loader_test_DEX_DEPS := GetMethodSignature Main Nested MultiDex ART_GTEST_dex2oat_environment_tests_DEX_DEPS := Main MainStripped MultiDex MultiDexModifiedSecondary MyClassNatives Nested VerifierDeps VerifierDepsMulti ART_GTEST_atomic_dex_ref_map_test_DEX_DEPS := Interfaces @@ -139,7 +140,6 @@ ART_GTEST_class_loader_context_test_DEX_DEPS := Main MultiDex MyClass ForClassLo ART_GTEST_class_table_test_DEX_DEPS := XandY ART_GTEST_compiler_driver_test_DEX_DEPS := AbstractMethod StaticLeafMethods ProfileTestMultiDex ART_GTEST_dex_cache_test_DEX_DEPS := Main Packages MethodTypes -ART_GTEST_dex_file_test_DEX_DEPS := GetMethodSignature Main Nested MultiDex ART_GTEST_dexlayout_test_DEX_DEPS := ManyMethods ART_GTEST_dex2oat_test_DEX_DEPS := $(ART_GTEST_dex2oat_environment_tests_DEX_DEPS) ManyMethods Statics VerifierDeps MainUncompressed EmptyUncompressed ART_GTEST_dex2oat_image_test_DEX_DEPS := $(ART_GTEST_dex2oat_environment_tests_DEX_DEPS) Statics VerifierDeps diff --git a/runtime/Android.bp b/runtime/Android.bp index db9bceaf29..832d50e691 100644 --- a/runtime/Android.bp +++ b/runtime/Android.bp @@ -637,10 +637,11 @@ art_cc_test { "class_loader_context_test.cc", "class_table_test.cc", "compiler_filter_test.cc", + "dex/art_dex_file_loader_test.cc", "dex/code_item_accessors_test.cc", "dex/compact_dex_debug_info_test.cc", "dex/compact_dex_file_test.cc", - "dex/dex_file_test.cc", + "dex/dex_file_loader_test.cc", "dex/dex_file_verifier_test.cc", "dex/dex_instruction_test.cc", "dex/utf_test.cc", diff --git a/runtime/dex/art_dex_file_loader_test.cc b/runtime/dex/art_dex_file_loader_test.cc new file mode 100644 index 0000000000..f4013810a4 --- /dev/null +++ b/runtime/dex/art_dex_file_loader_test.cc @@ -0,0 +1,308 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dex_file.h" + +#include <sys/mman.h> + +#include <memory> + +#include "art_dex_file_loader.h" +#include "base/stl_util.h" +#include "base/unix_file/fd_file.h" +#include "base64_test_util.h" +#include "code_item_accessors-inl.h" +#include "common_runtime_test.h" +#include "descriptors_names.h" +#include "dex_file-inl.h" +#include "dex_file_loader.h" +#include "mem_map.h" +#include "os.h" +#include "scoped_thread_state_change-inl.h" +#include "thread-current-inl.h" + +namespace art { + +class ArtDexFileLoaderTest : public CommonRuntimeTest {}; + +// TODO: Port OpenTestDexFile(s) need to be ported to use non-ART utilities, and +// the tests that depend upon them should be moved to dex_file_loader_test.cc + +TEST_F(ArtDexFileLoaderTest, Open) { + ScopedObjectAccess soa(Thread::Current()); + std::unique_ptr<const DexFile> dex(OpenTestDexFile("Nested")); + ASSERT_TRUE(dex.get() != nullptr); +} + +TEST_F(ArtDexFileLoaderTest, GetLocationChecksum) { + ScopedObjectAccess soa(Thread::Current()); + std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main")); + EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); +} + +TEST_F(ArtDexFileLoaderTest, GetChecksum) { + std::vector<uint32_t> checksums; + ScopedObjectAccess soa(Thread::Current()); + std::string error_msg; + const ArtDexFileLoader dex_file_loader; + EXPECT_TRUE(dex_file_loader.GetMultiDexChecksums(GetLibCoreDexFileNames()[0].c_str(), + &checksums, + &error_msg)) + << error_msg; + ASSERT_EQ(1U, checksums.size()); + EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksums[0]); +} + +TEST_F(ArtDexFileLoaderTest, GetMultiDexChecksums) { + std::string error_msg; + std::vector<uint32_t> checksums; + std::string multidex_file = GetTestDexFileName("MultiDex"); + const ArtDexFileLoader dex_file_loader; + EXPECT_TRUE(dex_file_loader.GetMultiDexChecksums(multidex_file.c_str(), + &checksums, + &error_msg)) << error_msg; + + std::vector<std::unique_ptr<const DexFile>> dexes = OpenTestDexFiles("MultiDex"); + ASSERT_EQ(2U, dexes.size()); + ASSERT_EQ(2U, checksums.size()); + + EXPECT_EQ(dexes[0]->GetLocation(), DexFileLoader::GetMultiDexLocation(0, multidex_file.c_str())); + EXPECT_EQ(dexes[0]->GetLocationChecksum(), checksums[0]); + + EXPECT_EQ(dexes[1]->GetLocation(), DexFileLoader::GetMultiDexLocation(1, multidex_file.c_str())); + EXPECT_EQ(dexes[1]->GetLocationChecksum(), checksums[1]); +} + +TEST_F(ArtDexFileLoaderTest, ClassDefs) { + ScopedObjectAccess soa(Thread::Current()); + std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested")); + ASSERT_TRUE(raw.get() != nullptr); + EXPECT_EQ(3U, raw->NumClassDefs()); + + const DexFile::ClassDef& c0 = raw->GetClassDef(0); + EXPECT_STREQ("LNested$1;", raw->GetClassDescriptor(c0)); + + const DexFile::ClassDef& c1 = raw->GetClassDef(1); + EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c1)); + + const DexFile::ClassDef& c2 = raw->GetClassDef(2); + EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c2)); +} + +TEST_F(ArtDexFileLoaderTest, GetMethodSignature) { + ScopedObjectAccess soa(Thread::Current()); + std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); + ASSERT_TRUE(raw.get() != nullptr); + EXPECT_EQ(1U, raw->NumClassDefs()); + + const DexFile::ClassDef& class_def = raw->GetClassDef(0); + ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def)); + + const uint8_t* class_data = raw->GetClassData(class_def); + ASSERT_TRUE(class_data != nullptr); + ClassDataItemIterator it(*raw, class_data); + + EXPECT_EQ(1u, it.NumDirectMethods()); + + // Check the signature for the static initializer. + { + ASSERT_EQ(1U, it.NumDirectMethods()); + const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); + const char* name = raw->StringDataByIdx(method_id.name_idx_); + ASSERT_STREQ("<init>", name); + std::string signature(raw->GetMethodSignature(method_id).ToString()); + ASSERT_EQ("()V", signature); + } + + // Check all virtual methods. + struct Result { + const char* name; + const char* signature; + const char* pretty_method; + }; + static const Result results[] = { + { + "m1", + "(IDJLjava/lang/Object;)Ljava/lang/Float;", + "java.lang.Float GetMethodSignature.m1(int, double, long, java.lang.Object)" + }, + { + "m2", + "(ZSC)LGetMethodSignature;", + "GetMethodSignature GetMethodSignature.m2(boolean, short, char)" + }, + { + "m3", + "()V", + "void GetMethodSignature.m3()" + }, + { + "m4", + "(I)V", + "void GetMethodSignature.m4(int)" + }, + { + "m5", + "(II)V", + "void GetMethodSignature.m5(int, int)" + }, + { + "m6", + "(II[[I)V", + "void GetMethodSignature.m6(int, int, int[][])" + }, + { + "m7", + "(II[[ILjava/lang/Object;)V", + "void GetMethodSignature.m7(int, int, int[][], java.lang.Object)" + }, + { + "m8", + "(II[[ILjava/lang/Object;[[Ljava/lang/Object;)V", + "void GetMethodSignature.m8(int, int, int[][], java.lang.Object, java.lang.Object[][])" + }, + { + "m9", + "()I", + "int GetMethodSignature.m9()" + }, + { + "mA", + "()[[I", + "int[][] GetMethodSignature.mA()" + }, + { + "mB", + "()[[Ljava/lang/Object;", + "java.lang.Object[][] GetMethodSignature.mB()" + }, + }; + ASSERT_EQ(arraysize(results), it.NumVirtualMethods()); + for (const Result& r : results) { + it.Next(); + const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); + + const char* name = raw->StringDataByIdx(method_id.name_idx_); + ASSERT_STREQ(r.name, name); + + std::string signature(raw->GetMethodSignature(method_id).ToString()); + ASSERT_EQ(r.signature, signature); + + std::string plain_method = std::string("GetMethodSignature.") + r.name; + ASSERT_EQ(plain_method, raw->PrettyMethod(it.GetMemberIndex(), /* with_signature */ false)); + ASSERT_EQ(r.pretty_method, raw->PrettyMethod(it.GetMemberIndex(), /* with_signature */ true)); + } +} + +TEST_F(ArtDexFileLoaderTest, FindStringId) { + ScopedObjectAccess soa(Thread::Current()); + std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); + ASSERT_TRUE(raw.get() != nullptr); + EXPECT_EQ(1U, raw->NumClassDefs()); + + const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", + "D", "I", "J", nullptr }; + for (size_t i = 0; strings[i] != nullptr; i++) { + const char* str = strings[i]; + const DexFile::StringId* str_id = raw->FindStringId(str); + const char* dex_str = raw->GetStringData(*str_id); + EXPECT_STREQ(dex_str, str); + } +} + +TEST_F(ArtDexFileLoaderTest, FindTypeId) { + for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { + const char* type_str = java_lang_dex_file_->StringByTypeIdx(dex::TypeIndex(i)); + const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); + ASSERT_TRUE(type_str_id != nullptr); + dex::StringIndex type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); + const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); + ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str)); + ASSERT_TRUE(type_id != nullptr); + EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id).index_, i); + } +} + +TEST_F(ArtDexFileLoaderTest, FindProtoId) { + for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { + const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); + const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); + std::vector<dex::TypeIndex> to_find_types; + if (to_find_tl != nullptr) { + for (size_t j = 0; j < to_find_tl->Size(); j++) { + to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); + } + } + const DexFile::ProtoId* found = + java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); + ASSERT_TRUE(found != nullptr); + EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); + } +} + +TEST_F(ArtDexFileLoaderTest, FindMethodId) { + for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { + const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); + const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); + const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); + const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); + const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); + ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": " + << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." + << java_lang_dex_file_->GetStringData(name) + << java_lang_dex_file_->GetMethodSignature(to_find); + EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); + } +} + +TEST_F(ArtDexFileLoaderTest, FindFieldId) { + for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { + const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); + const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); + const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); + const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); + const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); + ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": " + << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " + << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." + << java_lang_dex_file_->GetStringData(name); + EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); + } +} + +TEST_F(ArtDexFileLoaderTest, GetDexCanonicalLocation) { + ScratchFile file; + UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr)); + std::string dex_location(dex_location_real.get()); + + ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location.c_str())); + std::string multidex_location = DexFileLoader::GetMultiDexLocation(1, dex_location.c_str()); + ASSERT_EQ(multidex_location, DexFileLoader::GetDexCanonicalLocation(multidex_location.c_str())); + + std::string dex_location_sym = dex_location + "symlink"; + ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str())); + + ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location_sym.c_str())); + + std::string multidex_location_sym = DexFileLoader::GetMultiDexLocation( + 1, dex_location_sym.c_str()); + ASSERT_EQ(multidex_location, + DexFileLoader::GetDexCanonicalLocation(multidex_location_sym.c_str())); + + ASSERT_EQ(0, unlink(dex_location_sym.c_str())); +} + +} // namespace art diff --git a/runtime/dex/code_item_accessors-no_art-inl.h b/runtime/dex/code_item_accessors-no_art-inl.h deleted file mode 100644 index 8082be3818..0000000000 --- a/runtime/dex/code_item_accessors-no_art-inl.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_RUNTIME_DEX_CODE_ITEM_ACCESSORS_NO_ART_INL_H_ -#define ART_RUNTIME_DEX_CODE_ITEM_ACCESSORS_NO_ART_INL_H_ - -// TODO: delete this file once system/core is updated. -#include "code_item_accessors-inl.h" - -#endif // ART_RUNTIME_DEX_CODE_ITEM_ACCESSORS_NO_ART_INL_H_ diff --git a/runtime/dex/dex_file_test.cc b/runtime/dex/dex_file_loader_test.cc index 2bb86672dc..ab5c3f9a26 100644 --- a/runtime/dex/dex_file_test.cc +++ b/runtime/dex/dex_file_loader_test.cc @@ -16,33 +16,20 @@ #include "dex_file.h" -#include <sys/mman.h> - #include <memory> -#include "art_dex_file_loader.h" -#include "base/stl_util.h" -#include "base/unix_file/fd_file.h" #include "base64_test_util.h" #include "code_item_accessors-inl.h" -#include "common_runtime_test.h" #include "descriptors_names.h" #include "dex_file-inl.h" #include "dex_file_loader.h" -#include "mem_map.h" -#include "os.h" -#include "scoped_thread_state_change-inl.h" -#include "thread-current-inl.h" +#include "gtest/gtest.h" namespace art { -class DexFileTest : public CommonRuntimeTest {}; +class DexFileLoaderTest : public testing::Test {}; -TEST_F(DexFileTest, Open) { - ScopedObjectAccess soa(Thread::Current()); - std::unique_ptr<const DexFile> dex(OpenTestDexFile("Nested")); - ASSERT_TRUE(dex.get() != nullptr); -} +static constexpr char kLocationString[] = "/a/dex/file/location"; static inline std::vector<uint8_t> DecodeBase64Vec(const char* src) { std::vector<uint8_t> res; @@ -212,52 +199,41 @@ static const char kRawDexDebugInfoLocalNullType[] = "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA" "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA=="; -static void DecodeAndWriteDexFile(const char* base64, const char* location) { +static void DecodeDexFile(const char* base64, std::vector<uint8_t>* dex_bytes) { // decode base64 CHECK(base64 != nullptr); - std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64); - CHECK_NE(dex_bytes.size(), 0u); - - // write to provided file - std::unique_ptr<File> file(OS::CreateEmptyFile(location)); - CHECK(file.get() != nullptr); - if (!file->WriteFully(dex_bytes.data(), dex_bytes.size())) { - PLOG(FATAL) << "Failed to write base64 as dex file"; - } - if (file->FlushCloseOrErase() != 0) { - PLOG(FATAL) << "Could not flush and close test file."; - } + *dex_bytes = DecodeBase64Vec(base64); + CHECK_NE(dex_bytes->size(), 0u); } static bool OpenDexFilesBase64(const char* base64, const char* location, + std::vector<uint8_t>* dex_bytes, std::vector<std::unique_ptr<const DexFile>>* dex_files, std::string* error_msg) { - DecodeAndWriteDexFile(base64, location); + DecodeDexFile(base64, dex_bytes); // read dex file(s) - ScopedObjectAccess soa(Thread::Current()); static constexpr bool kVerifyChecksum = true; std::vector<std::unique_ptr<const DexFile>> tmp; - const ArtDexFileLoader dex_file_loader; - bool success = dex_file_loader.Open( - location, location, /* verify */ true, kVerifyChecksum, error_msg, &tmp); - if (success) { - for (std::unique_ptr<const DexFile>& dex_file : tmp) { - EXPECT_EQ(PROT_READ, dex_file->GetPermissions()); - EXPECT_TRUE(dex_file->IsReadOnly()); - } - *dex_files = std::move(tmp); - } + const DexFileLoader dex_file_loader; + bool success = dex_file_loader.OpenAll(dex_bytes->data(), + dex_bytes->size(), + location, + /* verify */ true, + kVerifyChecksum, + error_msg, + dex_files); return success; } static std::unique_ptr<const DexFile> OpenDexFileBase64(const char* base64, - const char* location) { + const char* location, + std::vector<uint8_t>* dex_bytes) { // read dex files. std::string error_msg; std::vector<std::unique_ptr<const DexFile>> dex_files; - bool success = OpenDexFilesBase64(base64, location, &dex_files, &error_msg); + bool success = OpenDexFilesBase64(base64, location, dex_bytes, &dex_files, &error_msg); CHECK(success) << error_msg; EXPECT_EQ(1U, dex_files.size()); return std::move(dex_files[0]); @@ -266,24 +242,17 @@ static std::unique_ptr<const DexFile> OpenDexFileBase64(const char* base64, static std::unique_ptr<const DexFile> OpenDexFileInMemoryBase64(const char* base64, const char* location, uint32_t location_checksum, - bool expect_success) { - CHECK(base64 != nullptr); - std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64); - CHECK_NE(dex_bytes.size(), 0u); + bool expect_success, + std::vector<uint8_t>* dex_bytes) { + DecodeDexFile(base64, dex_bytes); std::string error_message; - std::unique_ptr<MemMap> region(MemMap::MapAnonymous("test-region", - nullptr, - dex_bytes.size(), - PROT_READ | PROT_WRITE, - /* low_4gb */ false, - /* reuse */ false, - &error_message)); - memcpy(region->Begin(), dex_bytes.data(), dex_bytes.size()); - const ArtDexFileLoader dex_file_loader; - std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open(location, + const DexFileLoader dex_file_loader; + std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open(dex_bytes->data(), + dex_bytes->size(), + location, location_checksum, - std::move(region), + /* oat_dex_file */ nullptr, /* verify */ true, /* verify_checksum */ true, &error_message)); @@ -332,323 +301,96 @@ static void ValidateDexFileHeader(std::unique_ptr<const DexFile> dex_file) { EXPECT_EQ(header.checksum_, dex_file->GetLocationChecksum()); } -TEST_F(DexFileTest, Header) { - ScratchFile tmp; - std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); +TEST_F(DexFileLoaderTest, Header) { + std::vector<uint8_t> dex_bytes; + std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, kLocationString, &dex_bytes)); ValidateDexFileHeader(std::move(raw)); } -TEST_F(DexFileTest, HeaderInMemory) { - ScratchFile tmp; +TEST_F(DexFileLoaderTest, HeaderInMemory) { + std::vector<uint8_t> dex_bytes; std::unique_ptr<const DexFile> raw = - OpenDexFileInMemoryBase64(kRawDex, tmp.GetFilename().c_str(), 0x00d87910U, true); + OpenDexFileInMemoryBase64(kRawDex, kLocationString, 0x00d87910U, true, &dex_bytes); ValidateDexFileHeader(std::move(raw)); } -TEST_F(DexFileTest, Version38Accepted) { - ScratchFile tmp; - std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex38, tmp.GetFilename().c_str())); +TEST_F(DexFileLoaderTest, Version38Accepted) { + std::vector<uint8_t> dex_bytes; + std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex38, kLocationString, &dex_bytes)); ASSERT_TRUE(raw.get() != nullptr); const DexFile::Header& header = raw->GetHeader(); EXPECT_EQ(38u, header.GetVersion()); } -TEST_F(DexFileTest, Version39Accepted) { - ScratchFile tmp; - std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex39, tmp.GetFilename().c_str())); +TEST_F(DexFileLoaderTest, Version39Accepted) { + std::vector<uint8_t> dex_bytes; + std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex39, kLocationString, &dex_bytes)); ASSERT_TRUE(raw.get() != nullptr); const DexFile::Header& header = raw->GetHeader(); EXPECT_EQ(39u, header.GetVersion()); } -TEST_F(DexFileTest, Version40Rejected) { - ScratchFile tmp; - const char* location = tmp.GetFilename().c_str(); - DecodeAndWriteDexFile(kRawDex40, location); +TEST_F(DexFileLoaderTest, Version40Rejected) { + std::vector<uint8_t> dex_bytes; + DecodeDexFile(kRawDex40, &dex_bytes); - ScopedObjectAccess soa(Thread::Current()); static constexpr bool kVerifyChecksum = true; std::string error_msg; std::vector<std::unique_ptr<const DexFile>> dex_files; - const ArtDexFileLoader dex_file_loader; - ASSERT_FALSE(dex_file_loader.Open( - location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)); + const DexFileLoader dex_file_loader; + ASSERT_FALSE(dex_file_loader.OpenAll(dex_bytes.data(), + dex_bytes.size(), + kLocationString, + /* verify */ true, + kVerifyChecksum, + &error_msg, + &dex_files)); } -TEST_F(DexFileTest, Version41Rejected) { - ScratchFile tmp; - const char* location = tmp.GetFilename().c_str(); - DecodeAndWriteDexFile(kRawDex41, location); +TEST_F(DexFileLoaderTest, Version41Rejected) { + std::vector<uint8_t> dex_bytes; + DecodeDexFile(kRawDex41, &dex_bytes); - ScopedObjectAccess soa(Thread::Current()); static constexpr bool kVerifyChecksum = true; std::string error_msg; std::vector<std::unique_ptr<const DexFile>> dex_files; - const ArtDexFileLoader dex_file_loader; - ASSERT_FALSE(dex_file_loader.Open( - location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)); + const DexFileLoader dex_file_loader; + ASSERT_FALSE(dex_file_loader.OpenAll(dex_bytes.data(), + dex_bytes.size(), + kLocationString, + /* verify */ true, + kVerifyChecksum, + &error_msg, + &dex_files)); } -TEST_F(DexFileTest, ZeroLengthDexRejected) { - ScratchFile tmp; - const char* location = tmp.GetFilename().c_str(); - DecodeAndWriteDexFile(kRawDexZeroLength, location); +TEST_F(DexFileLoaderTest, ZeroLengthDexRejected) { + std::vector<uint8_t> dex_bytes; + DecodeDexFile(kRawDexZeroLength, &dex_bytes); - ScopedObjectAccess soa(Thread::Current()); static constexpr bool kVerifyChecksum = true; std::string error_msg; std::vector<std::unique_ptr<const DexFile>> dex_files; - const ArtDexFileLoader dex_file_loader; - ASSERT_FALSE(dex_file_loader.Open( - location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)); -} - -TEST_F(DexFileTest, GetLocationChecksum) { - ScopedObjectAccess soa(Thread::Current()); - std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main")); - EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); -} - -TEST_F(DexFileTest, GetChecksum) { - std::vector<uint32_t> checksums; - ScopedObjectAccess soa(Thread::Current()); - std::string error_msg; - const ArtDexFileLoader dex_file_loader; - EXPECT_TRUE(dex_file_loader.GetMultiDexChecksums(GetLibCoreDexFileNames()[0].c_str(), - &checksums, - &error_msg)) - << error_msg; - ASSERT_EQ(1U, checksums.size()); - EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksums[0]); -} - -TEST_F(DexFileTest, GetMultiDexChecksums) { - std::string error_msg; - std::vector<uint32_t> checksums; - std::string multidex_file = GetTestDexFileName("MultiDex"); - const ArtDexFileLoader dex_file_loader; - EXPECT_TRUE(dex_file_loader.GetMultiDexChecksums(multidex_file.c_str(), - &checksums, - &error_msg)) << error_msg; - - std::vector<std::unique_ptr<const DexFile>> dexes = OpenTestDexFiles("MultiDex"); - ASSERT_EQ(2U, dexes.size()); - ASSERT_EQ(2U, checksums.size()); - - EXPECT_EQ(dexes[0]->GetLocation(), DexFileLoader::GetMultiDexLocation(0, multidex_file.c_str())); - EXPECT_EQ(dexes[0]->GetLocationChecksum(), checksums[0]); - - EXPECT_EQ(dexes[1]->GetLocation(), DexFileLoader::GetMultiDexLocation(1, multidex_file.c_str())); - EXPECT_EQ(dexes[1]->GetLocationChecksum(), checksums[1]); -} - -TEST_F(DexFileTest, ClassDefs) { - ScopedObjectAccess soa(Thread::Current()); - std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested")); - ASSERT_TRUE(raw.get() != nullptr); - EXPECT_EQ(3U, raw->NumClassDefs()); - - const DexFile::ClassDef& c0 = raw->GetClassDef(0); - EXPECT_STREQ("LNested$1;", raw->GetClassDescriptor(c0)); - - const DexFile::ClassDef& c1 = raw->GetClassDef(1); - EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c1)); - - const DexFile::ClassDef& c2 = raw->GetClassDef(2); - EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c2)); -} - -TEST_F(DexFileTest, GetMethodSignature) { - ScopedObjectAccess soa(Thread::Current()); - std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); - ASSERT_TRUE(raw.get() != nullptr); - EXPECT_EQ(1U, raw->NumClassDefs()); - - const DexFile::ClassDef& class_def = raw->GetClassDef(0); - ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def)); - - const uint8_t* class_data = raw->GetClassData(class_def); - ASSERT_TRUE(class_data != nullptr); - ClassDataItemIterator it(*raw, class_data); - - EXPECT_EQ(1u, it.NumDirectMethods()); - - // Check the signature for the static initializer. - { - ASSERT_EQ(1U, it.NumDirectMethods()); - const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); - const char* name = raw->StringDataByIdx(method_id.name_idx_); - ASSERT_STREQ("<init>", name); - std::string signature(raw->GetMethodSignature(method_id).ToString()); - ASSERT_EQ("()V", signature); - } - - // Check all virtual methods. - struct Result { - const char* name; - const char* signature; - const char* pretty_method; - }; - static const Result results[] = { - { - "m1", - "(IDJLjava/lang/Object;)Ljava/lang/Float;", - "java.lang.Float GetMethodSignature.m1(int, double, long, java.lang.Object)" - }, - { - "m2", - "(ZSC)LGetMethodSignature;", - "GetMethodSignature GetMethodSignature.m2(boolean, short, char)" - }, - { - "m3", - "()V", - "void GetMethodSignature.m3()" - }, - { - "m4", - "(I)V", - "void GetMethodSignature.m4(int)" - }, - { - "m5", - "(II)V", - "void GetMethodSignature.m5(int, int)" - }, - { - "m6", - "(II[[I)V", - "void GetMethodSignature.m6(int, int, int[][])" - }, - { - "m7", - "(II[[ILjava/lang/Object;)V", - "void GetMethodSignature.m7(int, int, int[][], java.lang.Object)" - }, - { - "m8", - "(II[[ILjava/lang/Object;[[Ljava/lang/Object;)V", - "void GetMethodSignature.m8(int, int, int[][], java.lang.Object, java.lang.Object[][])" - }, - { - "m9", - "()I", - "int GetMethodSignature.m9()" - }, - { - "mA", - "()[[I", - "int[][] GetMethodSignature.mA()" - }, - { - "mB", - "()[[Ljava/lang/Object;", - "java.lang.Object[][] GetMethodSignature.mB()" - }, - }; - ASSERT_EQ(arraysize(results), it.NumVirtualMethods()); - for (const Result& r : results) { - it.Next(); - const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); - - const char* name = raw->StringDataByIdx(method_id.name_idx_); - ASSERT_STREQ(r.name, name); - - std::string signature(raw->GetMethodSignature(method_id).ToString()); - ASSERT_EQ(r.signature, signature); - - std::string plain_method = std::string("GetMethodSignature.") + r.name; - ASSERT_EQ(plain_method, raw->PrettyMethod(it.GetMemberIndex(), /* with_signature */ false)); - ASSERT_EQ(r.pretty_method, raw->PrettyMethod(it.GetMemberIndex(), /* with_signature */ true)); - } -} - -TEST_F(DexFileTest, FindStringId) { - ScopedObjectAccess soa(Thread::Current()); - std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); - ASSERT_TRUE(raw.get() != nullptr); - EXPECT_EQ(1U, raw->NumClassDefs()); - - const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", - "D", "I", "J", nullptr }; - for (size_t i = 0; strings[i] != nullptr; i++) { - const char* str = strings[i]; - const DexFile::StringId* str_id = raw->FindStringId(str); - const char* dex_str = raw->GetStringData(*str_id); - EXPECT_STREQ(dex_str, str); - } -} - -TEST_F(DexFileTest, FindTypeId) { - for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { - const char* type_str = java_lang_dex_file_->StringByTypeIdx(dex::TypeIndex(i)); - const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); - ASSERT_TRUE(type_str_id != nullptr); - dex::StringIndex type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); - const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); - ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str)); - ASSERT_TRUE(type_id != nullptr); - EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id).index_, i); - } -} - -TEST_F(DexFileTest, FindProtoId) { - for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { - const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); - const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); - std::vector<dex::TypeIndex> to_find_types; - if (to_find_tl != nullptr) { - for (size_t j = 0; j < to_find_tl->Size(); j++) { - to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); - } - } - const DexFile::ProtoId* found = - java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); - ASSERT_TRUE(found != nullptr); - EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); - } -} - -TEST_F(DexFileTest, FindMethodId) { - for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { - const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); - const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); - const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); - const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); - const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); - ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": " - << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." - << java_lang_dex_file_->GetStringData(name) - << java_lang_dex_file_->GetMethodSignature(to_find); - EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); - } -} - -TEST_F(DexFileTest, FindFieldId) { - for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { - const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); - const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); - const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); - const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); - const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); - ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": " - << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " - << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." - << java_lang_dex_file_->GetStringData(name); - EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); - } + const DexFileLoader dex_file_loader; + ASSERT_FALSE(dex_file_loader.OpenAll(dex_bytes.data(), + dex_bytes.size(), + kLocationString, + /* verify */ true, + kVerifyChecksum, + &error_msg, + &dex_files)); } -TEST_F(DexFileTest, GetMultiDexClassesDexName) { +TEST_F(DexFileLoaderTest, GetMultiDexClassesDexName) { ASSERT_EQ("classes.dex", DexFileLoader::GetMultiDexClassesDexName(0)); ASSERT_EQ("classes2.dex", DexFileLoader::GetMultiDexClassesDexName(1)); ASSERT_EQ("classes3.dex", DexFileLoader::GetMultiDexClassesDexName(2)); ASSERT_EQ("classes100.dex", DexFileLoader::GetMultiDexClassesDexName(99)); } -TEST_F(DexFileTest, GetMultiDexLocation) { +TEST_F(DexFileLoaderTest, GetMultiDexLocation) { std::string dex_location_str = "/system/app/framework.jar"; const char* dex_location = dex_location_str.c_str(); ASSERT_EQ("/system/app/framework.jar", DexFileLoader::GetMultiDexLocation(0, dex_location)); @@ -658,28 +400,6 @@ TEST_F(DexFileTest, GetMultiDexLocation) { DexFileLoader::GetMultiDexLocation(100, dex_location)); } -TEST_F(DexFileTest, GetDexCanonicalLocation) { - ScratchFile file; - UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr)); - std::string dex_location(dex_location_real.get()); - - ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location.c_str())); - std::string multidex_location = DexFileLoader::GetMultiDexLocation(1, dex_location.c_str()); - ASSERT_EQ(multidex_location, DexFileLoader::GetDexCanonicalLocation(multidex_location.c_str())); - - std::string dex_location_sym = dex_location + "symlink"; - ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str())); - - ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location_sym.c_str())); - - std::string multidex_location_sym = DexFileLoader::GetMultiDexLocation( - 1, dex_location_sym.c_str()); - ASSERT_EQ(multidex_location, - DexFileLoader::GetDexCanonicalLocation(multidex_location_sym.c_str())); - - ASSERT_EQ(0, unlink(dex_location_sym.c_str())); -} - TEST(DexFileUtilsTest, GetBaseLocationAndMultiDexSuffix) { EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar")); EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar!classes2.dex")); @@ -689,43 +409,56 @@ TEST(DexFileUtilsTest, GetBaseLocationAndMultiDexSuffix) { EXPECT_EQ("!classes8.dex", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar!classes8.dex")); } -TEST_F(DexFileTest, ZipOpenClassesPresent) { - ScratchFile tmp; +TEST_F(DexFileLoaderTest, ZipOpenClassesPresent) { + std::vector<uint8_t> dex_bytes; std::vector<std::unique_ptr<const DexFile>> dex_files; std::string error_msg; - ASSERT_TRUE(OpenDexFilesBase64(kRawZipClassesDexPresent, tmp.GetFilename().c_str(), &dex_files, + ASSERT_TRUE(OpenDexFilesBase64(kRawZipClassesDexPresent, + kLocationString, + &dex_bytes, + &dex_files, &error_msg)); EXPECT_EQ(dex_files.size(), 1u); } -TEST_F(DexFileTest, ZipOpenClassesAbsent) { - ScratchFile tmp; +TEST_F(DexFileLoaderTest, ZipOpenClassesAbsent) { + std::vector<uint8_t> dex_bytes; std::vector<std::unique_ptr<const DexFile>> dex_files; std::string error_msg; - ASSERT_FALSE(OpenDexFilesBase64(kRawZipClassesDexAbsent, tmp.GetFilename().c_str(), &dex_files, + ASSERT_FALSE(OpenDexFilesBase64(kRawZipClassesDexAbsent, + kLocationString, + &dex_bytes, + &dex_files, &error_msg)); EXPECT_EQ(dex_files.size(), 0u); } -TEST_F(DexFileTest, ZipOpenThreeDexFiles) { - ScratchFile tmp; +TEST_F(DexFileLoaderTest, ZipOpenThreeDexFiles) { + std::vector<uint8_t> dex_bytes; std::vector<std::unique_ptr<const DexFile>> dex_files; std::string error_msg; - ASSERT_TRUE(OpenDexFilesBase64(kRawZipThreeDexFiles, tmp.GetFilename().c_str(), &dex_files, + ASSERT_TRUE(OpenDexFilesBase64(kRawZipThreeDexFiles, + kLocationString, + &dex_bytes, + &dex_files, &error_msg)); EXPECT_EQ(dex_files.size(), 3u); } -TEST_F(DexFileTest, OpenDexBadMapOffset) { - ScratchFile tmp; +TEST_F(DexFileLoaderTest, OpenDexBadMapOffset) { + std::vector<uint8_t> dex_bytes; std::unique_ptr<const DexFile> raw = - OpenDexFileInMemoryBase64(kRawDexBadMapOffset, tmp.GetFilename().c_str(), 0xb3642819U, false); + OpenDexFileInMemoryBase64(kRawDexBadMapOffset, + kLocationString, + 0xb3642819U, + false, + &dex_bytes); EXPECT_EQ(raw, nullptr); } -TEST_F(DexFileTest, GetStringWithNoIndex) { - ScratchFile tmp; - std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); +TEST_F(DexFileLoaderTest, GetStringWithNoIndex) { + std::vector<uint8_t> dex_bytes; + std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, kLocationString, &dex_bytes)); dex::TypeIndex idx; EXPECT_EQ(raw->StringByTypeIdx(idx), nullptr); } @@ -734,10 +467,13 @@ static void Callback(void* context ATTRIBUTE_UNUSED, const DexFile::LocalInfo& entry ATTRIBUTE_UNUSED) { } -TEST_F(DexFileTest, OpenDexDebugInfoLocalNullType) { - ScratchFile tmp; - std::unique_ptr<const DexFile> raw = OpenDexFileInMemoryBase64( - kRawDexDebugInfoLocalNullType, tmp.GetFilename().c_str(), 0xf25f2b38U, true); +TEST_F(DexFileLoaderTest, OpenDexDebugInfoLocalNullType) { + std::vector<uint8_t> dex_bytes; + std::unique_ptr<const DexFile> raw = OpenDexFileInMemoryBase64(kRawDexDebugInfoLocalNullType, + kLocationString, + 0xf25f2b38U, + true, + &dex_bytes); const DexFile::ClassDef& class_def = raw->GetClassDef(0); constexpr uint32_t kMethodIdx = 1; const DexFile::CodeItem* code_item = raw->GetCodeItem(raw->FindCodeItemOffset(class_def, |