blob: aa9229ecde60942d5f7920212296103074c95275 [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() {
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 Carlstromf734cf52011-08-17 16:28:14 -070065class CommonTest : public testing::Test {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070066 protected:
67 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070068 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
69
Elliott Hughes0af55432011-08-17 18:37:28 -070070 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 Carlstromb0460ea2011-07-29 10:08:05 -070079 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 Hughesa5b897e2011-08-16 11:33:06 -070090 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070092 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094 runtime_.reset(Runtime::Create(boot_class_path_));
Carl Shapiro7a909592011-07-24 19:21:59 -070095 ASSERT_TRUE(runtime_ != NULL);
96 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070097 }
98
Brian Carlstromb0460ea2011-07-29 10:08:05 -070099 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 Wilsonac5b9e22011-07-27 15:11:13 -0400120 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700121 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 Hughes0af55432011-08-17 18:37:28 -0700126
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 Carlstromb0460ea2011-07-29 10:08:05 -0700135 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400136
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700137 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 Carlstrom9f30b382011-08-28 22:41:38 -0700146 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700147 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700148 return DexFile::OpenZip(libcore_dex_file_name);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400149 }
150
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700151 PathClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700152 CHECK(dex_file != NULL);
153 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700154 std::vector<const DexFile*> dex_files;
155 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700156 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700157 }
158
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700159 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 Carlstrom4a96b602011-07-26 16:40:23 -0700174 bool is_host_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700175 scoped_ptr_malloc<char> android_data_;
176 std::string art_cache_;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700177 scoped_ptr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700178 std::vector<const DexFile*> boot_class_path_;
Carl Shapiro7a909592011-07-24 19:21:59 -0700179 scoped_ptr<Runtime> runtime_;
180 ClassLinker* class_linker_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700181};
182
Brian Carlstrom934486c2011-07-12 23:42:50 -0700183} // namespace art