blob: c79cea6e108e2f2fe859fb1a032c3087b0940427 [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
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "base64.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "class_loader.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070013#include "gtest/gtest.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070014#include "heap.h"
15#include "runtime.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070016#include "stringprintf.h"
17#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070018#include "unicode/uclean.h"
19#include "unicode/uvernum.h"
20
Brian Carlstrom934486c2011-07-12 23:42:50 -070021namespace art {
22
Brian Carlstrom9f30b382011-08-28 22:41:38 -070023static inline const DexFile* OpenDexFileBase64(const char* base64,
24 const std::string& location) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 CHECK(base64 != NULL);
26 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070027 byte* dex_bytes = DecodeBase64(base64, &length);
28 CHECK(dex_bytes != NULL);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070029 const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070031 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032}
33
Brian Carlstromdb4d5402011-08-09 12:18:28 -070034class ScratchFile {
35 public:
36 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070037 filename_ = getenv("ANDROID_DATA");
38 filename_ += "/TmpFile-XXXXXX";
39 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040 CHECK_NE(-1, fd_);
41 }
42
43 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070044 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070045 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 Hughes34023802011-08-30 12:06:17 -070051 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 }
53
54 int GetFd() const {
55 return fd_;
56 }
57
58 private:
Elliott Hughes34023802011-08-30 12:06:17 -070059 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070060 int fd_;
61};
62
Brian Carlstromf734cf52011-08-17 16:28:14 -070063class CommonTest : public testing::Test {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070064 protected:
65 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070066 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
67
Elliott Hughes0af55432011-08-17 18:37:28 -070068 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 Hughes34023802011-08-30 12:06:17 -070077 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 Carlstromb0460ea2011-07-29 10:08:05 -070081 art_cache_.append("/art-cache");
82 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
83 ASSERT_EQ(mkdir_result, 0);
84
Elliott Hughesa5b897e2011-08-16 11:33:06 -070085 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070087 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070089 runtime_.reset(Runtime::Create(boot_class_path_));
Elliott Hughes90a33692011-08-30 13:27:07 -070090 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070091 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 }
93
Brian Carlstromb0460ea2011-07-29 10:08:05 -070094 virtual void TearDown() {
95 const char* android_data = getenv("ANDROID_DATA");
96 ASSERT_TRUE(android_data != NULL);
97 DIR* dir = opendir(art_cache_.c_str());
98 ASSERT_TRUE(dir != NULL);
99 while (true) {
100 struct dirent entry;
101 struct dirent* entry_ptr;
102 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
103 ASSERT_EQ(0, readdir_result);
104 if (entry_ptr == NULL) {
105 break;
106 }
107 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
108 continue;
109 }
110 std::string filename(art_cache_);
111 filename.push_back('/');
112 filename.append(entry_ptr->d_name);
113 int unlink_result = unlink(filename.c_str());
114 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400115 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700116 closedir(dir);
117 int rmdir_cache_result = rmdir(art_cache_.c_str());
118 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700119 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700120 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700121
122 // icu4c has a fixed 10-element array "gCommonICUDataArray".
123 // If we run > 10 tests, we fill that array and u_setCommonData fails.
124 // There's a function to clear the array, but it's not public...
125 typedef void (*IcuCleanupFn)();
126 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
127 CHECK(sym != NULL);
128 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
129 (*icu_cleanup_fn)();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700130 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400131
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700132 std::string GetLibCoreDexFileName() {
133 if (is_host_) {
134 const char* host_dir = getenv("ANDROID_HOST_OUT");
135 CHECK(host_dir != NULL);
136 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
137 }
138 return std::string("/system/framework/core.jar");
139 }
140
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700141 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700142 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700143 return DexFile::OpenZip(libcore_dex_file_name);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400144 }
145
Brian Carlstrom8a487412011-08-29 20:08:52 -0700146 const PathClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700147 CHECK(dex_file != NULL);
148 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700149 std::vector<const DexFile*> dex_files;
150 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700151 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700152 }
153
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700154 const DexFile* OpenTestDexFile(const char* name) {
155 CHECK(name != NULL);
156 std::string filename;
157 if (is_host_) {
158 // on the host, just read target dex file
159 filename += getenv("ANDROID_PRODUCT_OUT");
160 }
161 filename += "/system/framework/art-test-dex-";
162 filename += name;
163 filename += ".jar";
164 const DexFile* dex_file = DexFile::OpenZip(filename);
165 CHECK(dex_file != NULL) << "Could not open " << filename;
166 return dex_file;
167 }
168
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700169 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700170 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700171 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700172 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700173 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700174 UniquePtr<Runtime> runtime_;
Carl Shapiro7a909592011-07-24 19:21:59 -0700175 ClassLinker* class_linker_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700176};
177
Brian Carlstrom934486c2011-07-12 23:42:50 -0700178} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700179
180namespace std {
181
182// TODO: isn't gtest supposed to be able to print STL types for itself?
183template <typename T>
184std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
185 os << "[";
186 for (size_t i = 0; i < rhs.size(); ++i) {
187 os << rhs[i];
188 if (i < rhs.size() - 1) {
189 os << ", ";
190 }
191 }
192 os << "]";
193 return os;
194}
195
196} // namespace std