• 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 
19 // Constructor can do set-up work for all test here.
FileHandleTest()20 FileHandleTest::FileHandleTest()
21 {
22   // create testee. modify it if need
23   m_pTestee = new FileHandle();
24 }
25 
26 // Destructor can do clean-up work that doesn't throw exceptions here.
~FileHandleTest()27 FileHandleTest::~FileHandleTest()
28 {
29   delete m_pTestee;
30 }
31 
32 // SetUp() will be called immediately before each test.
SetUp()33 void FileHandleTest::SetUp()
34 {
35 }
36 
37 // TearDown() will be called immediately after each test.
TearDown()38 void FileHandleTest::TearDown()
39 {
40 }
41 
42 //===----------------------------------------------------------------------===//
43 // Testcases
44 #include <iostream>
45 using namespace std;
46 
TEST_F(FileHandleTest,open_close)47 TEST_F(FileHandleTest, open_close) {
48   mcld::sys::fs::Path path(TOPDIR);
49   path.append("unittests/test.txt");
50   ASSERT_TRUE(m_pTestee->open(path, FileHandle::ReadOnly));
51   ASSERT_TRUE(m_pTestee->isOpened());
52   ASSERT_TRUE(m_pTestee->isGood());
53 
54   ASSERT_EQ(27, m_pTestee->size());
55 
56   ASSERT_TRUE(m_pTestee->close());
57   ASSERT_FALSE(m_pTestee->isOpened());
58   ASSERT_TRUE(m_pTestee->isGood());
59 
60   ASSERT_EQ(0, m_pTestee->size());
61 }
62 
TEST_F(FileHandleTest,delegate_close)63 TEST_F(FileHandleTest, delegate_close) {
64   mcld::sys::fs::Path path(TOPDIR);
65   path.append("unittests/test.txt");
66 
67   int fd = ::open(path.native().c_str(), O_RDONLY);
68 
69   ASSERT_TRUE(m_pTestee->delegate(fd, FileHandle::ReadOnly));
70   ASSERT_TRUE(m_pTestee->isOpened());
71   ASSERT_TRUE(m_pTestee->isGood());
72 
73   ASSERT_EQ(27, m_pTestee->size());
74 
75   ASSERT_TRUE(m_pTestee->close());
76   ASSERT_FALSE(m_pTestee->isOpened());
77   ASSERT_TRUE(m_pTestee->isGood());
78 
79   ASSERT_EQ(0, m_pTestee->size());
80 
81   int close_result = ::close(fd);
82   int close_err = errno;
83   ASSERT_EQ(-1, close_result);
84   ASSERT_EQ(EBADF, close_err);
85 }
86 
TEST_F(FileHandleTest,fail_close)87 TEST_F(FileHandleTest, fail_close) {
88   mcld::sys::fs::Path path(TOPDIR);
89   path.append("unittests/test.txt");
90   ASSERT_TRUE(m_pTestee->open(path, FileHandle::ReadOnly));
91   ASSERT_TRUE(m_pTestee->isOpened());
92   ASSERT_TRUE(m_pTestee->isGood());
93 
94   ASSERT_EQ(27, m_pTestee->size());
95 
96   int close_result = ::close(m_pTestee->handler());
97   ASSERT_EQ(0, close_result);
98 
99   ASSERT_FALSE(m_pTestee->close());
100   ASSERT_FALSE(m_pTestee->isOpened());
101   ASSERT_FALSE(m_pTestee->isGood());
102 }
103