summaryrefslogtreecommitdiff
path: root/ACE/tests/SOCK_SCTP_STREAM_Test.cpp
blob: e9222cda76b2323bdcdce1e2f907ee040e65ce12 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// $Id$
//
// *WARRANTY DISCLAIMER: LIMITATION OF LIABILITY. THE SOFTWARE AND 
// CONTENT ARE PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED 
// REPRESENTATIONS, GUARANTEES, OR WARRANTIES, INCLUDING BUT NOT LIMITED 
// TO SUCH REPRESENTATION, GUARANTEES OR WARRANTIES REGARDING THE 
// USABILITY, SUITABILITY, CONDITION, OPERATION OR ACCURACY THEREOF. *
// 
// *ALL OTHER WARRANTIES AND CONDITIONS (EXPRESS, IMPLIED OR STATUTORY) 
// ARE HEREBY DISCLAIMED, SUCH WARRANTIES AND CONDITIONS INCLUDING 
// WITHOUT LIMITATION, ALL WARRANTIES AND CONDITIONS OF MERCHANTABILITY, 
// TITLE, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, 
// COMPATIBILITY, AND SECURITY OR ACCURACY.*
// 
// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    SOCK_SCTP_STREAM_Test.cpp
//
// = DESCRIPTION
//    Performs several tests on the ACE_SOCK_SCTP_STREAM_Connector,
//    ACE_SOCK_SCTP_STREAM_Acceptor, and ACE_SOCK_SCTP_STREAM classes
//    specifically for SCTP using the loopback interface.  Attempts to
//    replicate behavior of SOCK_Test.cpp, but integrating IPv6 tests
//    directly.
//
// = AUTHOR
//    Dave Craig <dwc@qualcomm.com>
//

#include "test_config.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_select.h"
#include "ace/OS_NS_sys_wait.h"
#include "ace/SOCK_SCTP_STREAM_Connector.h"
#include "ace/SOCK_SCTP_STREAM_Acceptor.h"
#include "ace/Thread_Manager.h"
#include "ace/Handle_Set.h"

#define TTCPPORT 5001
#define BYTE_MESG 0xcd

#ifdef ACE_WIN64
// This arg is ignored on Windows and causes pointer truncation
// warnings on 64-bit compiled.
#define SELECT_WIDTH(x) 0
#else
#define SELECT_WIDTH(x) (x)
#endif

ACE_THR_FUNC_RETURN
Server (void *arg)
{
  ACE_SOCK_SCTP_STREAM_Acceptor *AcceptorSocket =
    reinterpret_cast<ACE_SOCK_SCTP_STREAM_Acceptor *> (arg);

  ACE_SOCK_SCTP_STREAM Stream;

  ACE_Handle_Set handle_set;

  const ACE_Time_Value def_timeout (ACE_DEFAULT_TIMEOUT);

  ACE_Time_Value tv (def_timeout);

  int select_width;

  int result;

  //
  // Make sure AcceptorSocket is in nonblocking mode so as not to
  // hang tests.
  //
  if (-1 == AcceptorSocket->enable (ACE_NONBLOCK))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p\n"),
                ACE_TEXT ("AcceptorSocket.enable (ACE_NONBLOCK)")));
  }

  //
  // Set up select to wait for I/O events.
  //
  handle_set.reset ();
  handle_set.set_bit (AcceptorSocket->get_handle ());

  select_width = SELECT_WIDTH(int (AcceptorSocket->get_handle ()) + 1);

  result = ACE_OS::select(select_width,
                          handle_set,
                          0,
                          0,
                          &tv);

  ACE_ASSERT (tv == def_timeout);

  if (-1 == result)
  {
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) %p\n"),
                       ACE_TEXT ("select")),
                      0);
  }
  else if (0 == result)
  {
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT("(%P|%t) select timed out, shutting down\n")),
                      0);
  }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) waiting for client to connect\n")));

  while (-1 != AcceptorSocket->accept (Stream)) {
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%P|%t) client connected\n")));

    //
    // Enable non-blocking I/O.
    //
    if (Stream.enable (ACE_NONBLOCK))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%P|%t) %p\n"),
                         ACE_TEXT ("Stream.enable (ACE_NONBLOCK)")),
                        0);
    }

    unsigned char byte = BYTE_MESG;

    if (-1 == Stream.send_n (&byte, 1))
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%P|%t) %p\n"),
                  ACE_TEXT ("Stream.send_n")));
    }

    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%P|%t) byte sent\n")));

    //
    // Ubruptly terminate the association.
    //
    if (-1 == Stream.abort ())
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%P|%t) %p\n"),
                  ACE_TEXT ("Association.abort")));
    }

    //
    // Negative test: make sure that we cannot send on a closed association.
    //
    if (-1 != Stream.send_n (&byte, 1))
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%P|%t) Negative test fail: Association")
                  ACE_TEXT(".send_n succeeded after abort()\n")));
    }

  }

  //
  // Close server socket.
  //
  if (-1 == AcceptorSocket->close ())
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p\n"),
                ACE_TEXT ("AcceptorSocket.close")));
  }

  return 0;
}

