4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief UDPServer.test unit tests */
31 //#include "UDPServer.test.hh"
32 //#include "UDPServer.test.ih"
36 #include <senf/Scheduler/Scheduler.hh>
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 senf::scheduler::terminate();
53 unsigned port(unsigned i)
56 base_pid = ::getpid();
57 return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
60 std::string localhost4str(unsigned i)
62 return (boost::format("localhost:%d") % port(i)).str();
65 std::string localhost6str(unsigned i)
67 return (boost::format("[::1]:%d") % port(i)).str();
74 void read(senf::ConnectedUDPv4ClientSocketHandle socket, int ev)
81 senf::ClockService::clock_type delay(unsigned ms)
83 return senf::ClockService::now() + senf::ClockService::milliseconds(ms);
88 SENF_AUTO_UNIT_TEST(udpServer)
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);
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" );
106 // Check exception handling
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" );
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') );
128 // Check that we go back to the root dir for every packet
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" );
140 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"