blob: 6cfa62d1d153d5d3694025c58cf00ca44b302d44 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "common_test.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07004#include "file.h"
5#include "image.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07006#include "image_writer.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07007#include "os.h"
8#include "space.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07009
10#include "gtest/gtest.h"
11
12namespace art {
13
Brian Carlstromf734cf52011-08-17 16:28:14 -070014class ImageTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070015
16TEST_F(ImageTest, WriteRead) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070017 scoped_ptr<DexFile> libcore_dex_file(GetLibCoreDex());
18 EXPECT_TRUE(libcore_dex_file.get() != NULL);
19
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070020 // TODO: garbage collect before writing
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021 const std::vector<Space*>& spaces = Heap::GetSpaces();
22 // can't currently deal with writing a space that might have pointers between spaces
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070023 ASSERT_EQ(1U, spaces.size());
24 Space* space = spaces[0];
Brian Carlstromdb4d5402011-08-09 12:18:28 -070025
26 ImageWriter writer;
27 ScratchFile tmp;
Brian Carlstrom8a436592011-08-15 21:27:23 -070028 const int image_base = 0x5000000;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070029 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 Carlstrom8a436592011-08-15 21:27:23 -070040
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 Carlstromdb4d5402011-08-09 12:18:28 -070057}
58
59} // namespace art