ACE_THR_FUNC_RETURN
Client(void *arg)
{
  ACE_Multihomed_INET_Addr *ServerAddr =
    reinterpret_cast<ACE_Multihomed_INET_Addr *> (arg);

  ACE_SOCK_SCTP_STREAM_Connector Connector;

  ACE_SOCK_SCTP_STREAM Stream;

  ACE_Time_Value tv (ACE_DEFAULT_TIMEOUT);

  char b;
  size_t bytes;

  if (-1 == Connector.connect (Stream,
                               *ServerAddr,
                               &tv,
                               ACE_Addr::sap_any,
                               1))
  {
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) %p to %s:%d\n"),
                       ACE_TEXT ("Connector.connect"),
                       ServerAddr->get_host_name (),
                       ServerAddr->get_port_number ()),
                      0);
  }

  if (-1 == Stream.disable (ACE_NONBLOCK))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p\n"),
                ACE_TEXT ("Association.disable (ACE_NONBLOCK)")));
  }


  if (-1 == Stream.recv_n (&b, 1, &tv, &bytes))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p\n"),
                ACE_TEXT ("Association.recv_n")));
  }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Client received %d bytes\n"),
              bytes));
  ACE_ASSERT(1 == bytes);

  //
  // Give server a little time to abort the association.
  //
  ACE_OS::sleep(1);

  if (-1 != Stream.recv_n (&b, 1, &tv, &bytes))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) Negative test failed Association")
                ACE_TEXT (".recv_n succeeded after abort()\n")));
  }

  return 0;
}

//
// Spawn server and client threads and then wait until they complete the
// test.  There must be a timeout on the wait, so executable does not hang the
// tests indefinitely.
//
int
spawn_test(bool ipv6_test)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) spawn_test started ipv6 %d\n"),
              ipv6_test));

  ACE_SOCK_SCTP_STREAM_Acceptor AcceptorSocket;

  ACE_Multihomed_INET_Addr ServerAddr (TTCPPORT,
#ifdef ACE_HAS_IPV6
                                      (ipv6_test ?
                                       ACE_IPV6_LOCALHOST :
                                       ACE_LOCALHOST)
#else /* ! ACE_HAS_IPV6 */
                                       ACE_LOCALHOST
#endif /* ! ACE_HAS_IPV6 */
                                       );

  if (-1 == AcceptorSocket.open (ServerAddr,
                                 1,
#ifdef ACE_HAS_IPV6
                                 (ipv6_test ? AF_INET6 : AF_INET),
#else /* ! ACE_HAS_IPV6 */
                                 AF_INET,
#endif /* ! ACE_HAS_IPV6 */
                                 ACE_DEFAULT_BACKLOG
#if defined (IPPROTO_SCTP)
                                 ,IPPROTO_SCTP
#endif /* IPPROTO_SCTP */
                                ))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p\n"),
                ACE_TEXT ("AcceptorSocket.open")));
  }

  if (-1 == AcceptorSocket.get_local_addr (ServerAddr))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p\n"),
                ACE_TEXT ("AcceptorSocket.get_local_addr")));
  }

  struct sockaddr_in inaddr;

  ServerAddr.get_addresses(&inaddr, 1);

  ACE_ASSERT ((TTCPPORT == ServerAddr.get_port_number ()));
  ACE_ASSERT ((ipv6_test || INADDR_LOOPBACK == ServerAddr.get_ip_address ()));
  ACE_ASSERT ((!ipv6_test ||
               ACE_Multihomed_INET_Addr(TTCPPORT, "::1") == ServerAddr));

#ifndef ACE_LACKS_FORK
  switch (ACE_OS::fork (ACE_TEXT ("child")))
  {
  case -1:
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p%a"),
                ACE_TEXT ("fork failed")));
    break;
  case 0:
    Client (&ServerAddr);
    ACE_OS::exit (0);
    break;
  default:
    Server (reinterpret_cast<void *> (&AcceptorSocket));
    ACE_OS::wait ();
    break;
  }
#elif defined (ACE_HAS_THREADS)
  if (-1 == ACE_Thread_Manager::instance ()->spawn
      (Server,
       reinterpret_cast<void *> (&AcceptorSocket),
       THR_NEW_LWP | THR_DETACHED))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p%a"),
                ACE_TEXT ("thread create failed")));
  }

  if (-1 == ACE_Thread_Manager::instance ()->spawn
      (Client,
       reinterpret_cast<void *> (&ServerAddr),
       THR_NEW_LWP | THR_DETACHED))
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) %p%a"),
                ACE_TEXT ("thread create failed")));
  }

  ACE_Thread_Manager::instance ()->wait ();
#else /* ACE_LACKS_FORK && ! ACE_HAS_THREADS */
  ACE_ERROR ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) \n"),
              ACE_TEXT ("only one thread may be run ")
              ACE_TEXT ("in a process on this platform\n")));
#endif /* ACE_LACKS_FORK && ! ACE_HAS_THREADS */

  return 0;
}

int
do_test(void)
{
  spawn_test(false);

#ifdef ACE_HAS_IPV6
  spawn_test(true);
#endif

  return 0;
}

int run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);

  ACE_START_TEST (ACE_TEXT ("SOCK_SCTP_STREAM_Test"));

  //
  // Check whether host OS has SCTP support before starting this test.
  // If not, just pass because there is not a hope of testing
  // SOCK_SCTP_STREAM.
  //
  int status = 0;

#ifdef ACE_HAS_SCTP
  status = do_test();
#else /* ! ACE_HAS_SCTP */
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT("SCTP not supported by ACE.\n")
              ACE_TEXT("This test will not do anything.\n")));
#endif /* ! ACE_HAS_SCTP */

  ACE_END_TEST;

  return status;
}