Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 17 | #include "file.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 19 | #include "UniquePtr.h" |
| 20 | #include "common_test.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 21 | #include "os.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 25 | class FileTest : public CommonTest {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 26 | |
| 27 | TEST_F(FileTest, Read) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 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_STREQ(filename.c_str(), file->name()); |
| 32 | char buffer[3]; |
| 33 | buffer[0] = '\0'; |
| 34 | EXPECT_TRUE(file->ReadFully(buffer, 2)); // ReadFully returns true. |
| 35 | buffer[2] = '\0'; |
| 36 | EXPECT_STREQ("PK", buffer); // zip file magic |
| 37 | EXPECT_FALSE(file->WriteByte(1)); // Cannot write to a read-only file. |
| 38 | } |
| 39 | |
| 40 | |
| 41 | TEST_F(FileTest, FileLength) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 42 | std::string filename(GetLibCoreDexFileName()); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 43 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 44 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | EXPECT_NE(0, file->Length()); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | TEST_F(FileTest, FilePosition) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 50 | std::string filename(GetLibCoreDexFileName()); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 51 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 52 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 53 | char buf[4]; |
| 54 | EXPECT_TRUE(file->ReadFully(buf, 2)); |
| 55 | EXPECT_EQ(2, file->Position()); |
| 56 | EXPECT_TRUE(file->ReadFully(buf, 2)); |
| 57 | EXPECT_EQ(4, file->Position()); |
| 58 | } |
| 59 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 60 | |
| 61 | TEST_F(FileTest, FileFd) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 62 | std::string filename(GetLibCoreDexFileName()); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 63 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 64 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 65 | EXPECT_NE(-1, file->Fd()); |
| 66 | } |
| 67 | |
| 68 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 69 | } // namespace art |