File: test-socket.c

package info (click to toggle)
event-dance 0.2.0-8~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,096 kB
  • sloc: ansic: 29,033; javascript: 2,709; makefile: 434; xml: 247; sh: 30; python: 27
file content (165 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (3)
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
/*
 * test-socket.c
 *
 * EventDance project - An event distribution framework (http://eventdance.org)
 *
 * Copyright (C) 2009, Igalia S.L.
 *
 * Authors:
 *   Eduardo Lima Mitev <elima@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <glib.h>
#include <gio/gio.h>
#include <glib/gstdio.h>

#ifdef HAVE_GIO_UNIX
#include <gio/gunixsocketaddress.h>
#endif

#include <evd.h>
#include <evd-socket-manager.h>

#include "test-socket-common.c"

/* test initial state */
static void
evd_socket_test_initial_state (EvdSocketFixture *f,
                               gconstpointer     test_data)
{
  /* EvdStream */
  g_assert (EVD_IS_SOCKET_BASE (f->socket));
  g_assert (EVD_IS_SOCKET (f->socket));

  g_assert (evd_socket_base_get_on_read (EVD_SOCKET_BASE (f->socket)) == NULL);
  g_assert (evd_socket_base_get_on_write (EVD_SOCKET_BASE (f->socket)) == NULL);

  g_assert_cmpuint (evd_socket_base_get_total_read (EVD_SOCKET_BASE (f->socket)), ==, 0);
  g_assert_cmpuint (evd_socket_base_get_total_written (EVD_SOCKET_BASE (f->socket)), ==, 0);

  /* EvdSocket */
  g_assert (evd_socket_get_socket (f->socket) == NULL);
  g_assert (evd_socket_get_context (f->socket) == NULL);
  g_assert (evd_socket_get_group (f->socket) == NULL);

  g_assert_cmpint (evd_socket_get_status (f->socket),
                   ==,
                   EVD_SOCKET_STATE_CLOSED);
  g_assert_cmpint (evd_socket_get_priority (f->socket),
                   ==,
                   G_PRIORITY_DEFAULT);

  evd_socket_test_config (f->socket,
                          G_SOCKET_FAMILY_INVALID,
                          G_SOCKET_TYPE_INVALID,
                          G_SOCKET_PROTOCOL_UNKNOWN);

  g_assert (evd_socket_can_read (f->socket) == FALSE);
  g_assert (evd_socket_can_write (f->socket) == FALSE);
}

/* test inet socket */

static void
evd_socket_inet_ipv4_fixture_setup (EvdSocketFixture *fixture,
                                    gconstpointer     test_data)
{
  gint port;
  GInetAddress *inet_addr;

  evd_socket_fixture_setup (fixture, test_data);

  inet_addr = g_inet_address_new_from_string ("127.0.0.1");
  port = g_random_int_range (1024, 0xFFFF-1);

  fixture->socket_addr = g_inet_socket_address_new (inet_addr, port);
  g_object_unref (inet_addr);
}

/* test inet socket */

static void
evd_socket_inet_ipv6_fixture_setup (EvdSocketFixture *fixture,
                                    gconstpointer     test_data)
{
  gint port;
  GInetAddress *inet_addr;

  evd_socket_fixture_setup (fixture, test_data);

  inet_addr = g_inet_address_new_from_string ("::1");
  port = g_random_int_range (1024, 0xFFFF-1);

  fixture->socket_addr = g_inet_socket_address_new (inet_addr, port);
  g_object_unref (inet_addr);
}

/* test unix socket */

static void
evd_socket_unix_fixture_setup (EvdSocketFixture *fixture,
                               gconstpointer     test_data)
{
  const gchar *UNIX_FILENAME = "/tmp/evd-test-socket-unix";

  evd_socket_fixture_setup (fixture, test_data);

  g_unlink (UNIX_FILENAME);
  fixture->socket_addr =
    G_SOCKET_ADDRESS (g_unix_socket_address_new (UNIX_FILENAME));
}

gint
main (gint argc, gchar *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);

  g_test_add ("/evd/socket/initial-state",
              EvdSocketFixture,
              NULL,
              evd_socket_fixture_setup,
              evd_socket_test_initial_state,
              evd_socket_fixture_teardown);

#ifdef HAVE_GIO_UNIX
  g_test_add ("/evd/socket/unix",
              EvdSocketFixture,
              NULL,
              evd_socket_unix_fixture_setup,
              evd_socket_test,
              evd_socket_fixture_teardown);
#endif

  g_test_add ("/evd/socket/inet/ipv4",
              EvdSocketFixture,
              NULL,
              evd_socket_inet_ipv4_fixture_setup,
              evd_socket_test,
              evd_socket_fixture_teardown);

  g_test_add ("/evd/socket/inet/ipv6",
              EvdSocketFixture,
              NULL,
              evd_socket_inet_ipv6_fixture_setup,
              evd_socket_test,
              evd_socket_fixture_teardown);

  g_test_run ();

  return 0;
}