GSocket: set protocol when using g_socket_new_from_fd()
[platform/upstream/glib.git] / gio / tests / socket.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24
25 #ifdef G_OS_UNIX
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <gio/gunixconnection.h>
29 #endif
30
31 #ifdef G_OS_UNIX
32 static void
33 test_unix_from_fd (void)
34 {
35   gint fd;
36   GError *error;
37   GSocket *s;
38
39   fd = socket (AF_UNIX, SOCK_STREAM, 0);
40   g_assert_cmpint (fd, !=, -1);
41
42   error = NULL;
43   s = g_socket_new_from_fd (fd, &error);
44   g_assert_no_error (error);
45   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
46   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
47   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
48   g_object_unref (s);
49 }
50
51 static void
52 test_unix_connection (void)
53 {
54   gint fd;
55   GError *error;
56   GSocket *s;
57   GSocketConnection *c;
58
59   fd = socket (AF_UNIX, SOCK_STREAM, 0);
60   g_assert_cmpint (fd, !=, -1);
61
62   error = NULL;
63   s = g_socket_new_from_fd (fd, &error);
64   g_assert_no_error (error);
65   c = g_socket_connection_factory_create_connection (s);
66   g_assert (G_IS_UNIX_CONNECTION (c));
67   g_object_unref (c);
68   g_object_unref (s);
69 }
70 #endif /* G_OS_UNIX */
71
72 int
73 main (int   argc,
74       char *argv[])
75 {
76   g_type_init ();
77   g_test_init (&argc, &argv, NULL);
78
79 #ifdef G_OS_UNIX
80   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
81   g_test_add_func ("/socket/unix-connection", test_unix_connection);
82 #endif
83
84   return g_test_run();
85 }
86