File: test-xsession.c

package info (click to toggle)
nodm 0.13-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 548 kB
  • sloc: ansic: 1,942; sh: 368; makefile: 84
file content (160 lines) | stat: -rw-r--r-- 4,535 bytes parent folder | download | duplicates (5)
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
/*
 * test-xsession - test that we are able to run a X session
 *
 * Copyright 2011  Enrico Zini <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "log.h"
#include "common.h"
#include "dm.h"
#include "xserver.h"
#include "xsession-child.h"
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>

static char orig_windowpath[1024];

int test_session(struct nodm_xsession_child* s)
{
    int res = nodm_xsession_child_common_env(s);
    if (res != E_SUCCESS) return res;

    // Check environment
    ensure_not_equals(orig_windowpath, getenv_with_default("WINDOWPATH", ""));

    return E_SUCCESS;
}

int test_session_bad(struct nodm_xsession_child* s)
{
    int res = nodm_xsession_child_common_env(s);
    if (res != E_SUCCESS) return res;

    return E_USAGE;
}

int test_session_x_killer(struct nodm_xsession_child* s)
{
    int res = nodm_xsession_child_common_env(s);
    if (res != E_SUCCESS) return res;

    if (s->srv->pid == -1)
    {
        fprintf(stderr, "server PID has not been set\n");
        return E_BAD_ARG;
    }
    kill(s->srv->pid, SIGTERM);
    sleep(10);
    return E_SUCCESS;
}

// X server starts, X session quits with success
void test_trivial_session()
{
    log_verb("test_trivial_session");
    struct nodm_display_manager dm;
    test_setup_dm(&dm, NULL);
    dm.session.child_body = test_session;

    ensure_succeeds(nodm_display_manager_start(&dm));
    int sstatus;
    ensure_equali(nodm_display_manager_wait(&dm, &sstatus), E_SESSION_DIED);
    ensure_equali(sstatus, E_SUCCESS);
    ensure_succeeds(nodm_display_manager_stop(&dm));
    nodm_display_manager_cleanup(&dm);
}

// X server does not start
void test_bad_x_server()
{
    log_verb("test_bad_x_server");
    struct nodm_display_manager dm;
    test_setup_dm(&dm, "/bin/false");
    dm.session.child_body = test_session;

    ensure_equali(nodm_display_manager_start(&dm), E_X_SERVER_DIED);

    // If xserver died before being ready for connections, it should reflect on
    // the tracked pid
    ensure_equali(dm.srv.pid, -1);

    // Session must not have been started
    ensure_equali(dm.session.pid, -1);

    ensure_succeeds(nodm_display_manager_stop(&dm));
    nodm_display_manager_cleanup(&dm);
}

// X server starts, X session quits with error
void test_failing_x_session()
{
    log_verb("test_failing_x_session");
    struct nodm_display_manager dm;
    test_setup_dm(&dm, NULL);
    dm.session.child_body = test_session_bad;

    ensure_succeeds(nodm_display_manager_start(&dm));

    int sstatus;
    ensure_equali(nodm_display_manager_wait(&dm, &sstatus), E_SESSION_DIED);
    ensure_equali(WIFEXITED(sstatus) ? 1 : 0, 1);
    ensure_equali(WEXITSTATUS(sstatus), E_USAGE);

    ensure_succeeds(nodm_display_manager_stop(&dm));
    nodm_display_manager_cleanup(&dm);
}

// X server starts, X session starts, then server dies
void test_dying_x_server()
{
    log_verb("test_dying_x_server");
    struct nodm_display_manager dm;
    test_setup_dm(&dm, NULL);
    dm.session.child_body = test_session_x_killer;

    ensure_succeeds(nodm_display_manager_start(&dm));

    int sstatus;
    ensure_equali(nodm_display_manager_wait(&dm, &sstatus), E_X_SERVER_DIED);
    ensure_equali(sstatus, -1);

    ensure_succeeds(nodm_display_manager_stop(&dm));
    nodm_display_manager_cleanup(&dm);
}

int main(int argc, char* argv[])
{
    test_start("test-xsession", true);

    // Save original window path to check if it changed in the X session
    (void)bounded_strcpy(orig_windowpath, getenv_with_default("WINDOWPATH", ""));

    test_trivial_session();
    test_bad_x_server();
    test_failing_x_session();
    test_dying_x_server();

    // TODO:
    //  - test a wrong username (error before starting X session)

    test_ok();
}