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