Line data Source code
1 : #include "sd-bus.h"
2 :
3 : #include "bus-internal.h"
4 : #include "log.h"
5 : #include "string-util.h"
6 : #include "strv.h"
7 : #include "tests.h"
8 :
9 13 : static void test_one_address(sd_bus *b,
10 : const char *host,
11 : int result, const char *expected) {
12 : int r;
13 :
14 13 : r = bus_set_address_system_remote(b, host);
15 13 : log_info("\"%s\" → %d, \"%s\"", host, r, strna(r >= 0 ? b->address : NULL));
16 13 : if (result < 0 || expected) {
17 13 : assert(r == result);
18 13 : if (r >= 0)
19 6 : assert_se(streq(b->address, expected));
20 : }
21 13 : }
22 :
23 1 : static void test_bus_set_address_system_remote(char **args) {
24 1 : _cleanup_(sd_bus_unrefp) sd_bus *b = NULL;
25 :
26 1 : assert_se(sd_bus_new(&b) >= 0);
27 1 : if (!strv_isempty(args)) {
28 : char **a;
29 0 : STRV_FOREACH(a, args)
30 0 : test_one_address(b, *a, 0, NULL);
31 0 : return;
32 : };
33 :
34 1 : test_one_address(b, "host",
35 : 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=host,argv4=systemd-stdio-bridge");
36 1 : test_one_address(b, "host:123",
37 : 0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=123,argv4=--,argv5=host,argv6=systemd-stdio-bridge");
38 1 : test_one_address(b, "host:123:123",
39 : -EINVAL, NULL);
40 1 : test_one_address(b, "host:",
41 : -EINVAL, NULL);
42 1 : test_one_address(b, "user@host",
43 : 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40host,argv4=systemd-stdio-bridge");
44 1 : test_one_address(b, "user@host@host",
45 : -EINVAL, NULL);
46 1 : test_one_address(b, "[::1]",
47 : 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=%3a%3a1,argv4=systemd-stdio-bridge");
48 1 : test_one_address(b, "user@[::1]",
49 : 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40%3a%3a1,argv4=systemd-stdio-bridge");
50 1 : test_one_address(b, "user@[::1]:99",
51 : 0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=99,argv4=--,argv5=user%40%3a%3a1,argv6=systemd-stdio-bridge");
52 1 : test_one_address(b, "user@[::1]:",
53 : -EINVAL, NULL);
54 1 : test_one_address(b, "user@[::1:",
55 : -EINVAL, NULL);
56 1 : test_one_address(b, "user@",
57 : -EINVAL, NULL);
58 1 : test_one_address(b, "user@@",
59 : -EINVAL, NULL);
60 : }
61 :
62 1 : int main(int argc, char *argv[]) {
63 1 : test_setup_logging(LOG_INFO);
64 :
65 1 : test_bus_set_address_system_remote(argv + 1);
66 :
67 1 : return 0;
68 : }
|