File: test.c

package info (click to toggle)
nodm 0.13-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 548 kB
  • sloc: ansic: 1,942; sh: 368; makefile: 84
file content (119 lines) | stat: -rw-r--r-- 2,970 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
/*
 * test - nodm test utilities
 *
 * Copyright 2011  Enrico Zini <enrico@enricozini.org>
 *
 * 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 "test.h"
#include "common.h"
#include "dm.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

void test_setup_dm(struct nodm_display_manager* dm, const char* xcmdline)
{
    bool run_nested = getenv("DISPLAY") != NULL;
    bool is_root = getuid() == 0;

    nodm_display_manager_init(dm);
    if (xcmdline == NULL)
    {
        if (run_nested)
            xcmdline = "/usr/bin/Xnest :1 -geometry 1x1+0+0";
        else
            xcmdline = "";
    }
    ensure_succeeds(nodm_display_manager_parse_xcmdline(dm, xcmdline));
    if (is_root)
        strcpy(dm->session.conf_run_as, "root");
    else
    {
        dm->session.conf_use_pam = false;
        dm->session.conf_cleanup_xse = false;
        dm->session.conf_run_as[0] = 0;
        dm->vt.conf_initial_vt = -1;
    }
}

void test_start(const char* testname, bool verbose)
{
    static struct log_config cfg;
    cfg.program_name = testname,
    cfg.log_to_syslog = false,
    cfg.log_to_stderr = true,
    cfg.log_level = NODM_LL_INFO;
    cfg.log_level = NODM_LL_VERB;
    log_start(&cfg);
}

void test_fail()
{
    log_end();
    exit(E_PROGRAMMING);
}

void test_ok()
{
    log_end();
    exit(0);
}

void ensure_equals(const char* a, const char* b)
{
    if (a == NULL && b == NULL)
        return;
    if (a == NULL || b == NULL || strcmp(a, b) != 0)
    {
        log_warn("strings differ: \"%s\" != \"%s\"", a, b);
        test_fail();
    }
}

void ensure_not_equals(const char* a, const char* b)
{
    if (a == NULL && b == NULL)
    {
        log_warn("strings are both NULL");
        test_fail();
    }
    if (a != NULL && b != NULL && strcmp(a, b) == 0)
    {
        log_warn("strings are both \"%s\"", a);
        test_fail();
    }
}

void ensure_equali(int a, int b)
{
    if (a != b)
    {
        log_warn("values differ: %d != %d", a, b);
        test_fail();
    }
}

void _ensure_succeeds(int code, const char* file, int line, const char* desc)
{
    if (code != E_SUCCESS)
    {
        log_err("%s:%d:%s failed with code %d (%s)", file, line, desc, code, nodm_strerror(code));
        test_fail();
    }
}