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