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