summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-bus/test-bus-address.c
blob: b92558fea9d43ad5dbfac4e5fe85f3e007ea6642 (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
70
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "sd-bus.h"

#include "bus-internal.h"
#include "log.h"
#include "string-util.h"
#include "strv.h"
#include "tests.h"

static void test_one_address(sd_bus *b,
                             const char *host,
                             int result, const char *expected) {
        int r;

        r = bus_set_address_system_remote(b, host);
        log_info("\"%s\" → %d, \"%s\"", host, r, strna(r >= 0 ? b->address : NULL));
        if (result < 0 || expected) {
                assert(r == result);
                if (r >= 0)
                        assert_se(streq(b->address, expected));
        }
}

static void test_bus_set_address_system_remote(char **args) {
        _cleanup_(sd_bus_unrefp) sd_bus *b = NULL;

        assert_se(sd_bus_new(&b) >= 0);
        if (!strv_isempty(args)) {
                char **a;
                STRV_FOREACH(a, args)
                        test_one_address(b, *a, 0, NULL);
                return;
        };

        test_one_address(b, "host",
                         0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=host,argv4=systemd-stdio-bridge");
        test_one_address(b, "host:123",
                         0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=123,argv4=--,argv5=host,argv6=systemd-stdio-bridge");
        test_one_address(b, "host:123:123",
                         -EINVAL, NULL);
                test_one_address(b, "host:",
                                 -EINVAL, NULL);
        test_one_address(b, "user@host",
                         0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40host,argv4=systemd-stdio-bridge");
        test_one_address(b, "user@host@host",
                         -EINVAL, NULL);
        test_one_address(b, "[::1]",
                         0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=%3a%3a1,argv4=systemd-stdio-bridge");
        test_one_address(b, "user@[::1]",
                         0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40%3a%3a1,argv4=systemd-stdio-bridge");
        test_one_address(b, "user@[::1]:99",
                         0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=99,argv4=--,argv5=user%40%3a%3a1,argv6=systemd-stdio-bridge");
        test_one_address(b, "user@[::1]:",
                         -EINVAL, NULL);
        test_one_address(b, "user@[::1:",
                         -EINVAL, NULL);
        test_one_address(b, "user@",
                         -EINVAL, NULL);
        test_one_address(b, "user@@",
                         -EINVAL, NULL);
}

int main(int argc, char *argv[]) {
        test_setup_logging(LOG_INFO);

        test_bus_set_address_system_remote(argv + 1);

        return 0;
}