File: childskstreamtest.h

package info (click to toggle)
skstream 0.3.9-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,896 kB
  • sloc: sh: 10,997; cpp: 2,605; makefile: 64
file content (276 lines) | stat: -rw-r--r-- 8,640 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// test cases for children of basic_socket_stream
// Copyright (C) 2002 Dan Tomalesky and The WorldForge Project
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//  For information about Worldforge and its authors, please contact
//  the Worldforge Web Site at http://www.wordforge.org.
//
//  Author: Dan Tomalesky
//  Created: 2002-02-23 by Dan Tomalesky
//
// $Log$
// Revision 1.6  2004-12-01 16:48:04  alriddoch
// 2004-11-30  Al Riddoch  <alriddoch@zepler.org>
//
// 	* Release 0.3.3, interface version 1.0.0.
//
// Revision 1.5  2003/09/27 21:15:03  alriddoch
//  2003-09-27 Al Riddoch <alriddoch@zepler.org>
//     - test/childskstreamtest.h: Get rid of some of the java-isms, and
//       handle some failures a little more cleanly. Fix the udp test.
//     - test/skstreamtestrunner.cpp: Ensure that the tests exit status
//       reflects whether it passes or fails.
//     - Fix Makefile.am so tests are actually run.
//
// Revision 1.4  2003/05/06 21:53:11  alriddoch
//  2003-05-06 Al Riddoch <alriddoch@zepler.org>
//     - skstream/skstream.h, skstream/skstream.cpp, skstream_unix.h:
//       Re-work basic_socket_stream so it can have either stream or datagram
//       buffers.
//     - ping/ping.cpp, ping/ping.h, test/basicskstreamtest.h,
//       test/childskstreamtest.h, test/skservertest.h: Get the tests and examples
//       building again.
//
// Revision 1.3  2002/12/09 22:13:21  rsteinke
//     - created basic_socket, a virtual base class
//       for basic_socket_stream and basic_socket_server,
//       so that the polling code has a common base
//       with getSocket() to access
//     - created basic_socket_poll polling class
//     - changed tcp_socket_stream::is_ready() to isReady()
//       to match standards for function naming, left is_ready()
//       as a wrapper of isReady() for compatibility
//
// Revision 1.2  2002/05/21 07:29:37  malcolm
// Added rsteinke's nonblocking connect patch.  Works on linux; does not break API
// (I bumped version to 0.2.3 anyway).  May not work on win32, though I did test it
// and socket communication does happen.
//
// Revision 1.1  2002/02/24 03:15:41  grimicus
// 02/23/2002 Dan Tomalesky <grim@xynesis.com>
//
//     * Added in CVS logging variable so that changes show up in modified files
//       This will help keep changes made by worldforge in each file that is
//       changed as required by the GPL.
//
//     * Changed some parameter variables to have better meaning.
//       (ad -> address, etc.)
//
//     * Added some code into tcp_sk_stream::open so that it calls setLastError()
//       when the connection fails.
//
//     * Added some comments into skstream.h to better describe SOCKET_TYPE as
//       there can be some confusion between what it is actually for
//       (pointer/file descriptor/windows cludge of the socket) and the various
//       types of sockets (tcp, udp, raw, etc)
//
//     * Changed some more formatting for readability.
//
//     * Uncommented some commented out code in skstream.h so that the sync()
//       method returns 0 on the else, rather than not returning anything.
//
//     * Added some code into setBroadcast() so that setLastError() is called
//       if it fails to perform the getsocketopt().
//
//     * Modified the test/Makefile.am to remove the header files from the SOURCES
//       as the .h files do not seem to affect the build.
//
//     * Updated all the current test so that they use a socket instead of the
//       absolutely wrong stuff I was doing before.
//
//     * Added tests for tcp, udp, and raw skstreams child classes.
//

#ifndef CHILDSKSTREAMTEST_H
#define CHILDSKSTREAMTEST_H

#include <skstream/skstream.h>

#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>

#include <errno.h>

