test-bsd.c 12.9 KB
Newer Older
1 2 3
/*
 * This file is part of the Nice GLib ICE library.
 *
4
 * (C) 2006, 2007, 2014 Collabora Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *  Contact: Dafydd Harries
 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
 *  Contact: Kai Vehmanen
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Dafydd Harries, Collabora Ltd.
26
 *   Philip Withnall, Collabora Ltd.
27 28 29 30 31 32 33 34 35 36 37
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */
38 39 40
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
41 42 43

#include <string.h>

44
#include "socket.h"
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
static gssize
socket_recv (NiceSocket *sock, NiceAddress *addr, gsize buf_len, gchar *buf)
{
  GInputVector local_buf = { buf, buf_len };
  NiceInputMessage local_message = { &local_buf, 1, addr, 0 };
  gint ret;

  ret = nice_socket_recv_messages (sock, &local_message, 1);
  if (ret <= 0)
    return ret;

  return local_buf.size;
}

static void
test_socket_initial_properties (void)
{
  NiceSocket *sock;

  sock = nice_udp_bsd_socket_new (NULL);
  g_assert (sock != NULL);

  // not bound to a particular interface
  g_assert_cmpint (sock->addr.s.ip4.sin_addr.s_addr, ==, 0);
  // is bound to a particular port
  g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0);
72 73

  nice_socket_free (sock);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

static void
test_socket_address_properties (void)
{
  NiceSocket *sock;
  NiceAddress tmp;

  sock = nice_udp_bsd_socket_new (NULL);
  g_assert (sock != NULL);

  g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
  g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0);
  nice_address_set_port (&tmp, nice_address_get_port (&sock->addr));
  g_assert_cmpuint (nice_address_get_port (&tmp), !=, 0);
89 90

  nice_socket_free (sock);
91 92 93 94
}

static void
test_simple_send_recv (void)
95
{
96 97
  NiceSocket *server;
  NiceSocket *client;
98
  NiceAddress tmp;
99 100
  gchar buf[5];

101
  server = nice_udp_bsd_socket_new (NULL);
102
  g_assert (server != NULL);
103

104 105
  client = nice_udp_bsd_socket_new (NULL);
  g_assert (client != NULL);
106

107 108 109 110
  g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
  nice_address_set_port (&tmp, nice_address_get_port (&server->addr));

  /* Send and receive stuff. */
111
  g_assert_cmpint (nice_socket_send (client, &tmp, 5, "hello"), ==, 5);
112 113 114 115

  g_assert_cmpint (socket_recv (server, &tmp, 5, buf), ==, 5);
  g_assert_cmpint (strncmp (buf, "hello", 5), ==, 0);

116
  g_assert_cmpint (nice_socket_send (server, &tmp, 5, "uryyb"), ==, 5);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  g_assert_cmpint (socket_recv (client, &tmp, 5, buf), ==, 5);
  g_assert_cmpint (strncmp (buf, "uryyb", 5), ==, 0);

  nice_socket_free (client);
  nice_socket_free (server);
}

/* Check that sending and receiving to/from zero-length buffers returns
 * immediately. */
static void
test_zero_send_recv (void)
{
  NiceSocket *sock;
  NiceAddress tmp;
  gchar buf[5];
133 134
  NiceOutputMessage local_out_message;
  NiceInputMessage local_in_message;
135 136 137 138 139 140 141 142 143

  sock = nice_udp_bsd_socket_new (NULL);
  g_assert (sock != NULL);

  g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
  g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0);
  nice_address_set_port (&tmp, nice_address_get_port (&sock->addr));
  g_assert_cmpuint (nice_address_get_port (&tmp), !=, 0);

144 145
  g_assert_cmpint (nice_socket_send (sock, &tmp, 0, "ignore-me"), ==, 0);
  g_assert_cmpint (nice_socket_send (sock, &tmp, 0, NULL), ==, 0);
146 147 148 149

  g_assert_cmpint (socket_recv (sock, &tmp, 0, buf), ==, 0);
  g_assert_cmpint (socket_recv (sock, &tmp, 0, NULL), ==, 0);

150
  /* And again with messages. */
151
  g_assert_cmpint (nice_socket_send_messages (sock, &tmp,
152
      &local_out_message, 0), ==, 0);
153
  g_assert_cmpint (nice_socket_send_messages (sock, &tmp, NULL, 0), ==, 0);
154 155 156 157 158

  g_assert_cmpint (nice_socket_recv_messages (sock,
      &local_in_message, 0), ==, 0);
  g_assert_cmpint (nice_socket_recv_messages (sock, NULL, 0), ==, 0);

159 160 161 162 163 164 165 166 167 168 169 170
  nice_socket_free (sock);
}

