]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/test-session.c
Don't send session stdout to .xsession-errors, add regression tests to confirm loggin...
[sojka/lightdm.git] / tests / src / test-session.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <xcb/xcb.h>
8 #include <glib.h>
9 #include <glib-object.h>
10 #include <gio/gio.h>
11
12 #include "status.h"
13
14 static GKeyFile *config;
15
16 static void
17 quit_cb (int signum)
18 {
19     status_notify ("SESSION %s TERMINATE SIGNAL=%d", getenv ("DISPLAY"), signum);
20     exit (EXIT_SUCCESS);
21 }
22
23 static void
24 request_cb (const gchar *request)
25 {
26     gchar *r;
27   
28     r = g_strdup_printf ("SESSION %s LOGOUT", getenv ("DISPLAY"));
29     if (strcmp (request, r) == 0)
30         exit (EXIT_SUCCESS);
31     g_free (r);
32   
33     r = g_strdup_printf ("SESSION %s CRASH", getenv ("DISPLAY"));
34     if (strcmp (request, r) == 0)
35         kill (getpid (), SIGSEGV);
36     g_free (r);
37
38     r = g_strdup_printf ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
39     if (strcmp (request, r) == 0)
40     {
41         g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
42                                      "org.freedesktop.DisplayManager",
43                                      getenv ("XDG_SEAT_PATH"),
44                                      "org.freedesktop.DisplayManager.Seat",
45                                      "Lock",
46                                      g_variant_new ("()"),
47                                      G_VARIANT_TYPE ("()"),
48                                      G_DBUS_CALL_FLAGS_NONE,
49                                      1000,
50                                      NULL,
51                                      NULL);
52         status_notify ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
53     }
54     g_free (r);
55
56     r = g_strdup_printf ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
57     if (strcmp (request, r) == 0)
58     {
59         g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
60                                      "org.freedesktop.DisplayManager",
61                                      getenv ("XDG_SESSION_PATH"),
62                                      "org.freedesktop.DisplayManager.Session",
63                                      "Lock",
64                                      g_variant_new ("()"),
65                                      G_VARIANT_TYPE ("()"),
66                                      G_DBUS_CALL_FLAGS_NONE,
67                                      1000,
68                                      NULL,
69                                      NULL);
70         status_notify ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
71     }
72     g_free (r);
73
74     r = g_strdup_printf ("SESSION %s WRITE-STDOUT TEXT=", getenv ("DISPLAY"));
75     if (g_str_has_prefix (request, r))
76         g_print ("%s\n", request + strlen (r));
77     g_free (r);
78
79     r = g_strdup_printf ("SESSION %s WRITE-STDERR TEXT=", getenv ("DISPLAY"));
80     if (g_str_has_prefix (request, r))
81         g_printerr ("%s\n", request + strlen (r));
82     g_free (r);
83
84     r = g_strdup_printf ("SESSION %s READ-XSESSION-ERRORS", getenv ("DISPLAY"));
85     if (strcmp (request, r) == 0)
86     {
87         gchar *contents;
88         GError *error = NULL;
89
90         if (g_file_get_contents (".xsession-errors", &contents, NULL, &error))
91             status_notify ("SESSION %s READ-XSESSION-ERRORS TEXT=%s", getenv ("DISPLAY"), contents);
92         else
93             status_notify ("SESSION %s READ-XSESSION-ERRORS ERROR=%s", getenv ("DISPLAY"), error->message);
94         g_clear_error (&error);
95     }
96     g_free (r);
97 }
98
99 int
100 main (int argc, char **argv)
101 {
102     GMainLoop *loop;
103     xcb_connection_t *connection;
104
105     signal (SIGINT, quit_cb);
106     signal (SIGTERM, quit_cb);
107
108     g_type_init ();
109
110     loop = g_main_loop_new (NULL, FALSE);
111
112     status_connect (request_cb);
113
114     if (argc > 1)
115         status_notify ("SESSION %s START NAME=%s USER=%s", getenv ("DISPLAY"), argv[1], getenv ("USER"));
116     else
117         status_notify ("SESSION %s START USER=%s", getenv ("DISPLAY"), getenv ("USER"));
118
119     config = g_key_file_new ();
120     if (g_getenv ("LIGHTDM_TEST_CONFIG"))
121         g_key_file_load_from_file (config, g_getenv ("LIGHTDM_TEST_CONFIG"), G_KEY_FILE_NONE, NULL);
122
123     connection = xcb_connect (NULL, NULL);
124
125     if (xcb_connection_has_error (connection))
126     {
127         status_notify ("SESSION %s CONNECT-XSERVER-ERROR", getenv ("DISPLAY"));
128         return EXIT_FAILURE;
129     }
130
131     status_notify ("SESSION %s CONNECT-XSERVER", getenv ("DISPLAY"));
132
133     g_main_loop_run (loop);    
134
135     return EXIT_SUCCESS;
136 }