blob: 99e4b41a8dfa5def8fd82a01a2f64b9118113ae9 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2009 Google Inc. All Rights Reserved.
2
Brian Carlstromdb4d5402011-08-09 12:18:28 -07003#include "file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07004
Elliott Hughes90a33692011-08-30 13:27:07 -07005#include "UniquePtr.h"
6#include "common_test.h"
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "os.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07008
9namespace art {
10
Brian Carlstromf734cf52011-08-17 16:28:14 -070011class FileTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070012
13TEST_F(FileTest, Read) {
14 std::string filename = GetLibCoreDexFileName();
Elliott Hughes90a33692011-08-30 13:27:07 -070015 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
16 ASSERT_TRUE(file.get() != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070017 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
27TEST_F(FileTest, FileLength) {
28 std::string filename = GetLibCoreDexFileName();
Elliott Hughes90a33692011-08-30 13:27:07 -070029 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
30 ASSERT_TRUE(file.get() != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070031 EXPECT_NE(0, file->Length());
32}
33
34
35TEST_F(FileTest, FilePosition) {
36 std::string filename = GetLibCoreDexFileName();
Elliott Hughes90a33692011-08-30 13:27:07 -070037 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
38 ASSERT_TRUE(file.get() != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039 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 Carlstrom4a289ed2011-08-16 17:17:49 -070046
47TEST_F(FileTest, FileFd) {
48 std::string filename = GetLibCoreDexFileName();
Elliott Hughes90a33692011-08-30 13:27:07 -070049 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
50 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070051 EXPECT_NE(-1, file->Fd());
52}
53
54
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055} // namespace art