Use G_SOURCE_CONTINUE/REMOVE internally
[platform/upstream/glib.git] / glib / tests / unix.c
1 /* 
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * This work is provided "as is"; redistribution and modification
5  * in whole or in part, in any medium, physical or electronic is
6  * permitted without restriction.
7  *
8  * This work is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * In no event shall the authors or contributors be liable for any
13  * direct, indirect, incidental, special, exemplary, or consequential
14  * damages (including, but not limited to, procurement of substitute
15  * goods or services; loss of use, data, or profits; or business
16  * interruption) however caused and on any theory of liability, whether
17  * in contract, strict liability, or tort (including negligence or
18  * otherwise) arising in any way out of the use of this software, even
19  * if advised of the possibility of such damage.
20  *
21  * Author: Colin Walters <walters@verbum.org> 
22  */
23
24 #include "config.h"
25
26 #include "glib-unix.h"
27 #include <string.h>
28
29 static void
30 test_pipe (void)
31 {
32   GError *error = NULL;
33   int pipefd[2];
34   char buf[1024];
35   ssize_t bytes_read;
36   gboolean res;
37
38   res = g_unix_open_pipe (pipefd, FD_CLOEXEC, &error);
39   g_assert (res);
40   g_assert_no_error (error);
41
42   write (pipefd[1], "hello", sizeof ("hello"));
43   memset (buf, 0, sizeof (buf));
44   bytes_read = read (pipefd[0], buf, sizeof(buf) - 1);
45   g_assert_cmpint (bytes_read, >, 0);
46
47   close (pipefd[0]);
48   close (pipefd[1]);
49
50   g_assert (g_str_has_prefix (buf, "hello"));
51 }
52
53 static void
54 test_error (void)
55 {
56   GError *error = NULL;
57   gboolean res;
58
59   res = g_unix_set_fd_nonblocking (123456, TRUE, &error);
60   g_assert_cmpint (errno, ==, EBADF);
61   g_assert (!res);
62   g_assert_error (error, G_UNIX_ERROR, 0);
63   g_clear_error (&error);
64 }
65
66 static gboolean sig_received = FALSE;
67
68 static gboolean
69 on_sig_received (gpointer user_data)
70 {
71   GMainLoop *loop = user_data;
72   g_main_loop_quit (loop);
73   sig_received = TRUE;
74   return G_SOURCE_REMOVE;
75 }
76
77 static gboolean
78 sig_not_received (gpointer data)
79 {
80   GMainLoop *loop = data;
81   (void) loop;
82   g_error ("Timed out waiting for signal");
83   return G_SOURCE_REMOVE;
84 }
85
86 static gboolean
87 exit_mainloop (gpointer data)
88 {
89   GMainLoop *loop = data;
90   g_main_loop_quit (loop);
91   return G_SOURCE_REMOVE;
92 }
93
94 static void
95 test_signal (int signum)
96 {
97   GMainLoop *mainloop;
98
99   mainloop = g_main_loop_new (NULL, FALSE);
100
101   sig_received = FALSE;
102   g_unix_signal_add (signum, on_sig_received, mainloop);
103   kill (getpid (), signum);
104   g_assert (!sig_received);
105   g_timeout_add (5000, sig_not_received, mainloop);
106   g_main_loop_run (mainloop);
107   g_assert (sig_received);
108   sig_received = FALSE;
109
110   /* Ensure we don't get double delivery */
111   g_timeout_add (500, exit_mainloop, mainloop);
112   g_main_loop_run (mainloop);
113   g_assert (!sig_received);
114   g_main_loop_unref (mainloop);
115
116 }
117
118 static void
119 test_sighup (void)
120 {
121   test_signal (SIGHUP);
122 }
123
124 static void
125 test_sigterm (void)
126 {
127   test_signal (SIGTERM);
128 }
129
130 static void
131 test_sighup_add_remove (void)
132 {
133   GMainLoop *mainloop;
134   guint id;
135
136   mainloop = g_main_loop_new (NULL, FALSE);
137
138   sig_received = FALSE;
139   id = g_unix_signal_add (SIGHUP, on_sig_received, mainloop);
140   g_source_remove (id);
141   kill (getpid (), SIGHUP);
142   g_assert (!sig_received);
143   g_main_loop_unref (mainloop);
144
145 }
146
147 int
148 main (int   argc,
149       char *argv[])
150 {
151   g_test_init (&argc, &argv, NULL);
152
153   g_test_add_func ("/glib-unix/pipe", test_pipe);
154   g_test_add_func ("/glib-unix/error", test_error);
155   g_test_add_func ("/glib-unix/sighup", test_sighup);
156   g_test_add_func ("/glib-unix/sigterm", test_sigterm);
157   g_test_add_func ("/glib-unix/sighup_again", test_sighup);
158   g_test_add_func ("/glib-unix/sighup_add_remove", test_sighup_add_remove);
159
160   return g_test_run();
161 }