class tcpskstreamtest : public CppUnit::TestCase
{
    //some macros for building the suite() method
    CPPUNIT_TEST_SUITE(tcpskstreamtest);
    CPPUNIT_TEST(testConstructor_1);
    CPPUNIT_TEST(testConstructor_2);
    CPPUNIT_TEST(testOpen);
    CPPUNIT_TEST(testOpenNonblock);
    CPPUNIT_TEST_SUITE_END();

    private: 
        tcp_socket_stream *skstream;
        std::string hostname;
        int port;

    public:
        tcpskstreamtest(std::string name) : TestCase(name) { }
        tcpskstreamtest() { }

        void testConstructor_1()
        {
            CPPUNIT_ASSERT(skstream);
        }

        void testConstructor_2()
        {
            tcp_socket_stream *sks = new tcp_socket_stream(hostname, port);

            CPPUNIT_ASSERT(sks);

            if(!sks->is_open())
            {
                if(sks->getLastError() == ECONNREFUSED)
                {
                    CPPUNIT_ASSERT_MESSAGE("Check that echo service is running on local machine", sks->is_open());
                }
                else
                {
                    CPPUNIT_ASSERT(sks->is_open());
                }
            }
                    
            delete sks;
        }

        void testOpen()
        {
            skstream->open(hostname, port);
            
            if(!skstream->is_open())
            {
                if(skstream->getLastError() == ECONNREFUSED)
                {
                    CPPUNIT_ASSERT_MESSAGE("Check that echo service is running on local machine", skstream->is_open());
                }
                else
                {
                    CPPUNIT_ASSERT(skstream->is_open());
                }
            }
                    
        }

        void testOpenNonblock()
        {
            skstream->open(hostname, port, true);

            int waitcount = 0;
            while(!skstream->isReady()) // wait
                ++waitcount;

            // FIXME print waitcount somehow, to verify we're not connecting
            // immediately
         
            if(!skstream->is_open())
            {
                if(skstream->getLastError() == ECONNREFUSED)
                {
                    CPPUNIT_ASSERT_MESSAGE("Check that echo service is running on local machine", skstream->is_open());
                }
                else
                {
                    CPPUNIT_ASSERT(skstream->is_open());
                }
            }
   
        }

        void setUp()
        {
            skstream = new tcp_socket_stream();
            
            //echo service must be running (check inetd settings or if you
            //are a winders user, you have to install it)
            hostname = "127.0.0.1";
                //new std::string("localhost");
            port = 7;
        }

        void tearDown()
        {
            delete skstream;
            port = 0;
        }

};

class udpskstreamtest : public CppUnit::TestCase
{
    //some macros for building the suite() method
    CPPUNIT_TEST_SUITE(udpskstreamtest);
    CPPUNIT_TEST(testConstructor_1);
    CPPUNIT_TEST_SUITE_END();

    public:
        udpskstreamtest(std::string name) : TestCase(name) { }
        udpskstreamtest() { }

        void testConstructor_1()
        {
            udp_socket_stream skstream;
            CPPUNIT_ASSERT(skstream);

            CPPUNIT_ASSERT(!skstream.is_open());
        }

        void setUp()
        {
        }

        void tearDown()
        {
        }

};

#ifdef SOCK_RAW
class rawskstreamtest : public CppUnit::TestCase
{
    //some macros for building the suite() method
    CPPUNIT_TEST_SUITE(rawskstreamtest);
    CPPUNIT_TEST(testConstructor_1);
    CPPUNIT_TEST_SUITE_END();

    public:
        rawskstreamtest(std::string name) : TestCase(name) { }
        rawskstreamtest() { }

        void testConstructor_1()
        {
            raw_socket_stream skstream;
            CPPUNIT_ASSERT(skstream);

            CPPUNIT_ASSERT_MESSAGE(
                    "Raw only works on GNU/Linux and you must be root", 
                    skstream.is_open());
        }

        void setUp()
        {
        }

        void tearDown()
        {
        }

};
#endif

#endif