/* Test receiving into multiple tiny buffers. */
static void
test_multi_buffer_recv (void)
{
  NiceSocket *server;
  NiceSocket *client;
  NiceAddress tmp;
  guint8 buf[20];
  guint8 dummy_buf[9];
171

172 173 174 175 176 177 178
  server = nice_udp_bsd_socket_new (NULL);
  g_assert (server != NULL);

  client = nice_udp_bsd_socket_new (NULL);
  g_assert (client != NULL);

  g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
179
  nice_address_set_port (&tmp, nice_address_get_port (&server->addr));
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  /* Send and receive stuff. */
  {
    GInputVector bufs[7] = {
      { &buf[0], 1 },
      { &buf[1], 4 },
      { &buf[1], 0 },  /* should be unused (zero-length) */
      { &buf[5], 1 },
      { &buf[6], 5 },
      { &buf[11], 9 },  /* should be unused (message fits in prior buffers) */
      { &buf[11], 0 },  /* should be unused (zero-length) */
    };
    NiceInputMessage message = { bufs, G_N_ELEMENTS (bufs), NULL, 0 };

    /* Initialise the buffers so we can try and catch out-of-bounds accesses. */
    memset (buf, 0xaa, sizeof (buf));
    memset (dummy_buf, 0xaa, sizeof (dummy_buf));
197

198
    /* Send and receive. */
199
    g_assert_cmpint (nice_socket_send (client, &tmp, 11, "hello-world"), ==, 11);
200 201
    g_assert_cmpuint (nice_socket_recv_messages (server, &message, 1), ==, 1);
    g_assert_cmpuint (message.length, ==, 11);
202

203 204 205 206 207 208 209 210 211 212 213 214
    /* Check all of the things. The sizes should not have been modified. */
    g_assert_cmpuint (bufs[0].size, ==, 1);
    g_assert_cmpuint (bufs[1].size, ==, 4);
    g_assert_cmpuint (bufs[2].size, ==, 0);
    g_assert_cmpuint (bufs[3].size, ==, 1);
    g_assert_cmpuint (bufs[4].size, ==, 5);
    g_assert_cmpuint (bufs[5].size, ==, 9);
    g_assert_cmpuint (bufs[6].size, ==, 0);

    g_assert_cmpint (strncmp ((gchar *) buf, "hello-world", 11), ==, 0);
    g_assert_cmpint (memcmp (buf + 11, dummy_buf, 9), ==, 0);
  }
215

216 217
  nice_socket_free (client);
  nice_socket_free (server);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}

/* Fill a buffer with deterministic but non-repeated data, so that transmission
 * and reception corruption is more likely to be detected. */
static void
fill_send_buf (guint8 *buf, gsize buf_len, guint seed)
{
  gsize i;

  for (i = 0; i < buf_len; i++) {
    buf[i] = '0' + (seed % 10);
    seed++;
  }
}

