summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_logind.c
blob: f91782ce7836724932669c84c2b5e6c591a13120 (plain)
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
/*
 * SPDX-FileCopyrightText:  2023, Iker Pedrosa <ipedrosa@redhat.com>
 *
 * SPDX-License-Identifier:  BSD-3-Clause
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <pwd.h>
#include <stdlib.h>
#include <sys/types.h>

#include "prototypes.h"

/***********************
 * WRAPPERS
 **********************/
struct passwd *
__wrap_prefix_getpwnam(uid_t uid)
{
    return (struct passwd*) mock();
}

int
__wrap_sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions)
{
    return mock();
}

/***********************
 * TEST
 **********************/
static void test_active_sessions_count_return_ok(void **state)
{
    int count;
    struct passwd *pw = malloc(sizeof(struct passwd));

    will_return(__wrap_prefix_getpwnam, pw);
    will_return(__wrap_sd_uid_get_sessions, 1);

    count = active_sessions_count("testuser", 0);

    assert_int_equal(count, 1);
}

static void test_active_sessions_count_prefix_getpwnam_failure(void **state)
{
    int count;
    struct passwd *pw = NULL;

    will_return(__wrap_prefix_getpwnam, pw);

    count = active_sessions_count("testuser", 0);

    assert_int_equal(count, 0);
}

int main(void)
{
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(test_active_sessions_count_return_ok),
        cmocka_unit_test(test_active_sessions_count_prefix_getpwnam_failure),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}