blob: bb9b93cd9c5c182890f0e4e7649cdd0e2a25815d [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"
11#include "dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070012#include "heap.h"
13#include "gtest/gtest.h"
14#include "stringprintf.h"
15#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070016#include "unicode/uclean.h"
17#include "unicode/uvernum.h"
18
Brian Carlstrom934486c2011-07-12 23:42:50 -070019namespace art {
20
Brian Carlstrom9f30b382011-08-28 22:41:38 -070021static inline const DexFile* OpenDexFileBase64(const char* base64,
22 const std::string& location) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023 CHECK(base64 != NULL);
24 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070025 byte* dex_bytes = DecodeBase64(base64, &length);
26 CHECK(dex_bytes != NULL);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070027 const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070029 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030}
31
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032class ScratchFile {
33 public:
34 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070035 filename_ = getenv("ANDROID_DATA");
36 filename_ += "/TmpFile-XXXXXX";
37 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038 CHECK_NE(-1, fd_);
39 }
40
41 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070042 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 CHECK_EQ(0, unlink_result);
44 int close_result = close(fd_);
45 CHECK_EQ(0, close_result);
46 }
47
48 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070049 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050 }
51
52 int GetFd() const {
53 return fd_;
54 }
55
56 private:
Elliott Hughes34023802011-08-30 12:06:17 -070057 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070058 int fd_;
59};
60
Brian Carlstromf734cf52011-08-17 16:28:14 -070061class CommonTest : public testing::Test {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070062 protected:
63 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070064 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
65
Elliott Hughes0af55432011-08-17 18:37:28 -070066 if (is_host_) {
67 // $ANDROID_ROOT is set on the device, but not on the host.
68 // We need to set this so that icu4c can find its locale data.
69 std::string root;
70 root += getenv("ANDROID_BUILD_TOP");
71 root += "/out/host/linux-x86";
72 setenv("ANDROID_ROOT", root.c_str(), 1);
73 }
74
Elliott Hughes34023802011-08-30 12:06:17 -070075 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/sdcard/art-data-XXXXXX");
76 ASSERT_TRUE(mkdtemp(&android_data_[0]) != NULL);
77 setenv("ANDROID_DATA", android_data_.c_str(), 1);
78 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -070079 art_cache_.append("/art-cache");
80 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
81 ASSERT_EQ(mkdir_result, 0);
82
Elliott Hughesa5b897e2011-08-16 11:33:06 -070083 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070084
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070085 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070087 runtime_.reset(Runtime::Create(boot_class_path_));
Elliott Hughes90a33692011-08-30 13:27:07 -070088 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070089 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070090 }
91
Brian Carlstromb0460ea2011-07-29 10:08:05 -070092 virtual void TearDown() {
93 const char* android_data = getenv("ANDROID_DATA");
94 ASSERT_TRUE(android_data != NULL);
95 DIR* dir = opendir(art_cache_.c_str());
96 ASSERT_TRUE(dir != NULL);
97 while (true) {
98 struct dirent entry;
99 struct dirent* entry_ptr;
100 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
101 ASSERT_EQ(0, readdir_result);
102 if (entry_ptr == NULL) {
103 break;
104 }
105 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
106 continue;
107 }
108 std::string filename(art_cache_);
109 filename.push_back('/');
110 filename.append(entry_ptr->d_name);
111 int unlink_result = unlink(filename.c_str());
112 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400113 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700114 closedir(dir);
115 int rmdir_cache_result = rmdir(art_cache_.c_str());
116 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700117 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700118 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700119
120 // icu4c has a fixed 10-element array "gCommonICUDataArray".
121 // If we run > 10 tests, we fill that array and u_setCommonData fails.
122 // There's a function to clear the array, but it's not public...
123 typedef void (*IcuCleanupFn)();
124 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
125 CHECK(sym != NULL);
126 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
127 (*icu_cleanup_fn)();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700128 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400129
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700130 std::string GetLibCoreDexFileName() {
131 if (is_host_) {
132 const char* host_dir = getenv("ANDROID_HOST_OUT");
133 CHECK(host_dir != NULL);
134 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
135 }
136 return std::string("/system/framework/core.jar");
137 }
138
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700139 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700140 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700141 return DexFile::OpenZip(libcore_dex_file_name);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400142 }
143
Brian Carlstrom8a487412011-08-29 20:08:52 -0700144 const PathClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700145 CHECK(dex_file != NULL);
146 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700147 std::vector<const DexFile*> dex_files;
148 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700149 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700150 }
151
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700152 const DexFile* OpenTestDexFile(const char* name) {
153 CHECK(name != NULL);
154 std::string filename;
155 if (is_host_) {
156 // on the host, just read target dex file
157 filename += getenv("ANDROID_PRODUCT_OUT");
158 }
159 filename += "/system/framework/art-test-dex-";
160 filename += name;
161 filename += ".jar";
162 const DexFile* dex_file = DexFile::OpenZip(filename);
163 CHECK(dex_file != NULL) << "Could not open " << filename;
164 return dex_file;
165 }
166
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700167 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700168 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700169 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700170 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700171 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700172 UniquePtr<Runtime> runtime_;
Carl Shapiro7a909592011-07-24 19:21:59 -0700173 ClassLinker* class_linker_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700174};
175
Brian Carlstrom934486c2011-07-12 23:42:50 -0700176} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700177
178namespace std {
179
180// TODO: isn't gtest supposed to be able to print STL types for itself?
181template <typename T>
182std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
183 os << "[";
184 for (size_t i = 0; i < rhs.size(); ++i) {
185 os << rhs[i];
186 if (i < rhs.size() - 1) {
187 os << ", ";
188 }
189 }
190 os << "]";
191 return os;
192}
193
194} // namespace std