Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "common_test.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame^] | 4 | #include "file.h" |
| 5 | #include "image.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 6 | #include "image_writer.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame^] | 7 | #include "os.h" |
| 8 | #include "space.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 9 | |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | namespace art { |
| 13 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 14 | class ImageTest : public CommonTest {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 15 | |
| 16 | TEST_F(ImageTest, WriteRead) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 17 | scoped_ptr<DexFile> libcore_dex_file(GetLibCoreDex()); |
| 18 | EXPECT_TRUE(libcore_dex_file.get() != NULL); |
| 19 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame^] | 20 | // TODO: garbage collect before writing |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 21 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 22 | // can't currently deal with writing a space that might have pointers between spaces |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame^] | 23 | ASSERT_EQ(1U, spaces.size()); |
| 24 | Space* space = spaces[0]; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 25 | |
| 26 | ImageWriter writer; |
| 27 | ScratchFile tmp; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 28 | const int image_base = 0x5000000; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame^] | 29 | bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base)); |
| 30 | ASSERT_TRUE(success); |
| 31 | |
| 32 | { |
| 33 | scoped_ptr<File> file(OS::OpenFile(tmp.GetFilename(), false)); |
| 34 | ASSERT_TRUE(file != NULL); |
| 35 | ImageHeader image_header; |
| 36 | file->ReadFully(&image_header, sizeof(image_header)); |
| 37 | ASSERT_TRUE(image_header.IsValid()); |
| 38 | ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length())); |
| 39 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 40 | |
| 41 | // tear down old runtime and make a new one |
| 42 | delete runtime_.release(); |
| 43 | java_lang_dex_file_.reset(GetLibCoreDex()); |
| 44 | |
| 45 | std::vector<const DexFile*> boot_class_path; |
| 46 | boot_class_path.push_back(java_lang_dex_file_.get()); |
| 47 | |
| 48 | Runtime::Options options; |
| 49 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
| 50 | std::string boot_image("-Xbootimage:"); |
| 51 | boot_image.append(tmp.GetFilename()); |
| 52 | options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL))); |
| 53 | |
| 54 | runtime_.reset(Runtime::Create(options, false)); |
| 55 | ASSERT_TRUE(runtime_ != NULL); |
| 56 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace art |