5c4ec85dae2731bf1b5e5d628c67d0d0c3139510
[senf.git] / senf / Utils / Console / UDPServer.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief UDPServer.test unit tests */
30
31 //#include "UDPServer.test.hh"
32 //#include "UDPServer.test.ih"
33
34 // Custom includes
35 #include "Console.hh"
36 #include <senf/Scheduler/Scheduler.hh>
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45
46     void timeout()
47     {
48         senf::scheduler::terminate();
49     }
50
51     int base_pid = 0;
52
53     unsigned port(unsigned i)
54     {
55         if (! base_pid)
56             base_pid = ::getpid();
57         return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
58     }
59
60     std::string localhost4str(unsigned i)
61     {
62         return (boost::format("localhost:%d") % port(i)).str();
63     }
64
65     std::string localhost6str(unsigned i)
66     {
67         return (boost::format("[::1]:%d") % port(i)).str();
68     }
69
70     unsigned nread (0);
71     unsigned flags (0);
72     std::string data;
73
74     void read(senf::ConnectedUDPv4ClientSocketHandle socket, int ev)
75     {
76         flags |= ev;
77         ++nread;
78         data = socket.read();
79     }
80
81     senf::ClockService::clock_type delay(unsigned ms)
82     {
83         return senf::ClockService::now() + senf::ClockService::milliseconds(ms);
84     }
85
86 }
87
88 SENF_AUTO_UNIT_TEST(udpServer)
89 {
90     senf::console::UDPServer server (senf::INet4SocketAddress(localhost4str(0)));
91     senf::ConnectedUDPv4ClientSocketHandle socket (senf::INet4SocketAddress(localhost4str(0)));
92     senf::scheduler::TimerEvent timer ("udpServer test timer", &timeout);
93     senf::scheduler::FdEvent fdev ("udpServer test fd", boost::bind(&read, socket, _1),
94                                    socket, senf::scheduler::FdEvent::EV_READ);
95
96     nread = 0;
97     flags = 0;
98     data = "";
99     socket.write(std::string("ll"));
100     timer.timeout(delay(300));
101     senf::scheduler::process();
102     BOOST_CHECK_EQUAL( nread, 1 );
103     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
104     BOOST_CHECK_EQUAL( data, "sys/                        \n" );
105
106     // Check exception handling
107     nread = 0;
108     flags = 0;
109     data = "";
110     socket.write(std::string("sys"));
111     timer.timeout(delay(300));
112     senf::scheduler::process();
113     BOOST_CHECK_EQUAL( nread, 1 );
114     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
115     BOOST_CHECK_EQUAL( data, "invalid command\n" "at <unknown>:1:4\n" );
116
117     // switch directory
118     nread = 0;
119     flags = 0;
120     data = "";
121     socket.write(std::string("cd sys"));
122     timer.timeout(delay(300));
123     senf::scheduler::process();
124     BOOST_CHECK_EQUAL( nread, 1 );
125     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
126     BOOST_CHECK_EQUAL( data, std::string(1, '\0') );
127
128     // Check that we go back to the root dir for every packet
129     nread = 0;
130     flags = 0;
131     data = "";
132     socket.write(std::string("ll"));
133     timer.timeout(delay(300));
134     senf::scheduler::process();
135     BOOST_CHECK_EQUAL( nread, 1 );
136     BOOST_CHECK_EQUAL( flags, senf::scheduler::FdEvent::EV_READ );
137     BOOST_CHECK_EQUAL( data, "sys/                        \n" );
138 }
139
140 //-/////////////////////////////////////////////////////////////////////////////////////////////////
141 #undef prefix_
142
143 \f
144 // Local Variables:
145 // mode: c++
146 // fill-column: 100
147 // comment-column: 40
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // End: