blob: 4fcf44d12869fb5758f5222bb92c9d02274b79a9 [file] [log] [blame]
Brian Carlstrom934486c2011-07-12 23:42:50 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstromb0460ea2011-07-29 10:08:05 -07003#include <dirent.h>
Elliott Hughes0af55432011-08-17 18:37:28 -07004#include <dlfcn.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -07005#include <sys/stat.h>
6#include <sys/types.h>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "base64.h"
9#include "heap.h"
10#include "thread.h"
Jesse Wilsonac5b9e22011-07-27 15:11:13 -040011#include "stringprintf.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "class_linker.h"
13#include "dex_file.h"
Brian Carlstroma331b3c2011-07-18 17:47:56 -070014
Elliott Hughes0af55432011-08-17 18:37:28 -070015#include "unicode/uclean.h"
16#include "unicode/uvernum.h"
17
Brian Carlstroma331b3c2011-07-18 17:47:56 -070018#include "gtest/gtest.h"
19
Brian Carlstrom934486c2011-07-12 23:42:50 -070020namespace art {
21
Brian Carlstrom9f30b382011-08-28 22:41:38 -070022static inline const DexFile* OpenDexFileBase64(const char* base64,
23 const std::string& location) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024 CHECK(base64 != NULL);
25 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070026 byte* dex_bytes = DecodeBase64(base64, &length);
27 CHECK(dex_bytes != NULL);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070028 const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070030 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031}
32
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033class ScratchFile {
34 public:
35 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070036 filename_ = getenv("ANDROID_DATA");
37 filename_ += "/TmpFile-XXXXXX";
38 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039 CHECK_NE(-1, fd_);
40 }
41
42 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070043 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 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 Hughes34023802011-08-30 12:06:17 -070050 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051 }
52
53 int GetFd() const {
54 return fd_;
55 }
56
57 private:
Elliott Hughes34023802011-08-30 12:06:17 -070058 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070059 int fd_;
60};
61
Brian Carlstromf734cf52011-08-17 16:28:14 -070062class CommonTest : public testing::Test {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070063 protected:
64 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070065 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
66
Elliott Hughes0af55432011-08-17 18:37:28 -070067 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 Hughes34023802011-08-30 12:06:17 -070076 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 Carlstromb0460ea2011-07-29 10:08:05 -070080 art_cache_.append("/art-cache");
81 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
82 ASSERT_EQ(mkdir_result, 0);
83
Elliott Hughesa5b897e2011-08-16 11:33:06 -070084 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070085
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070086 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070088 runtime_.reset(Runtime::Create(boot_class_path_));
Carl Shapiro7a909592011-07-24 19:21:59 -070089 ASSERT_TRUE(runtime_ != NULL);
90 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 }
92
Brian Carlstromb0460ea2011-07-29 10:08:05 -070093 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 Wilsonac5b9e22011-07-27 15:11:13 -0400114 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700115 closedir(dir);
116 int rmdir_cache_result = rmdir(art_cache_.c_str());
117 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700118 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700119 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700120
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 Carlstromb0460ea2011-07-29 10:08:05 -0700129 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400130
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700131 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 Carlstrom9f30b382011-08-28 22:41:38 -0700140 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700141 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700142 return DexFile::OpenZip(libcore_dex_file_name);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400143 }
144
Brian Carlstrom8a487412011-08-29 20:08:52 -0700145 const PathClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700146 CHECK(dex_file != NULL);
147 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700148 std::vector<const DexFile*> dex_files;
149 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700150 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700151 }
152
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700153 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 Carlstrom4a96b602011-07-26 16:40:23 -0700168 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700169 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700170 std::string art_cache_;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700171 scoped_ptr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700172 std::vector<const DexFile*> boot_class_path_;
Carl Shapiro7a909592011-07-24 19:21:59 -0700173 scoped_ptr<Runtime> runtime_;
174 ClassLinker* class_linker_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700175};
176
Brian Carlstrom934486c2011-07-12 23:42:50 -0700177} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700178
179namespace std {
180
181// TODO: isn't gtest supposed to be able to print STL types for itself?
182template <typename T>
183std::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