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