• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- implTest.cpp -------------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "mcld/Support/FileHandle.h"
10 #include "mcld/Support/Path.h"
11 #include <fcntl.h>
12 #include <errno.h>
13 #include "FileHandleTest.h"
14 
15 using namespace mcld;
16 using namespace mcldtest;
17 
18 // Constructor can do set-up work for all test here.
FileHandleTest()19 FileHandleTest::FileHandleTest() {
20   // create testee. modify it if need
21   m_pTestee = new FileHandle();
22 }
23 
24 // Destructor can do clean-up work that doesn't throw exceptions here.
~FileHandleTest()25 FileHandleTest::~FileHandleTest() {
26   delete m_pTestee;
27 }
28 
29 // SetUp() will be called immediately before each test.
SetUp()30 void FileHandleTest::SetUp() {
31 }
32 
33 // TearDown() will be called immediately after each test.
TearDown()34 void FileHandleTest::TearDown() {
35 }
36 
37 //===----------------------------------------------------------------------===//
38 // Testcases
39 //===----------------------------------------------------------------------===//
TEST_F(FileHandleTest,open_close)40 TEST_F(FileHandleTest, open_close) {
41   mcld::sys::fs::Path path(TOPDIR);
42   path.append("unittests/test.txt");
43   ASSERT_TRUE(m_pTestee->open(path, FileHandle::OpenMode(FileHandle::ReadOnly),
44                               FileHandle::Permission(FileHandle::System)));
45   ASSERT_TRUE(m_pTestee->isOpened());
46   ASSERT_TRUE(m_pTestee->isGood());
47   ASSERT_TRUE(m_pTestee->isOwned());
48 
49   ASSERT_TRUE(27 == m_pTestee->size());
50 
51   ASSERT_TRUE(m_pTestee->close());
52   ASSERT_FALSE(m_pTestee->isOpened());
53   ASSERT_TRUE(m_pTestee->isGood());
54 
55   ASSERT_TRUE(0 == m_pTestee->size());
56 }
57 
TEST_F(FileHandleTest,delegate_close)58 TEST_F(FileHandleTest, delegate_close) {
59   mcld::sys::fs::Path path(TOPDIR);
60   path.append("unittests/test.txt");
61 
62   int fd = ::open(path.native().c_str(), O_RDONLY);
63 
64   ASSERT_TRUE(m_pTestee->delegate(fd, FileHandle::ReadOnly));
65   ASSERT_TRUE(m_pTestee->isOpened());
66   ASSERT_TRUE(m_pTestee->isGood());
67   ASSERT_FALSE(m_pTestee->isOwned());
68 
69   ASSERT_TRUE(27 == m_pTestee->size());
70 
71   ASSERT_TRUE(m_pTestee->close());
72   ASSERT_FALSE(m_pTestee->isOpened());
73   ASSERT_TRUE(m_pTestee->isGood());
74   ASSERT_TRUE(m_pTestee->isOwned());
75 
76   ASSERT_TRUE(0 == m_pTestee->size());
77 
78   int close_result = ::close(fd);
79   ASSERT_EQ(0, close_result);
80 }
81 
TEST_F(FileHandleTest,fail_close)82 TEST_F(FileHandleTest, fail_close) {
83   mcld::sys::fs::Path path(TOPDIR);
84   path.append("unittests/test.txt");
85   ASSERT_TRUE(m_pTestee->open(path, FileHandle::OpenMode(FileHandle::ReadOnly),
86                               FileHandle::Permission(FileHandle::System)));
87   ASSERT_TRUE(m_pTestee->isOpened());
88   ASSERT_TRUE(m_pTestee->isGood());
89 
90   ASSERT_TRUE(27 == m_pTestee->size());
91 
92   int close_result = ::close(m_pTestee->handler());
93   ASSERT_EQ(0, close_result);
94 
95   ASSERT_FALSE(m_pTestee->close());
96   ASSERT_FALSE(m_pTestee->isOpened());
97   ASSERT_FALSE(m_pTestee->isGood());
98 }
99