b663c9dcfa720b344f0e0fe76c7e0cad5dfe8680
[senf.git] / senf / Utils / Console / UDPServer.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 UDPServer.test unit tests */
25
26 //#include "UDPServer.test.hh"
27 //#include "UDPServer.test.ih"
28
29 // Custom includes
30 #include "Console.hh"
31 #include <senf/Scheduler/Scheduler.hh>
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace {
40
41     void timeout()
42     {
43         senf::scheduler::terminate();
44     }
45
46     int base_pid = 0;
47
48     unsigned port(unsigned i)
49     {
50         if (! base_pid)
51             base_pid = ::getpid();
52         return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
53     }
54
55     std::string localhost4str(unsigned i)
56     {
57         return (boost::format("localhost:%d") % port(i)).str();
58     }
59
60     std::string localhost6str(unsigned i)
61     {
62         return (boost::format("[::1]:%d") % port(i)).str();
63     }
64
65     unsigned nread (0);
66     unsigned flags (0);
67     std::string data;
68
69     void read(senf::ConnectedUDPv4ClientSocketHandle socket, int ev)
70     {
71         flags |= ev;
72         ++nread;
73         data = socket.read();
74     }
75
76     senf::ClockService::clock_type delay(unsigned ms)
77     {
78         return senf::ClockService::now() + senf::ClockService::milliseconds(ms);
79     }
80
81 }
82
83 SENF_AUTO_UNIT_TEST(udpServer)
84 {
85     senf::console::UDPServer server (senf::INet4SocketAddress(localhost4str(0)));
86     senf::ConnectedUDPv4ClientSocketHandle socket (senf::INet4SocketAddress(localhost4str(0)));
87     senf::scheduler::TimerEvent timer ("udpServer test timer", &timeout);
88     senf::scheduler::FdEvent fdev ("udpServer test fd", boost::bind(&read, socket, _1),
89                                    socket, senf::scheduler::FdEvent::EV_READ);
90
91     nread = 0;
92     flags = 0;
93     data = "";
94     socket.write(std::string("ll"));
95     timer.timeout(delay(300));
96     senf::scheduler::process();
97     BOOST_CHECK_EQUAL( nread, 1 );
98     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
99     BOOST_CHECK_EQUAL( data, "sys/                        \n" );
100
101     // Check exception handling
102     nread = 0;
103     flags = 0;
104     data = "";
105     socket.write(std::string("sys"));
106     timer.timeout(delay(300));
107     senf::scheduler::process();
108     BOOST_CHECK_EQUAL( nread, 1 );
109     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
110     BOOST_CHECK_EQUAL( data, "invalid command\n" "at <unknown>:1:4\n" );
111
112     // switch directory
113     nread = 0;
114     flags = 0;
115     data = "";
116     socket.write(std::string("cd sys"));
117     timer.timeout(delay(300));
118     senf::scheduler::process();
119     BOOST_CHECK_EQUAL( nread, 1 );
120     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
121     BOOST_CHECK_EQUAL( data, std::string(1, '\0') );
122
123     // Check that we go back to the root dir for every packet
124     nread = 0;
125     flags = 0;
126     data = "";
127     socket.write(std::string("ll"));
128     timer.timeout(delay(300));
129     senf::scheduler::process();
130     BOOST_CHECK_EQUAL( nread, 1 );
131     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
132     BOOST_CHECK_EQUAL( data, "sys/                        \n" );
133 }
134
135 //-/////////////////////////////////////////////////////////////////////////////////////////////////
136 #undef prefix_
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // comment-column: 40
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // End: