Make libdexfile tests independent of runtime

Remove dependencies on common_runtime_test and related so that
libdexfile's tests can be run independently of the runtime directory.

Bug: 22322814
Test: make -j 50 test-art-host-gtest
Change-Id: If306c1995e87cbfc944e11fb13c927bc287014ad
diff --git a/dexlayout/dexlayout_test.cc b/dexlayout/dexlayout_test.cc
index bebdc20..a1901f0 100644
--- a/dexlayout/dexlayout_test.cc
+++ b/dexlayout/dexlayout_test.cc
@@ -24,6 +24,7 @@
 #include "base/unix_file/fd_file.h"
 #include "common_runtime_test.h"
 #include "dex/art_dex_file_loader.h"
+#include "dex/base64_test_util.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_file-inl.h"
 #include "dex/dex_file_loader.h"
diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc
index 39dbebf..8c268d8 100644
--- a/runtime/common_runtime_test.cc
+++ b/runtime/common_runtime_test.cc
@@ -73,74 +73,6 @@
 
 using android::base::StringPrintf;
 
-static const uint8_t kBase64Map[256] = {
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
-  52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
-  255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
-    7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
-   19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
-  255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
-   37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
-   49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-  255, 255, 255, 255
-};
-
-uint8_t* DecodeBase64(const char* src, size_t* dst_size) {
-  CHECK(dst_size != nullptr);
-  std::vector<uint8_t> tmp;
-  uint32_t t = 0, y = 0;
-  int g = 3;
-  for (size_t i = 0; src[i] != '\0'; ++i) {
-    uint8_t c = kBase64Map[src[i] & 0xFF];
-    if (c == 255) continue;
-    // the final = symbols are read and used to trim the remaining bytes
-    if (c == 254) {
-      c = 0;
-      // prevent g < 0 which would potentially allow an overflow later
-      if (--g < 0) {
-        *dst_size = 0;
-        return nullptr;
-      }
-    } else if (g != 3) {
-      // we only allow = to be at the end
-      *dst_size = 0;
-      return nullptr;
-    }
-    t = (t << 6) | c;
-    if (++y == 4) {
-      tmp.push_back((t >> 16) & 255);
-      if (g > 1) {
-        tmp.push_back((t >> 8) & 255);
-      }
-      if (g > 2) {
-        tmp.push_back(t & 255);
-      }
-      y = t = 0;
-    }
-  }
-  if (y != 0) {
-    *dst_size = 0;
-    return nullptr;
-  }
-  std::unique_ptr<uint8_t[]> dst(new uint8_t[tmp.size()]);
-  *dst_size = tmp.size();
-  std::copy(tmp.begin(), tmp.end(), dst.get());
-  return dst.release();
-}
-
 ScratchFile::ScratchFile() {
   // ANDROID_DATA needs to be set
   CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
diff --git a/runtime/common_runtime_test.h b/runtime/common_runtime_test.h
index 0aed70a..b2b4d54 100644
--- a/runtime/common_runtime_test.h
+++ b/runtime/common_runtime_test.h
@@ -53,8 +53,6 @@
 class Thread;
 class VariableSizedHandleScope;
 
-uint8_t* DecodeBase64(const char* src, size_t* dst_size);
-
 class ScratchFile {
  public:
   ScratchFile();
diff --git a/runtime/dex/base64_test_util.h b/runtime/dex/base64_test_util.h
new file mode 100644
index 0000000..0657f9f
--- /dev/null
+++ b/runtime/dex/base64_test_util.h
@@ -0,0 +1,99 @@
+/*
+ * 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.
+ */
+
+#ifndef ART_RUNTIME_DEX_BASE64_TEST_UTIL_H_
+#define ART_RUNTIME_DEX_BASE64_TEST_UTIL_H_
+
+#include <base/logging.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <memory>
+#include <vector>
+
+namespace art {
+
+static inline uint8_t* DecodeBase64(const char* src, size_t* dst_size) {
+  static const uint8_t kBase64Map[256] = {
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
+    52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
+    255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
+      7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
+     19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
+    255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
+     37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
+     49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+    255, 255, 255, 255
+  };
+
+  CHECK(dst_size != nullptr);
+  std::vector<uint8_t> tmp;
+  uint32_t t = 0, y = 0;
+  int g = 3;
+  for (size_t i = 0; src[i] != '\0'; ++i) {
+    uint8_t c = kBase64Map[src[i] & 0xFF];
+    if (c == 255) continue;
+    // the final = symbols are read and used to trim the remaining bytes
+    if (c == 254) {
+      c = 0;
+      // prevent g < 0 which would potentially allow an overflow later
+      if (--g < 0) {
+        *dst_size = 0;
+        return nullptr;
+      }
+    } else if (g != 3) {
+      // we only allow = to be at the end
+      *dst_size = 0;
+      return nullptr;
+    }
+    t = (t << 6) | c;
+    if (++y == 4) {
+      tmp.push_back((t >> 16) & 255);
+      if (g > 1) {
+        tmp.push_back((t >> 8) & 255);
+      }
+      if (g > 2) {
+        tmp.push_back(t & 255);
+      }
+      y = t = 0;
+    }
+  }
+  if (y != 0) {
+    *dst_size = 0;
+    return nullptr;
+  }
+  std::unique_ptr<uint8_t[]> dst(new uint8_t[tmp.size()]);
+  *dst_size = tmp.size();
+  std::copy(tmp.begin(), tmp.end(), dst.get());
+  return dst.release();
+}
+
+}  // namespace art
+
+#endif  // ART_RUNTIME_DEX_BASE64_TEST_UTIL_H_
diff --git a/runtime/dex/code_item_accessors_test.cc b/runtime/dex/code_item_accessors_test.cc
index 1bd12a6..2bb4dde 100644
--- a/runtime/dex/code_item_accessors_test.cc
+++ b/runtime/dex/code_item_accessors_test.cc
@@ -18,42 +18,35 @@
 
 #include <sys/mman.h>
 #include <memory>
+#include <vector>
 
-#include "common_runtime_test.h"
-#include "art_dex_file_loader.h"
 #include "dex_file_loader.h"
-#include "mem_map.h"
+#include "gtest/gtest.h"
 
 namespace art {
 
-class CodeItemAccessorsTest : public CommonRuntimeTest {};
+class CodeItemAccessorsTest : public testing::Test {};
 
-std::unique_ptr<const DexFile> CreateFakeDex(bool compact_dex) {
-  std::string error_msg;
-  std::unique_ptr<MemMap> map(
-      MemMap::MapAnonymous(/*name*/ "map",
-                           /*addr*/ nullptr,
-                           /*byte_count*/ kPageSize,
-                           PROT_READ | PROT_WRITE,
-                           /*low_4gb*/ false,
-                           /*reuse*/ false,
-                           &error_msg));
-  CHECK(map != nullptr) << error_msg;
+std::unique_ptr<const DexFile> CreateFakeDex(bool compact_dex, std::vector<uint8_t>* data) {
+  data->resize(kPageSize);
   if (compact_dex) {
     CompactDexFile::Header* header =
-        const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(map->Begin()));
+        const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(data->data()));
     CompactDexFile::WriteMagic(header->magic_);
     CompactDexFile::WriteCurrentVersion(header->magic_);
     header->data_off_ = 0;
-    header->data_size_ = map->Size();
+    header->data_size_ = data->size();
   } else {
-    StandardDexFile::WriteMagic(map->Begin());
-    StandardDexFile::WriteCurrentVersion(map->Begin());
+    StandardDexFile::WriteMagic(data->data());
+    StandardDexFile::WriteCurrentVersion(data->data());
   }
-  const ArtDexFileLoader dex_file_loader;
-  std::unique_ptr<const DexFile> dex(dex_file_loader.Open("location",
+  const DexFileLoader dex_file_loader;
+  std::string error_msg;
+  std::unique_ptr<const DexFile> dex(dex_file_loader.Open(data->data(),
+                                                          data->size(),
+                                                          "location",
                                                           /*location_checksum*/ 123,
-                                                          std::move(map),
+                                                          /*oat_dex_file*/nullptr,
                                                           /*verify*/false,
                                                           /*verify_checksum*/false,
                                                           &error_msg));
@@ -62,10 +55,13 @@
 }
 
 TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor) {
-  MemMap::Init();
-  std::unique_ptr<const DexFile> standard_dex(CreateFakeDex(/*compact_dex*/false));
+  std::vector<uint8_t> standard_dex_data;
+  std::unique_ptr<const DexFile> standard_dex(CreateFakeDex(/*compact_dex*/false,
+                                                            &standard_dex_data));
   ASSERT_TRUE(standard_dex != nullptr);
-  std::unique_ptr<const DexFile> compact_dex(CreateFakeDex(/*compact_dex*/true));
+  std::vector<uint8_t> compact_dex_data;
+  std::unique_ptr<const DexFile> compact_dex(CreateFakeDex(/*compact_dex*/true,
+                                                           &compact_dex_data));
   ASSERT_TRUE(compact_dex != nullptr);
   static constexpr uint16_t kRegisterSize = 2;
   static constexpr uint16_t kInsSize = 1;
diff --git a/runtime/dex/compact_dex_debug_info_test.cc b/runtime/dex/compact_dex_debug_info_test.cc
index 02b95e6..3267612 100644
--- a/runtime/dex/compact_dex_debug_info_test.cc
+++ b/runtime/dex/compact_dex_debug_info_test.cc
@@ -15,18 +15,14 @@
  */
 
 #include <vector>
-#include <sys/mman.h>
 
 #include "base/logging.h"
 #include "dex/compact_dex_debug_info.h"
 #include "gtest/gtest.h"
-#include "mem_map.h"
 
 namespace art {
 
 TEST(CompactDexDebugInfoTest, TestBuildAndAccess) {
-  MemMap::Init();
-
   const size_t kDebugInfoMinOffset = 1234567;
   std::vector<uint32_t> offsets = {
       0, 17, 2, 3, 11, 0, 0, 0, 0, 1, 0, 1552, 100, 122, 44, 1234567, 0, 0,
@@ -58,17 +54,10 @@
   std::string error_msg;
   // Leave some extra room since we don't copy the table at the start (for testing).
   constexpr size_t kExtraOffset = 4 * 128;
-  std::unique_ptr<MemMap> fake_dex(MemMap::MapAnonymous("fake dex",
-                                                        nullptr,
-                                                        data.size() + kExtraOffset,
-                                                        PROT_READ | PROT_WRITE,
-                                                        /*low_4gb*/ false,
-                                                        /*reuse*/ false,
-                                                        &error_msg));
-  ASSERT_TRUE(fake_dex != nullptr) << error_msg;
-  std::copy(data.begin(), data.end(), fake_dex->Begin() + kExtraOffset);
+  std::vector<uint8_t> fake_dex(data.size() + kExtraOffset);
+  std::copy(data.begin(), data.end(), fake_dex.data() + kExtraOffset);
 
-  CompactDexDebugInfoOffsetTable::Accessor accessor(fake_dex->Begin() + kExtraOffset,
+  CompactDexDebugInfoOffsetTable::Accessor accessor(fake_dex.data() + kExtraOffset,
                                                     base_offset,
                                                     table_offset);
   for (size_t i = 0; i < offsets.size(); ++i) {
diff --git a/runtime/dex/dex_file_test.cc b/runtime/dex/dex_file_test.cc
index 998bfd6..2bb8667 100644
--- a/runtime/dex/dex_file_test.cc
+++ b/runtime/dex/dex_file_test.cc
@@ -23,6 +23,7 @@
 #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"
diff --git a/runtime/dex/dex_file_verifier_test.cc b/runtime/dex/dex_file_verifier_test.cc
index d73a7fb..1cd4b2c 100644
--- a/runtime/dex/dex_file_verifier_test.cc
+++ b/runtime/dex/dex_file_verifier_test.cc
@@ -16,28 +16,26 @@
 
 #include "dex_file_verifier.h"
 
-#include <sys/mman.h>
 #include <zlib.h>
 
 #include <functional>
 #include <memory>
 
-#include "art_dex_file_loader.h"
 #include "base/bit_utils.h"
 #include "base/macros.h"
-#include "base/unix_file/fd_file.h"
-#include "common_runtime_test.h"
+#include "base64_test_util.h"
 #include "descriptors_names.h"
 #include "dex_file-inl.h"
 #include "dex_file_loader.h"
 #include "dex_file_types.h"
+#include "gtest/gtest.h"
 #include "leb128.h"
-#include "scoped_thread_state_change-inl.h"
 #include "standard_dex_file.h"
-#include "thread-current-inl.h"
 
 namespace art {
 
+static constexpr char kLocationString[] = "dex_file_location";
+
 // Make the Dex file version 37.
 static void MakeDexVersion37(DexFile* dex_file) {
   size_t offset = OFFSETOF_MEMBER(DexFile::Header, magic_) + 6;
@@ -55,7 +53,7 @@
   header->checksum_ = adler_checksum;
 }
 
-class DexFileVerifierTest : public CommonRuntimeTest {
+class DexFileVerifierTest : public testing::Test {
  protected:
   DexFile* GetDexFile(const uint8_t* dex_bytes, size_t length) {
     return new StandardDexFile(dex_bytes, length, "tmp", 0, nullptr, nullptr);
@@ -101,28 +99,19 @@
   std::unique_ptr<uint8_t[]> dex_bytes(DecodeBase64(base64, &length));
   CHECK(dex_bytes.get() != nullptr);
 
-  // write to provided file
-  std::unique_ptr<File> file(OS::CreateEmptyFile(location));
-  CHECK(file.get() != nullptr);
-  if (!file->WriteFully(dex_bytes.get(), length)) {
-    PLOG(FATAL) << "Failed to write base64 as dex file";
-  }
-  if (file->FlushCloseOrErase() != 0) {
-    PLOG(FATAL) << "Could not flush and close test file.";
-  }
-  file.reset();
-
-  // read dex file
-  ScopedObjectAccess soa(Thread::Current());
+  // read dex
   std::vector<std::unique_ptr<const DexFile>> tmp;
-  const ArtDexFileLoader dex_file_loader;
-  bool success = dex_file_loader.Open(
-      location, location, /* verify */ true, /* verify_checksum */ true, error_msg, &tmp);
+  const DexFileLoader dex_file_loader;
+  bool success = dex_file_loader.OpenAll(dex_bytes.get(),
+                                         length,
+                                         location,
+                                         /* verify */ true,
+                                         /* verify_checksum */ true,
+                                         error_msg,
+                                         &tmp);
   CHECK(success) << *error_msg;
   EXPECT_EQ(1U, tmp.size());
   std::unique_ptr<const DexFile> dex_file = std::move(tmp[0]);
-  EXPECT_EQ(PROT_READ, dex_file->GetPermissions());
-  EXPECT_TRUE(dex_file->IsReadOnly());
   return dex_file;
 }
 
@@ -148,9 +137,9 @@
     "AAIgAAANAAAAWgEAAAMgAAACAAAA6AEAAAAgAAABAAAA8wEAAAAQAAABAAAABAIAAA==";
 
 TEST_F(DexFileVerifierTest, GoodDex) {
-  ScratchFile tmp;
   std::string error_msg;
-  std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kGoodTestDex, tmp.GetFilename().c_str(),
+  std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kGoodTestDex,
+                                                       kLocationString,
                                                        &error_msg));
   ASSERT_TRUE(raw.get() != nullptr) << error_msg;
 }
@@ -1311,10 +1300,9 @@
 TEST_F(DexFileVerifierTest, DebugInfoTypeIdxTest) {
   {
     // The input dex file should be good before modification.
-    ScratchFile tmp;
     std::string error_msg;
     std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kDebugInfoTestDex,
-                                                         tmp.GetFilename().c_str(),
+                                                         kLocationString,
                                                          &error_msg));
     ASSERT_TRUE(raw.get() != nullptr) << error_msg;
   }
@@ -1333,10 +1321,9 @@
   {
     // The input dex file should be good before modification. Any file is fine, as long as it
     // uses all sections.
-    ScratchFile tmp;
     std::string error_msg;
     std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kGoodTestDex,
-                                                         tmp.GetFilename().c_str(),
+                                                         kLocationString,
                                                          &error_msg));
     ASSERT_TRUE(raw.get() != nullptr) << error_msg;
   }
@@ -1417,10 +1404,9 @@
 TEST_F(DexFileVerifierTest, ProtoOrdering) {
   {
     // The input dex file should be good before modification.
-    ScratchFile tmp;
     std::string error_msg;
     std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kProtoOrderingTestDex,
-                                                         tmp.GetFilename().c_str(),
+                                                         kLocationString,
                                                          &error_msg));
     ASSERT_TRUE(raw.get() != nullptr) << error_msg;
   }
diff --git a/runtime/dex/utf_test.cc b/runtime/dex/utf_test.cc
index d1e9751..d2f22d1 100644
--- a/runtime/dex/utf_test.cc
+++ b/runtime/dex/utf_test.cc
@@ -16,15 +16,15 @@
 
 #include "utf.h"
 
-#include "common_runtime_test.h"
-#include "utf-inl.h"
-
 #include <map>
 #include <vector>
 
+#include "gtest/gtest.h"
+#include "utf-inl.h"
+
 namespace art {
 
-class UtfTest : public CommonRuntimeTest {};
+class UtfTest : public testing::Test {};
 
 TEST_F(UtfTest, GetLeadingUtf16Char) {
   EXPECT_EQ(0xffff, GetLeadingUtf16Char(0xeeeeffff));