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 "base/mutex.h"
     26 #include "globals.h"
     27 #include "os.h"
     28 
     29 namespace art {
     30 
     31 class ClassLinker;
     32 class CompilerCallbacks;
     33 class DexFile;
     34 class JavaVMExt;
     35 class Runtime;
     36 typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
     37 
     38 class ScratchFile {
     39  public:
     40   ScratchFile();
     41 
     42   ScratchFile(const ScratchFile& other, const char* suffix);
     43 
     44   explicit ScratchFile(File* file);
     45 
     46   ~ScratchFile();
     47 
     48   const std::string& GetFilename() const {
     49     return filename_;
     50   }
     51 
     52   File* GetFile() const {
     53     return file_.get();
     54   }
     55 
     56   int GetFd() const;
     57 
     58   void Close();
     59   void Unlink();
     60 
     61  private:
     62   std::string filename_;
     63   std::unique_ptr<File> file_;
     64 };
     65 
     66 class CommonRuntimeTest : public testing::Test {
     67  public:
     68   static void SetUpAndroidRoot();
     69 
     70   // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
     71   // non-derived class, be sure to also call the corresponding tear-down below.
     72   static void SetUpAndroidData(std::string& android_data);
     73 
     74   static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
     75 
     76   CommonRuntimeTest();
     77   ~CommonRuntimeTest();
     78 
     79  protected:
     80   static bool IsHost() {
     81     return !kIsTargetBuild;
     82   }
     83 
     84   const DexFile* LoadExpectSingleDexFile(const char* location);
     85 
     86   virtual void SetUp();
     87 
     88   // Allow subclases such as CommonCompilerTest to add extra options.
     89   virtual void SetUpRuntimeOptions(RuntimeOptions* options) {}
     90 
     91   void ClearDirectory(const char* dirpath);
     92 
     93   virtual void TearDown();
     94 
     95   // Gets the path of the libcore dex file.
     96   std::string GetLibCoreDexFileName();
     97 
     98   // Gets the path of the specified dex file for host or target.
     99   std::string GetDexFileName(const std::string& jar_prefix);
    100 
    101   std::string GetTestAndroidRoot();
    102 
    103   std::vector<const DexFile*> OpenTestDexFiles(const char* name)
    104       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    105 
    106   const DexFile* OpenTestDexFile(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    107 
    108   jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    109 
    110   std::string android_data_;
    111   std::string dalvik_cache_;
    112   const DexFile* java_lang_dex_file_;  // owned by runtime_
    113   std::vector<const DexFile*> boot_class_path_;
    114   std::unique_ptr<Runtime> runtime_;
    115   // Owned by the runtime
    116   ClassLinker* class_linker_;
    117 
    118  private:
    119   std::unique_ptr<CompilerCallbacks> callbacks_;
    120   std::vector<const DexFile*> opened_dex_files_;
    121 };
    122 
    123 // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
    124 // rather than aborting, so be careful!
    125 class CheckJniAbortCatcher {
    126  public:
    127   CheckJniAbortCatcher();
    128 
    129   ~CheckJniAbortCatcher();
    130 
    131   void Check(const char* expected_text);
    132 
    133  private:
    134   static void Hook(void* data, const std::string& reason);
    135 
    136   JavaVMExt* vm_;
    137   std::string actual_;
    138 
    139   DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
    140 };
    141 
    142 // TODO: These tests were disabled for portable when we went to having
    143 // MCLinker link LLVM ELF output because we no longer just have code
    144 // blobs in memory. We'll need to dlopen to load and relocate
    145 // temporary output to resurrect these tests.
    146 #define TEST_DISABLED_FOR_PORTABLE() \
    147   if (kUsePortableCompiler) { \
    148     printf("WARNING: TEST DISABLED FOR PORTABLE\n"); \
    149     return; \
    150   }
    151 
    152 // TODO: When heap reference poisoning works with the compiler, get rid of this.
    153 #define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \
    154   if (kPoisonHeapReferences) { \
    155     printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \
    156     return; \
    157   }
    158 
    159 #define TEST_DISABLED_FOR_MIPS() \
    160   if (kRuntimeISA == kMips || kRuntimeISA == kMips64) { \
    161     printf("WARNING: TEST DISABLED FOR MIPS\n"); \
    162     return; \
    163   }
    164 
    165 }  // namespace art
    166 
    167 namespace std {
    168 
    169 // TODO: isn't gtest supposed to be able to print STL types for itself?
    170 template <typename T>
    171 std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
    172 
    173 }  // namespace std
    174 
    175 #endif  // ART_RUNTIME_COMMON_RUNTIME_TEST_H_
    176