Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 3 | #include <dirent.h> |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 4 | #include <dlfcn.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 8 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "base64.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 11 | #include "class_loader.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | #include "dex_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 14 | #include "heap.h" |
| 15 | #include "runtime.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 16 | #include "stringprintf.h" |
| 17 | #include "thread.h" |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 18 | #include "unicode/uclean.h" |
| 19 | #include "unicode/uvernum.h" |
| 20 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 23 | static inline const DexFile* OpenDexFileBase64(const char* base64, |
| 24 | const std::string& location) { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | CHECK(base64 != NULL); |
| 26 | size_t length; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 27 | byte* dex_bytes = DecodeBase64(base64, &length); |
| 28 | CHECK(dex_bytes != NULL); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 29 | const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 31 | return dex_file; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 34 | class ScratchFile { |
| 35 | public: |
| 36 | ScratchFile() { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 37 | filename_ = getenv("ANDROID_DATA"); |
| 38 | filename_ += "/TmpFile-XXXXXX"; |
| 39 | fd_ = mkstemp(&filename_[0]); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | CHECK_NE(-1, fd_); |
| 41 | } |
| 42 | |
| 43 | ~ScratchFile() { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 44 | int unlink_result = unlink(filename_.c_str()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | CHECK_EQ(0, unlink_result); |
| 46 | int close_result = close(fd_); |
| 47 | CHECK_EQ(0, close_result); |
| 48 | } |
| 49 | |
| 50 | const char* GetFilename() const { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 51 | return filename_.c_str(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int GetFd() const { |
| 55 | return fd_; |
| 56 | } |
| 57 | |
| 58 | private: |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 59 | std::string filename_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 60 | int fd_; |
| 61 | }; |
| 62 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 63 | class CommonTest : public testing::Test { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 64 | protected: |
| 65 | virtual void SetUp() { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 66 | is_host_ = getenv("ANDROID_BUILD_TOP") != NULL; |
| 67 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 68 | if (is_host_) { |
| 69 | // $ANDROID_ROOT is set on the device, but not on the host. |
| 70 | // We need to set this so that icu4c can find its locale data. |
| 71 | std::string root; |
| 72 | root += getenv("ANDROID_BUILD_TOP"); |
| 73 | root += "/out/host/linux-x86"; |
| 74 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 75 | } |
| 76 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 77 | android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/sdcard/art-data-XXXXXX"); |
| 78 | ASSERT_TRUE(mkdtemp(&android_data_[0]) != NULL); |
| 79 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
| 80 | art_cache_.append(android_data_.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 81 | art_cache_.append("/art-cache"); |
| 82 | int mkdir_result = mkdir(art_cache_.c_str(), 0700); |
| 83 | ASSERT_EQ(mkdir_result, 0); |
| 84 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 85 | java_lang_dex_file_.reset(GetLibCoreDex()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 86 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 87 | boot_class_path_.push_back(java_lang_dex_file_.get()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 88 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 89 | runtime_.reset(Runtime::Create(boot_class_path_)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 90 | ASSERT_TRUE(runtime_.get() != NULL); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 91 | class_linker_ = runtime_->GetClassLinker(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame^] | 92 | |
| 93 | Heap::VerifyHeap(); // Check for heap corruption before the test |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 96 | virtual void TearDown() { |
| 97 | const char* android_data = getenv("ANDROID_DATA"); |
| 98 | ASSERT_TRUE(android_data != NULL); |
| 99 | DIR* dir = opendir(art_cache_.c_str()); |
| 100 | ASSERT_TRUE(dir != NULL); |
| 101 | while (true) { |
| 102 | struct dirent entry; |
| 103 | struct dirent* entry_ptr; |
| 104 | int readdir_result = readdir_r(dir, &entry, &entry_ptr); |
| 105 | ASSERT_EQ(0, readdir_result); |
| 106 | if (entry_ptr == NULL) { |
| 107 | break; |
| 108 | } |
| 109 | if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) { |
| 110 | continue; |
| 111 | } |
| 112 | std::string filename(art_cache_); |
| 113 | filename.push_back('/'); |
| 114 | filename.append(entry_ptr->d_name); |
| 115 | int unlink_result = unlink(filename.c_str()); |
| 116 | ASSERT_EQ(0, unlink_result); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 117 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 118 | closedir(dir); |
| 119 | int rmdir_cache_result = rmdir(art_cache_.c_str()); |
| 120 | ASSERT_EQ(0, rmdir_cache_result); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 121 | int rmdir_data_result = rmdir(android_data_.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 122 | ASSERT_EQ(0, rmdir_data_result); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 123 | |
| 124 | // icu4c has a fixed 10-element array "gCommonICUDataArray". |
| 125 | // If we run > 10 tests, we fill that array and u_setCommonData fails. |
| 126 | // There's a function to clear the array, but it's not public... |
| 127 | typedef void (*IcuCleanupFn)(); |
| 128 | void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); |
| 129 | CHECK(sym != NULL); |
| 130 | IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); |
| 131 | (*icu_cleanup_fn)(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame^] | 132 | |
| 133 | Heap::VerifyHeap(); // Check for heap corruption after the test |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 134 | } |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 135 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 136 | std::string GetLibCoreDexFileName() { |
| 137 | if (is_host_) { |
| 138 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 139 | CHECK(host_dir != NULL); |
| 140 | return StringPrintf("%s/framework/core-hostdex.jar", host_dir); |
| 141 | } |
| 142 | return std::string("/system/framework/core.jar"); |
| 143 | } |
| 144 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 145 | const DexFile* GetLibCoreDex() { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 146 | std::string libcore_dex_file_name = GetLibCoreDexFileName(); |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 147 | return DexFile::OpenZip(libcore_dex_file_name); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 150 | const PathClassLoader* AllocPathClassLoader(const DexFile* dex_file) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 151 | CHECK(dex_file != NULL); |
| 152 | class_linker_->RegisterDexFile(*dex_file); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 153 | std::vector<const DexFile*> dex_files; |
| 154 | dex_files.push_back(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 155 | return PathClassLoader::Alloc(dex_files); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 158 | const DexFile* OpenTestDexFile(const char* name) { |
| 159 | CHECK(name != NULL); |
| 160 | std::string filename; |
| 161 | if (is_host_) { |
| 162 | // on the host, just read target dex file |
| 163 | filename += getenv("ANDROID_PRODUCT_OUT"); |
| 164 | } |
| 165 | filename += "/system/framework/art-test-dex-"; |
| 166 | filename += name; |
| 167 | filename += ".jar"; |
| 168 | const DexFile* dex_file = DexFile::OpenZip(filename); |
| 169 | CHECK(dex_file != NULL) << "Could not open " << filename; |
| 170 | return dex_file; |
| 171 | } |
| 172 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 173 | bool is_host_; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 174 | std::string android_data_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 175 | std::string art_cache_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 176 | UniquePtr<const DexFile> java_lang_dex_file_; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 177 | std::vector<const DexFile*> boot_class_path_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 178 | UniquePtr<Runtime> runtime_; |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 179 | ClassLinker* class_linker_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 182 | } // namespace art |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 183 | |
| 184 | namespace std { |
| 185 | |
| 186 | // TODO: isn't gtest supposed to be able to print STL types for itself? |
| 187 | template <typename T> |
| 188 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { |
| 189 | os << "["; |
| 190 | for (size_t i = 0; i < rhs.size(); ++i) { |
| 191 | os << rhs[i]; |
| 192 | if (i < rhs.size() - 1) { |
| 193 | os << ", "; |
| 194 | } |
| 195 | } |
| 196 | os << "]"; |
| 197 | return os; |
| 198 | } |
| 199 | |
| 200 | } // namespace std |