/* Test receiving multiple messages in a single call. */
static void
test_multi_message_recv (guint n_sends, guint n_receives,
    guint n_bufs_per_message, gsize send_buf_size, gsize recv_buf_size,
237
    guint expected_n_received_messages, guint expected_n_sent_messages)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
  NiceSocket *server;
  NiceSocket *client;
  NiceAddress tmp;

  server = nice_udp_bsd_socket_new (NULL);
  g_assert (server != NULL);

  client = nice_udp_bsd_socket_new (NULL);
  g_assert (client != NULL);

  g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
  nice_address_set_port (&tmp, nice_address_get_port (&server->addr));

  /* Send and receive stuff. */
  {
254 255 256 257
    GInputVector *recv_bufs;
    NiceInputMessage *recv_messages;
    GOutputVector *send_bufs;
    NiceOutputMessage *send_messages;
258
    guint i, j;
259
    guint8 *_expected_recv_buf;
260 261
    gsize expected_recv_buf_len;

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    /* Set up the send buffers. */
    send_bufs = g_malloc0_n (n_sends * n_bufs_per_message,
        sizeof (GOutputVector));
    send_messages = g_malloc0_n (n_sends, sizeof (NiceOutputMessage));

    for (i = 0; i < n_sends; i++) {
      for (j = 0; j < n_bufs_per_message; j++) {
        guint8 *buf = g_slice_alloc (send_buf_size);

        send_bufs[i * n_bufs_per_message + j].buffer = buf;
        send_bufs[i * n_bufs_per_message + j].size = send_buf_size;

        /* Set up the buffer data. */
        fill_send_buf (buf, send_buf_size, i);
      }

      send_messages[i].buffers = send_bufs + i * n_bufs_per_message;
      send_messages[i].n_buffers = n_bufs_per_message;
    }

282
    /* Set up the receive buffers. Yay for dynamic tests! */
283 284 285
    recv_bufs = g_malloc0_n (n_receives * n_bufs_per_message,
        sizeof (GInputVector));
    recv_messages = g_malloc0_n (n_receives, sizeof (NiceInputMessage));
286 287 288

    for (i = 0; i < n_receives; i++) {
      for (j = 0; j < n_bufs_per_message; j++) {
289 290 291
        recv_bufs[i * n_bufs_per_message + j].buffer =
            g_slice_alloc (recv_buf_size);
        recv_bufs[i * n_bufs_per_message + j].size = recv_buf_size;
292 293

        /* Initialise the buffer to try to catch out-of-bounds accesses. */
294 295
        memset (recv_bufs[i * n_bufs_per_message + j].buffer, 0xaa,
            recv_buf_size);
296 297
      }

298 299 300 301
      recv_messages[i].buffers = recv_bufs + i * n_bufs_per_message;
      recv_messages[i].n_buffers = n_bufs_per_message;
      recv_messages[i].from = NULL;
      recv_messages[i].length = 0;
302 303 304
    }

    /* Send multiple packets. */
305
    g_assert_cmpint (
306
        nice_socket_send_messages (client, &tmp, send_messages, n_sends), ==,
307
        expected_n_sent_messages);
308 309

    /* Receive things. */
310 311
    g_assert_cmpint (
        nice_socket_recv_messages (server, recv_messages, n_receives), ==,
312 313 314 315 316 317 318
        expected_n_received_messages);

    /* Check all of the things. The sizes should not have been modified. */
    expected_recv_buf_len = recv_buf_size * n_bufs_per_message;
    _expected_recv_buf = g_slice_alloc (expected_recv_buf_len);

    for (i = 0; i < expected_n_received_messages; i++) {
319
      NiceInputMessage *message = &recv_messages[i];
320 321 322
      guint8 *expected_recv_buf = _expected_recv_buf;
      gsize expected_len;

323 324
      expected_len = MIN (send_buf_size * n_bufs_per_message,
          expected_recv_buf_len);
325 326 327 328
      g_assert_cmpuint (message->length, ==, expected_len);

      /* Build the expected buffer as a concatenation of the expected values of
       * all receive buffers in the message. */
329
      memset (expected_recv_buf, 0xaa, expected_recv_buf_len);
330 331 332 333 334 335 336 337 338 339 340 341 342
      fill_send_buf (expected_recv_buf, expected_len, i);

      for (j = 0; j < n_bufs_per_message; j++) {
        g_assert_cmpuint (message->buffers[j].size, ==, recv_buf_size);
        g_assert_cmpint (
            memcmp (message->buffers[j].buffer, expected_recv_buf,
                recv_buf_size), ==, 0);

        expected_recv_buf += recv_buf_size;
      }
    }

    g_slice_free1 (expected_recv_buf_len, _expected_recv_buf);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    for (i = 0; i < n_receives; i++) {
      for (j = 0; j < n_bufs_per_message; j++) {
        g_slice_free1 (recv_buf_size,
                       recv_bufs[i * n_bufs_per_message + j].buffer);
      }
    }

    for (i = 0; i < n_sends; i++) {
      for (j = 0; j < n_bufs_per_message; j++) {
        g_slice_free1 (send_buf_size,
                       (gpointer) send_bufs[i * n_bufs_per_message + j].buffer);
      }
    }

    g_free (recv_messages);
    g_free (recv_bufs);
    g_free (send_messages);
    g_free (send_bufs);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  }

  nice_socket_free (client);
  nice_socket_free (server);
}

int
main (void)
{
  test_socket_initial_properties ();
  test_socket_address_properties ();
  test_simple_send_recv ();
  test_zero_send_recv ();
  test_multi_buffer_recv ();

  /* Multi-message testing. Serious business. */
  {
    guint i;
    struct {
381 382 383 384 385 386
      guint n_sends;  /* messages */
      guint expected_n_sent_messages;

      guint n_receives;  /* messages */
      guint expected_n_received_messages;

387 388 389 390
      guint n_bufs_per_message;
      gsize send_buf_size;
      gsize recv_buf_size;
    } test_cases[] = {
391 392 393 394 395 396 397 398 399 400
      /* same number of sends and receives */
      {   2,   2,   2,   2,   1, 100, 100 },  /* send 200B, receive 200B */
      /* more sends than receives */
      {   4,   4,   2,   2,   2, 100,  77 },  /* send 800B, receive 308B */
      /* more receives than sends */
      {   1,   1,   4,   1,   4,  10, 100 },  /* send 40B, receive 1600B */
      /* small receive buffer (data loss) */
      { 100, 100, 100, 100,   1, 100,  64 },  /* send 10000B, receive 6400B */
      /* small receive buffers (data loss) */
      {  50,  50,  50,  50,  10, 100,   8 },  /* send 50000B, receive 4000B */
401 402 403 404 405 406
    };

    for (i = 0; i < G_N_ELEMENTS (test_cases); i++) {
      test_multi_message_recv (test_cases[i].n_sends, test_cases[i].n_receives,
          test_cases[i].n_bufs_per_message, test_cases[i].send_buf_size,
          test_cases[i].recv_buf_size,
407 408
          test_cases[i].expected_n_received_messages,
          test_cases[i].expected_n_sent_messages);
409 410 411
    }
  }

412 413 414
  return 0;
}