02d5392acbc172bb5be3f88452237a2817c6a013
[senf.git] / senf / Socket / FileHandle.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FileHandle unit tests */
25
26 //#include "FileHandle.test.hh"
27 //#include "FileHandle.test.ih"
28
29 // Custom includes
30 #include <iostream>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include "FileHandle.hh"
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42     class FHandle : public senf::FileHandle
43     {
44     public:
45         FHandle() {}
46         FHandle(int fd)
47             : senf::FileHandle(std::auto_ptr<senf::FileBody>(
48                                           new senf::FileBody(fd))) {}
49         FHandle(std::string const & name)
50             : senf::FileHandle(std::auto_ptr<senf::FileBody>(
51                                           new senf::FileBody()))
52             {
53                 int rv = ::open(name.c_str(),O_RDWR|O_NONBLOCK) ;
54                 if (rv<0)
55                     throw senf::SystemException();
56                 fd(rv);
57             }
58     };
59 }
60
61 SENF_AUTO_UNIT_TEST(fileHandle)
62 {
63     try {
64         {
65             FHandle fh("/dev/null");
66             BOOST_CHECK(fh.fd() != -1);
67             BOOST_CHECK(fh.valid());
68             BOOST_CHECK(fh);
69             BOOST_CHECK(!!fh);
70
71             FHandle fh2;
72             BOOST_CHECK( ! fh2.valid() );
73             fh2 = fh;
74             BOOST_CHECK_EQUAL(fh.fd(), fh2.fd());
75
76             BOOST_CHECK(fh.writeable());
77             SENF_CHECK_NO_THROW(fh.close());
78             BOOST_CHECK_THROW(fh.close(),senf::SystemException);
79             SENF_CHECK_NO_THROW(fh.terminate());
80         }
81
82         {
83             FHandle fh("/dev/zero");
84             BOOST_CHECK(fh.readable());
85         }
86
87         {
88             int fds[2];
89             BOOST_REQUIRE(pipe(fds) == 0);
90
91             FHandle fh(fds[0]);
92             BOOST_CHECK(!fh.readable());
93
94             // Set non-blocking IO and fill the pipe buffer
95             int flags = fcntl(fds[1],F_GETFL,0);
96             if (flags == -1)
97                 BOOST_FAIL(strerror(errno));
98             if (fcntl(fds[1],F_SETFL,flags|O_NONBLOCK) == -1)
99                 BOOST_FAIL(strerror(errno));
100             char buffer[1024];
101             ::memset(buffer, 0, sizeof(buffer));
102             while (write(fds[1],buffer,1024) == 1024) ;
103
104             FHandle fh2(fds[1]);
105             BOOST_CHECK(!fh.writeable());
106         }
107     }
108     catch (std::exception const & ex) {
109         BOOST_FAIL(ex.what());
110     }
111 }
112
113 ///////////////////////////////cc.e////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40
125 // End: