Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2011 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 #ifndef ART_RUNTIME_COMMON_RUNTIME_TEST_H_
     18 #define ART_RUNTIME_COMMON_RUNTIME_TEST_H_
     19 
     20 #include <gtest/gtest.h>
     21 #include <jni.h>
     22 
     23 #include <string>
     24 
     25 #include "arch/instruction_set.h"
     26 #include "base/mutex.h"
     27 #include "globals.h"
     28 #include "os.h"
     29 
     30 namespace art {
     31 
     32 class ClassLinker;
     33 class CompilerCallbacks;
     34 class DexFile;
     35 class JavaVMExt;
     36 class Runtime;
     37 typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
     38 
     39 class ScratchFile {
     40  public:
     41   ScratchFile();
     42 
     43   explicit ScratchFile(const std::string& filename);
     44 
     45   ScratchFile(const ScratchFile& other, const char* suffix);
     46 
     47   explicit ScratchFile(ScratchFile&& other);
     48 
     49   ScratchFile& operator=(ScratchFile&& other);
     50 
     51   explicit ScratchFile(File* file);
     52 
     53   ~ScratchFile();
     54 
     55   const std::string& GetFilename() const {
     56     return filename_;
     57   }
     58 
     59   File* GetFile() const {
     60     return file_.get();
     61   }
     62 
     63   int GetFd() const;
     64 
     65   void Close();
     66   void Unlink();
     67 
     68  private:
     69   std::string filename_;
     70   std::unique_ptr<File> file_;
     71 };
     72 
     73 class CommonRuntimeTestImpl {
     74  public:
     75   CommonRuntimeTestImpl();
     76   virtual ~CommonRuntimeTestImpl();
     77   static void SetUpAndroidRoot();
     78 
     79   // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
     80   // non-derived class, be sure to also call the corresponding tear-down below.
     81   static void SetUpAndroidData(std::string& android_data);
     82 
     83   static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
     84 
     85   // Gets the paths of the libcore dex files.
     86   static std::vector<std::string> GetLibCoreDexFileNames();
     87 
     88   // Returns bin directory which contains host's prebuild tools.
     89   static std::string GetAndroidHostToolsDir();
     90 
     91   // Returns bin directory wahich contains target's prebuild tools.
     92   static std::string GetAndroidTargetToolsDir(InstructionSet isa);
     93 
     94  protected:
     95   // Allow subclases such as CommonCompilerTest to add extra options.
     96   virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
     97 
     98   // Called before the runtime is created.
     99   virtual void PreRuntimeCreate() {}
    100 
    101   // Called after the runtime is created.
    102   virtual void PostRuntimeCreate() {}
    103 
    104   static bool IsHost() {
    105     return !kIsTargetBuild;
    106   }
    107 
    108   // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
    109   static std::string GetCoreArtLocation();
    110 
    111   // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
    112   static std::string GetCoreOatLocation();
    113 
    114   std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
    115 
    116   void ClearDirectory(const char* dirpath);
    117 
    118   std::string GetTestAndroidRoot();
    119 
    120   std::string GetTestDexFileName(const char* name) const;
    121 
    122   std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
    123 
    124   std::unique_ptr<const DexFile> OpenTestDexFile(const char* name)
    125       SHARED_REQUIRES(Locks::mutator_lock_);
    126 
    127   jobject LoadDex(const char* dex_name) SHARED_REQUIRES(Locks::mutator_lock_);
    128 
    129   std::string android_data_;
    130   std::string dalvik_cache_;
    131 
    132   std::unique_ptr<Runtime> runtime_;
    133 
    134   // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
    135   // owned by the runtime.
    136   ClassLinker* class_linker_;
    137   const DexFile* java_lang_dex_file_;
    138   std::vector<const DexFile*> boot_class_path_;
    139 
    140   // Get the dex files from a PathClassLoader. This in order of the dex elements and their dex
    141   // arrays.
    142   std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
    143 
    144   // Get the first dex file from a PathClassLoader. Will abort if it is null.
    145   const DexFile* GetFirstDexFile(jobject jclass_loader);
    146 
    147   std::unique_ptr<CompilerCallbacks> callbacks_;
    148 
    149   void SetUp();
    150 
    151   void TearDown();
    152 
    153   void FinalizeSetup();
    154 
    155  private:
    156   static std::string GetCoreFileLocation(const char* suffix);
    157 
    158   std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
    159 };
    160 
    161 template <typename TestType>
    162 class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
    163  public:
    164   CommonRuntimeTestBase() {}
    165   virtual ~CommonRuntimeTestBase() {}
    166 
    167  protected:
    168   virtual void SetUp() {
    169     CommonRuntimeTestImpl::SetUp();
    170   }
    171 
    172   virtual void TearDown() {
    173     CommonRuntimeTestImpl::TearDown();
    174   }
    175 
    176   // Called to finish up runtime creation and filling test fields. By default runs root
    177   // initializers, initialize well-known classes, and creates the heap thread pool.
    178   virtual void FinalizeSetup() {
    179     CommonRuntimeTestImpl::FinalizeSetup();
    180   }
    181 };
    182 
    183 using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
    184 
    185 template <typename Param>
    186 using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
    187 
    188 // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
    189 // rather than aborting, so be careful!
    190 class CheckJniAbortCatcher {
    191  public:
    192   CheckJniAbortCatcher();
    193 
    194   ~CheckJniAbortCatcher();
    195 
    196   void Check(const char* expected_text);
    197 
    198  private:
    199   static void Hook(void* data, const std::string& reason);
    200 
    201   JavaVMExt* const vm_;
    202   std::string actual_;
    203 
    204   DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
    205 };
    206 
    207 #define TEST_DISABLED_FOR_MIPS() \
    208   if (kRuntimeISA == kMips) { \
    209     printf("WARNING: TEST DISABLED FOR MIPS\n"); \
    210     return; \
    211   }
    212 
    213 }  // namespace art
    214 
    215 namespace std {
    216 
    217 // TODO: isn't gtest supposed to be able to print STL types for itself?
    218 template <typename T>
    219 std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
    220 
    221 }  // namespace std
    222 
    223 #endif  // ART_RUNTIME_COMMON_RUNTIME_TEST_H_
    224