blob: 85c130e914d0a032a59068b65b1b7cc834f26353 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstromdb4d5402011-08-09 12:18:28 -070016
Brian Carlstromdb4d5402011-08-09 12:18:28 -070017#include "file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070018
Elliott Hughes90a33692011-08-30 13:27:07 -070019#include "UniquePtr.h"
20#include "common_test.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070021#include "os.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022
23namespace art {
24
Brian Carlstromf734cf52011-08-17 16:28:14 -070025class FileTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070026
27TEST_F(FileTest, Read) {
Elliott Hughes95572412011-12-13 18:14:20 -080028 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_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
41TEST_F(FileTest, FileLength) {
Elliott Hughes95572412011-12-13 18:14:20 -080042 std::string filename(GetLibCoreDexFileName());
Elliott Hughes90a33692011-08-30 13:27:07 -070043 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
44 ASSERT_TRUE(file.get() != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070045 EXPECT_NE(0, file->Length());
46}
47
48
49TEST_F(FileTest, FilePosition) {
Elliott Hughes95572412011-12-13 18:14:20 -080050 std::string filename(GetLibCoreDexFileName());
Elliott Hughes90a33692011-08-30 13:27:07 -070051 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
52 ASSERT_TRUE(file.get() != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070053 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 Carlstrom4a289ed2011-08-16 17:17:49 -070060
61TEST_F(FileTest, FileFd) {
Elliott Hughes95572412011-12-13 18:14:20 -080062 std::string filename(GetLibCoreDexFileName());
Elliott Hughes90a33692011-08-30 13:27:07 -070063 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
64 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070065 EXPECT_NE(-1, file->Fd());
66}
67
68
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069} // namespace art