Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2009 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 3 | #include "file.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 4 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 5 | #include "UniquePtr.h" |
| 6 | #include "common_test.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include "os.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 8 | |
| 9 | namespace art { |
| 10 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 11 | class FileTest : public CommonTest {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 12 | |
| 13 | TEST_F(FileTest, Read) { |
| 14 | std::string filename = GetLibCoreDexFileName(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 15 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 16 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 17 | EXPECT_STREQ(filename.c_str(), file->name()); |
| 18 | char buffer[3]; |
| 19 | buffer[0] = '\0'; |
| 20 | EXPECT_TRUE(file->ReadFully(buffer, 2)); // ReadFully returns true. |
| 21 | buffer[2] = '\0'; |
| 22 | EXPECT_STREQ("PK", buffer); // zip file magic |
| 23 | EXPECT_FALSE(file->WriteByte(1)); // Cannot write to a read-only file. |
| 24 | } |
| 25 | |
| 26 | |
| 27 | TEST_F(FileTest, FileLength) { |
| 28 | std::string filename = GetLibCoreDexFileName(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 29 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 30 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 31 | EXPECT_NE(0, file->Length()); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | TEST_F(FileTest, FilePosition) { |
| 36 | std::string filename = GetLibCoreDexFileName(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 37 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 38 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 39 | char buf[4]; |
| 40 | EXPECT_TRUE(file->ReadFully(buf, 2)); |
| 41 | EXPECT_EQ(2, file->Position()); |
| 42 | EXPECT_TRUE(file->ReadFully(buf, 2)); |
| 43 | EXPECT_EQ(4, file->Position()); |
| 44 | } |
| 45 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 46 | |
| 47 | TEST_F(FileTest, FileFd) { |
| 48 | std::string filename = GetLibCoreDexFileName(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 49 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 50 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 51 | EXPECT_NE(-1, file->Fd()); |
| 52 | } |
| 53 | |
| 54 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 55 | } // namespace art |