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 */
16
17 #include "base/unix_file/fd_file.h"
18 #include "base/unix_file/random_access_file_test.h"
19 #include "gtest/gtest.h"
20
21 namespace unix_file {
22
23 class FdFileTest : public RandomAccessFileTest {
24 protected:
MakeTestFile()25 virtual RandomAccessFile* MakeTestFile() {
26 return new FdFile(fileno(tmpfile()));
27 }
28 };
29
TEST_F(FdFileTest,Read)30 TEST_F(FdFileTest, Read) {
31 TestRead();
32 }
33
TEST_F(FdFileTest,SetLength)34 TEST_F(FdFileTest, SetLength) {
35 TestSetLength();
36 }
37
TEST_F(FdFileTest,Write)38 TEST_F(FdFileTest, Write) {
39 TestWrite();
40 }
41
TEST_F(FdFileTest,UnopenedFile)42 TEST_F(FdFileTest, UnopenedFile) {
43 FdFile file;
44 EXPECT_EQ(-1, file.Fd());
45 EXPECT_FALSE(file.IsOpened());
46 EXPECT_TRUE(file.GetPath().empty());
47 }
48
TEST_F(FdFileTest,OpenClose)49 TEST_F(FdFileTest, OpenClose) {
50 std::string good_path(GetTmpPath("some-file.txt"));
51 FdFile file;
52 ASSERT_TRUE(file.Open(good_path, O_CREAT | O_WRONLY));
53 EXPECT_GE(file.Fd(), 0);
54 EXPECT_TRUE(file.IsOpened());
55 EXPECT_EQ(0, file.Close());
56 EXPECT_EQ(-1, file.Fd());
57 EXPECT_FALSE(file.IsOpened());
58 EXPECT_TRUE(file.Open(good_path, O_RDONLY));
59 EXPECT_GE(file.Fd(), 0);
60 EXPECT_TRUE(file.IsOpened());
61 }
62
63 } // namespace unix_file
64