dd2243559be77ab7d213c9e600246a4426ce3890
[senf.git] / Socket / FileHandle.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 // Unit tests
24
25 //#include "FileHandle.test.hh"
26 //#include "FileHandle.test.ih"
27
28 // Custom includes
29 #include <iostream>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include "FileHandle.hh"
33
34 #include <boost/test/auto_unit_test.hpp>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     class FHandle : public senf::FileHandle
42     {
43     public:
44         FHandle(int fd=-1) 
45             : senf::FileHandle(std::auto_ptr<senf::FileBody>(
46                                           new senf::FileBody(fd))) {}
47         FHandle(std::string name) 
48             : senf::FileHandle(std::auto_ptr<senf::FileBody>(
49                                           new senf::FileBody()))
50             {
51                 int rv = ::open(name.c_str(),O_RDWR|O_NONBLOCK) ;
52                 if (rv<0)
53                     throw senf::SystemException(errno);
54                 fd(rv);
55             }
56     };
57 }
58
59 BOOST_AUTO_UNIT_TEST(fileHandle)
60 {
61     try {
62         {
63             FHandle fh("/dev/null");
64             BOOST_CHECK(fh.fd() != -1);
65             BOOST_CHECK(fh.valid());
66             BOOST_CHECK(fh);
67             BOOST_CHECK(!!fh);
68
69             FHandle fh2(fh);
70             BOOST_CHECK_EQUAL(fh.fd(), fh2.fd());
71             
72             BOOST_CHECK(fh.writeable());
73             BOOST_CHECK_NO_THROW(fh.close());
74             BOOST_CHECK_THROW(fh.close(),senf::SystemException);
75             BOOST_CHECK_NO_THROW(fh.terminate());
76         }
77         
78         {
79             FHandle fh("/dev/zero");
80             BOOST_CHECK(fh.readable());
81         }
82
83         {
84             int fds[2];
85             BOOST_REQUIRE(pipe(fds) == 0);
86
87             FHandle fh(fds[0]);
88             BOOST_CHECK(!fh.readable());
89
90             // Set non-blocking IO and fill the pipe buffer
91             int flags = fcntl(fds[1],F_GETFL,0);
92             if (flags == -1)
93                 BOOST_FAIL(strerror(errno));
94             if (fcntl(fds[1],F_SETFL,flags|O_NONBLOCK) == -1)
95                 BOOST_FAIL(strerror(errno));
96             char buffer[1024];
97             while (write(fds[1],buffer,1024) == 1024) ;
98
99             FHandle fh2(fds[1]);
100             BOOST_CHECK(!fh.writeable());
101         }
102     }
103     catch (std::exception const & ex) {
104         BOOST_FAIL(ex.what());
105     }
106 }
107     
108 ///////////////////////////////cc.e////////////////////////////////////////
109 #undef prefix_
110
111 \f
112 // Local Variables:
113 // mode: c++
114 // c-file-style: "senf"
115 // End: